purge pending.

This commit is contained in:
Christopher Jeffrey 2016-02-19 11:16:39 -08:00
parent 3c3e9a496a
commit 53158edf4f

View File

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