From 4faf0cfc1c53a76b23323777bd7c961053f1d63f Mon Sep 17 00:00:00 2001 From: masterchief164 <63920595+masterchief164@users.noreply.github.com> Date: Wed, 14 Jun 2023 19:46:22 +0530 Subject: [PATCH] feat: headers first sync complete --- lib/blockchain/chain.js | 30 ++++++++++++++++++++---------- lib/net/pool.js | 1 + lib/node/neutrino.js | 4 +++- lib/protocol/networks.js | 2 +- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index 907f2eb3b..1688463f9 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -583,7 +583,7 @@ class Chain extends AsyncEmitter { // UASF is now enforced (bip148) (mainnet-only). if (this.options.bip148 && this.network === Network.main) { if (witness !== thresholdStates.LOCKED_IN - && witness !== thresholdStates.ACTIVE) { + && witness !== thresholdStates.ACTIVE) { // The BIP148 MTP check is nonsensical in // that it includes the _current_ entry's // timestamp. This requires some hackery, @@ -2000,20 +2000,30 @@ class Chain extends AsyncEmitter { */ maybeSync() { + // console.log('maybeSync'); + // console.log(this.synced); if (this.synced) return; + console.log('checkpoints'); if (this.options.checkpoints) { if (this.height < this.network.lastCheckpoint) return; } - - if (this.tip.time < util.now() - this.network.block.maxTipAge) + console.log(this.tip.time); + console.log(util.now() - this.network.block.maxTipAge); + console.log(this.network.block); + console.log(this.network.block.maxTipAge); + console.log(this.tip.time < util.now() - this.network.block.maxTipAge); + if (this.options.neutrino && this.tip.time < util.now() - 24 * 60 * 60) return; - - if (!this.hasChainwork()) + else if (!this.options.neutrino && + this.tip.time < util.now() - this.network.block.maxTipAge) return; + if (!this.options.neutrino && !this.hasChainwork()) + return; + console.log('full\n\n\n'); this.synced = true; this.emit('full'); } @@ -2144,7 +2154,7 @@ class Chain extends AsyncEmitter { assert(hash); - for (;;) { + for (; ;) { const orphan = this.orphanMap.get(hash); if (!orphan) @@ -2221,8 +2231,8 @@ class Chain extends AsyncEmitter { return pow.bits; while (prev.height !== 0 - && prev.height % pow.retargetInterval !== 0 - && prev.bits === pow.bits) { + && prev.height % pow.retargetInterval !== 0 + && prev.bits === pow.bits) { const cache = this.getPrevCache(prev); if (cache) @@ -2455,7 +2465,7 @@ class Chain extends AsyncEmitter { const state = await this.getState(prev, deployment); if (state === thresholdStates.LOCKED_IN - || state === thresholdStates.STARTED) { + || state === thresholdStates.STARTED) { version |= 1 << deployment.bit; } } @@ -2640,7 +2650,7 @@ class ChainOptions { fromOptions(options) { if (!options.spv) { assert(options.blocks && typeof options.blocks === 'object', - 'Chain requires a blockstore.'); + 'Chain requires a blockstore.'); } this.blocks = options.blocks; diff --git a/lib/net/pool.js b/lib/net/pool.js index 0919112eb..7dafd5b2f 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -786,6 +786,7 @@ class Pool extends EventEmitter { this.logger.info('No loader peer.'); return; } + this.chain.synced = false; peer.sendGetHeaders(locator); // this.emit('headersFull'); } diff --git a/lib/node/neutrino.js b/lib/node/neutrino.js index b98db92ed..7b9519e56 100644 --- a/lib/node/neutrino.js +++ b/lib/node/neutrino.js @@ -164,7 +164,9 @@ class Neutrino extends Node { this.emit('reset', tip); }); - this.pool.on('headersFull', () => { + this.chain.on('full', () => { + if (this.chain.height === 0) + return; this.logger.info('Block Headers are fully synced'); console.log('Block Headers are fully synced \n\n\n\n\n'); // this.pool.startFilterCheckPtSync(); // TODO: Maybe implement this later diff --git a/lib/protocol/networks.js b/lib/protocol/networks.js index 16e6bedf7..8c2db9e8e 100644 --- a/lib/protocol/networks.js +++ b/lib/protocol/networks.js @@ -792,7 +792,7 @@ regtest.block = { bip66hash: null, pruneAfterHeight: 1000, keepBlocks: 10000, - maxTipAge: 0xffffffff, + maxTipAge: 24 * 60 * 60, slowHeight: 0 };