Skip to content

Commit 81e763f

Browse files
committed
wallet: Have GetBalance report used amount directly without two calls
1 parent abe7cbf commit 81e763f

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

src/interfaces/wallet.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,13 @@ struct WalletBalances
369369
CAmount balance = 0;
370370
CAmount unconfirmed_balance = 0;
371371
CAmount immature_balance = 0;
372+
CAmount used_balance = 0;
372373

373374
bool balanceChanged(const WalletBalances& prev) const
374375
{
375376
return balance != prev.balance || unconfirmed_balance != prev.unconfirmed_balance ||
376-
immature_balance != prev.immature_balance;
377+
immature_balance != prev.immature_balance ||
378+
used_balance != prev.used_balance;
377379
}
378380
};
379381

src/wallet/interfaces.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ class WalletImpl : public Wallet
388388
result.balance = bal.m_mine_trusted;
389389
result.unconfirmed_balance = bal.m_mine_untrusted_pending;
390390
result.immature_balance = bal.m_mine_immature;
391+
result.used_balance = bal.m_mine_used;
391392
return result;
392393
}
393394
bool tryGetBalances(WalletBalances& balances, uint256& block_hash) override

src/wallet/receive.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,17 +255,25 @@ Balance GetBalance(const CWallet& wallet, const int min_depth, bool avoid_reuse)
255255
const bool is_trusted{CachedTxIsTrusted(wallet, wtx, trusted_parents)};
256256
const int tx_depth{wallet.GetTxDepthInMainChain(wtx)};
257257

258-
if (!wallet.IsSpent(outpoint) && (allow_used_addresses || !wallet.IsSpentKey(txo.GetTxOut().scriptPubKey))) {
259-
// Get the amounts for mine
260-
CAmount credit_mine = txo.GetTxOut().nValue;
258+
if (!wallet.IsSpent(outpoint)) {
259+
CAmount* bucket = nullptr;
261260

262261
// Set the amounts in the return object
263262
if (wallet.IsTxImmatureCoinBase(wtx) && wtx.isConfirmed()) {
264-
ret.m_mine_immature += credit_mine;
263+
bucket = &ret.m_mine_immature;
265264
} else if (is_trusted && tx_depth >= min_depth) {
266-
ret.m_mine_trusted += credit_mine;
265+
bucket = &ret.m_mine_trusted;
267266
} else if (!is_trusted && wtx.InMempool()) {
268-
ret.m_mine_untrusted_pending += credit_mine;
267+
bucket = &ret.m_mine_untrusted_pending;
268+
}
269+
if (bucket) {
270+
// Get the amounts for mine
271+
CAmount credit_mine = txo.GetTxOut().nValue;
272+
273+
if (!allow_used_addresses && wallet.IsSpentKey(txo.GetTxOut().scriptPubKey)) {
274+
bucket = &ret.m_mine_used;
275+
}
276+
*bucket += credit_mine;
269277
}
270278
}
271279
}

src/wallet/receive.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct Balance {
4747
CAmount m_mine_trusted{0}; //!< Trusted, at depth=GetBalance.min_depth or more
4848
CAmount m_mine_untrusted_pending{0}; //!< Untrusted, but in mempool (pending)
4949
CAmount m_mine_immature{0}; //!< Immature coinbases in the main chain
50+
CAmount m_mine_used{0}; //!< Trusted/untrusted/immature funds in utxos that have already been spent from (only populated if AVOID REUSE wallet flag is set)
5051
};
5152
Balance GetBalance(const CWallet& wallet, int min_depth = 0, bool avoid_reuse = true);
5253

src/wallet/rpc/coins.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,18 +433,16 @@ RPCHelpMan getbalances()
433433

434434
LOCK(wallet.cs_wallet);
435435

436-
const auto bal = GetBalance(wallet);
436+
const auto bal = GetBalance(wallet, /*min_depth=*/0, /*avoid_reuse=*/true);
437+
437438
UniValue balances{UniValue::VOBJ};
438439
{
439440
UniValue balances_mine{UniValue::VOBJ};
440441
balances_mine.pushKV("trusted", ValueFromAmount(bal.m_mine_trusted));
441442
balances_mine.pushKV("untrusted_pending", ValueFromAmount(bal.m_mine_untrusted_pending));
442443
balances_mine.pushKV("immature", ValueFromAmount(bal.m_mine_immature));
443444
if (wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE)) {
444-
// If the AVOID_REUSE flag is set, bal has been set to just the un-reused address balance. Get
445-
// the total balance, and then subtract bal to get the reused address balance.
446-
const auto full_bal = GetBalance(wallet, 0, false);
447-
balances_mine.pushKV("used", ValueFromAmount(full_bal.m_mine_trusted + full_bal.m_mine_untrusted_pending - bal.m_mine_trusted - bal.m_mine_untrusted_pending));
445+
balances_mine.pushKV("used", ValueFromAmount(bal.m_mine_used));
448446
}
449447
balances.pushKV("mine", std::move(balances_mine));
450448
}

0 commit comments

Comments
 (0)