From d94eae99f5a74074f4d012c999486e5c88767e42 Mon Sep 17 00:00:00 2001 From: Manav Desai Date: Fri, 21 Jul 2023 01:40:28 +0530 Subject: [PATCH] test: checkFilter --- lib/wallet/walletdb.js | 6 ++- test/wallet-neutrino-test.js | 90 ++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 test/wallet-neutrino-test.js diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 31ef5e04c..8e652500c 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -584,6 +584,7 @@ class WalletDB extends EventEmitter { } async checkFilter (blockHash, filter) { + let foundMatch = false; this.filterHeight = this.filterHeight + 1; const gcsKey = blockHash.slice(0, 16); @@ -603,13 +604,16 @@ class WalletDB extends EventEmitter { } else if (data.length === 32) { address = Address.fromWitnessScripthash(data); } + const script = Script.fromAddress(address); const match = filter.match(gcsKey, script.toRaw()); if (match) { - await this.client.getBlockFromNode(blockHash); + foundMatch = true; return; } }); + + return foundMatch; } /** diff --git a/test/wallet-neutrino-test.js b/test/wallet-neutrino-test.js new file mode 100644 index 000000000..767bc1b59 --- /dev/null +++ b/test/wallet-neutrino-test.js @@ -0,0 +1,90 @@ +'use strict'; + +const assert = require('bsert'); +const WalletDB = require('../lib/wallet/walletdb'); +const { Network } = require('../lib/protocol'); +const WorkerPool = require('../lib/workers/workerpool'); +const Chain = require('../lib/blockchain/chain'); +const BlockStore = require('../lib/blockstore/level'); +const Miner = require('../lib/mining/miner'); +const CoinView = require('../lib/coins/coinview'); + +const wdb = new WalletDB(); + +const network = Network.get('regtest'); + +const workers = new WorkerPool({ + enabled: true, + size: 2 +}); + +const blocks = new BlockStore({ + memory: true, + network + }); + +const chain = new Chain({ + memory: true, + blocks, + network, + workers +}); + +const miner = new Miner({ + chain, + version: 4, + workers +}); + +let wallet = null; +const addresses = []; +const minedBlocks = []; +const filters = []; + +describe('wallet-neutrino', function() { + before(async () => { + await wdb.open(); + }); + + after(async () => { + await wdb.close(); + }); + + it('should open wallet', async () => { + wallet = await wdb.create(); + }); + + it('should create accounts', async () => { + await wallet.createAccount('foo'); + }); + + it('should generate addresses', async () => { + for (let i = 0; i < 3; i++) { + const key = await wallet.createReceive(0); + const address = key.getAddress(); + addresses.push(address); + } + }); + + it('should create 3 blocks', async () => { + for (let i = 0; i < 3; i++) { + const addr = addresses[i]; + const block = await miner.mineBlock(null, addr); + minedBlocks.push(block); + } + }); + + it('should create filters', async () => { + for (let i = 0; i < 3; i++) { + const filter = minedBlocks[i].toBasicFilter(new CoinView()); + filters.push(filter); + } + }); + + it('should match the filters', async () => { + for (let i = 0; i < 3; i++) { + const match = await wdb.checkFilter(minedBlocks[i].hash(), filters[i]); + assert(match); + } + }); +});