From 322658c739b9bd15262e1fd6baed4ff69d530df0 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 | 22 +++++++++++++--------- lib/net/pool.js | 4 +--- lib/node/neutrino.js | 4 +++- lib/protocol/networks.js | 2 +- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index 907f2eb3b..ed27f2aca 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,6 +2000,8 @@ class Chain extends AsyncEmitter { */ maybeSync() { + // console.log('maybeSync'); + // console.log(this.synced); if (this.synced) return; @@ -2008,12 +2010,14 @@ class Chain extends AsyncEmitter { return; } - if (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; this.synced = true; this.emit('full'); } @@ -2144,7 +2148,7 @@ class Chain extends AsyncEmitter { assert(hash); - for (;;) { + for (; ;) { const orphan = this.orphanMap.get(hash); if (!orphan) @@ -2221,8 +2225,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 +2459,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 +2644,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..a54b80ff7 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'); } @@ -2472,10 +2473,7 @@ class Pool extends EventEmitter { async addBlock(peer, block, flags) { const hash = block.hash(); - console.log(hash); - console.log('addBlock'); const unlock = await this.locker.lock(hash); - console.log('lockedHash'); try { return await this._addBlock(peer, block, flags); } finally { 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 };