chain: refactor orphan list.

This commit is contained in:
Christopher Jeffrey 2016-12-24 13:34:16 -08:00
parent f3bd64689d
commit 3ada25564e
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 40 additions and 42 deletions

View File

@ -88,12 +88,10 @@ function Chain(options) {
this.state = new DeploymentState();
this._time = util.hrtime();
this.orphan = {
map: {},
bmap: {},
count: 0,
size: 0
};
this.orphanMap = {};
this.orphanPrev = {};
this.orphanCount = 0;
this.orphanSize = 0;
this._init();
}
@ -533,12 +531,12 @@ Chain.prototype.setDeploymentState = function setDeploymentState(state) {
if (!this.state.hasCLTV() && state.hasCLTV())
this.logger.warning('BIP65 has been activated.');
if (!this.state.hasWitness() && state.hasWitness())
this.logger.warning('Segwit has been activated.');
if (!this.state.hasCSV() && state.hasCSV())
this.logger.warning('CSV has been activated.');
if (!this.state.hasWitness() && state.hasWitness())
this.logger.warning('Segwit has been activated.');
this.state = state;
};
@ -1433,7 +1431,7 @@ Chain.prototype.verifyCheckpoint = function verifyCheckpoint(hash, height) {
*/
Chain.prototype.seenOrphan = function seenOrphan(block) {
var orphan = this.orphan.map[block.prevBlock];
var orphan = this.orphanPrev[block.prevBlock];
var hash;
if (!orphan)
@ -1466,10 +1464,10 @@ Chain.prototype.storeOrphan = function storeOrphan(block) {
var hash = block.hash('hex');
var height = block.getCoinbaseHeight();
this.orphan.count++;
this.orphan.size += block.getSize();
this.orphan.map[block.prevBlock] = block;
this.orphan.bmap[hash] = block;
this.orphanCount++;
this.orphanSize += block.getSize();
this.orphanPrev[block.prevBlock] = block;
this.orphanMap[hash] = block;
// Update the best height based on the coinbase.
// We do this even for orphans (peers will send
@ -1489,16 +1487,16 @@ Chain.prototype.storeOrphan = function storeOrphan(block) {
*/
Chain.prototype.resolveOrphan = function resolveOrphan(hash) {
var block = this.orphan.map[hash];
var block = this.orphanPrev[hash];
if (!block)
return;
delete this.orphan.bmap[block.hash('hex')];
delete this.orphan.map[hash];
delete this.orphanMap[block.hash('hex')];
delete this.orphanPrev[hash];
this.orphan.count--;
this.orphan.size -= block.getSize();
this.orphanCount--;
this.orphanSize -= block.getSize();
return block;
};
@ -1508,16 +1506,16 @@ Chain.prototype.resolveOrphan = function resolveOrphan(hash) {
*/
Chain.prototype.purgeOrphans = function purgeOrphans() {
var count = this.orphan.count;
var size = this.orphan.size;
var count = this.orphanCount;
var size = this.orphanSize;
if (count === 0)
return;
this.orphan.map = {};
this.orphan.bmap = {};
this.orphan.count = 0;
this.orphan.size = 0;
this.orphanPrev = {};
this.orphanMap = {};
this.orphanCount = 0;
this.orphanSize = 0;
this.emit('purge', count, size);
};
@ -1530,20 +1528,20 @@ Chain.prototype.purgeOrphans = function purgeOrphans() {
Chain.prototype.pruneOrphans = function pruneOrphans() {
var i, hashes, hash, orphan, height, best, last;
if (this.orphan.size <= this.orphanLimit)
if (this.orphanSize <= this.orphanLimit)
return false;
hashes = Object.keys(this.orphan.map);
hashes = Object.keys(this.orphanPrev);
if (hashes.length === 0)
return false;
for (i = 0; i < hashes.length; i++) {
hash = hashes[i];
orphan = this.orphan.map[hash];
orphan = this.orphanPrev[hash];
height = orphan.getCoinbaseHeight();
delete this.orphan.map[hash];
delete this.orphanPrev[hash];
if (!best || height > best.getCoinbaseHeight())
best = orphan;
@ -1556,26 +1554,26 @@ Chain.prototype.pruneOrphans = function pruneOrphans() {
if (best.getCoinbaseHeight() <= 0)
best = last;
hashes = Object.keys(this.orphan.bmap);
hashes = Object.keys(this.orphanMap);
for (i = 0; i < hashes.length; i++) {
hash = hashes[i];
orphan = this.orphan.bmap[hash];
orphan = this.orphanMap[hash];
delete this.orphan.bmap[hash];
delete this.orphanMap[hash];
if (orphan !== best)
this.emit('unresolved', orphan);
}
this.emit('purge',
this.orphan.count - 1,
this.orphan.size - best.getSize());
this.orphanCount - 1,
this.orphanSize - best.getSize());
this.orphan.map[best.prevBlock] = best;
this.orphan.bmap[best.hash('hex')] = best;
this.orphan.count = 1;
this.orphan.size = best.getSize();
this.orphanPrev[best.prevBlock] = best;
this.orphanMap[best.hash('hex')] = best;
this.orphanCount = 1;
this.orphanSize = best.getSize();
return true;
};
@ -1721,7 +1719,7 @@ Chain.prototype.getEntry = function getEntry(hash) {
*/
Chain.prototype.getOrphan = function getOrphan(hash) {
return this.orphan.bmap[hash] || null;
return this.orphanMap[hash] || null;
};
/**
@ -1892,9 +1890,9 @@ Chain.prototype.getOrphanRoot = function getOrphanRoot(hash) {
assert(hash);
while (this.orphan.bmap[hash]) {
while (this.orphanMap[hash]) {
root = hash;
hash = this.orphan.bmap[hash].prevBlock;
hash = this.orphanMap[hash].prevBlock;
}
return root;

View File

@ -1121,7 +1121,7 @@ Pool.prototype.handleBlock = co(function* handleBlock(block, peer) {
this.chain.bestHeight,
(this.chain.getProgress() * 100).toFixed(2) + '%',
this.chain.total,
this.chain.orphan.count,
this.chain.orphanCount,
this.activeBlocks,
peer.queueBlock.size,
block.bits,