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() {
var self = this;
var best, last;
var i, hashes, orphan, height, best, last;
best = Object.keys(this.orphan.map).reduce(function(best, prevBlock) {
var orphan = self.orphan.map[prevBlock];
var height = orphan.getCoinbaseHeight();
hashes = Object.keys(this.orphan.map);
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())
return orphan;
best = orphan;
return best;
}, null);
last = orphan;
}
// Save the best for last... or the
// last for the best in this case.
if (!best || best.getCoinbaseHeight() <= 0)
// last for best in this case.
if (best.getCoinbaseHeight() <= 0)
best = last;
this.emit('purge',
this.orphan.count - (best ? 1 : 0),
this.orphan.size - (best ? best.getSize() : 0));
hashes = Object.keys(this.orphan.bmap);
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)
self.emit('unresolved', orphan);
});
this.emit('unresolved', orphan);
}
this.orphan.map = {};
this.orphan.bmap = {};
this.orphan.count = 0;
this.orphan.size = 0;
if (!best)
return;
this.emit('purge',
this.orphan.count - 1,
this.orphan.size - best.getSize());
this.orphan.map[best.prevBlock] = best;
this.orphan.bmap[best.hash('hex')] = best;
this.orphan.count++;
this.orphan.size += best.getSize();
this.orphan.count = 1;
this.orphan.size = best.getSize();
};
/**