Skip to content

Commit

Permalink
wdb: calculate pagination count indexes per wallet instead of per block.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Sep 22, 2024
1 parent 9832a3d commit 03aeaf3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
24 changes: 23 additions & 1 deletion lib/wallet/txdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,25 @@ class TXDB {
return BlockRecord.decode(data);
}

/**
* Get block hashes size.
* @param {Number} height
* @returns {Promise<Number>}
*/

async getBlockTXsSize(height) {
const data = await this.bucket.get(layout.b.encode(height));

if (!data)
return 0;

return data.readUInt32LE(40, true);
}

/**
* Append to the global block record.
* @param {Batch} b
* @param {Hash} hash
* @param {Hash} hash - transaction hash.
* @param {BlockMeta} block
* @returns {Promise}
*/
Expand Down Expand Up @@ -870,6 +885,9 @@ class TXDB {
if (!block)
return null;

// Get txIndex of the wallet.
extra.txIndex = await this.getBlockTXsSize(block.height);

// Confirm transaction.
return this.confirm(existing, block, extra);
}
Expand All @@ -890,6 +908,9 @@ class TXDB {
await this.removeDoubleOpen(tx);
}

if (block)
extra.txIndex = await this.getBlockTXsSize(block.height);

// Finally we can do a regular insertion.
return this.insert(wtx, block, extra);
}
Expand Down Expand Up @@ -1625,6 +1646,7 @@ class TXDB {
/** @type {BlockExtraInfo} */
const extra = {
medianTime: mtp,
// txIndex is not used in revert.
txIndex: i
};

Expand Down
7 changes: 6 additions & 1 deletion lib/wallet/walletdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const {scanActions} = require('../blockchain/common');

/** @typedef {import('../primitives/tx')} TX */
/** @typedef {import('../blockchain/common').ScanAction} ScanAction */
/** @typedef {import('../types').Hash} Hash */

const {
ChainState,
Expand Down Expand Up @@ -529,6 +530,7 @@ class WalletDB extends EventEmitter {

const b = this.db.batch();
const entries = await this.client.getEntries();
assert(entries, 'Could not get chain entries.');

let tip = null;

Expand Down Expand Up @@ -2569,7 +2571,10 @@ class WalletDB extends EventEmitter {
/** @type {BlockExtraInfo} */
const extra = {
medianTime: mtp,
txIndex: walletTXs.length
// txIndex will be recalculated in txdb. It will be local index
// to the wallet instead of the whole walletdb index.
// @see TXDB#add.
txIndex: 0
};

const txadded = await this._addTX(tx, tip, extra);
Expand Down

0 comments on commit 03aeaf3

Please sign in to comment.