-
Notifications
You must be signed in to change notification settings - Fork 725
[Wallet] add cacheable amounts for caching credit/debit values #1757
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] add cacheable amounts for caching credit/debit values #1757
Conversation
27cd183 to
506e310
Compare
This is a modified version of btc@0c8ea6380c9f402ed9777fd015b117ba13125a35
Modified version of btc@6d714c3419b368671bd071a8992950c3dc00e613
Modified version of btc@4e91820531889e309dc4335fe0de8229c6426040
506e310 to
35de58c
Compare
|
rebased after get 1755 and #1666 merged. Ready for review. |
random-zebra
left a comment
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.
Very nice refactoring. Love the concept.
Few things (some of which can be tackled in a followup PR):
!fUseCache = recalculate(see comment inlined forGetColdStakingDebit/GetStakeDelegationDebit).- The change to
GetUnlockedCredit/GetLockedCreditis wrong. But we should probably just remove those methods. GetAvailableWatchOnlyCreditcan be refactored in terms ofGetCachableAmountor, this too, simply removed.GetUnspentCreditshould also be removed (as the maturity should not be checked here) and just replaced withGetCredit.
Adapted version of btc@c9e6e7ed79886b702614e627a966fc9a4850f8f0
|
Great catches :) . Some thoughts: |
35de58c to
1bf9dc4
Compare
|
Yeah I don't think we should check for |
Fuzzbawls
left a comment
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.
ACK 1bf9dc4
a13c245 AvailableCoins: remove unused "includeZeroValue" flag. (furszy) e74dcd8 AvailableCoins: improving core readability. (furszy) Pull request description: Built on top of #1759 . Improved code readability of the `CWallet::AvailableCoins` method decoupling nested statements and adding comments in between the checks. Plus, removed an unused "add zero value utxo" flag. An ugly area of the code could get pretty readable with some love. #1757 is going further on this area with some more improvements. ACKs for top commit: random-zebra: utACK a13c245 Fuzzbawls: utACK a13c245 Tree-SHA512: 1a5cb1eb058505d66b9a18de60ded211b248568150506e3b7ef32a5b3a8a818055ea85d321caa0c709ee7951dab12bf3450f057edadbf530e9514342194e55f8
|
I think that this PR highlights a problem with For example, calling CAmount credit = 0;
if (filter & ISMINE_SPENDABLE) {
// GetBalance can assume transactions in mapWallet won't change
credit += GetCachableAmount(CREDIT, ISMINE_SPENDABLE, recalculate, fUnspent);
}
if (filter & ISMINE_WATCH_ONLY) {
credit += GetCachableAmount(CREDIT, ISMINE_WATCH_ONLY, recalculate, fUnspent);
}
if (filter & ISMINE_COLD) {
credit += GetCachableAmount(CREDIT, ISMINE_COLD, recalculate, fUnspent);
}
if (filter & ISMINE_SPENDABLE_DELEGATED) {
credit += GetCachableAmount(CREDIT, ISMINE_SPENDABLE_DELEGATED, recalculate, fUnspent);
}
return credit;Also, this way, Fixing this would mean rewriting them as just: CAmount CWalletTx::GetCredit(const isminefilter& filter, bool recalculate, bool fUnspent) const
{
return GetCachableAmount(CREDIT, filter, recalculate, fUnspent);
}
CAmount CWalletTx::GetDebit(const isminefilter& filter) const
{
if (vin.empty())
return 0;
return GetCachableAmount(DEBIT, filter);
}(note that to make the the wallet_tests pass and fake out IsFromMe(), with this change, you would need to I'm not sure why there is a comment suggesting that and Alternatively, for now, we can just add some condition/hack for this particular situation. But in any case, in the future, we should properly reorganize this, as it seems highly inefficient as it is: we keep a vector of 16 cacheable amounts, to actually use only 4 of them. In upstream this is less relevant (they use 2 out of 4). |
|
Thinking more about it, for the problem explained above, we are forced to actually use the CAmount CWalletTx::GetCredit(const isminefilter& filter, bool recalculate, bool fUnspent) const
{
CAmount credit = 0;
if (filter & ISMINE_SPENDABLE) {
// GetBalance can assume transactions in mapWallet won't change
credit += GetCachableAmount(CREDIT, ISMINE_SPENDABLE, recalculate, fUnspent);
}
if (filter & ISMINE_WATCH_ONLY) {
credit += GetCachableAmount(CREDIT, ISMINE_WATCH_ONLY, recalculate, fUnspent);
}
bool fOnlyCold = (filter & ISMINE_COLD) && !(filter & ISMINE_SPENDABLE_DELEGATED);
bool fOnlyDelegated = !(filter & ISMINE_COLD) && (filter & ISMINE_SPENDABLE_DELEGATED);
if (fOnlyCold) {
credit += GetCachableAmount(CREDIT, ISMINE_COLD, recalculate, fUnspent);
} else if (fOnlyDelegated) {
credit += GetCachableAmount(CREDIT, ISMINE_SPENDABLE_DELEGATED, recalculate, fUnspent);
} else if (filter & ISMINE_SPENDABLE_STAKEABLE) {
// cached mixed balance (sum of outputs that are either COLD, or SPENDABLE_DELEGATED,
// counting them only once if the wallet has both coldstaking keys).
credit += GetCachableAmount(CREDIT, ISMINE_SPENDABLE_STAKEABLE, recalculate, fUnspent);
}
return credit;
}In the future, would be better to refactor |
|
Good catch zebra :) , cool that this work popped up few hidden issues and good discussions. Have something in mind, do we really need the When the wallet wants to check any output credit, it already know whether is a P2CS or not. If the P2CS is spendable then it's a ISMINE_SPENDABLE (the wallet has the owner priv key), if not then is ISMINE_COLD or a ISMINE_WATCH_ONLY. Going more general to describe this a little bit better. Doing this structural modification, we would be erasing the problem's root and making the flow simpler. |
|
Yes, we can do that, but then you would lose the mapping between cached amounts and ismine types. What we could do, though, is return ISMINE_SPENDABLE_DELEGATED also in the case of a wallet with both keys. This would change slightly the semantic of "cold balance" (as it would not include self-delegated outputs anymore) but it would remove the problem of having "overlapping" cached amounts: an output could be SPENDABLE_DELEGATED or COLD, but not both. Merging SPENDABLE_DELEGATED with SPENDABLE, as you suggest, would be an additional step, but then, again, we would lose the mapping (and I think it's convenient to have a separate cached balance just for delegations). Anyway, this kind of refactoring would require its own PR, so for now we can either add a comment in |
|
loose the one-to-one isminetype -> value cache mapping is good actually. As you said in the first comment, we are having there more balance types than what we are really using. We just need to store the basic types there, not all the possible combinations (those would be duplicating amounts, adding few other values already cached into other cached space). ofc not planning to do it here. Just continuing the nice convo where we started it :) . |
I agree. |
|
Added a commit following up the convo and zebra's last suggestion. |
|
|
ISMINE_SPENDABLE_DELEGATED will be taking precedence over ISMINE_COLD if the wallet has both priv keys.
147ae4b to
e02edd3
Compare
|
Done 👍 |
random-zebra
left a comment
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 e02edd3
…vable b425466 [Script] Add fColdStaking bool to IsSolvable (random-zebra) Pull request description: Fixing a minor bug introduced in #1757. `IsSolvable` always tries to produce a signature with the owner key, so it logs an error (and reports the coin as not solvable) for cold stakers. Fix it introducing a boolean argument in `IsSolvable`, to check the appropriate public key in the keystore. ACKs for top commit: furszy: Great catch 👌 , ACK b425466 Fuzzbawls: ACK b425466 Tree-SHA512: 9d4537218344c8176f1de500664905e78419fd852c9607b40cf83eedb55e5fb965977f31bba0a605fac35c9bee373613467d63c6d3f72386c598e7452b545101
Built on top of #1755 and #1666 .
The goal of this PR has been to improve the
CWalletTxto be able to add shielded cached balances in a much cleaner way.Including the following changes:
Adapted IsSolvable function from 0c8ea63 (without the witness check. Only used to check regular outputs to be able to decouple the output solvability from
ismine.henum)Solvability decoupled from
ismine.h. Back ported 6d714c3 and 4e91820 from Separate IsMine from solvability bitcoin/bitcoin#13142.And lastly, adapted wallet: add cachable amounts for caching credit/debit values bitcoin/bitcoin#15780 beauty cleanup to our sources.