diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index a1e53ab4f..63b825f2b 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -24,6 +24,14 @@ const NameUndo = require('../covenants/undo'); const {TXRecord} = records; const {types} = rules; +/** @typedef {import('./records').BlockMeta} BlockMeta */ + +/** + * @typedef {Object} BlockExtraInfo + * @property {Number} medianTime + * @property {Number} txIndex + */ + /* * Constants */ @@ -369,6 +377,7 @@ class TXDB { /** * Append to the global block record. + * @param {Batch} b * @param {Hash} hash * @param {BlockMeta} block * @returns {Promise} @@ -786,10 +795,11 @@ class TXDB { * Add transaction without a batch. * @param {TX} tx * @param {BlockMeta} [block] + * @param {BlockExtraInfo} [extra] * @returns {Promise} */ - async add(tx, block) { + async add(tx, block, extra) { const hash = tx.hash(); const existing = await this.getTX(hash); @@ -806,7 +816,7 @@ class TXDB { return null; // Confirm transaction. - return this.confirm(existing, block); + return this.confirm(existing, block, extra); } const wtx = TXRecord.fromTX(tx, block); @@ -825,7 +835,7 @@ class TXDB { } // Finally we can do a regular insertion. - return this.insert(wtx, block); + return this.insert(wtx, block, extra); } /** @@ -931,10 +941,11 @@ class TXDB { * @private * @param {TXRecord} wtx * @param {BlockMeta} [block] + * @param {BlockExtraInfo} [extra] * @returns {Promise
} */ - async insert(wtx, block) { + async insert(wtx, block, extra) { const b = this.bucket.batch(); const {tx, hash} = wtx; const height = block ? block.height : -1; @@ -1126,10 +1137,11 @@ class TXDB { * @private * @param {TXRecord} wtx * @param {BlockMeta} block + * @param {BlockExtraInfo} extra * @returns {Promise
} */ - async confirm(wtx, block) { + async confirm(wtx, block, extra) { const b = this.bucket.batch(); const {tx, hash} = wtx; const height = block.height; @@ -3423,6 +3435,12 @@ class Balance extends bio.Struct { */ class BalanceDelta { + /** @type {Balance} */ + wallet; + + /** @type {Map} */ + accounts; + /** * Create a balance delta. * @constructor diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 1d5a8ce79..fa2b31c72 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -40,6 +40,10 @@ const {BufferSet} = require('buffer-map'); const Coin = require('../primitives/coin'); const Outpoint = require('../primitives/outpoint'); +/** @typedef {import('./records').BlockMeta} BlockMeta */ +/** @typedef {import('../primitives/tx')} TX */ +/** @typedef {import('./txdb').BlockExtraInfo} BlockExtraInfo */ + /* * Constants */ @@ -4737,13 +4741,15 @@ class Wallet extends EventEmitter { /** * Add a transaction to the wallets TX history. * @param {TX} tx + * @param {BlockMeta} [block] + * @param {BlockExtraInfo} [extra] * @returns {Promise} */ - async add(tx, block) { + async add(tx, block, extra) { const unlock = await this.writeLock.lock(); try { - return await this._add(tx, block); + return await this._add(tx, block, extra); } finally { unlock(); } @@ -4754,11 +4760,13 @@ class Wallet extends EventEmitter { * Potentially resolves orphans. * @private * @param {TX} tx + * @param {BlockMeta} [block] + * @param {BlockExtraInfo} [extra] * @returns {Promise} */ - async _add(tx, block) { - const details = await this.txdb.add(tx, block); + async _add(tx, block, extra) { + const details = await this.txdb.add(tx, block, extra); if (details) { const derived = await this.syncOutputDepth(tx); diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 467f419f5..0a163978a 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -40,6 +40,9 @@ const { MapRecord } = records; +/** @typedef {import('./records').BlockMeta} BlockMeta */ +/** @typedef {import('./txdb').BlockExtraInfo} BlockExtraInfo */ + /** * WalletDB * @alias module:wallet.WalletDB @@ -2458,10 +2461,17 @@ class WalletDB extends EventEmitter { // added and the height is updated. this.confirming = true; + const mtp = await this.getMedianTimeTip(tip.height - 1, tip.time); + for (const tx of txs) { - if (await this._addTX(tx, tip)) { + /** @type {BlockExtraInfo} */ + const extra = { + medianTime: mtp, + txIndex: walletTxs.length + }; + + if (await this._addTX(tx, tip, extra)) walletTxs.push(tx); - } } // Sync the state to the new tip. @@ -2611,11 +2621,12 @@ class WalletDB extends EventEmitter { * Add a transaction to the database without a lock. * @private * @param {TX} tx - * @param {BlockMeta} block + * @param {BlockMeta} [block] + * @param {BlockExtraInfo} [extra] * @returns {Promise} */ - async _addTX(tx, block) { + async _addTX(tx, block, extra) { const wids = await this.getWalletsByTX(tx); assert(!tx.mutable, 'WDB: Cannot add mutable TX.'); @@ -2636,7 +2647,7 @@ class WalletDB extends EventEmitter { assert(wallet); - if (await wallet.add(tx, block)) { + if (await wallet.add(tx, block, extra)) { this.logger.info( 'Added transaction to wallet in WalletDB: %s (%d).', wallet.id, wid);