Skip to content

Commit

Permalink
wallet: add filterUpdated to wallet.add, wdb.addTX and wdb.addBlock.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Feb 13, 2024
1 parent 5aa95e3 commit 42c4f99
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 49 deletions.
5 changes: 4 additions & 1 deletion jsdoc.json
Expand Up @@ -8,7 +8,10 @@
"includePattern": ".+\\.js(doc)?$",
"excludePattern": "(^|\\/|\\\\)_"
},
"plugins": ["plugins/markdown"],
"plugins": [
"plugins/markdown",
"plugins/rm-imports"
],
"templates": {
"cleverLinks": false,
"monospaceLinks": false
Expand Down
2 changes: 1 addition & 1 deletion lib/wallet/account.js
Expand Up @@ -512,7 +512,7 @@ class Account extends bio.Struct {
* Allocate new lookahead addresses if necessary.
* @param {Number} receiveDepth
* @param {Number} changeDepth
* @returns {Promise} - Returns {@link WalletKey}.
* @returns {Promise<WalletKey?>}
*/

async syncDepth(b, receive, change) {
Expand Down
31 changes: 21 additions & 10 deletions lib/wallet/wallet.js
Expand Up @@ -46,6 +46,12 @@ const Outpoint = require('../primitives/outpoint');

const EMPTY = Buffer.alloc(0);

/**
* @typedef {Object} AddResult
* @property {Details} details
* @property {WalletKey[]} derived
*/

/**
* Wallet
* @alias module:wallet.Wallet
Expand Down Expand Up @@ -4360,7 +4366,7 @@ class Wallet extends EventEmitter {
* This is used for deriving new addresses when
* a confirmed transaction is seen.
* @param {TX} tx
* @returns {Promise}
* @returns {Promise<WalletKey[]>} - derived rings.
*/

async syncOutputDepth(tx) {
Expand Down Expand Up @@ -4737,7 +4743,7 @@ class Wallet extends EventEmitter {
/**
* Add a transaction to the wallets TX history.
* @param {TX} tx
* @returns {Promise}
* @returns {Promise<AddResult?>}
*/

async add(tx, block) {
Expand All @@ -4754,21 +4760,26 @@ class Wallet extends EventEmitter {
* Potentially resolves orphans.
* @private
* @param {TX} tx
* @returns {Promise}
* @returns {Promise<AddResult?>}
*/

async _add(tx, block) {
const details = await this.txdb.add(tx, block);

if (details) {
const derived = await this.syncOutputDepth(tx);
if (derived.length > 0) {
this.wdb.emit('address', this, derived);
this.emit('address', derived);
}
if (!details)
return null;

const derived = await this.syncOutputDepth(tx);

if (derived.length > 0) {
this.wdb.emit('address', this, derived);
this.emit('address', derived);
}

return details;
return {
details,
derived
};
}

/**
Expand Down
60 changes: 47 additions & 13 deletions lib/wallet/walletdb.js
Expand Up @@ -30,13 +30,26 @@ const layout = layouts.wdb;
const tlayout = layouts.txdb;
const {states} = require('../covenants/namestate');

/** @typedef {import('../primitives/tx')} TX */

const {
ChainState,
BlockMeta,
TXRecord,
MapRecord
} = records;

/**
* @typedef {Object} AddBlockResult
* @property {Number} txs - Number of transactions added on this add.
* @property {Boolean} filterUpdated - Whether the bloom filter was updated.
*/

/**
* @typedef {Object} AddTXResult
* @property {Number} wids - Wallet IDs affected.
* @property {Boolean} filterUpdated - Whether the bloom filter was updated.
/**
* WalletDB
* @alias module:wallet.WalletDB
Expand Down Expand Up @@ -2286,7 +2299,8 @@ class WalletDB extends EventEmitter {
/**
* Add a block's transactions and write the new best hash.
* @param {ChainEntry} entry
* @returns {Promise}
* @param {TX[]} txs
* @returns {Promise<AddBlockResult?>}
*/

async addBlock(entry, txs) {
Expand All @@ -2304,7 +2318,7 @@ class WalletDB extends EventEmitter {
* @private
* @param {ChainEntry} entry
* @param {TX[]} txs
* @returns {Promise<Number>} - Number of transactions added.
* @returns {Promise<AddBlockResult?>}
*/

async _addBlock(entry, txs) {
Expand All @@ -2324,7 +2338,8 @@ class WalletDB extends EventEmitter {
'Unusual reorg at low height (%d).',
tip.height);
}
return -1;

return null;
}

if (tip.height >= this.network.block.slowHeight)
Expand All @@ -2348,11 +2363,11 @@ class WalletDB extends EventEmitter {
tip.height);

// Maybe we can run syncChain here.
return -1;
return null;
}
} else if (tip.height !== this.state.height + 1) {
await this._rescan(this.state.height);
return -1;
return null;
}

let block;
Expand All @@ -2368,10 +2383,11 @@ class WalletDB extends EventEmitter {
'Unusual reorg at height (%d).',
tip.height);

return -1;
return null;
}

const walletTxs = [];
let filterUpdated = false;

try {
// We set the state as confirming so that
Expand All @@ -2381,8 +2397,13 @@ class WalletDB extends EventEmitter {
this.confirming = true;

for (const tx of txs) {
if (await this._addTX(tx, tip)) {
const txadded = await this._addTX(tx, tip);

if (txadded) {
walletTxs.push(tx);

if (txadded.filterUpdated)
filterUpdated = true;
}
}

Expand All @@ -2400,7 +2421,10 @@ class WalletDB extends EventEmitter {

this.emit('block connect', entry, walletTxs);

return walletTxs.length;
return {
txs: walletTxs.length,
filterUpdated: filterUpdated
};
}

/**
Expand Down Expand Up @@ -2505,7 +2529,7 @@ class WalletDB extends EventEmitter {
* to wallet IDs, potentially store orphans, resolve
* orphans, or confirm a transaction.
* @param {TX} tx
* @returns {Promise}
* @returns {Promise<AddTXResult?>}
*/

async addTX(tx) {
Expand All @@ -2522,7 +2546,7 @@ class WalletDB extends EventEmitter {
* @private
* @param {TX} tx
* @param {BlockMeta} block
* @returns {Promise}
* @returns {Promise<AddTXResult?>}
*/

async _addTX(tx, block) {
Expand All @@ -2538,6 +2562,7 @@ class WalletDB extends EventEmitter {
wids.size, tx.txid());

let result = false;
let filterUpdated = false;

// Insert the transaction
// into every matching wallet.
Expand All @@ -2546,18 +2571,27 @@ class WalletDB extends EventEmitter {

assert(wallet);

if (await wallet.add(tx, block)) {
const wadded = await wallet.add(tx, block);

if (wadded) {
result = true;

if (wadded.derived.length > 0)
filterUpdated = true;

this.logger.info(
'Added transaction to wallet in WalletDB: %s (%d).',
wallet.id, wid);
result = true;
}
}

if (!result)
return null;

return wids;
return {
wids,
filterUpdated
};
}

/**
Expand Down
8 changes: 5 additions & 3 deletions test/wallet-auction-test.js
Expand Up @@ -131,8 +131,9 @@ describe('Wallet Auction', function() {
const openMTX = openTXs[openIndex++];
const tx = openMTX.toTX();
const addResult = await wdb.addTX(tx);
assert.strictEqual(addResult.size, 1);
assert.ok(addResult.has(wallet.wid));
assert.ok(addResult);
assert.strictEqual(addResult.wids.size, 1);
assert.ok(addResult.wids.has(wallet.wid));

const pending = await wallet.getPending();
assert.strictEqual(pending.length, 1);
Expand Down Expand Up @@ -305,7 +306,8 @@ describe('Wallet Auction', function() {
// double opens are properly removed.
await wallet.sign(spendMTX);
const added = await wdb.addTX(spendMTX.toTX());
assert.strictEqual(added.size, 1);
assert.ok(added);
assert.strictEqual(added.wids.size, 1);
});

it('should mine enough blocks to expire auction (again)', async () => {
Expand Down

0 comments on commit 42c4f99

Please sign in to comment.