-
Notifications
You must be signed in to change notification settings - Fork 35.6k
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
Fix get balance #13566
Fix get balance #13566
Conversation
Feedback to address from @ryanofsky Some thoughts looking at code:
|
Are you saying that we also need to check |
GetBalance() can now take an ismine filter, which is passed down to GetAvailableCredit. This allows GetBalance to be used to get watch-only balances.
16e67cc
to
c57f200
Compare
I believe that I've addressed all of @ryanofsky's feedback. Russ - can you rereview and confirm? Need to address @jonasschnelli's comment. Jonas - can you please confirm my question above? Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK c57f2009f65ffedc7531644243d5d7ddd786edde if getbalance("") is not treated like getbalance(null), (see comment).
It looks like we might have had tests for this previously which were deleted. It could be better in the future to avoid dropping tests for deprecated features until the features are actually removed, though it is sometimes nice to simplify tests.
src/wallet/rpcwallet.cpp
Outdated
filter = filter | ISMINE_WATCH_ONLY; | ||
|
||
return ValueFromAmount(pwallet->GetLegacyBalance(filter, nMinDepth, account)); | ||
if (!IsDeprecatedRPCEnabled("accounts") && *account != "") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In commit "[RPC] [wallet] allow getbalance to use min_conf and watch_only"
I think the safest thing here would be to raise an error whenever accounts are deprecated and account is non-null, because previously getbalance(null)
, getbalance("*")
, and getbalance("")
each did different things, and it doesn't seem good to silently ignore a requested behavior. Another slightly less safe but maybe acceptable option would be to change ""
to "*"
on this line, since getbalance("*")
and getbalance(null)
at least were more similar than getbalance("*")
and getbalance("")
Previously, and currently with the deprecated accounts feature enabled:
getbalance(null)
would return total balancegetbalance("*")
would return total balance with legacy accounting methodgetbalance("account")
would return balance associated with a named accountgetbalance("")
would return leftover balance not associated with any named accounts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also want to note that another approach we could take here would not be to deprecate the account
argument at all, but instead just rename it to label
and pass it down as an option to the GetBalance() and GetAvailableCredit methods where it could be used to filter txouts there by address label. This would be a simple code change, just adding if (!label || *label == GetLabelName(txout.scriptPubKey))
.
c57f200
to
43937db
Compare
Thanks @ryanofsky . Updated to treat I don't think we want to entirely remove the possibility of calling this method with positional arguments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK 43937db46edab8c226ce190e185d7a3285b3b364, only change since last review is requiring "*" instead of "" to signify all accounts. Thanks for implementing this and all the changes.
Thanks for the review @ryanofsky ! |
This calls for a test, no? |
43937db
to
702ae1e
Compare
@Empact - test added. Review plz 🙂 |
Should |
|
||
isminefilter filter = ISMINE_SPENDABLE; | ||
if (!request.params[2].isNull() && request.params[2].get_bool()) { | ||
filter = filter | ISMINE_WATCH_ONLY; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: |=
@@ -853,8 +853,9 @@ static UniValue getbalance(const JSONRPCRequest& request) | |||
return NullUniValue; | |||
} | |||
|
|||
if (request.fHelp || (request.params.size() > 3 && IsDeprecatedRPCEnabled("accounts")) || (request.params.size() != 0 && !IsDeprecatedRPCEnabled("accounts"))) | |||
if (request.fHelp || (request.params.size() > 3 )) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: whitespace
utACK 702ae1e |
Note to reviewers: This pull request conflicts with the following ones:
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. |
utACK 702ae1e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK 702ae1e. Consider last comment as a way to not degrade GetBalance
performance.
} else if (filter == ISMINE_WATCH_ONLY) { | ||
cache = &nAvailableWatchCreditCached; | ||
cache_used = &fAvailableWatchCreditCached; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if filter == ISMINE_ALL
— which can be when include_watchonly=true
in RPC getbalance
— could sum both if cached?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like a reasonable optimization, but I don't think it's required. Could be done in a separate PR.
@@ -460,9 +460,8 @@ class CWalletTx : public CMerkleTx | |||
CAmount GetDebit(const isminefilter& filter) const; | |||
CAmount GetCredit(const isminefilter& filter) const; | |||
CAmount GetImmatureCredit(bool fUseCache=true) const; | |||
CAmount GetAvailableCredit(bool fUseCache=true) const; | |||
CAmount GetAvailableCredit(bool fUseCache=true, const isminefilter& filter=ISMINE_SPENDABLE) const; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, drop &
?
{ | ||
CAmount nTotal = 0; | ||
{ | ||
LOCK2(cs_main, cs_wallet); | ||
for (const auto& entry : mapWallet) | ||
{ | ||
const CWalletTx* pcoin = &entry.second; | ||
if (pcoin->IsTrusted()) | ||
nTotal += pcoin->GetAvailableCredit(); | ||
if (pcoin->IsTrusted() && pcoin->GetDepthInMainChain() >= min_depth) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IsTrusted()
already computes GetDepthInMainChain()
. Suggestion:
bool IsTrusted(int min_depth = 1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ignore my comment, I tried that refactor and it doesn't look good.
utACK 702ae1e |
702ae1e [RPC] [wallet] allow getbalance to use min_conf and watch_only without accounts. (John Newbery) cf15761 [wallet] GetBalance can take a min_depth argument. (John Newbery) 0f3d6e9 [wallet] factor out GetAvailableWatchOnlyBalance() (John Newbery) 7110c83 [wallet] deduplicate GetAvailableCredit logic (John Newbery) ef7bc88 [wallet] Factor out GetWatchOnlyBalance() (John Newbery) 4279da4 [wallet] GetBalance can take an isminefilter filter. (John Newbery) Pull request description: #12953 inadvertently removed the functionality to call `getbalance "*" <int> <bool>` to get the wallet's balance with either minconfs or include_watchonly. This restores that functionality (when `-deprecatedrpc=accounts`), and also makes it possible to call ``getbalance minconf=<int> include_watchonly=<bool>` when accounts are not being used. Tree-SHA512: 67e84de9291ed6d34b23c626f4dc5988ba0ae6c99708d02b87dd3aaad3f4b6baa6202a66cc2dadd30dd993a39de8036ee920fcaa8cbb1c5dfe606e6fac183344
@Empact @promag - I've addressed the remaining nits in https://github.com/jnewbery/bitcoin/tree/13566_nits, but there isn't much in there. I'm not sure if there's much point in opening a new PR. |
Yeah I would leave it as-is at this point. |
@jnewbery agree, not worth it. |
Agree. Thanks for the input. Sorry those changes didn't make it! |
702ae1e [RPC] [wallet] allow getbalance to use min_conf and watch_only without accounts. (John Newbery) cf15761 [wallet] GetBalance can take a min_depth argument. (John Newbery) 0f3d6e9 [wallet] factor out GetAvailableWatchOnlyBalance() (John Newbery) 7110c83 [wallet] deduplicate GetAvailableCredit logic (John Newbery) ef7bc88 [wallet] Factor out GetWatchOnlyBalance() (John Newbery) 4279da4 [wallet] GetBalance can take an isminefilter filter. (John Newbery) Pull request description: bitcoin#12953 inadvertently removed the functionality to call `getbalance "*" <int> <bool>` to get the wallet's balance with either minconfs or include_watchonly. This restores that functionality (when `-deprecatedrpc=accounts`), and also makes it possible to call ``getbalance minconf=<int> include_watchonly=<bool>` when accounts are not being used. Tree-SHA512: 67e84de9291ed6d34b23c626f4dc5988ba0ae6c99708d02b87dd3aaad3f4b6baa6202a66cc2dadd30dd993a39de8036ee920fcaa8cbb1c5dfe606e6fac183344
702ae1e [RPC] [wallet] allow getbalance to use min_conf and watch_only without accounts. (John Newbery) cf15761 [wallet] GetBalance can take a min_depth argument. (John Newbery) 0f3d6e9 [wallet] factor out GetAvailableWatchOnlyBalance() (John Newbery) 7110c83 [wallet] deduplicate GetAvailableCredit logic (John Newbery) ef7bc88 [wallet] Factor out GetWatchOnlyBalance() (John Newbery) 4279da4 [wallet] GetBalance can take an isminefilter filter. (John Newbery) Pull request description: bitcoin#12953 inadvertently removed the functionality to call `getbalance "*" <int> <bool>` to get the wallet's balance with either minconfs or include_watchonly. This restores that functionality (when `-deprecatedrpc=accounts`), and also makes it possible to call ``getbalance minconf=<int> include_watchonly=<bool>` when accounts are not being used. Tree-SHA512: 67e84de9291ed6d34b23c626f4dc5988ba0ae6c99708d02b87dd3aaad3f4b6baa6202a66cc2dadd30dd993a39de8036ee920fcaa8cbb1c5dfe606e6fac183344
[Backport] bitcoin/bitcoin#13825 kill accounts This is a backport of the following commits: - from bitcoin/bitcoin#13825 - pick bitcoin/bitcoin@c9c32e6 - from bitcoin/bitcoin#14023 - pick bitcoin/bitcoin@f0dc850 - from bitcoin/bitcoin#13566 - pick bitcoin/bitcoin@702ae1e - pick bitcoin/bitcoin@cf15761 - pick bitcoin/bitcoin@0f3d6e9 - pick bitcoin/bitcoin@7110c83 - pick bitcoin/bitcoin@ef7bc88 - pick bitcoin/bitcoin@4279da4 - pick bitcoin/bitcoin@c410f41 - from bitcoin/bitcoin#9614 - pick bitcoin/bitcoin@02d9f50 - pick bitcoin/bitcoin@82b7dc3 - from bitcoin/bitcoin#8061 - pick bitcoin/bitcoin@ecb9741 - from bitcoin/bitcoin#6851 - pick bitcoin/bitcoin@3e7c89196c - from bitcoin/bitcoin#8828 - pick bitcoin/bitcoin@86029e7 - from bitcoin/bitcoin#8696 - just the first two commits, as AccountingEntry is going away - pick bitcoin/bitcoin@d2e678d - pick bitcoin/bitcoin@59adc86 - from bitcoin/bitcoin#8028 - pick bitcoin/bitcoin@0fd5997
#12953 inadvertently removed the functionality to call
getbalance "*" <int> <bool>
to get the wallet's balance with either minconfs or include_watchonly.This restores that functionality (when
-deprecatedrpc=accounts
), and also makes it possible to call ``getbalance minconf= include_watchonly=` when accounts are not being used.