refactor orphan pruning.

This commit is contained in:
Christopher Jeffrey 2016-06-30 13:05:53 -07:00
parent c580557993
commit b4bbba1ad9
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -1640,48 +1640,51 @@ Chain.prototype.purgeOrphans = function purgeOrphans() {
*/ */
Chain.prototype.pruneOrphans = function pruneOrphans() { Chain.prototype.pruneOrphans = function pruneOrphans() {
var self = this; var i, hashes, orphan, height, best, last;
var best, last;
best = Object.keys(this.orphan.map).reduce(function(best, prevBlock) { hashes = Object.keys(this.orphan.map);
var orphan = self.orphan.map[prevBlock];
var height = orphan.getCoinbaseHeight();
last = orphan; if (hashes.length === 0)
return;
for (i = 0; i < hashes.length; i++) {
hash = hashes[i];
orphan = this.orphan.map[hash];
height = orphan.getCoinbaseHeight();
delete this.orphan.map[hash];
if (!best || height > best.getCoinbaseHeight()) if (!best || height > best.getCoinbaseHeight())
return orphan; best = orphan;
return best; last = orphan;
}, null); }
// Save the best for last... or the // Save the best for last... or the
// last for the best in this case. // last for best in this case.
if (!best || best.getCoinbaseHeight() <= 0) if (best.getCoinbaseHeight() <= 0)
best = last; best = last;
this.emit('purge', hashes = Object.keys(this.orphan.bmap);
this.orphan.count - (best ? 1 : 0),
this.orphan.size - (best ? best.getSize() : 0)); for (i = 0; i < hashes.length; i++) {
hash = hashes[i];
orphan = this.orphan.bmap[hash];
delete this.orphan.bmap[hash];
Object.keys(this.orphan.bmap).forEach(function(hash) {
var orphan = self.orphan.bmap[hash];
if (orphan !== best) if (orphan !== best)
self.emit('unresolved', orphan); this.emit('unresolved', orphan);
}); }
this.orphan.map = {}; this.emit('purge',
this.orphan.bmap = {}; this.orphan.count - 1,
this.orphan.count = 0; this.orphan.size - best.getSize());
this.orphan.size = 0;
if (!best)
return;
this.orphan.map[best.prevBlock] = best; this.orphan.map[best.prevBlock] = best;
this.orphan.bmap[best.hash('hex')] = best; this.orphan.bmap[best.hash('hex')] = best;
this.orphan.count++; this.orphan.count = 1;
this.orphan.size += best.getSize(); this.orphan.size = best.getSize();
}; };
/** /**