From 03aeaf367083777e9f2a863a07a853aca4b9a9cc Mon Sep 17 00:00:00 2001 From: Nodari Chkuaselidze Date: Sun, 22 Sep 2024 15:40:54 +0400 Subject: [PATCH] wdb: calculate pagination count indexes per wallet instead of per block. --- lib/wallet/txdb.js | 24 +++++++++++++++++++++++- lib/wallet/walletdb.js | 7 ++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 57b860251..eb2f73e33 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -383,10 +383,25 @@ class TXDB { return BlockRecord.decode(data); } + /** + * Get block hashes size. + * @param {Number} height + * @returns {Promise} + */ + + 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} */ @@ -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); } @@ -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); } @@ -1625,6 +1646,7 @@ class TXDB { /** @type {BlockExtraInfo} */ const extra = { medianTime: mtp, + // txIndex is not used in revert. txIndex: i }; diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 988f2afec..60705f159 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -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, @@ -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; @@ -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);