Skip to content

Commit

Permalink
wdb: pass tx index and median time to addTX.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Mar 19, 2024
1 parent 64a2491 commit 63bc227
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
28 changes: 23 additions & 5 deletions lib/wallet/txdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -369,6 +377,7 @@ class TXDB {

/**
* Append to the global block record.
* @param {Batch} b
* @param {Hash} hash
* @param {BlockMeta} block
* @returns {Promise}
Expand Down Expand Up @@ -786,10 +795,11 @@ class TXDB {
* Add transaction without a batch.
* @param {TX} tx
* @param {BlockMeta} [block]
* @param {BlockExtraInfo} [extra]
* @returns {Promise<Details?>}
*/

async add(tx, block) {
async add(tx, block, extra) {
const hash = tx.hash();
const existing = await this.getTX(hash);

Expand All @@ -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);
Expand All @@ -825,7 +835,7 @@ class TXDB {
}

// Finally we can do a regular insertion.
return this.insert(wtx, block);
return this.insert(wtx, block, extra);
}

/**
Expand Down Expand Up @@ -931,10 +941,11 @@ class TXDB {
* @private
* @param {TXRecord} wtx
* @param {BlockMeta} [block]
* @param {BlockExtraInfo} [extra]
* @returns {Promise<Details>}
*/

async insert(wtx, block) {
async insert(wtx, block, extra) {
const b = this.bucket.batch();
const {tx, hash} = wtx;
const height = block ? block.height : -1;
Expand Down Expand Up @@ -1126,10 +1137,11 @@ class TXDB {
* @private
* @param {TXRecord} wtx
* @param {BlockMeta} block
* @param {BlockExtraInfo} extra
* @returns {Promise<Details>}
*/

async confirm(wtx, block) {
async confirm(wtx, block, extra) {
const b = this.bucket.batch();
const {tx, hash} = wtx;
const height = block.height;
Expand Down Expand Up @@ -3423,6 +3435,12 @@ class Balance extends bio.Struct {
*/

class BalanceDelta {
/** @type {Balance} */
wallet;

/** @type {Map<Number, Balance>} */
accounts;

/**
* Create a balance delta.
* @constructor
Expand Down
16 changes: 12 additions & 4 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
Expand Down
21 changes: 16 additions & 5 deletions lib/wallet/walletdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const {
MapRecord
} = records;

/** @typedef {import('./records').BlockMeta} BlockMeta */
/** @typedef {import('./txdb').BlockExtraInfo} BlockExtraInfo */

/**
* WalletDB
* @alias module:wallet.WalletDB
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.');
Expand All @@ -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);
Expand Down

0 comments on commit 63bc227

Please sign in to comment.