From 217c80ec53f4a83ff628d08bb9a7a964528aabc7 Mon Sep 17 00:00:00 2001 From: masterchief164 <63920595+masterchief164@users.noreply.github.com> Date: Mon, 19 Jun 2023 00:07:22 +0530 Subject: [PATCH] test: added headers first sync tests --- lib/net/pool.js | 9 ++++-- test/neutrino-test.js | 64 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 test/neutrino-test.js diff --git a/lib/net/pool.js b/lib/net/pool.js index a3fa0184f..5578847cb 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -1676,6 +1676,12 @@ class Pool extends EventEmitter { if (this.checkpoints) return; + if (this.options.neutrino) { + const locator = await this.chain.getLocator(); + this.sendLocator(locator, peer); + return; + } + this.logger.debug( 'Received %d block hashes from peer (%s).', hashes.length, @@ -2181,7 +2187,6 @@ class Pool extends EventEmitter { const headers = packet.items; if (!this.checkpoints && !this.options.neutrino) - // todo add support for checkpoints return; if (!this.syncing) @@ -2257,7 +2262,7 @@ class Pool extends EventEmitter { peer.blockTime = Date.now(); // Request the blocks we just added. - if (checkpoint) { + if (checkpoint && !this.options.neutrino) { this.headerChain.shift(); this.resolveHeaders(peer); return; diff --git a/test/neutrino-test.js b/test/neutrino-test.js new file mode 100644 index 000000000..a73f2e61f --- /dev/null +++ b/test/neutrino-test.js @@ -0,0 +1,64 @@ +'use strict'; + +const FullNode = require('../lib/node/fullnode'); +const NeutrinoNode = require('../lib/node/neutrino'); +const {forValue} = require('./util/common'); +const assert = require('bsert'); +describe('neutrino', function () { + this.timeout(10000); + + const node1 = new NeutrinoNode({ + network: 'regtest', + memory: true, + port: 10000, + httpPort: 20000, + neutrino: true, + only: '127.0.0.1' + }); + + const node2 = new FullNode({ + network: 'regtest', + memory: true, + listen: true, + indexFilter: true, + bip157: true + }); + + async function mineBlocks(n) { + while (n) { + const block = await node2.miner.mineBlock(); + await node2.chain.add(block); + n--; + } + await forValue(node1.chain, 'height', node2.chain.height); + } + + before(async function () { + const waitForConnection = new Promise((resolve, reject) => { + node1.pool.once('peer open', async (peer) => { + resolve(peer); + }); + }); + + await node1.open(); + await node2.open(); + await node1.connect(); + await node2.connect(); + node1.startSync(); + node2.startSync(); + await mineBlocks(200); + await waitForConnection; + }); + + after(async () => { + await node1.close(); + await node2.close(); + }); + + describe('getheaders', () => { + it('should getheaders', async () => { + await mineBlocks(10); + assert.equal(node1.chain.height, node2.chain.height); + }); + }); +});