-
Notifications
You must be signed in to change notification settings - Fork 38.1k
wallet: add cachable amounts for caching credit/debit values #15780
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,22 +9,43 @@ | |
#include <script/standard.h> | ||
|
||
#include <stdint.h> | ||
#include <bitset> | ||
|
||
class CKeyStore; | ||
class CScript; | ||
|
||
/** IsMine() return codes */ | ||
enum isminetype | ||
{ | ||
ISMINE_NO = 0, | ||
ISMINE_WATCH_ONLY = 1, | ||
ISMINE_SPENDABLE = 2, | ||
ISMINE_ALL = ISMINE_WATCH_ONLY | ISMINE_SPENDABLE | ||
ISMINE_NO = 0, | ||
ISMINE_WATCH_ONLY = 1 << 0, | ||
ISMINE_SPENDABLE = 1 << 1, | ||
ISMINE_ALL = ISMINE_WATCH_ONLY | ISMINE_SPENDABLE, | ||
ISMINE_ENUM_ELEMENTS, | ||
}; | ||
/** used for bitflags of isminetype */ | ||
typedef uint8_t isminefilter; | ||
|
||
isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey); | ||
isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest); | ||
|
||
/** | ||
* Cachable amount subdivided into watchonly and spendable parts. | ||
*/ | ||
struct CachableAmount | ||
{ | ||
// NO and ALL are never (supposed to be) cached | ||
std::bitset<ISMINE_ENUM_ELEMENTS> m_cached; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could add a comment to say that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, done. I also fixed typo in wallet.cpp#1990 (said "Avoid caching ismine or NONE or all cases" before). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The two comments contradict each other: // NO and ALL are never (supposed to be) cached // Avoid caching ismine for NO or ALL cases (could remove this check and simplify in the future). If it's harmful to cache these values, we should drop the second comment and explain the harm. If it's not harmful, we should drop the first comment because it's misleading and irrelevant. In the future, it would be good to get rid of all unused variables and special cases by switching to a 2 part cache or just caching the 4 parts unconditionally. |
||
CAmount m_value[ISMINE_ENUM_ELEMENTS]; | ||
inline void Reset() | ||
{ | ||
m_cached.reset(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think leaving it public is ok, but I can be convinced to make it private. For now, I made |
||
} | ||
void Set(isminefilter filter, CAmount value) | ||
{ | ||
m_cached.set(filter); | ||
m_value[filter] = value; | ||
} | ||
}; | ||
|
||
#endif // BITCOIN_SCRIPT_ISMINE_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1932,33 +1932,26 @@ std::set<uint256> CWalletTx::GetConflicts() const | |
return result; | ||
} | ||
|
||
CAmount CWalletTx::GetCachableAmount(AmountType type, const isminefilter& filter, bool recalculate) const | ||
{ | ||
auto& amount = m_amounts[type]; | ||
if (recalculate || !amount.m_cached[filter]) { | ||
amount.Set(filter, type == DEBIT ? pwallet->GetDebit(*tx, filter) : pwallet->GetCredit(*tx, filter)); | ||
} | ||
return amount.m_value[filter]; | ||
} | ||
|
||
CAmount CWalletTx::GetDebit(const isminefilter& filter) const | ||
{ | ||
if (tx->vin.empty()) | ||
return 0; | ||
|
||
CAmount debit = 0; | ||
if(filter & ISMINE_SPENDABLE) | ||
{ | ||
if (fDebitCached) | ||
debit += nDebitCached; | ||
else | ||
{ | ||
nDebitCached = pwallet->GetDebit(*tx, ISMINE_SPENDABLE); | ||
fDebitCached = true; | ||
debit += nDebitCached; | ||
} | ||
if (filter & ISMINE_SPENDABLE) { | ||
debit += GetCachableAmount(DEBIT, ISMINE_SPENDABLE); | ||
} | ||
if(filter & ISMINE_WATCH_ONLY) | ||
{ | ||
if(fWatchDebitCached) | ||
debit += nWatchDebitCached; | ||
else | ||
{ | ||
nWatchDebitCached = pwallet->GetDebit(*tx, ISMINE_WATCH_ONLY); | ||
fWatchDebitCached = true; | ||
debit += nWatchDebitCached; | ||
} | ||
if (filter & ISMINE_WATCH_ONLY) { | ||
debit += GetCachableAmount(DEBIT, ISMINE_WATCH_ONLY); | ||
} | ||
return debit; | ||
} | ||
|
@@ -1970,40 +1963,20 @@ CAmount CWalletTx::GetCredit(interfaces::Chain::Lock& locked_chain, const ismine | |
return 0; | ||
|
||
CAmount credit = 0; | ||
if (filter & ISMINE_SPENDABLE) | ||
{ | ||
if (filter & ISMINE_SPENDABLE) { | ||
// GetBalance can assume transactions in mapWallet won't change | ||
if (fCreditCached) | ||
credit += nCreditCached; | ||
else | ||
{ | ||
nCreditCached = pwallet->GetCredit(*tx, ISMINE_SPENDABLE); | ||
fCreditCached = true; | ||
credit += nCreditCached; | ||
} | ||
credit += GetCachableAmount(CREDIT, ISMINE_SPENDABLE); | ||
} | ||
if (filter & ISMINE_WATCH_ONLY) | ||
{ | ||
if (fWatchCreditCached) | ||
credit += nWatchCreditCached; | ||
else | ||
{ | ||
nWatchCreditCached = pwallet->GetCredit(*tx, ISMINE_WATCH_ONLY); | ||
fWatchCreditCached = true; | ||
credit += nWatchCreditCached; | ||
} | ||
if (filter & ISMINE_WATCH_ONLY) { | ||
credit += GetCachableAmount(CREDIT, ISMINE_WATCH_ONLY); | ||
} | ||
return credit; | ||
} | ||
|
||
CAmount CWalletTx::GetImmatureCredit(interfaces::Chain::Lock& locked_chain, bool fUseCache) const | ||
{ | ||
if (IsImmatureCoinBase(locked_chain) && IsInMainChain(locked_chain)) { | ||
if (fUseCache && fImmatureCreditCached) | ||
return nImmatureCreditCached; | ||
nImmatureCreditCached = pwallet->GetCredit(*tx, ISMINE_SPENDABLE); | ||
fImmatureCreditCached = true; | ||
return nImmatureCreditCached; | ||
return GetCachableAmount(IMMATURE_CREDIT, ISMINE_SPENDABLE, !fUseCache); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't want to suggest a change for this PR, since it's good that this just rearranges variables and doesn't change any computation. But in the future, it seems like IMMATURE_CREDIT could be replaced with CREDIT here without affecting anything, and IMMATURE_CREDIT and AVAILABLE_CREDIT could go away. Unless I'm missing something, it seems like a lot of the previous variables like nImmatureCreditCached were just redundant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! That would definitely improve things. I agree that's beyond this PR but will gladly dig into it if nobody else does after merge. |
||
} | ||
|
||
return 0; | ||
|
@@ -2014,23 +1987,15 @@ CAmount CWalletTx::GetAvailableCredit(interfaces::Chain::Lock& locked_chain, boo | |
if (pwallet == nullptr) | ||
return 0; | ||
|
||
// Avoid caching ismine for NO or ALL cases (could remove this check and simplify in the future). | ||
bool allow_cache = filter == ISMINE_SPENDABLE || filter == ISMINE_WATCH_ONLY; | ||
|
||
// Must wait until coinbase is safely deep enough in the chain before valuing it | ||
if (IsImmatureCoinBase(locked_chain)) | ||
return 0; | ||
|
||
CAmount* cache = nullptr; | ||
bool* cache_used = nullptr; | ||
|
||
if (filter == ISMINE_SPENDABLE) { | ||
cache = &nAvailableCreditCached; | ||
cache_used = &fAvailableCreditCached; | ||
} else if (filter == ISMINE_WATCH_ONLY) { | ||
cache = &nAvailableWatchCreditCached; | ||
cache_used = &fAvailableWatchCreditCached; | ||
} | ||
|
||
if (fUseCache && cache_used && *cache_used) { | ||
return *cache; | ||
if (fUseCache && allow_cache && m_amounts[AVAILABLE_CREDIT].m_cached[filter]) { | ||
return m_amounts[AVAILABLE_CREDIT].m_value[filter]; | ||
} | ||
|
||
CAmount nCredit = 0; | ||
|
@@ -2046,22 +2011,17 @@ CAmount CWalletTx::GetAvailableCredit(interfaces::Chain::Lock& locked_chain, boo | |
} | ||
} | ||
|
||
if (cache) { | ||
*cache = nCredit; | ||
assert(cache_used); | ||
*cache_used = true; | ||
if (allow_cache) { | ||
m_amounts[AVAILABLE_CREDIT].Set(filter, nCredit); | ||
} | ||
|
||
return nCredit; | ||
} | ||
|
||
CAmount CWalletTx::GetImmatureWatchOnlyCredit(interfaces::Chain::Lock& locked_chain, const bool fUseCache) const | ||
{ | ||
if (IsImmatureCoinBase(locked_chain) && IsInMainChain(locked_chain)) { | ||
if (fUseCache && fImmatureWatchCreditCached) | ||
return nImmatureWatchCreditCached; | ||
nImmatureWatchCreditCached = pwallet->GetCredit(*tx, ISMINE_WATCH_ONLY); | ||
fImmatureWatchCreditCached = true; | ||
return nImmatureWatchCreditCached; | ||
return GetCachableAmount(IMMATURE_CREDIT, ISMINE_WATCH_ONLY, !fUseCache); | ||
} | ||
|
||
return 0; | ||
|
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.
Note: Could add doxygen comment suggested previously #15780 (comment)
//! Cached amount subdivided into watchonly and spendable parts.
Uh oh!
There was an error while loading. Please reload this page.
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.
Oops, I missed that part. Thanks, done. (I used
/**...*/
)