From ff5eddbebbbadbe64486571696551db6f6e5e84c Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 30 Nov 2016 01:05:28 -0800 Subject: [PATCH] chain: move some methods. --- lib/blockchain/chain.js | 81 ++++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index d9cce0fb..addbb087 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -1200,40 +1200,6 @@ Chain.prototype.add = co(function* add(block) { } }); -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; - - // Update the best height based on the coinbase. - // We do this even for orphans (peers will send - // us their highest block during the initial - // getblocks sync, making it an orphan). - if (height > this.bestHeight) - this.bestHeight = height; - - this.emit('orphan', block, height); -}; - -Chain.prototype.resolveOrphan = function resolveOrphan(hash) { - var block = this.orphan.map[hash]; - - if (!block) - return; - - delete this.orphan.bmap[block.hash('hex')]; - delete this.orphan.map[hash]; - - this.orphan.count--; - this.orphan.size -= block.getSize(); - - return block; -}; - /** * Add a block to the chain without a lock. * @private @@ -1480,6 +1446,53 @@ Chain.prototype.finish = function finish(block, entry) { time); }; +/** + * Store an orphan. + * @private + * @param {Block} block + */ + +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; + + // Update the best height based on the coinbase. + // We do this even for orphans (peers will send + // us their highest block during the initial + // getblocks sync, making it an orphan). + if (height > this.bestHeight) + this.bestHeight = height; + + this.emit('orphan', block, height); +}; + +/** + * Resolve an orphan. + * @private + * @param {Hash} hash - Previous block hash. + * @returns {Block} + */ + +Chain.prototype.resolveOrphan = function resolveOrphan(hash) { + var block = this.orphan.map[hash]; + + if (!block) + return; + + delete this.orphan.bmap[block.hash('hex')]; + delete this.orphan.map[hash]; + + this.orphan.count--; + this.orphan.size -= block.getSize(); + + return block; +}; + /** * Purge any waiting orphans. */