Skip to content

Commit

Permalink
feat: cfilters sync complete
Browse files Browse the repository at this point in the history
  • Loading branch information
masterchief164 committed Jul 4, 2023
1 parent 9084146 commit 4c3681b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
2 changes: 0 additions & 2 deletions lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ const ChainEntry = require('./chainentry');
const CoinView = require('../coins/coinview');
const Script = require('../script/script');
const {VerifyError} = require('../protocol/errors');
const {filters} = require('../blockstore/common');
const {filtersByVal} = require('../net/common');
const thresholdStates = common.thresholdStates;

/**
Expand Down
1 change: 1 addition & 0 deletions lib/indexer/filterindexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class FilterIndexer extends Indexer {
filter.header = filterHeader;

await this.blocks.writeFilter(blockHash, filter.toRaw(), this.filterType);
console.log(layout.f.encode(blockHash));
this.put(layout.f.encode(blockHash), basicFilter.hash());
}

Expand Down
1 change: 0 additions & 1 deletion lib/indexer/indexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ class Indexer extends EventEmitter {
*/

put(key, value) {
console.log('put', key, value.toString('hex'));
this.batch.put(key, value);
}

Expand Down
21 changes: 19 additions & 2 deletions lib/net/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -2156,6 +2156,8 @@ class Pool extends EventEmitter {
const nextStopHash = await this.chain.getHash(nextStopHeight);
peer.sendGetCFHeaders(filterType, stopHeight + 1, nextStopHash);
}
this.logger.info(`CFilterHeaders sync height
${this.chain.db.neutrinoState.headerHeight}`);
}

/**
Expand All @@ -2179,11 +2181,26 @@ class Pool extends EventEmitter {
const filterBytes = packet.filterBytes;
const indexer = this.getFilterIndexer(filtersByVal[filterType]);
const basicFilter = new BasicFilter().fromRaw(filterBytes);
const filterHeader = indexer.getFilterHeader(blockHash);
const filterHeader = await indexer.getFilterHeader(blockHash);
await indexer.saveFilter(blockHash, basicFilter, filterHeader);
this.chain.db.neutrinoState.headerHeight =
this.chain.db.neutrinoState.filterHeight =
await this.chain.getHeight(blockHash);
await this.chain.db.saveNeutrinoState();
if (this.chain.db.neutrinoState.filterHeight <= this.chain.height) {
this.logger.info('CFilter sync complete.');
this.emit('cfilter');
} else {
const startHeight = this.chain.db.neutrinoState.filterHeight + 1;
const chainHeight = await this.chain.tip.height;
const stopHeight = chainHeight > 1000 ? 1000 : chainHeight;
const stopHash = await this.chain.getHash(stopHeight);
peer.sendGetCFilters(
common.FILTERS.BASIC,
startHeight,
stopHash);
}
this.logger.info(`CFilter sync height
${this.chain.db.neutrinoState.filterHeight}`);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions lib/node/neutrino.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ class Neutrino extends Node {
await this.http.open();
await this.handleOpen();

for (const filterindex of this.filterIndexers.values()) {
await filterindex.open();
}

this.logger.info('Node is loaded.');
}

Expand All @@ -217,6 +221,10 @@ class Neutrino extends Node {
await this.pool.close();
await this.chain.close();
await this.handleClose();

for (const filterindex of this.filterIndexers.values()) {
await filterindex.close();
}
}

/**
Expand Down

0 comments on commit 4c3681b

Please sign in to comment.