Skip to content

Commit

Permalink
feat: CFHeaders sync complete
Browse files Browse the repository at this point in the history
  • Loading branch information
masterchief164 committed Jun 17, 2023
1 parent 63c1dd9 commit a19e1b2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
10 changes: 8 additions & 2 deletions bin/neutrino
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ const node = new Neutrino({
logLevel: 'debug',
db: 'leveldb',
memory: false,
persistent: true,
workers: true,
listen: true,
loader: require
});

Expand All @@ -42,3 +40,11 @@ if (!node.config.bool('no-wallet') && !node.has('walletdb')) {
console.error(err.stack);
process.exit(1);
});

process.on('unhandledRejection', (err, promise) => {
throw err;
});

process.on('SIGINT', async () => {
await node.close();
});
13 changes: 7 additions & 6 deletions lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -2000,26 +2000,27 @@ class Chain extends AsyncEmitter {
*/

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

if (this.options.checkpoints) {
if (this.height < this.network.lastCheckpoint)
return;
}

if (this.options.neutrino && this.tip.time < util.now() - 24 * 60 * 60)
if (this.options.neutrino && this.tip.time < 1686851917)
// TODO change this later
return;
else if (!this.options.neutrino &&
this.tip.time < util.now() - this.network.block.maxTipAge)
return;

if (!this.options.neutrino && !this.hasChainwork())
if (!this.hasChainwork())
return;
this.synced = true;
this.emit('full');
if (this.options.neutrino)
this.emit('headersFull');
else
this.emit('full');
}

/**
Expand Down
54 changes: 43 additions & 11 deletions lib/net/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,10 @@ class Pool extends EventEmitter {

const tip = this.chain.tip;
if (this.options.neutrino) {
// this.headerTip = tip;
this.headerChain.push(new HeaderEntry(tip.hash, tip.height));
return;
this.headerChain.push(new HeaderEntry(tip.hash, tip.height));
this.cfHeaderChain = new List();
this.cfHeaderChain.push(new CFHeaderEntry(consensus.ZERO_HASH, 0));
return;
}
if (tip.height < this.network.lastCheckpoint) {
this.checkpoints = true;
Expand Down Expand Up @@ -648,7 +649,7 @@ class Pool extends EventEmitter {

peer.loader = true;
this.peers.load = peer;
this.sendSync(peer);
this.sendSync(peer);

this.emit('loader', peer);
}
Expand Down Expand Up @@ -730,8 +731,11 @@ class Pool extends EventEmitter {
return;

this.filterSyncing = true;
const startHeight = 0;
const stopHash = await this.chain.getHash(29);
const startHeight = 1;
const chainHeight = await this.chain.tip.height;
console.log('chainHeight', chainHeight);
const stopHeight = chainHeight > 2000 ? 2000 : chainHeight;
const stopHash = await this.chain.getHash(stopHeight);
this.peers.load.sendGetCFHeaders(
common.FILTERS.BASIC,
startHeight,
Expand Down Expand Up @@ -772,7 +776,7 @@ class Pool extends EventEmitter {
async startHeadersSync() {
if (!this.syncing)
return;

console.log('startHeadersSync');
let locator;
try {
locator = await this.chain.getLocator();
Expand Down Expand Up @@ -2156,7 +2160,7 @@ class Pool extends EventEmitter {
assert(stopHash.equals(this.getcfheadersStopHash));
let previousFilterHeader = packet.previousFilterHeader;
const filterHashes = packet.filterHashes;
assert(filterHashes.length <= 2000)
assert(filterHashes.length <= 2000);
let blockHeight = await this.chain.getHeight(stopHash)
- filterHashes.length;
const stopHeight = await this.chain.getHeight(stopHash);
Expand All @@ -2165,14 +2169,26 @@ class Pool extends EventEmitter {
const basicFilter = new BasicFilter();
basicFilter._hash = filterHash;
const filterHeader = basicFilter.header(previousFilterHeader);
const lastFilterHeader = this.cfHeaderChain.tail;
const cfHeaderEntry = new CFHeaderEntry(
filterHash, lastFilterHeader.height + 1);
this.cfHeaderChain.push(cfHeaderEntry);
// todo: verify the filterHeader
// todo: save the filterHeader
previousFilterHeader = filterHeader;
this.chain.db.neutrinoState.headerHeight = blockHeight;
blockHeight++;
}
await this.chain.db.saveNeutrinoState();
this.emit('cfheaders');
if (this.headerChain.tail.height <= stopHeight)
this.emit('cfheaders');
else {
const nextStopHeight = stopHeight + 2000 < this.chain.height
? stopHeight + 2000 : this.chain.height;
const nextStopHash = await this.chain.getHash(nextStopHeight);
this.getcfheadersStopHash = nextStopHash;
peer.sendGetCFHeaders(filterType, stopHeight + 1, nextStopHash);
}
}

async handleCFilters(peer, packet) {
Expand All @@ -2191,7 +2207,8 @@ class Pool extends EventEmitter {
assert(filterType === this.getcfheadersFilterType);
const blockHeight = await this.chain.getHeight(blockHash);
const stopHeight = await this.chain.getHeight(this.getcfiltersStopHash);
assert(blockHeight >= this.getcfiltersStartHeight && blockHeight <= stopHeight);
assert(blockHeight >= this.getcfiltersStartHeight
&& blockHeight <= stopHeight);

// todo: save the filter
const basicFilter = new BasicFilter();
Expand Down Expand Up @@ -2357,7 +2374,6 @@ class Pool extends EventEmitter {
const last = this.headerChain.tail;
const hash = header.hash();
const height = last.height + 1;
console.log('header');

if (!header.verify()) {
this.logger.warning(
Expand Down Expand Up @@ -2415,6 +2431,8 @@ class Pool extends EventEmitter {
}

// Request more headers.
if (this.chain.synced)
return;
if (this.options.neutrino)
peer.sendGetHeaders([node.hash]);
else
Expand Down Expand Up @@ -4724,6 +4742,20 @@ class HeaderEntry {
}
}

class CFHeaderEntry {
/**
* Create cfheader entry.
* @constructor
*/

constructor(hash, height) {
this.hash = hash;
this.height = height;
this.prev = null;
this.next = null;
}
}

/*
* Expose
*/
Expand Down
3 changes: 1 addition & 2 deletions lib/node/neutrino.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,10 @@ class Neutrino extends Node {
this.emit('reset', tip);
});

this.chain.on('full', () => {
this.chain.on('headersFull', () => {
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
this.pool.startFilterHeadersSync();
});
Expand Down

0 comments on commit a19e1b2

Please sign in to comment.