diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 36a5a905..43f90a89 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -69,6 +69,7 @@ function Chain(options) { this.loaded = false; this.db = new bcoin.chaindb(this, options); this.total = 0; + this.adding = false; this.orphanLimit = options.orphanLimit || (20 << 20); this.pendingLimit = options.pendingLimit || (1024 << 20); 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); }; +/** + * 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. * @param {Block|MerkleBlock|CompactBlock} block @@ -1321,6 +1331,8 @@ Chain.prototype.add = function add(block, callback, force) { callback = utils.wrap(callback, unlock); + this.adding = true; + (function next(block, initial) { var hash = block.hash('hex'); var prevHash = block.prevBlock; @@ -1539,7 +1551,7 @@ Chain.prototype.add = function add(block, callback, force) { if (entry.chainwork.cmp(self.tip.chainwork) <= 0) { return self.db.save(entry, block, false, function(err) { if (err) - return callback(err); + return done(err); // Keep track of the number of blocks we // added and the number of orphans resolved. @@ -1599,6 +1611,8 @@ Chain.prototype.add = function add(block, callback, force) { self.emit('full'); } + self.adding = false; + if (err) callback(err); else diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 0179c592..3547110a 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -467,10 +467,11 @@ Pool.prototype._startInterval = function _startInterval() { if (self.chain.isFull()) return; - if (self.chain.locker.pending.length > 0) + if (self.chain.isBusy()) return self._startTimer(); bcoin.debug('Stall recovery: loading again.'); + // self._load(); } @@ -1643,7 +1644,7 @@ Pool.prototype.scheduleRequests = function scheduleRequests(peer) { Pool.prototype._sendRequests = function _sendRequests(peer) { var size, items; - if (this.chain.locker.pending.length > 0) + if (this.chain.isBusy()) return; if (peer.queue.block.length === 0)