Skip to content

Conversation

@ryanofsky
Copy link
Contributor

This is just code cleanup, no behavior is changing. It deduplicates getbalance code and makes it comprehsible so it is not doing bizarre things like subtracting negative fees (#9167).

For each wallet transaction, the balance is updated as follows:

+        CAmount debit = wtx.GetDebit(filter);
+        const bool outgoing = debit > 0;
+        for (const CTxOut& out : wtx.tx->vout) {
+            if (outgoing && IsChange(out)) {
+                debit -= out.nValue;
+            } else if (IsMine(out) & filter && depth >= minDepth && (!account || *account == GetAccountName(out.scriptPubKey))) {
+                balance += out.nValue;
+            }
+        }
+
+        if (outgoing && (!account || *account == wtx.strFromAccount)) {
+            balance -= debit;
+        }

if (request.params.size() == 0)
return ValueFromAmount(pwalletMain->GetBalance());

const string* account = request.params[0].get_str() != "*" ? &request.params[0].get_str() : nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this results in no longer throwing RPC_WALLET_INVALID_ACCOUNT_NAME when someone tries to get balance of "*", instead, it returns back the total balance. But I think this is what we want....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is just keeping the current behavior (line 698 before this commit, which is removed in the next commit).

Copy link
Contributor

@jonasschnelli jonasschnelli left a comment

Choose a reason for hiding this comment

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

Nice.
utACK 4b49ed6

I could imagine a follow-up refactoring that get rid of all the individual caches...

    mutable bool fDebitCached;
    mutable bool fCreditCached;
    mutable bool fImmatureCreditCached;
    mutable bool fAvailableCreditCached;
    mutable bool fWatchDebitCached;
    mutable bool fWatchCreditCached;
    mutable bool fImmatureWatchCreditCached;
    mutable bool fAvailableWatchCreditCached;
    mutable bool fChangeCached;
    mutable CAmount nDebitCached;
    mutable CAmount nCreditCached;
    mutable CAmount nImmatureCreditCached;
    mutable CAmount nAvailableCreditCached;
    mutable CAmount nWatchDebitCached;
    mutable CAmount nWatchCreditCached;
    mutable CAmount nImmatureWatchCreditCached;
    mutable CAmount nAvailableWatchCreditCached;
    mutable CAmount nChangeCached;

... and manages them in a std::map or similar.

Copy link
Contributor Author

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

I could imagine a follow-up refactoring that get rid of all the individual caches...

I also think this could be good to unify/organize these. #9167 adds two more caches, so they could be integrated as well.

if (request.params.size() == 0)
return ValueFromAmount(pwalletMain->GetBalance());

const string* account = request.params[0].get_str() != "*" ? &request.params[0].get_str() : nullptr;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is just keeping the current behavior (line 698 before this commit, which is removed in the next commit).

@ryanofsky ryanofsky force-pushed the pr/getbalance-cleanup branch from 4b49ed6 to e8ce6a4 Compare February 6, 2017 17:42
@ryanofsky
Copy link
Contributor Author

Rebased 4b49ed6 -> e8ce6a4 (getbalance-cleanup.0 -> getbalance-cleanup.1), fixing trivial merge conflict with #9613.

@ryanofsky ryanofsky mentioned this pull request Feb 28, 2017
@ryanofsky ryanofsky force-pushed the pr/getbalance-cleanup branch from e8ce6a4 to be74f34 Compare March 1, 2017 10:35
@ryanofsky
Copy link
Contributor Author

Rebased e8ce6a4 -> be74f34 (getbalance-cleanup.1 -> getbalance-cleanup.2) because of conflict with #9764.

@ryanofsky ryanofsky force-pushed the pr/getbalance-cleanup branch from be74f34 to 0b9520d Compare March 3, 2017 19:40
@ryanofsky
Copy link
Contributor Author

Rebased be74f34 -> 0b9520d (pr/getbalance-cleanup.2 -> pr/getbalance-cleanup.3) because of conflicts with pwalletMain renames in #9775.

@laanwj
Copy link
Member

laanwj commented Apr 26, 2017

Needs rebase again (sorry!)

utACK otherwise.

This adds a simpler new implementation of getbalance logic along with asserts
to confirm it behaves identically to the old logic. The old logic is removed in
the next commit.
@ryanofsky ryanofsky force-pushed the pr/getbalance-cleanup branch from 0b9520d to 02d9f50 Compare April 26, 2017 10:42
@ryanofsky
Copy link
Contributor Author

ryanofsky commented Apr 26, 2017

No problem, thanks for the review!

Rebased 0b9520d -> 02d9f50 (pr/getbalance-cleanup.3 -> pr/getbalance-cleanup.4). No changes except adding another std:: prefix and passing the right new argument to the CWalletDB constructor.

@laanwj laanwj merged commit 02d9f50 into bitcoin:master Apr 26, 2017
laanwj added a commit that referenced this pull request Apr 26, 2017
02d9f50 [wallet] Remove unneeded legacy getbalance code (Russell Yanofsky)
82b7dc3 [wallet] Add GetLegacyBalance method to simplify getbalance RPC (Russell Yanofsky)

Tree-SHA512: c3b890ff1f5a3df7e886309bad94bdf5cc3c12b72983bb79c72f8655ce16edf581bff0faaade2f18c2cb723f50d516e53f87792f81c3f8143b0c4377c0d80e87
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 24, 2019
…ation

02d9f50 [wallet] Remove unneeded legacy getbalance code (Russell Yanofsky)
82b7dc3 [wallet] Add GetLegacyBalance method to simplify getbalance RPC (Russell Yanofsky)

Tree-SHA512: c3b890ff1f5a3df7e886309bad94bdf5cc3c12b72983bb79c72f8655ce16edf581bff0faaade2f18c2cb723f50d516e53f87792f81c3f8143b0c4377c0d80e87
Fuzzbawls added a commit to Fuzzbawls/PIVX that referenced this pull request Jul 8, 2020
@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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants