keep orphans off the heap.

This commit is contained in:
Christopher Jeffrey 2016-02-24 02:47:33 -08:00
parent 07a4c3a465
commit 466429dbe2

View File

@ -1070,13 +1070,13 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
if (orphan) { if (orphan) {
// If the orphan chain forked, simply // If the orphan chain forked, simply
// reset the orphans and find a new peer. // reset the orphans and find a new peer.
if (orphan.hash('hex') !== hash) { if (orphan.hash !== hash) {
self.purgeOrphans(); self.purgeOrphans();
self.purgePending(); self.purgePending();
self.emit('fork', block, { self.emit('fork', block, {
height: -1, height: -1,
expected: orphan.hash('hex'), expected: orphan.hash,
received: hash, received: hash,
checkpoint: false checkpoint: false
}, peer); }, peer);
@ -1103,15 +1103,22 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
// If previous block wasn't ever seen, // If previous block wasn't ever seen,
// add it current to orphans and break. // add it current to orphans and break.
if (prevHeight === -1) { if (prevHeight === -1) {
self.orphan.count++;
self.orphan.size += block.getSize();
self.orphan.map[prevHash] = block;
self.orphan.bmap[hash] = block;
self.emit('orphan', block, { self.emit('orphan', block, {
height: -1, height: -1,
hash: hash, hash: hash,
seen: false seen: false
}, peer); }, peer);
block = {
data: block._raw,
subtype: block.subtype,
hash: block.hash('hex'),
prevBlock: block.prevBlock,
coinbaseHeight: block.getCoinbaseHeight()
};
self.orphan.count++;
self.orphan.size += block.data.length;
self.orphan.map[prevHash] = block;
self.orphan.bmap[hash] = block;
return done(); return done();
} }
@ -1288,10 +1295,12 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
// An orphan chain was found, start resolving. // An orphan chain was found, start resolving.
block = self.orphan.map[hash]; block = self.orphan.map[hash];
delete self.orphan.bmap[block.hash('hex')]; delete self.orphan.bmap[block.hash];
delete self.orphan.map[hash]; delete self.orphan.map[hash];
self.orphan.count--; self.orphan.count--;
self.orphan.size -= block.getSize(); self.orphan.size -= block.data.length;
block = bcoin.block.fromRaw(block.data, block.subtype);
block.network = true;
next(block); next(block);
} }
@ -1338,11 +1347,11 @@ Chain.prototype.pruneOrphans = function pruneOrphans(peer) {
best = Object.keys(this.orphan.map).reduce(function(best, prevBlock, i) { best = Object.keys(this.orphan.map).reduce(function(best, prevBlock, i) {
var orphan = self.orphan.map[prevBlock]; var orphan = self.orphan.map[prevBlock];
var height = orphan.getCoinbaseHeight(); var height = orphan.coinbaseHeight;
last = orphan; last = orphan;
if (!best || height > best.getCoinbaseHeight()) if (!best || height > best.coinbaseHeight)
return orphan; return orphan;
return best; return best;
@ -1350,12 +1359,12 @@ Chain.prototype.pruneOrphans = function pruneOrphans(peer) {
// Save the best for last... or the // Save the best for last... or the
// last for the best in this case. // last for the best in this case.
if (!best || best.getCoinbaseHeight() <= 0) if (!best || best.coinbaseHeight <= 0)
best = last; best = last;
this.emit('purge', this.emit('purge',
this.orphan.count - (best ? 1 : 0), this.orphan.count - (best ? 1 : 0),
this.orphan.size - (best ? best.getSize() : 0)); this.orphan.size - (best ? best.data.length : 0));
Object.keys(this.orphan.bmap).forEach(function(hash) { Object.keys(this.orphan.bmap).forEach(function(hash) {
var orphan = self.orphan.bmap[hash]; var orphan = self.orphan.bmap[hash];
@ -1374,7 +1383,7 @@ Chain.prototype.pruneOrphans = function pruneOrphans(peer) {
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++;
this.orphan.size += best.getSize(); this.orphan.size += best.data.length;
}; };
Chain.prototype.purgePending = function purgePending() { Chain.prototype.purgePending = function purgePending() {