chain: refactor reorgs.
This commit is contained in:
parent
b0d3ad8452
commit
e2d51352a8
@ -748,46 +748,48 @@ Chain.prototype.findFork = co(function* findFork(fork, longer) {
|
|||||||
* Called when a competing chain with a higher chainwork
|
* Called when a competing chain with a higher chainwork
|
||||||
* is received.
|
* is received.
|
||||||
* @private
|
* @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.
|
* @param {Block|MerkleBlock} block - The being being added.
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Chain.prototype.reorganize = co(function* reorganize(entry, block) {
|
Chain.prototype.reorganize = co(function* reorganize(competitor, block) {
|
||||||
var tip = this.tip;
|
var tip = this.tip;
|
||||||
var fork = yield this.findFork(tip, entry);
|
var fork = yield this.findFork(tip, competitor);
|
||||||
var disconnect = [];
|
var disconnect = [];
|
||||||
var connect = [];
|
var connect = [];
|
||||||
var i, e;
|
var i, entry;
|
||||||
|
|
||||||
assert(fork);
|
assert(fork);
|
||||||
|
|
||||||
// Disconnect blocks/txs.
|
// Blocks to disconnect.
|
||||||
e = tip;
|
entry = tip;
|
||||||
while (e.hash !== fork.hash) {
|
while (entry.hash !== fork.hash) {
|
||||||
disconnect.push(e);
|
disconnect.push(entry);
|
||||||
e = yield e.getPrevious();
|
entry = yield entry.getPrevious();
|
||||||
assert(e);
|
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++) {
|
for (i = 0; i < disconnect.length; i++) {
|
||||||
e = disconnect[i];
|
entry = disconnect[i];
|
||||||
yield this.disconnect(e);
|
yield this.disconnect(entry);
|
||||||
}
|
|
||||||
|
|
||||||
// Disconnect blocks/txs.
|
|
||||||
e = entry;
|
|
||||||
while (e.hash !== fork.hash) {
|
|
||||||
connect.push(e);
|
|
||||||
e = yield e.getPrevious();
|
|
||||||
assert(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Connect blocks/txs.
|
||||||
// We don't want to connect the new tip here.
|
// We don't want to connect the new tip here.
|
||||||
// That will be done outside in setBestChain.
|
// That will be done outside in setBestChain.
|
||||||
for (i = connect.length - 1; i >= 1; i--) {
|
for (i = connect.length - 1; i >= 1; i--) {
|
||||||
e = connect[i];
|
entry = connect[i];
|
||||||
yield this.reconnect(e);
|
yield this.reconnect(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit('reorganize', block, tip.height, tip.hash);
|
this.emit('reorganize', block, tip.height, tip.hash);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user