Skip to content

Commit

Permalink
wallet-test: Add test case for the chainstate.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Nov 2, 2023
1 parent 6446828 commit 949a03e
Show file tree
Hide file tree
Showing 4 changed files with 642 additions and 56 deletions.
8 changes: 6 additions & 2 deletions lib/wallet/walletdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -2142,7 +2142,7 @@ class WalletDB extends EventEmitter {
/**
* Get a wallet block meta.
* @param {Hash} hash
* @returns {Promise}
* @returns {Promise<BlockMeta?>}
*/

async getBlock(height) {
Expand All @@ -2157,7 +2157,7 @@ class WalletDB extends EventEmitter {
/**
* Get wallet tip.
* @param {Hash} hash
* @returns {Promise}
* @returns {Promise<BlockMeta>}
*/

async getTip() {
Expand Down Expand Up @@ -2301,6 +2301,7 @@ class WalletDB extends EventEmitter {
// increment by one until the block is fully
// added and the height is updated.
this.confirming = true;

for (const tx of txs) {
if (await this._addTX(tx, tip)) {
walletTxs.push(tx);
Expand Down Expand Up @@ -2453,6 +2454,9 @@ class WalletDB extends EventEmitter {
if (!wids)
return null;

// This is better place than _addBlock because here above check
// makes sure we really own the txs and it was not a false positive
// from the filter.
if (block && !this.state.marked)
await this.markState(block);

Expand Down
61 changes: 61 additions & 0 deletions test/util/wallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

const blake2b = require('bcrypto/lib/blake2b');
const random = require('bcrypto/lib/random');
const Block = require('../../lib/primitives/block');
const ChainEntry = require('../../lib/blockchain/chainentry');
const Input = require('../../lib/primitives/input');
const Outpoint = require('../../lib/primitives/outpoint');

const walletUtils = exports;

walletUtils.fakeBlock = (height) => {
const prev = blake2b.digest(fromU32((height - 1) >>> 0));
const hash = blake2b.digest(fromU32(height >>> 0));
const root = blake2b.digest(fromU32((height | 0x80000000) >>> 0));

return {
hash: hash,
prevBlock: prev,
merkleRoot: root,
time: 500000000 + (height * (10 * 60)),
bits: 0,
nonce: 0,
height: height,
version: 0,
witnessRoot: Buffer.alloc(32),
treeRoot: Buffer.alloc(32),
reservedRoot: Buffer.alloc(32),
extraNonce: Buffer.alloc(24),
mask: Buffer.alloc(32)
};
};

walletUtils.dummyInput = () => {
const hash = random.randomBytes(32);
return Input.fromOutpoint(new Outpoint(hash, 0));
};

walletUtils.nextBlock = (wdb) => {
return walletUtils.fakeBlock(wdb.state.height + 1);
};

walletUtils.curBlock = (wdb) => {
return walletUtils.fakeBlock(wdb.state.height);
};

walletUtils.nextEntry = (wdb) => {
const cur = walletUtils.curEntry(wdb);
const next = new Block(walletUtils.nextBlock(wdb));
return ChainEntry.fromBlock(next, cur);
};

walletUtils.curEntry = (wdb) => {
return new ChainEntry(walletUtils.curBlock(wdb));
};

function fromU32(num) {
const data = Buffer.allocUnsafe(4);
data.writeUInt32LE(num, 0, true);
return data;
}
Loading

0 comments on commit 949a03e

Please sign in to comment.