chain: refactor orphan list.
This commit is contained in:
parent
f3bd64689d
commit
3ada25564e
@ -88,12 +88,10 @@ function Chain(options) {
|
|||||||
this.state = new DeploymentState();
|
this.state = new DeploymentState();
|
||||||
this._time = util.hrtime();
|
this._time = util.hrtime();
|
||||||
|
|
||||||
this.orphan = {
|
this.orphanMap = {};
|
||||||
map: {},
|
this.orphanPrev = {};
|
||||||
bmap: {},
|
this.orphanCount = 0;
|
||||||
count: 0,
|
this.orphanSize = 0;
|
||||||
size: 0
|
|
||||||
};
|
|
||||||
|
|
||||||
this._init();
|
this._init();
|
||||||
}
|
}
|
||||||
@ -533,12 +531,12 @@ Chain.prototype.setDeploymentState = function setDeploymentState(state) {
|
|||||||
if (!this.state.hasCLTV() && state.hasCLTV())
|
if (!this.state.hasCLTV() && state.hasCLTV())
|
||||||
this.logger.warning('BIP65 has been activated.');
|
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())
|
if (!this.state.hasCSV() && state.hasCSV())
|
||||||
this.logger.warning('CSV has been activated.');
|
this.logger.warning('CSV has been activated.');
|
||||||
|
|
||||||
|
if (!this.state.hasWitness() && state.hasWitness())
|
||||||
|
this.logger.warning('Segwit has been activated.');
|
||||||
|
|
||||||
this.state = state;
|
this.state = state;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1433,7 +1431,7 @@ Chain.prototype.verifyCheckpoint = function verifyCheckpoint(hash, height) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Chain.prototype.seenOrphan = function seenOrphan(block) {
|
Chain.prototype.seenOrphan = function seenOrphan(block) {
|
||||||
var orphan = this.orphan.map[block.prevBlock];
|
var orphan = this.orphanPrev[block.prevBlock];
|
||||||
var hash;
|
var hash;
|
||||||
|
|
||||||
if (!orphan)
|
if (!orphan)
|
||||||
@ -1466,10 +1464,10 @@ Chain.prototype.storeOrphan = function storeOrphan(block) {
|
|||||||
var hash = block.hash('hex');
|
var hash = block.hash('hex');
|
||||||
var height = block.getCoinbaseHeight();
|
var height = block.getCoinbaseHeight();
|
||||||
|
|
||||||
this.orphan.count++;
|
this.orphanCount++;
|
||||||
this.orphan.size += block.getSize();
|
this.orphanSize += block.getSize();
|
||||||
this.orphan.map[block.prevBlock] = block;
|
this.orphanPrev[block.prevBlock] = block;
|
||||||
this.orphan.bmap[hash] = block;
|
this.orphanMap[hash] = block;
|
||||||
|
|
||||||
// Update the best height based on the coinbase.
|
// Update the best height based on the coinbase.
|
||||||
// We do this even for orphans (peers will send
|
// 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) {
|
Chain.prototype.resolveOrphan = function resolveOrphan(hash) {
|
||||||
var block = this.orphan.map[hash];
|
var block = this.orphanPrev[hash];
|
||||||
|
|
||||||
if (!block)
|
if (!block)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
delete this.orphan.bmap[block.hash('hex')];
|
delete this.orphanMap[block.hash('hex')];
|
||||||
delete this.orphan.map[hash];
|
delete this.orphanPrev[hash];
|
||||||
|
|
||||||
this.orphan.count--;
|
this.orphanCount--;
|
||||||
this.orphan.size -= block.getSize();
|
this.orphanSize -= block.getSize();
|
||||||
|
|
||||||
return block;
|
return block;
|
||||||
};
|
};
|
||||||
@ -1508,16 +1506,16 @@ Chain.prototype.resolveOrphan = function resolveOrphan(hash) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Chain.prototype.purgeOrphans = function purgeOrphans() {
|
Chain.prototype.purgeOrphans = function purgeOrphans() {
|
||||||
var count = this.orphan.count;
|
var count = this.orphanCount;
|
||||||
var size = this.orphan.size;
|
var size = this.orphanSize;
|
||||||
|
|
||||||
if (count === 0)
|
if (count === 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this.orphan.map = {};
|
this.orphanPrev = {};
|
||||||
this.orphan.bmap = {};
|
this.orphanMap = {};
|
||||||
this.orphan.count = 0;
|
this.orphanCount = 0;
|
||||||
this.orphan.size = 0;
|
this.orphanSize = 0;
|
||||||
|
|
||||||
this.emit('purge', count, size);
|
this.emit('purge', count, size);
|
||||||
};
|
};
|
||||||
@ -1530,20 +1528,20 @@ Chain.prototype.purgeOrphans = function purgeOrphans() {
|
|||||||
Chain.prototype.pruneOrphans = function pruneOrphans() {
|
Chain.prototype.pruneOrphans = function pruneOrphans() {
|
||||||
var i, hashes, hash, orphan, height, best, last;
|
var i, hashes, hash, orphan, height, best, last;
|
||||||
|
|
||||||
if (this.orphan.size <= this.orphanLimit)
|
if (this.orphanSize <= this.orphanLimit)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
hashes = Object.keys(this.orphan.map);
|
hashes = Object.keys(this.orphanPrev);
|
||||||
|
|
||||||
if (hashes.length === 0)
|
if (hashes.length === 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (i = 0; i < hashes.length; i++) {
|
for (i = 0; i < hashes.length; i++) {
|
||||||
hash = hashes[i];
|
hash = hashes[i];
|
||||||
orphan = this.orphan.map[hash];
|
orphan = this.orphanPrev[hash];
|
||||||
height = orphan.getCoinbaseHeight();
|
height = orphan.getCoinbaseHeight();
|
||||||
|
|
||||||
delete this.orphan.map[hash];
|
delete this.orphanPrev[hash];
|
||||||
|
|
||||||
if (!best || height > best.getCoinbaseHeight())
|
if (!best || height > best.getCoinbaseHeight())
|
||||||
best = orphan;
|
best = orphan;
|
||||||
@ -1556,26 +1554,26 @@ Chain.prototype.pruneOrphans = function pruneOrphans() {
|
|||||||
if (best.getCoinbaseHeight() <= 0)
|
if (best.getCoinbaseHeight() <= 0)
|
||||||
best = last;
|
best = last;
|
||||||
|
|
||||||
hashes = Object.keys(this.orphan.bmap);
|
hashes = Object.keys(this.orphanMap);
|
||||||
|
|
||||||
for (i = 0; i < hashes.length; i++) {
|
for (i = 0; i < hashes.length; i++) {
|
||||||
hash = hashes[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)
|
if (orphan !== best)
|
||||||
this.emit('unresolved', orphan);
|
this.emit('unresolved', orphan);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit('purge',
|
this.emit('purge',
|
||||||
this.orphan.count - 1,
|
this.orphanCount - 1,
|
||||||
this.orphan.size - best.getSize());
|
this.orphanSize - best.getSize());
|
||||||
|
|
||||||
this.orphan.map[best.prevBlock] = best;
|
this.orphanPrev[best.prevBlock] = best;
|
||||||
this.orphan.bmap[best.hash('hex')] = best;
|
this.orphanMap[best.hash('hex')] = best;
|
||||||
this.orphan.count = 1;
|
this.orphanCount = 1;
|
||||||
this.orphan.size = best.getSize();
|
this.orphanSize = best.getSize();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
@ -1721,7 +1719,7 @@ Chain.prototype.getEntry = function getEntry(hash) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Chain.prototype.getOrphan = function getOrphan(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);
|
assert(hash);
|
||||||
|
|
||||||
while (this.orphan.bmap[hash]) {
|
while (this.orphanMap[hash]) {
|
||||||
root = hash;
|
root = hash;
|
||||||
hash = this.orphan.bmap[hash].prevBlock;
|
hash = this.orphanMap[hash].prevBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
|
|||||||
@ -1121,7 +1121,7 @@ Pool.prototype.handleBlock = co(function* handleBlock(block, peer) {
|
|||||||
this.chain.bestHeight,
|
this.chain.bestHeight,
|
||||||
(this.chain.getProgress() * 100).toFixed(2) + '%',
|
(this.chain.getProgress() * 100).toFixed(2) + '%',
|
||||||
this.chain.total,
|
this.chain.total,
|
||||||
this.chain.orphan.count,
|
this.chain.orphanCount,
|
||||||
this.activeBlocks,
|
this.activeBlocks,
|
||||||
peer.queueBlock.size,
|
peer.queueBlock.size,
|
||||||
block.bits,
|
block.bits,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user