Skip to content

Commit 215e599

Browse files
committed
wallet: Remove unused CachedTxGet{Available,Immature}Credit
These two functions are no longer used as GetBalances now uses the TXO set rather than per-tx cached balances
1 parent 49675de commit 215e599

File tree

4 files changed

+1
-142
lines changed

4 files changed

+1
-142
lines changed

src/wallet/receive.cpp

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -146,52 +146,6 @@ CAmount CachedTxGetChange(const CWallet& wallet, const CWalletTx& wtx)
146146
return wtx.nChangeCached;
147147
}
148148

149-
CAmount CachedTxGetImmatureCredit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter)
150-
{
151-
AssertLockHeld(wallet.cs_wallet);
152-
153-
if (wallet.IsTxImmatureCoinBase(wtx) && wtx.isConfirmed()) {
154-
return GetCachableAmount(wallet, wtx, CWalletTx::IMMATURE_CREDIT, filter);
155-
}
156-
157-
return 0;
158-
}
159-
160-
CAmount CachedTxGetAvailableCredit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter)
161-
{
162-
AssertLockHeld(wallet.cs_wallet);
163-
164-
// Avoid caching ismine for NO or ALL cases (could remove this check and simplify in the future).
165-
bool allow_cache = (filter & ISMINE_ALL) && (filter & ISMINE_ALL) != ISMINE_ALL;
166-
167-
// Must wait until coinbase is safely deep enough in the chain before valuing it
168-
if (wallet.IsTxImmatureCoinBase(wtx))
169-
return 0;
170-
171-
if (allow_cache && wtx.m_amounts[CWalletTx::AVAILABLE_CREDIT].m_cached[filter]) {
172-
return wtx.m_amounts[CWalletTx::AVAILABLE_CREDIT].m_value[filter];
173-
}
174-
175-
bool allow_used_addresses = (filter & ISMINE_USED) || !wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE);
176-
CAmount nCredit = 0;
177-
Txid hashTx = wtx.GetHash();
178-
for (unsigned int i = 0; i < wtx.tx->vout.size(); i++) {
179-
const CTxOut& txout = wtx.tx->vout[i];
180-
if (!wallet.IsSpent(COutPoint(hashTx, i)) && (allow_used_addresses || !wallet.IsSpentKey(txout.scriptPubKey))) {
181-
nCredit += OutputGetCredit(wallet, txout, filter);
182-
if (!MoneyRange(nCredit))
183-
throw std::runtime_error(std::string(__func__) + " : value out of range");
184-
}
185-
}
186-
187-
if (allow_cache) {
188-
wtx.m_amounts[CWalletTx::AVAILABLE_CREDIT].Set(filter, nCredit);
189-
wtx.m_is_cache_empty = false;
190-
}
191-
192-
return nCredit;
193-
}
194-
195149
void CachedTxGetAmounts(const CWallet& wallet, const CWalletTx& wtx,
196150
std::list<COutputEntry>& listReceived,
197151
std::list<COutputEntry>& listSent, CAmount& nFee, const isminefilter& filter,

src/wallet/receive.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ CAmount CachedTxGetCredit(const CWallet& wallet, const CWalletTx& wtx, const ism
3030
//! filter decides which addresses will count towards the debit
3131
CAmount CachedTxGetDebit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter);
3232
CAmount CachedTxGetChange(const CWallet& wallet, const CWalletTx& wtx);
33-
CAmount CachedTxGetImmatureCredit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter)
34-
EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
35-
CAmount CachedTxGetAvailableCredit(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter = ISMINE_SPENDABLE)
36-
EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
3733
struct COutputEntry
3834
{
3935
CTxDestination destination;

src/wallet/test/wallet_tests.cpp

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -244,35 +244,6 @@ BOOST_FIXTURE_TEST_CASE(write_wallet_settings_concurrently, TestingSetup)
244244
/*num_expected_wallets=*/0);
245245
}
246246

247-
// Check that GetImmatureCredit() returns a newly calculated value instead of
248-
// the cached value after a MarkDirty() call.
249-
//
250-
// This is a regression test written to verify a bugfix for the immature credit
251-
// function. Similar tests probably should be written for the other credit and
252-
// debit functions.
253-
BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup)
254-
{
255-
CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
256-
257-
LOCK(wallet.cs_wallet);
258-
LOCK(Assert(m_node.chainman)->GetMutex());
259-
CWalletTx wtx{m_coinbase_txns.back(), TxStateConfirmed{m_node.chainman->ActiveChain().Tip()->GetBlockHash(), m_node.chainman->ActiveChain().Height(), /*index=*/0}};
260-
wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
261-
wallet.SetupDescriptorScriptPubKeyMans();
262-
263-
wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
264-
265-
// Call GetImmatureCredit() once before adding the key to the wallet to
266-
// cache the current immature credit amount, which is 0.
267-
BOOST_CHECK_EQUAL(CachedTxGetImmatureCredit(wallet, wtx, ISMINE_SPENDABLE), 0);
268-
269-
// Invalidate the cached value, add the key, and make sure a new immature
270-
// credit amount is calculated.
271-
wtx.MarkDirty();
272-
AddKey(wallet, coinbaseKey);
273-
BOOST_CHECK_EQUAL(CachedTxGetImmatureCredit(wallet, wtx, ISMINE_SPENDABLE), 50*COIN);
274-
}
275-
276247
static int64_t AddTx(ChainstateManager& chainman, CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime)
277248
{
278249
CMutableTransaction tx;
@@ -751,65 +722,5 @@ BOOST_FIXTURE_TEST_CASE(RemoveTxs, TestChain100Setup)
751722
TestUnloadWallet(std::move(wallet));
752723
}
753724

754-
/**
755-
* Checks a wallet invalid state where the inputs (prev-txs) of a new arriving transaction are not marked dirty,
756-
* while the transaction that spends them exist inside the in-memory wallet tx map (not stored on db due a db write failure).
757-
*/
758-
BOOST_FIXTURE_TEST_CASE(wallet_sync_tx_invalid_state_test, TestingSetup)
759-
{
760-
CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
761-
{
762-
LOCK(wallet.cs_wallet);
763-
wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
764-
wallet.SetupDescriptorScriptPubKeyMans();
765-
}
766-
767-
// Add tx to wallet
768-
const auto op_dest{*Assert(wallet.GetNewDestination(OutputType::BECH32M, ""))};
769-
770-
CMutableTransaction mtx;
771-
mtx.vout.emplace_back(COIN, GetScriptForDestination(op_dest));
772-
mtx.vin.emplace_back(Txid::FromUint256(m_rng.rand256()), 0);
773-
const auto& tx_id_to_spend = wallet.AddToWallet(MakeTransactionRef(mtx), TxStateInMempool{})->GetHash();
774-
775-
{
776-
// Cache and verify available balance for the wtx
777-
LOCK(wallet.cs_wallet);
778-
const CWalletTx* wtx_to_spend = wallet.GetWalletTx(tx_id_to_spend);
779-
BOOST_CHECK_EQUAL(CachedTxGetAvailableCredit(wallet, *wtx_to_spend), 1 * COIN);
780-
}
781-
782-
// Now the good case:
783-
// 1) Add a transaction that spends the previously created transaction
784-
// 2) Verify that the available balance of this new tx and the old one is updated (prev tx is marked dirty)
785-
786-
mtx.vin.clear();
787-
mtx.vin.emplace_back(tx_id_to_spend, 0);
788-
wallet.transactionAddedToMempool(MakeTransactionRef(mtx));
789-
const auto good_tx_id{mtx.GetHash()};
790-
791-
{
792-
// Verify balance update for the new tx and the old one
793-
LOCK(wallet.cs_wallet);
794-
const CWalletTx* new_wtx = wallet.GetWalletTx(good_tx_id);
795-
BOOST_CHECK_EQUAL(CachedTxGetAvailableCredit(wallet, *new_wtx), 1 * COIN);
796-
797-
// Now the old wtx
798-
const CWalletTx* wtx_to_spend = wallet.GetWalletTx(tx_id_to_spend);
799-
BOOST_CHECK_EQUAL(CachedTxGetAvailableCredit(wallet, *wtx_to_spend), 0 * COIN);
800-
}
801-
802-
// Now the bad case:
803-
// 1) Make db always fail
804-
// 2) Try to add a transaction that spends the previously created transaction and
805-
// verify that we are not moving forward if the wallet cannot store it
806-
GetMockableDatabase(wallet).m_pass = false;
807-
mtx.vin.clear();
808-
mtx.vin.emplace_back(good_tx_id, 0);
809-
BOOST_CHECK_EXCEPTION(wallet.transactionAddedToMempool(MakeTransactionRef(mtx)),
810-
std::runtime_error,
811-
HasReason("DB error adding transaction to wallet, write failed"));
812-
}
813-
814725
BOOST_AUTO_TEST_SUITE_END()
815726
} // namespace wallet

src/wallet/transaction.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class CWalletTx
219219
std::multimap<int64_t, CWalletTx*>::const_iterator m_it_wtxOrdered;
220220

221221
// memory only
222-
enum AmountType { DEBIT, CREDIT, IMMATURE_CREDIT, AVAILABLE_CREDIT, AMOUNTTYPE_ENUM_ELEMENTS };
222+
enum AmountType { DEBIT, CREDIT, AMOUNTTYPE_ENUM_ELEMENTS };
223223
mutable CachableAmount m_amounts[AMOUNTTYPE_ENUM_ELEMENTS];
224224
/**
225225
* This flag is true if all m_amounts caches are empty. This is particularly
@@ -316,8 +316,6 @@ class CWalletTx
316316
{
317317
m_amounts[DEBIT].Reset();
318318
m_amounts[CREDIT].Reset();
319-
m_amounts[IMMATURE_CREDIT].Reset();
320-
m_amounts[AVAILABLE_CREDIT].Reset();
321319
fChangeCached = false;
322320
m_is_cache_empty = true;
323321
}

0 commit comments

Comments
 (0)