Skip to content

Commit a19e1b2

Browse files
feat: CFHeaders sync complete
1 parent 63c1dd9 commit a19e1b2

File tree

4 files changed

+59
-21
lines changed

4 files changed

+59
-21
lines changed

bin/neutrino

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ const node = new Neutrino({
1818
logLevel: 'debug',
1919
db: 'leveldb',
2020
memory: false,
21-
persistent: true,
2221
workers: true,
23-
listen: true,
2422
loader: require
2523
});
2624

@@ -42,3 +40,11 @@ if (!node.config.bool('no-wallet') && !node.has('walletdb')) {
4240
console.error(err.stack);
4341
process.exit(1);
4442
});
43+
44+
process.on('unhandledRejection', (err, promise) => {
45+
throw err;
46+
});
47+
48+
process.on('SIGINT', async () => {
49+
await node.close();
50+
});

lib/blockchain/chain.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,26 +2000,27 @@ class Chain extends AsyncEmitter {
20002000
*/
20012001

20022002
maybeSync() {
2003-
// console.log('maybeSync');
2004-
// console.log(this.synced);
20052003
if (this.synced)
20062004
return;
20072005

20082006
if (this.options.checkpoints) {
20092007
if (this.height < this.network.lastCheckpoint)
20102008
return;
20112009
}
2012-
2013-
if (this.options.neutrino && this.tip.time < util.now() - 24 * 60 * 60)
2010+
if (this.options.neutrino && this.tip.time < 1686851917)
2011+
// TODO change this later
20142012
return;
20152013
else if (!this.options.neutrino &&
20162014
this.tip.time < util.now() - this.network.block.maxTipAge)
20172015
return;
20182016

2019-
if (!this.options.neutrino && !this.hasChainwork())
2017+
if (!this.hasChainwork())
20202018
return;
20212019
this.synced = true;
2022-
this.emit('full');
2020+
if (this.options.neutrino)
2021+
this.emit('headersFull');
2022+
else
2023+
this.emit('full');
20232024
}
20242025

20252026
/**

lib/net/pool.js

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,10 @@ class Pool extends EventEmitter {
223223

224224
const tip = this.chain.tip;
225225
if (this.options.neutrino) {
226-
// this.headerTip = tip;
227-
this.headerChain.push(new HeaderEntry(tip.hash, tip.height));
228-
return;
226+
this.headerChain.push(new HeaderEntry(tip.hash, tip.height));
227+
this.cfHeaderChain = new List();
228+
this.cfHeaderChain.push(new CFHeaderEntry(consensus.ZERO_HASH, 0));
229+
return;
229230
}
230231
if (tip.height < this.network.lastCheckpoint) {
231232
this.checkpoints = true;
@@ -648,7 +649,7 @@ class Pool extends EventEmitter {
648649

649650
peer.loader = true;
650651
this.peers.load = peer;
651-
this.sendSync(peer);
652+
this.sendSync(peer);
652653

653654
this.emit('loader', peer);
654655
}
@@ -730,8 +731,11 @@ class Pool extends EventEmitter {
730731
return;
731732

732733
this.filterSyncing = true;
733-
const startHeight = 0;
734-
const stopHash = await this.chain.getHash(29);
734+
const startHeight = 1;
735+
const chainHeight = await this.chain.tip.height;
736+
console.log('chainHeight', chainHeight);
737+
const stopHeight = chainHeight > 2000 ? 2000 : chainHeight;
738+
const stopHash = await this.chain.getHash(stopHeight);
735739
this.peers.load.sendGetCFHeaders(
736740
common.FILTERS.BASIC,
737741
startHeight,
@@ -772,7 +776,7 @@ class Pool extends EventEmitter {
772776
async startHeadersSync() {
773777
if (!this.syncing)
774778
return;
775-
779+
console.log('startHeadersSync');
776780
let locator;
777781
try {
778782
locator = await this.chain.getLocator();
@@ -2156,7 +2160,7 @@ class Pool extends EventEmitter {
21562160
assert(stopHash.equals(this.getcfheadersStopHash));
21572161
let previousFilterHeader = packet.previousFilterHeader;
21582162
const filterHashes = packet.filterHashes;
2159-
assert(filterHashes.length <= 2000)
2163+
assert(filterHashes.length <= 2000);
21602164
let blockHeight = await this.chain.getHeight(stopHash)
21612165
- filterHashes.length;
21622166
const stopHeight = await this.chain.getHeight(stopHash);
@@ -2165,14 +2169,26 @@ class Pool extends EventEmitter {
21652169
const basicFilter = new BasicFilter();
21662170
basicFilter._hash = filterHash;
21672171
const filterHeader = basicFilter.header(previousFilterHeader);
2172+
const lastFilterHeader = this.cfHeaderChain.tail;
2173+
const cfHeaderEntry = new CFHeaderEntry(
2174+
filterHash, lastFilterHeader.height + 1);
2175+
this.cfHeaderChain.push(cfHeaderEntry);
21682176
// todo: verify the filterHeader
21692177
// todo: save the filterHeader
21702178
previousFilterHeader = filterHeader;
21712179
this.chain.db.neutrinoState.headerHeight = blockHeight;
21722180
blockHeight++;
21732181
}
21742182
await this.chain.db.saveNeutrinoState();
2175-
this.emit('cfheaders');
2183+
if (this.headerChain.tail.height <= stopHeight)
2184+
this.emit('cfheaders');
2185+
else {
2186+
const nextStopHeight = stopHeight + 2000 < this.chain.height
2187+
? stopHeight + 2000 : this.chain.height;
2188+
const nextStopHash = await this.chain.getHash(nextStopHeight);
2189+
this.getcfheadersStopHash = nextStopHash;
2190+
peer.sendGetCFHeaders(filterType, stopHeight + 1, nextStopHash);
2191+
}
21762192
}
21772193

21782194
async handleCFilters(peer, packet) {
@@ -2191,7 +2207,8 @@ class Pool extends EventEmitter {
21912207
assert(filterType === this.getcfheadersFilterType);
21922208
const blockHeight = await this.chain.getHeight(blockHash);
21932209
const stopHeight = await this.chain.getHeight(this.getcfiltersStopHash);
2194-
assert(blockHeight >= this.getcfiltersStartHeight && blockHeight <= stopHeight);
2210+
assert(blockHeight >= this.getcfiltersStartHeight
2211+
&& blockHeight <= stopHeight);
21952212

21962213
// todo: save the filter
21972214
const basicFilter = new BasicFilter();
@@ -2357,7 +2374,6 @@ class Pool extends EventEmitter {
23572374
const last = this.headerChain.tail;
23582375
const hash = header.hash();
23592376
const height = last.height + 1;
2360-
console.log('header');
23612377

23622378
if (!header.verify()) {
23632379
this.logger.warning(
@@ -2415,6 +2431,8 @@ class Pool extends EventEmitter {
24152431
}
24162432

24172433
// Request more headers.
2434+
if (this.chain.synced)
2435+
return;
24182436
if (this.options.neutrino)
24192437
peer.sendGetHeaders([node.hash]);
24202438
else
@@ -4724,6 +4742,20 @@ class HeaderEntry {
47244742
}
47254743
}
47264744

4745+
class CFHeaderEntry {
4746+
/**
4747+
* Create cfheader entry.
4748+
* @constructor
4749+
*/
4750+
4751+
constructor(hash, height) {
4752+
this.hash = hash;
4753+
this.height = height;
4754+
this.prev = null;
4755+
this.next = null;
4756+
}
4757+
}
4758+
47274759
/*
47284760
* Expose
47294761
*/

lib/node/neutrino.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,10 @@ class Neutrino extends Node {
164164
this.emit('reset', tip);
165165
});
166166

167-
this.chain.on('full', () => {
167+
this.chain.on('headersFull', () => {
168168
if (this.chain.height === 0)
169169
return;
170170
this.logger.info('Block Headers are fully synced');
171-
console.log('Block Headers are fully synced \n\n\n\n\n');
172171
// this.pool.startFilterCheckPtSync(); // TODO: Maybe implement this later
173172
this.pool.startFilterHeadersSync();
174173
});

0 commit comments

Comments
 (0)