Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wallet: (Refactor) GetBalance to calculate used balance #29062

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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

For detailed information about the code coverage, see the test coverage report.

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:

  • #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
src/wallet/receive.cpp Outdated Show resolved Hide resolved
@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

}
ret.m_mine_immature += CachedTxGetImmatureCredit(wallet, wtx, ISMINE_SPENDABLE);
ret.m_watchonly_immature += CachedTxGetImmatureCredit(wallet, wtx, ISMINE_WATCH_ONLY);
}
ret.m_mine_used -= (ret.m_mine_trusted + ret.m_mine_untrusted_pending);
Copy link
Member

Choose a reason for hiding this comment

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

It would be a bit easier to understand if this subtraction was done with each addition during the loop, rather than all at the end. Also a comment for why this is being done would be useful, as it was not immediately obvious to me at first that the purpose of this is to remove the overlap.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@achow101 do the added comments make the PR easy to follow?

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

Successfully merging this pull request may close these issues.

None yet

4 participants