diff --git a/lib/chain/chain.js b/lib/chain/chain.js index 5fd07c10..53815c71 100644 --- a/lib/chain/chain.js +++ b/lib/chain/chain.js @@ -748,46 +748,48 @@ Chain.prototype.findFork = co(function* findFork(fork, longer) { * Called when a competing chain with a higher chainwork * is received. * @private - * @param {ChainEntry} entry - The competing chain's tip. + * @param {ChainEntry} competitor - The competing chain's tip. * @param {Block|MerkleBlock} block - The being being added. * @returns {Promise} */ -Chain.prototype.reorganize = co(function* reorganize(entry, block) { +Chain.prototype.reorganize = co(function* reorganize(competitor, block) { var tip = this.tip; - var fork = yield this.findFork(tip, entry); + var fork = yield this.findFork(tip, competitor); var disconnect = []; var connect = []; - var i, e; + var i, entry; assert(fork); - // Disconnect blocks/txs. - e = tip; - while (e.hash !== fork.hash) { - disconnect.push(e); - e = yield e.getPrevious(); - assert(e); + // Blocks to disconnect. + entry = tip; + while (entry.hash !== fork.hash) { + disconnect.push(entry); + entry = yield entry.getPrevious(); + assert(entry); } + // Blocks to connect. + entry = competitor; + while (entry.hash !== fork.hash) { + connect.push(entry); + entry = yield entry.getPrevious(); + assert(entry); + } + + // Disconnect blocks/txs. for (i = 0; i < disconnect.length; i++) { - e = disconnect[i]; - yield this.disconnect(e); - } - - // Disconnect blocks/txs. - e = entry; - while (e.hash !== fork.hash) { - connect.push(e); - e = yield e.getPrevious(); - assert(e); + entry = disconnect[i]; + yield this.disconnect(entry); } + // Connect blocks/txs. // We don't want to connect the new tip here. // That will be done outside in setBestChain. for (i = connect.length - 1; i >= 1; i--) { - e = connect[i]; - yield this.reconnect(e); + entry = connect[i]; + yield this.reconnect(entry); } this.emit('reorganize', block, tip.height, tip.hash);