Skip to content

Commit

Permalink
feat: headers first sync complete
Browse files Browse the repository at this point in the history
  • Loading branch information
masterchief164 committed Jun 14, 2023
1 parent cc6d3d8 commit 322658c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
22 changes: 13 additions & 9 deletions lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -2000,6 +2000,8 @@ class Chain extends AsyncEmitter {
*/

maybeSync() {
// console.log('maybeSync');
// console.log(this.synced);
if (this.synced)
return;

Expand All @@ -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');
}
Expand Down Expand Up @@ -2144,7 +2148,7 @@ class Chain extends AsyncEmitter {

assert(hash);

for (;;) {
for (; ;) {
const orphan = this.orphanMap.get(hash);

if (!orphan)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions lib/net/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion lib/node/neutrino.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/protocol/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ regtest.block = {
bip66hash: null,
pruneAfterHeight: 1000,
keepBlocks: 10000,
maxTipAge: 0xffffffff,
maxTipAge: 24 * 60 * 60,
slowHeight: 0
};

Expand Down

0 comments on commit 322658c

Please sign in to comment.