Skip to content

Commit

Permalink
test<wallet>: checkFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
manavdesai27 committed Jul 20, 2023
1 parent 496e866 commit d94eae9
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/wallet/walletdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
}

/**
Expand Down
90 changes: 90 additions & 0 deletions test/wallet-neutrino-test.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
});

0 comments on commit d94eae9

Please sign in to comment.