Skip to content

Commit

Permalink
test: added headers first sync tests
Browse files Browse the repository at this point in the history
  • Loading branch information
masterchief164 committed Jun 23, 2023
1 parent a45770d commit abc8bc2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/net/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,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,
Expand Down Expand Up @@ -2179,7 +2185,6 @@ class Pool extends EventEmitter {
const headers = packet.items;

if (!this.checkpoints && !this.options.neutrino)
// todo add support for checkpoints
return;

if (!this.syncing)
Expand Down Expand Up @@ -2255,7 +2260,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;
Expand Down
64 changes: 64 additions & 0 deletions test/neutrino-test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit abc8bc2

Please sign in to comment.