diff --git a/bin/neutrino b/bin/neutrino index 890e88bb6..df3b5fdb7 100755 --- a/bin/neutrino +++ b/bin/neutrino @@ -18,9 +18,7 @@ const node = new Neutrino({ logLevel: 'debug', db: 'leveldb', memory: false, - persistent: true, workers: true, - listen: true, loader: require }); @@ -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(); +}); diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index ed27f2aca..050134719 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -2000,8 +2000,6 @@ class Chain extends AsyncEmitter { */ maybeSync() { - // console.log('maybeSync'); - // console.log(this.synced); if (this.synced) return; @@ -2009,17 +2007,20 @@ class Chain extends AsyncEmitter { 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'); } /** diff --git a/lib/net/pool.js b/lib/net/pool.js index a54b80ff7..46b3f2153 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -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; @@ -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); } @@ -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, @@ -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(); @@ -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); @@ -2165,6 +2169,10 @@ 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; @@ -2172,7 +2180,15 @@ class Pool extends EventEmitter { 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) { @@ -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(); @@ -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( @@ -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 @@ -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 */ diff --git a/lib/node/neutrino.js b/lib/node/neutrino.js index 7b9519e56..79e38e8e5 100644 --- a/lib/node/neutrino.js +++ b/lib/node/neutrino.js @@ -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(); });