add nlr check to reorganize function

This commit is contained in:
ohryan 2020-07-15 17:45:56 -07:00
parent f3606f2fe6
commit 430276b592
3 changed files with 41 additions and 3 deletions

View File

@ -855,6 +855,37 @@ class Chain extends AsyncEmitter {
assert(fork, 'No free space or data corruption.');
// Check NLR (No Large Reorganization)
if (tip.height - fork.height >= this.nlrLimit) {
if (this.nlrLimit !== 0) {
this.logger.warning(
'NLR Activated: current=%h(%d) fork=%h(%d) reorg_size=%d',
tip.hash,
tip.height,
competitor.hash,
competitor.height,
tip.height - fork.height
);
const entryWithMostWork = (tip.height > competitor.height) ? tip : competitor
// mark invalid_child from tip of fork to second block of fork
while (entryWithMostWork.height > fork.height + 2) {
const previous = await this.getPrevious(entryWithMostWork)
this.invalidate(previous.hash)
}
// mark invalid first block of fork
const previous = await this.getPrevious(entryWithMostWork)
this.invalidate(previous.hash)
// check
assert(this.getPrevious(previous), fork)
return true
}
}
// Blocks to disconnect.
const disconnect = [];
let entry = tip;

View File

@ -58,6 +58,7 @@ class Network {
this.maxFeeRate = options.maxFeeRate;
this.selfConnect = options.selfConnect;
this.requestMempool = options.requestMempool;
this.nlrLimit = options.nlrLimit;
this.time = new TimeData();
this.init();

View File

@ -563,6 +563,8 @@ main.selfConnect = false;
main.requestMempool = false;
main.nlrLimit = 100 // 100 * ~40s = ~66 minutes
/*
* Testnet (v3)
* https://en.bitcoin.it/wiki/Testnet
@ -800,6 +802,8 @@ testnet.selfConnect = false;
testnet.requestMempool = false;
testnet.nlrLimit = 50; // 50 * ~40s = ~33 minutes
/*
* Regtest
*/
@ -1019,6 +1023,8 @@ regtest.selfConnect = true;
regtest.requestMempool = true;
regtest.nlrLimit = 10;
/*
* Simnet (btcd)
*/