From 4c3681bbbbe8c4fd7bf619e681b17d1a85f580f8 Mon Sep 17 00:00:00 2001 From: masterchief164 <63920595+masterchief164@users.noreply.github.com> Date: Tue, 4 Jul 2023 12:18:36 +0530 Subject: [PATCH] feat: cfilters sync complete --- lib/blockchain/chain.js | 2 -- lib/indexer/filterindexer.js | 1 + lib/indexer/indexer.js | 1 - lib/net/pool.js | 21 +++++++++++++++++++-- lib/node/neutrino.js | 8 ++++++++ 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index ef55aeea4..38201fe8e 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -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; /** diff --git a/lib/indexer/filterindexer.js b/lib/indexer/filterindexer.js index ce25df7e6..15ddfb231 100644 --- a/lib/indexer/filterindexer.js +++ b/lib/indexer/filterindexer.js @@ -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()); } diff --git a/lib/indexer/indexer.js b/lib/indexer/indexer.js index 68f8487f9..97d85f76b 100644 --- a/lib/indexer/indexer.js +++ b/lib/indexer/indexer.js @@ -76,7 +76,6 @@ class Indexer extends EventEmitter { */ put(key, value) { - console.log('put', key, value.toString('hex')); this.batch.put(key, value); } diff --git a/lib/net/pool.js b/lib/net/pool.js index 2a1bd7d9e..687f3a982 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -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}`); } /** @@ -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}`); } /** diff --git a/lib/node/neutrino.js b/lib/node/neutrino.js index 684384b84..0e9291f38 100644 --- a/lib/node/neutrino.js +++ b/lib/node/neutrino.js @@ -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.'); } @@ -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(); + } } /**