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/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(); + } } /**