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

Fix get balance #13566

Merged
merged 6 commits into from
Jul 14, 2018
Merged

Fix get balance #13566

merged 6 commits into from
Jul 14, 2018

Conversation

jnewbery
Copy link
Contributor

#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.

@jnewbery
Copy link
Contributor Author

Feedback to address from @ryanofsky

Some thoughts looking at code:

  • It seems like it would be good to raise an error if IsDeprecatedRPCEnabled("accounts") and the account argument is not null or "*". Otherwise the argument would be silently ignored.
  • Documentation seems to say default minconf is 1, while implementation uses 0 or 1.
  • Maybe could dedup caching code in GetAvailableCredit a little with something like
CAmount* cache = filter == ISMINE_SPENDABLE  ? nAvailableCreditCached : 
                 filter == ISMINE_WATCH_ONLY ? nAvailableWatchCreditCached :
                 nullptr;

@maflcko maflcko added this to the 0.17.0 milestone Jun 28, 2018
@jnewbery
Copy link
Contributor Author

I guess that would not work for min dept of 0 (unconfirmed).

Are you saying that we also need to check pcoin->InMempool() for coins with 0 depth?

@jnewbery
Copy link
Contributor Author

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!

Copy link
Contributor

@ryanofsky ryanofsky left a 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.

filter = filter | ISMINE_WATCH_ONLY;

return ValueFromAmount(pwallet->GetLegacyBalance(filter, nMinDepth, account));
if (!IsDeprecatedRPCEnabled("accounts") && *account != "") {
Copy link
Contributor

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 balance
  • getbalance("*") would return total balance with legacy accounting method
  • getbalance("account") would return balance associated with a named account
  • getbalance("") would return leftover balance not associated with any named accounts

Copy link
Contributor

Choose a reason for hiding this comment

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

#13566 (comment)

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)).

@jnewbery
Copy link
Contributor Author

Thanks @ryanofsky . Updated to treat * as an acceptable dummy argument.

I don't think we want to entirely remove the possibility of calling this method with positional arguments.

Copy link
Contributor

@ryanofsky ryanofsky left a 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.

@jnewbery
Copy link
Contributor Author

Thanks for the review @ryanofsky !

@Empact
Copy link
Member

Empact commented Jun 29, 2018

This calls for a test, no?

@jnewbery
Copy link
Contributor Author

@Empact - test added. Review plz 🙂

@Empact
Copy link
Member

Empact commented Jun 29, 2018

Should CWallet::GetUnconfirmedBalance use the cache?


isminefilter filter = ISMINE_SPENDABLE;
if (!request.params[2].isNull() && request.params[2].get_bool()) {
filter = filter | ISMINE_WATCH_ONLY;
Copy link
Member

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 ))
Copy link
Member

Choose a reason for hiding this comment

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

nit: whitespace

@Empact
Copy link
Member

Empact commented Jun 29, 2018

utACK 702ae1e

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 30, 2018

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.

@laanwj
Copy link
Member

laanwj commented Jul 4, 2018

utACK 702ae1e

Copy link
Member

@promag promag left a 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;
}
Copy link
Member

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?

Copy link
Contributor Author

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;
Copy link
Member

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) {
Copy link
Member

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)

Copy link
Member

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.

@sipa
Copy link
Member

sipa commented Jul 14, 2018

utACK 702ae1e

@sipa sipa merged commit 702ae1e into bitcoin:master Jul 14, 2018
sipa added a commit that referenced this pull request Jul 14, 2018
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
@jnewbery
Copy link
Contributor Author

@Empact @promag - thanks for the reviews. I'll look at addressing nits in a follow-up.

@jnewbery
Copy link
Contributor Author

jnewbery commented Nov 1, 2018

@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.

@Empact
Copy link
Member

Empact commented Nov 2, 2018

Yeah I would leave it as-is at this point.

@promag
Copy link
Member

promag commented Nov 2, 2018

@jnewbery agree, not worth it.

@jnewbery jnewbery deleted the fix_get_balance branch November 2, 2018 15:34
@jnewbery
Copy link
Contributor Author

jnewbery commented Nov 2, 2018

Agree. Thanks for the input. Sorry those changes didn't make it!

xdustinface pushed a commit to xdustinface/dash that referenced this pull request Dec 22, 2020
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
xdustinface pushed a commit to xdustinface/dash that referenced this pull request Dec 22, 2020
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
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants