diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index ff66b32f..60754a5f 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -702,10 +702,23 @@ Chain.prototype.resetHeight = function resetHeight(height, force) { // have been some orphans on a forked chain we // no longer need. this.purgeOrphans(); + this.purgePending(); unlock(); }; +Chain.prototype.purgePending = function purgePending() { + var self = this; + this.pending.forEach(function(block) { + delete self.pendingBlocks[block.hash('hex')]; + self.pendingSize -= block.getSize(); + }); + this.pending.length = 0; + this.jobs = this.jobs.filter(function(item) { + return item[0] !== Chain.prototype.add; + }); +}; + // Maybe do this: // Chain.prototype._lock = function _lock(func, args, callback, force) { // And return wrapped callback with an unlock call in it @@ -728,14 +741,7 @@ Chain.prototype._lock = function _lock(func, args, force) { if (this.pendingSize > this.pendingLimit) { utils.debug('Warning: %dmb of pending blocks. Purging.', utils.mb(this.pendingSize)); - this.pending.forEach(function(block) { - delete self.pendingBlocks[block.hash('hex')]; - self.pendingSize -= block.getSize(); - }); - this.pending.length = 0; - this.jobs = this.jobs.filter(function(item) { - return item[0] !== Chain.prototype.add; - }); + this.purgePending(); return; } } @@ -848,6 +854,7 @@ Chain.prototype.resetHeightAsync = function resetHeightAsync(height, callback, f // have been some orphans on a forked chain we // no longer need. self.purgeOrphans(); + self.purgePending(); return done(); }); @@ -1094,6 +1101,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) { // reset the orphans and find a new peer. if (orphan.hash('hex') !== hash) { self.purgeOrphans(); + self.purgePending(); self.emit('fork', block, { height: -1, @@ -1164,6 +1172,9 @@ Chain.prototype.add = function add(initial, peer, callback, force) { // been killed and hopefully we find a peer // who isn't trying to fool us. if (entry.hash !== checkpoint) { + self.purgeOrphans(); + self.purgePending(); + self.emit('fork', block, { height: entry.height, expected: network.checkpoints[entry.height],