mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 06:52:36 +01:00
Merge bitcoin/bitcoin#25543: wallet: cleanup cached amount and input mine check code
47ea70fbb8
wallet: clean AllInputsMine code, use InputIsMine internally (furszy)bf310b0e8c
wallet: clean InputIsMine code, use GetWalletTx (furszy)0cb177263c
wallet: unify CachedTxGetImmatureCredit and CachedTxGetImmatureWatchOnlyCredit (furszy)04c6423f7b
wallet: remove always true 'fUseCache' arg from CachedTxGetAvailableCredit (furszy)4f0ca9bff6
wallet: remove always false 'recalculate' arg from GetCachableAmount (furszy)47b1012677
wallet: remove always true 'fUseCache' from CachedTxGetImmatureWatchOnlyCredit (furszy)da8f62de2c
wallet: remove always true 'fUseCache' from CachedTxGetImmatureCredit (furszy) Pull request description: Another wallet's code garbage collector work. Part of the `mapWallet` encapsulation goal. Focused on the following points: 1) Remove always true `fUseCache` argument from `CachedTxGetImmatureCredit`, `CachedTxGetImmatureWatchOnly` and `CachedTxGetAvailableCredit`. 2) Remove always false `recalculate` argument from `GetCachableAmount`. 3) Merge `CachedTxGetImmatureCredit` and `CachedTxGetImmatureWatchOnlyCredit` as they do share the exact same code. 4) Clean `InputIsMine` method; use `GetWalletTx` instead of access the wallet's map directly. 5) Clean `AllInputsMine` method; use `InputIsMine` instead of duplicate the exact same code internally. ACKs for top commit: aureleoules: re-ACK47ea70fbb8
achow101: ACK47ea70fbb8
theStack: re-ACK47ea70fbb8
Tree-SHA512: e9b64b57de7be6165c5e5552e28cd8a03d4736b0a3707d29d129e3a0a3db6a855c2abf47a24917236060835a297b564a97b66d4c8b178d6bdafb93a12a7c0b40
This commit is contained in:
commit
d1e42659bb
3 changed files with 20 additions and 48 deletions
|
@ -9,15 +9,12 @@
|
||||||
#include <wallet/wallet.h>
|
#include <wallet/wallet.h>
|
||||||
|
|
||||||
namespace wallet {
|
namespace wallet {
|
||||||
isminetype InputIsMine(const CWallet& wallet, const CTxIn &txin)
|
isminetype InputIsMine(const CWallet& wallet, const CTxIn& txin)
|
||||||
{
|
{
|
||||||
AssertLockHeld(wallet.cs_wallet);
|
AssertLockHeld(wallet.cs_wallet);
|
||||||
std::map<uint256, CWalletTx>::const_iterator mi = wallet.mapWallet.find(txin.prevout.hash);
|
const CWalletTx* prev = wallet.GetWalletTx(txin.prevout.hash);
|
||||||
if (mi != wallet.mapWallet.end())
|
if (prev && txin.prevout.n < prev->tx->vout.size()) {
|
||||||
{
|
return wallet.IsMine(prev->tx->vout[txin.prevout.n]);
|
||||||
const CWalletTx& prev = (*mi).second;
|
|
||||||
if (txin.prevout.n < prev.tx->vout.size())
|
|
||||||
return wallet.IsMine(prev.tx->vout[txin.prevout.n]);
|
|
||||||
}
|
}
|
||||||
return ISMINE_NO;
|
return ISMINE_NO;
|
||||||
}
|
}
|
||||||
|
@ -25,20 +22,8 @@ isminetype InputIsMine(const CWallet& wallet, const CTxIn &txin)
|
||||||
bool AllInputsMine(const CWallet& wallet, const CTransaction& tx, const isminefilter& filter)
|
bool AllInputsMine(const CWallet& wallet, const CTransaction& tx, const isminefilter& filter)
|
||||||
{
|
{
|
||||||
LOCK(wallet.cs_wallet);
|
LOCK(wallet.cs_wallet);
|
||||||
|
for (const CTxIn& txin : tx.vin) {
|
||||||
for (const CTxIn& txin : tx.vin)
|
if (!(InputIsMine(wallet, txin) & filter)) return false;
|
||||||
{
|
|
||||||
auto mi = wallet.mapWallet.find(txin.prevout.hash);
|
|
||||||
if (mi == wallet.mapWallet.end())
|
|
||||||
return false; // any unknown inputs can't be from us
|
|
||||||
|
|
||||||
const CWalletTx& prev = (*mi).second;
|
|
||||||
|
|
||||||
if (txin.prevout.n >= prev.tx->vout.size())
|
|
||||||
return false; // invalid input!
|
|
||||||
|
|
||||||
if (!(wallet.IsMine(prev.tx->vout[txin.prevout.n]) & filter))
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -111,10 +96,10 @@ CAmount TxGetChange(const CWallet& wallet, const CTransaction& tx)
|
||||||
return nChange;
|
return nChange;
|
||||||
}
|
}
|
||||||
|
|
||||||
static CAmount GetCachableAmount(const CWallet& wallet, const CWalletTx& wtx, CWalletTx::AmountType type, const isminefilter& filter, bool recalculate = false)
|
static CAmount GetCachableAmount(const CWallet& wallet, const CWalletTx& wtx, CWalletTx::AmountType type, const isminefilter& filter)
|
||||||
{
|
{
|
||||||
auto& amount = wtx.m_amounts[type];
|
auto& amount = wtx.m_amounts[type];
|
||||||
if (recalculate || !amount.m_cached[filter]) {
|
if (!amount.m_cached[filter]) {
|
||||||
amount.Set(filter, type == CWalletTx::DEBIT ? wallet.GetDebit(*wtx.tx, filter) : TxGetCredit(wallet, *wtx.tx, filter));
|
amount.Set(filter, type == CWalletTx::DEBIT ? wallet.GetDebit(*wtx.tx, filter) : TxGetCredit(wallet, *wtx.tx, filter));
|
||||||
wtx.m_is_cache_empty = false;
|
wtx.m_is_cache_empty = false;
|
||||||
}
|
}
|
||||||
|
@ -160,29 +145,18 @@ CAmount CachedTxGetChange(const CWallet& wallet, const CWalletTx& wtx)
|
||||||
return wtx.nChangeCached;
|
return wtx.nChangeCached;
|
||||||
}
|
}
|
||||||
|
|
||||||
CAmount CachedTxGetImmatureCredit(const CWallet& wallet, const CWalletTx& wtx, bool fUseCache)
|
CAmount CachedTxGetImmatureCredit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter)
|
||||||
{
|
{
|
||||||
AssertLockHeld(wallet.cs_wallet);
|
AssertLockHeld(wallet.cs_wallet);
|
||||||
|
|
||||||
if (wallet.IsTxImmatureCoinBase(wtx) && wallet.IsTxInMainChain(wtx)) {
|
if (wallet.IsTxImmatureCoinBase(wtx) && wallet.IsTxInMainChain(wtx)) {
|
||||||
return GetCachableAmount(wallet, wtx, CWalletTx::IMMATURE_CREDIT, ISMINE_SPENDABLE, !fUseCache);
|
return GetCachableAmount(wallet, wtx, CWalletTx::IMMATURE_CREDIT, filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CAmount CachedTxGetImmatureWatchOnlyCredit(const CWallet& wallet, const CWalletTx& wtx, const bool fUseCache)
|
CAmount CachedTxGetAvailableCredit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter)
|
||||||
{
|
|
||||||
AssertLockHeld(wallet.cs_wallet);
|
|
||||||
|
|
||||||
if (wallet.IsTxImmatureCoinBase(wtx) && wallet.IsTxInMainChain(wtx)) {
|
|
||||||
return GetCachableAmount(wallet, wtx, CWalletTx::IMMATURE_CREDIT, ISMINE_WATCH_ONLY, !fUseCache);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
CAmount CachedTxGetAvailableCredit(const CWallet& wallet, const CWalletTx& wtx, bool fUseCache, const isminefilter& filter)
|
|
||||||
{
|
{
|
||||||
AssertLockHeld(wallet.cs_wallet);
|
AssertLockHeld(wallet.cs_wallet);
|
||||||
|
|
||||||
|
@ -193,7 +167,7 @@ CAmount CachedTxGetAvailableCredit(const CWallet& wallet, const CWalletTx& wtx,
|
||||||
if (wallet.IsTxImmatureCoinBase(wtx))
|
if (wallet.IsTxImmatureCoinBase(wtx))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (fUseCache && allow_cache && wtx.m_amounts[CWalletTx::AVAILABLE_CREDIT].m_cached[filter]) {
|
if (allow_cache && wtx.m_amounts[CWalletTx::AVAILABLE_CREDIT].m_cached[filter]) {
|
||||||
return wtx.m_amounts[CWalletTx::AVAILABLE_CREDIT].m_value[filter];
|
return wtx.m_amounts[CWalletTx::AVAILABLE_CREDIT].m_value[filter];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -328,8 +302,8 @@ Balance GetBalance(const CWallet& wallet, const int min_depth, bool avoid_reuse)
|
||||||
const CWalletTx& wtx = entry.second;
|
const CWalletTx& wtx = entry.second;
|
||||||
const bool is_trusted{CachedTxIsTrusted(wallet, wtx, trusted_parents)};
|
const bool is_trusted{CachedTxIsTrusted(wallet, wtx, trusted_parents)};
|
||||||
const int tx_depth{wallet.GetTxDepthInMainChain(wtx)};
|
const int tx_depth{wallet.GetTxDepthInMainChain(wtx)};
|
||||||
const CAmount tx_credit_mine{CachedTxGetAvailableCredit(wallet, wtx, /*fUseCache=*/true, ISMINE_SPENDABLE | reuse_filter)};
|
const CAmount tx_credit_mine{CachedTxGetAvailableCredit(wallet, wtx, ISMINE_SPENDABLE | reuse_filter)};
|
||||||
const CAmount tx_credit_watchonly{CachedTxGetAvailableCredit(wallet, wtx, /*fUseCache=*/true, ISMINE_WATCH_ONLY | reuse_filter)};
|
const CAmount tx_credit_watchonly{CachedTxGetAvailableCredit(wallet, wtx, ISMINE_WATCH_ONLY | reuse_filter)};
|
||||||
if (is_trusted && tx_depth >= min_depth) {
|
if (is_trusted && tx_depth >= min_depth) {
|
||||||
ret.m_mine_trusted += tx_credit_mine;
|
ret.m_mine_trusted += tx_credit_mine;
|
||||||
ret.m_watchonly_trusted += tx_credit_watchonly;
|
ret.m_watchonly_trusted += tx_credit_watchonly;
|
||||||
|
@ -338,8 +312,8 @@ Balance GetBalance(const CWallet& wallet, const int min_depth, bool avoid_reuse)
|
||||||
ret.m_mine_untrusted_pending += tx_credit_mine;
|
ret.m_mine_untrusted_pending += tx_credit_mine;
|
||||||
ret.m_watchonly_untrusted_pending += tx_credit_watchonly;
|
ret.m_watchonly_untrusted_pending += tx_credit_watchonly;
|
||||||
}
|
}
|
||||||
ret.m_mine_immature += CachedTxGetImmatureCredit(wallet, wtx);
|
ret.m_mine_immature += CachedTxGetImmatureCredit(wallet, wtx, ISMINE_SPENDABLE);
|
||||||
ret.m_watchonly_immature += CachedTxGetImmatureWatchOnlyCredit(wallet, wtx);
|
ret.m_watchonly_immature += CachedTxGetImmatureCredit(wallet, wtx, ISMINE_WATCH_ONLY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
@ -29,11 +29,9 @@ CAmount CachedTxGetCredit(const CWallet& wallet, const CWalletTx& wtx, const ism
|
||||||
//! filter decides which addresses will count towards the debit
|
//! filter decides which addresses will count towards the debit
|
||||||
CAmount CachedTxGetDebit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter);
|
CAmount CachedTxGetDebit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter);
|
||||||
CAmount CachedTxGetChange(const CWallet& wallet, const CWalletTx& wtx);
|
CAmount CachedTxGetChange(const CWallet& wallet, const CWalletTx& wtx);
|
||||||
CAmount CachedTxGetImmatureCredit(const CWallet& wallet, const CWalletTx& wtx, bool fUseCache = true)
|
CAmount CachedTxGetImmatureCredit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter)
|
||||||
EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
|
EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
|
||||||
CAmount CachedTxGetImmatureWatchOnlyCredit(const CWallet& wallet, const CWalletTx& wtx, const bool fUseCache = true)
|
CAmount CachedTxGetAvailableCredit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter = ISMINE_SPENDABLE)
|
||||||
EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
|
|
||||||
CAmount CachedTxGetAvailableCredit(const CWallet& wallet, const CWalletTx& wtx, bool fUseCache = true, const isminefilter& filter = ISMINE_SPENDABLE)
|
|
||||||
EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
|
EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
|
||||||
struct COutputEntry
|
struct COutputEntry
|
||||||
{
|
{
|
||||||
|
|
|
@ -360,13 +360,13 @@ BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup)
|
||||||
|
|
||||||
// Call GetImmatureCredit() once before adding the key to the wallet to
|
// Call GetImmatureCredit() once before adding the key to the wallet to
|
||||||
// cache the current immature credit amount, which is 0.
|
// cache the current immature credit amount, which is 0.
|
||||||
BOOST_CHECK_EQUAL(CachedTxGetImmatureCredit(wallet, wtx), 0);
|
BOOST_CHECK_EQUAL(CachedTxGetImmatureCredit(wallet, wtx, ISMINE_SPENDABLE), 0);
|
||||||
|
|
||||||
// Invalidate the cached value, add the key, and make sure a new immature
|
// Invalidate the cached value, add the key, and make sure a new immature
|
||||||
// credit amount is calculated.
|
// credit amount is calculated.
|
||||||
wtx.MarkDirty();
|
wtx.MarkDirty();
|
||||||
AddKey(wallet, coinbaseKey);
|
AddKey(wallet, coinbaseKey);
|
||||||
BOOST_CHECK_EQUAL(CachedTxGetImmatureCredit(wallet, wtx), 50*COIN);
|
BOOST_CHECK_EQUAL(CachedTxGetImmatureCredit(wallet, wtx, ISMINE_SPENDABLE), 50*COIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int64_t AddTx(ChainstateManager& chainman, CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime)
|
static int64_t AddTx(ChainstateManager& chainman, CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime)
|
||||||
|
|
Loading…
Add table
Reference in a new issue