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 4faf0cf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
30 changes: 20 additions & 10 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,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');
}
Expand Down Expand Up @@ -2144,7 +2154,7 @@ class Chain extends AsyncEmitter {

assert(hash);

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

if (!orphan)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 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
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 4faf0cf

Please sign in to comment.