From 7b70e3ef78563f0ee42e501715004de837f0d0ce Mon Sep 17 00:00:00 2001 From: Manav Desai Date: Mon, 10 Jul 2023 15:32:49 -0500 Subject: [PATCH] test: fix p2p-test --- test/p2p-bip157-test.js | 100 ++++++++++++++++++++++++++++++++++++++++ test/p2p-test.js | 55 +--------------------- 2 files changed, 101 insertions(+), 54 deletions(-) create mode 100644 test/p2p-bip157-test.js diff --git a/test/p2p-bip157-test.js b/test/p2p-bip157-test.js new file mode 100644 index 000000000..b72c7175a --- /dev/null +++ b/test/p2p-bip157-test.js @@ -0,0 +1,100 @@ +/* eslint-env mocha */ +/* eslint prefer-arrow-callback: "off" */ + +'use strict'; + +const assert = require('bsert'); +const FullNode = require('../lib/node/fullnode'); +const NeutrinoNode = require('../lib/node/neutrino'); +const {forValue} = require('./util/common'); +const {MAX_CFILTERS} = require('../lib/net/common'); +const packets = require('../lib/net/packets'); + +describe('P2P', function () { + this.timeout(50000); + + const node1 = new NeutrinoNode({ + network: 'regtest', + memory: true, + port: 10000, + httpPort: 20000, + only: '127.0.0.1', + neutrino: true + }); + + const node2 = new FullNode({ + network: 'regtest', + memory: true, + listen: true, + indexFilter: true, + bip157: true + }); + + let peer; + const nodePackets = {}; + + node1.pool.on('packet', (packet) => { + if (!nodePackets[packet.cmd]) + nodePackets[packet.cmd] = [packet]; + else + nodePackets[packet.cmd].push(packet); + }); + + async function mineBlocks(n) { + while (n) { + const block = await node2.miner.mineBlock(); + await node2.chain.add(block); + await new Promise(resolve => setTimeout(resolve, 20)); + n--; + } + await forValue(node1.chain, 'height', node2.chain.height); + } + + before(async () => { + 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(); + + // `peer` is node2, from node1's perspective. + // So peer.send() sends a packet from node1 to node2, + // and `nodePackets` catches the response packets that + // node2 sends back to node1. + peer = await waitForConnection; + }); + + after(async () => { + await node1.close(); + await node2.close(); + }); + + describe('BIP157', function () { + before(async () => { + // Do not exceed limit, including genesis block + await mineBlocks(MAX_CFILTERS - node1.chain.height - 1); + }); + + it('CFCheckpt', async () => { + nodePackets.cfcheckpt = []; + + await mineBlocks(2); + + const pkt = new packets.GetCFCheckptPacket( + 0, + node1.chain.tip.hash + ); + + peer.send(pkt); + await forValue(nodePackets.cfcheckpt, 'length', 1); + assert.strictEqual(nodePackets.cfcheckpt[0].filterHeaders.length, 1); + }); + }); +}); diff --git a/test/p2p-test.js b/test/p2p-test.js index 85eae0849..b9502550d 100644 --- a/test/p2p-test.js +++ b/test/p2p-test.js @@ -6,8 +6,6 @@ const assert = require('bsert'); const FullNode = require('../lib/node/fullnode'); const {forValue} = require('./util/common'); -const {MAX_CFILTERS} = require('../lib/net/common'); -const packets = require('../lib/net/packets'); describe('P2P', function () { this.timeout(5000); @@ -60,6 +58,7 @@ describe('P2P', function () { await node2.connect(); node1.startSync(); node2.startSync(); + await mineBlocks(1); // `peer` is node2, from node1's perspective. // So peer.send() sends a packet from node1 to node2, @@ -73,58 +72,6 @@ describe('P2P', function () { await node2.close(); }); - describe('BIP157', function () { - before(async () => { - // Do not exceed limit, including genesis block - await mineBlocks(MAX_CFILTERS - node1.chain.height - 1); - }); - - it('CFilters', async () => { - nodePackets.cfilter = []; - - const pkt = new packets.GetCFiltersPacket( - 0, - 0, - node1.chain.tip.hash - ); - - peer.send(pkt); - await forValue(nodePackets.cfilter, 'length', MAX_CFILTERS); - }); - - it('CFHeaders', async () => { - nodePackets.cfheaders = []; - - const pkt = new packets.GetCFHeadersPacket( - 0, - 0, - node1.chain.tip.hash - ); - - peer.send(pkt); - await forValue(nodePackets.cfheaders, 'length', 1); - assert.strictEqual( - nodePackets.cfheaders[0].filterHashes.length, - node1.chain.height + 1 - ); - }); - - it('CFCheckpt', async () => { - nodePackets.cfcheckpt = []; - - await mineBlocks(2); - - const pkt = new packets.GetCFCheckptPacket( - 0, - node1.chain.tip.hash - ); - - peer.send(pkt); - await forValue(nodePackets.cfcheckpt, 'length', 1); - assert.strictEqual(nodePackets.cfcheckpt[0].filterHeaders.length, 1); - }); - }); - describe('Compact Blocks', function () { it('should get compact block in low bandwidth mode', async () => { nodePackets.inv = [];