add adding state to chain.

This commit is contained in:
Christopher Jeffrey 2016-05-05 03:00:24 -07:00
parent 019617162c
commit 2faf8513c4
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 18 additions and 3 deletions

View File

@ -69,6 +69,7 @@ function Chain(options) {
this.loaded = false; this.loaded = false;
this.db = new bcoin.chaindb(this, options); this.db = new bcoin.chaindb(this, options);
this.total = 0; this.total = 0;
this.adding = false;
this.orphanLimit = options.orphanLimit || (20 << 20); this.orphanLimit = options.orphanLimit || (20 << 20);
this.pendingLimit = options.pendingLimit || (1024 << 20); this.pendingLimit = options.pendingLimit || (1024 << 20);
this.locker = new bcoin.locker(this, this.add, this.pendingLimit); this.locker = new bcoin.locker(this, this.add, this.pendingLimit);
@ -1302,6 +1303,15 @@ Chain.prototype.onFlush = function onFlush(callback) {
return this.locker.onFlush(callback); return this.locker.onFlush(callback);
}; };
/**
* Test whether the chain is in the process of adding blocks.
* @returns {Boolean}
*/
Chain.prototype.isBusy = function isBusy() {
return this.adding || this.locker.pending.length > 0;
};
/** /**
* Add a block to the chain, perform all necessary verification. * Add a block to the chain, perform all necessary verification.
* @param {Block|MerkleBlock|CompactBlock} block * @param {Block|MerkleBlock|CompactBlock} block
@ -1321,6 +1331,8 @@ Chain.prototype.add = function add(block, callback, force) {
callback = utils.wrap(callback, unlock); callback = utils.wrap(callback, unlock);
this.adding = true;
(function next(block, initial) { (function next(block, initial) {
var hash = block.hash('hex'); var hash = block.hash('hex');
var prevHash = block.prevBlock; var prevHash = block.prevBlock;
@ -1539,7 +1551,7 @@ Chain.prototype.add = function add(block, callback, force) {
if (entry.chainwork.cmp(self.tip.chainwork) <= 0) { if (entry.chainwork.cmp(self.tip.chainwork) <= 0) {
return self.db.save(entry, block, false, function(err) { return self.db.save(entry, block, false, function(err) {
if (err) if (err)
return callback(err); return done(err);
// Keep track of the number of blocks we // Keep track of the number of blocks we
// added and the number of orphans resolved. // added and the number of orphans resolved.
@ -1599,6 +1611,8 @@ Chain.prototype.add = function add(block, callback, force) {
self.emit('full'); self.emit('full');
} }
self.adding = false;
if (err) if (err)
callback(err); callback(err);
else else

View File

@ -467,10 +467,11 @@ Pool.prototype._startInterval = function _startInterval() {
if (self.chain.isFull()) if (self.chain.isFull())
return; return;
if (self.chain.locker.pending.length > 0) if (self.chain.isBusy())
return self._startTimer(); return self._startTimer();
bcoin.debug('Stall recovery: loading again.'); bcoin.debug('Stall recovery: loading again.');
// self._load(); // self._load();
} }
@ -1643,7 +1644,7 @@ Pool.prototype.scheduleRequests = function scheduleRequests(peer) {
Pool.prototype._sendRequests = function _sendRequests(peer) { Pool.prototype._sendRequests = function _sendRequests(peer) {
var size, items; var size, items;
if (this.chain.locker.pending.length > 0) if (this.chain.isBusy())
return; return;
if (peer.queue.block.length === 0) if (peer.queue.block.length === 0)