Skip to content

Conversation

BrandonOdiwuor
Copy link
Contributor

@BrandonOdiwuor BrandonOdiwuor commented Dec 12, 2023

Add GetFullBalance() to compute used balance in getbalances RPC and on GUI GUI PR #775

Check #28776 (comment) by @achow101

However, I think it would be better to move all of this into GetBalance itself and just have it always compute the used balance for us rather than having the caller do this extra computation.

Update:

  • Compute used balance from GetBalance(...)

@DrahtBot
Copy link
Contributor

DrahtBot commented Dec 12, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/29062.

Reviews

See the guideline for information on the review process.

Type Reviewers
Concept ACK pablomartin4btc

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #32618 (wallet: Remove ISMINE_WATCHONLY and watchonly from RPCs by achow101)
  • #32523 (wallet: Remove watchonly behavior and isminetypes by achow101)
  • #27865 (wallet: Track no-longer-spendable TXOs separately by achow101)
  • #27286 (wallet: Keep track of the wallet's own transaction outputs in memory by achow101)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@BrandonOdiwuor BrandonOdiwuor changed the title Calculate used balance from GetBalance(...) Wallet: Calculate used balance from GetBalance(...) Dec 12, 2023
@BrandonOdiwuor BrandonOdiwuor marked this pull request as ready for review December 13, 2023 11:01
Copy link
Member

@pablomartin4btc pablomartin4btc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

@@ -303,18 +303,23 @@ Balance GetBalance(const CWallet& wallet, const int min_depth, bool avoid_reuse)
const bool is_trusted{CachedTxIsTrusted(wallet, wtx, trusted_parents)};
const int tx_depth{wallet.GetTxDepthInMainChain(wtx)};
const CAmount tx_credit_mine{CachedTxGetAvailableCredit(wallet, wtx, ISMINE_SPENDABLE | reuse_filter)};
const CAmount tx_credit_mine_used{
avoid_reuse ? CachedTxGetAvailableCredit(wallet, wtx, ISMINE_SPENDABLE | ISMINE_USED) : tx_credit_mine};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about caculating avoid_reuse locally (e.g. const bool avoid = !(m_wallet->IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE)); at the beginning of the function), instead of passing it? I thought @achow101's comment was referring to include that too but maybe I'm wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have it always compute the used balance for us rather than having the caller do this extra computation.

My understanding of this part of @achow101 comment was to move the calculation for the used balance to GetBalance(...)

I have updated the PR and added GetFullBalance() which adds used balance to GetBalance(...)

@BrandonOdiwuor BrandonOdiwuor changed the title Wallet: Calculate used balance from GetBalance(...) Wallet: Add GetFullBalance(...) which included used balance Dec 17, 2023
@achow101 achow101 self-requested a review April 9, 2024 15:02
@BrandonOdiwuor BrandonOdiwuor changed the title Wallet: Add GetFullBalance(...) which included used balance Wallet: (Refactor) GetBalance to calculate used balance Apr 22, 2024
if (is_trusted && tx_depth >= min_depth) {
ret.m_mine_trusted += tx_credit_mine;
ret.m_watchonly_trusted += tx_credit_watchonly;
}
if (is_trusted) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be in the above if so that the min_depth check is still being done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const auto full_bal = GetBalance(wallet, 0, false);

My understanding from the above line is min_depth should be zero when calculating full_balance hence I didn't include the statement below in the above if since it uses min_depth provided by the caller

I didn't include the check in this if since tx_depth is always >= 0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it makes sense to return m_mine_used without respecting min_depth. The original code passes 0 for min_depth because that is the default value but it needs to pass the avoid_reuse flag. It is not because used balance should always calculated with a min depth of 0. Ignoring it is a layer violation here.

if (is_trusted && tx_depth >= min_depth) {
ret.m_mine_trusted += tx_credit_mine;
ret.m_watchonly_trusted += tx_credit_watchonly;
ret.m_mine_used -= tx_credit_mine; // remove overlap from used_balance
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the avoid_reuse flag is passed, tx_credit_mine and tx_credit_mine_used will be the same so the returned used_balance will be 0. That seems incorrect.

@fanquake
Copy link
Member

Moved to draft, given this doesn't seem to be active. @BrandonOdiwuor are you still working on this?

@fanquake fanquake marked this pull request as draft February 20, 2025 15:25
@DrahtBot
Copy link
Contributor

DrahtBot commented Jul 1, 2025

🐙 This pull request conflicts with the target branch and needs rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants