Merge pull request #16 from oipwg/nlr
add nlr check to reorganize function
This commit is contained in:
commit
a7486eaae1
@ -855,6 +855,9 @@ class Chain extends AsyncEmitter {
|
|||||||
|
|
||||||
assert(fork, 'No free space or data corruption.');
|
assert(fork, 'No free space or data corruption.');
|
||||||
|
|
||||||
|
// Check NLR
|
||||||
|
if (await this.noLongReorg(tip, fork, competitor)) return true
|
||||||
|
|
||||||
// Blocks to disconnect.
|
// Blocks to disconnect.
|
||||||
const disconnect = [];
|
const disconnect = [];
|
||||||
let entry = tip;
|
let entry = tip;
|
||||||
@ -912,6 +915,9 @@ class Chain extends AsyncEmitter {
|
|||||||
|
|
||||||
assert(fork, 'No free space or data corruption.');
|
assert(fork, 'No free space or data corruption.');
|
||||||
|
|
||||||
|
// Check NLR
|
||||||
|
if (await this.noLongReorg(tip, fork, competitor)) return true
|
||||||
|
|
||||||
// Buffer disconnected blocks.
|
// Buffer disconnected blocks.
|
||||||
const disconnect = [];
|
const disconnect = [];
|
||||||
let entry = tip;
|
let entry = tip;
|
||||||
@ -950,6 +956,48 @@ class Chain extends AsyncEmitter {
|
|||||||
return this.emitAsync('reorganize', tip, competitor);
|
return this.emitAsync('reorganize', tip, competitor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a reorganization breaks the set nlrLimit,
|
||||||
|
* if it does, it invalidates necessary blocks and returns true
|
||||||
|
* else return false
|
||||||
|
* @param {ChainEntry} tip - Current tip of this chain.
|
||||||
|
* @param {ChainEntry} fork - The tip of the fork.
|
||||||
|
* @param {ChainEntry} competitor - The competing chain's tip.
|
||||||
|
* @returns {Promise<boolean>}
|
||||||
|
*/
|
||||||
|
async noLongReorg (tip, fork, competitor) {
|
||||||
|
if (tip.height - fork.height >= this.network.nlrLimit) {
|
||||||
|
if (this.network.nlrLimit !== 0) {
|
||||||
|
this.logger.warning(
|
||||||
|
'NLR Activated. Preventing reorganization: current=%h(%d) competitor=%h(%d) fork=%h(%d) reorg_size=%d nlr=%d',
|
||||||
|
tip.hash,
|
||||||
|
tip.height,
|
||||||
|
competitor.hash,
|
||||||
|
competitor.height,
|
||||||
|
fork.hash,
|
||||||
|
fork.height,
|
||||||
|
tip.height - fork.height,
|
||||||
|
this.network.nlrLimit
|
||||||
|
);
|
||||||
|
|
||||||
|
let indexWalk = competitor
|
||||||
|
|
||||||
|
// mark invalid_child from tip of competitor to first block of fork
|
||||||
|
while (indexWalk.height > fork.height) {
|
||||||
|
await this.setInvalid(indexWalk.hash)
|
||||||
|
indexWalk = await this.getPrevious(indexWalk)
|
||||||
|
}
|
||||||
|
|
||||||
|
// check
|
||||||
|
indexWalk = await this.getPrevious(indexWalk)
|
||||||
|
assert.strictEqual(indexWalk, fork)
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disconnect an entry from the chain (updates the tip).
|
* Disconnect an entry from the chain (updates the tip).
|
||||||
* @param {ChainEntry} entry
|
* @param {ChainEntry} entry
|
||||||
|
|||||||
@ -58,6 +58,7 @@ class Network {
|
|||||||
this.maxFeeRate = options.maxFeeRate;
|
this.maxFeeRate = options.maxFeeRate;
|
||||||
this.selfConnect = options.selfConnect;
|
this.selfConnect = options.selfConnect;
|
||||||
this.requestMempool = options.requestMempool;
|
this.requestMempool = options.requestMempool;
|
||||||
|
this.nlrLimit = options.nlrLimit;
|
||||||
this.time = new TimeData();
|
this.time = new TimeData();
|
||||||
|
|
||||||
this.init();
|
this.init();
|
||||||
|
|||||||
@ -563,6 +563,8 @@ main.selfConnect = false;
|
|||||||
|
|
||||||
main.requestMempool = false;
|
main.requestMempool = false;
|
||||||
|
|
||||||
|
main.nlrLimit = 100 // 100 * ~40s = ~66 minutes
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Testnet (v3)
|
* Testnet (v3)
|
||||||
* https://en.bitcoin.it/wiki/Testnet
|
* https://en.bitcoin.it/wiki/Testnet
|
||||||
@ -800,6 +802,8 @@ testnet.selfConnect = false;
|
|||||||
|
|
||||||
testnet.requestMempool = false;
|
testnet.requestMempool = false;
|
||||||
|
|
||||||
|
testnet.nlrLimit = 50; // 50 * ~40s = ~33 minutes
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Regtest
|
* Regtest
|
||||||
*/
|
*/
|
||||||
@ -1019,6 +1023,8 @@ regtest.selfConnect = true;
|
|||||||
|
|
||||||
regtest.requestMempool = true;
|
regtest.requestMempool = true;
|
||||||
|
|
||||||
|
regtest.nlrLimit = 10;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Simnet (btcd)
|
* Simnet (btcd)
|
||||||
*/
|
*/
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user