Skip to content

Commit 9a5dc62

Browse files
committed
partial bitcoin#22100: Clean up new wallet spend, receive files added bitcoin#21207
Includes changes that primarily affect `wallet/receive.cpp`
1 parent 258cdc2 commit 9a5dc62

19 files changed

+350
-358
lines changed

src/bench/coin_selection.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static void addCoin(const CAmount& nValue, const CWallet& wallet, std::vector<st
1717
tx.nLockTime = nextLockTime++; // so all transactions get different hashes
1818
tx.vout.resize(1);
1919
tx.vout[0].nValue = nValue;
20-
wtxs.push_back(std::make_unique<CWalletTx>(&wallet, MakeTransactionRef(std::move(tx))));
20+
wtxs.push_back(std::make_unique<CWalletTx>(MakeTransactionRef(std::move(tx))));
2121
}
2222

2323
// Simple benchmark for wallet coin selection. Note that it maybe be necessary
@@ -44,7 +44,7 @@ static void CoinSelection(benchmark::Bench& bench)
4444
// Create coins
4545
std::vector<COutput> coins;
4646
for (const auto& wtx : wtxs) {
47-
coins.emplace_back(wtx.get(), 0 /* iIn */, 6 * 24 /* nDepthIn */, true /* spendable */, true /* solvable */, true /* safe */);
47+
coins.emplace_back(wallet, *wtx, 0 /* iIn */, 6 * 24 /* nDepthIn */, true /* spendable */, true /* solvable */, true /* safe */);
4848
}
4949
const CoinEligibilityFilter filter_standard(1, 6, 0);
5050
const CoinSelectionParams coin_selection_params(/* change_output_size= */ 34,

src/bench/wallet_balance.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <test/util/setup_common.h>
1010
#include <test/util/wallet.h>
1111
#include <validationinterface.h>
12+
#include <wallet/receive.h>
1213
#include <wallet/wallet.h>
1314

1415
#include <optional>
@@ -35,11 +36,11 @@ static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const b
3536
}
3637
SyncWithValidationInterfaceQueue();
3738

38-
auto bal = wallet.GetBalance(); // Cache
39+
auto bal = GetBalance(wallet); // Cache
3940

4041
bench.minEpochIterations(epoch_iters).run([&] {
4142
if (set_dirty) wallet.MarkDirty();
42-
bal = wallet.GetBalance();
43+
bal = GetBalance(wallet);
4344
if (add_mine) assert(bal.m_mine_trusted > 0);
4445
});
4546
}

src/coinjoin/client.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <wallet/coincontrol.h>
2727
#include <wallet/coinjoin.h>
2828
#include <wallet/fees.h>
29+
#include <wallet/receive.h>
2930
#include <walletinitinterface.h>
3031

3132
#include <memory>
@@ -825,7 +826,7 @@ bool CCoinJoinClientSession::DoAutomaticDenominating(ChainstateManager& chainman
825826
return false;
826827
}
827828

828-
const auto bal = m_wallet->GetBalance();
829+
const auto bal = GetBalance(*m_wallet);
829830

830831
// check if there is anything left to do
831832
CAmount nBalanceAnonymized = bal.m_anonymized;

src/rpc/coinjoin.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <util/check.h>
1313
#include <util/strencodings.h>
1414
#include <validation.h>
15+
#include <wallet/receive.h>
1516
#include <wallet/rpc/util.h>
1617
#include <walletinitinterface.h>
1718

@@ -278,7 +279,7 @@ static RPCHelpMan coinjoinsalt_generate()
278279
}
279280
}
280281

281-
const auto wallet_balance{wallet->GetBalance()};
282+
const auto wallet_balance{GetBalance(*wallet)};
282283
const bool has_balance{(wallet_balance.m_anonymized
283284
+ wallet_balance.m_denominated_trusted
284285
+ wallet_balance.m_denominated_untrusted_pending) > 0};
@@ -380,7 +381,7 @@ static RPCHelpMan coinjoinsalt_set()
380381
}
381382
}
382383

383-
const auto wallet_balance{wallet->GetBalance()};
384+
const auto wallet_balance{GetBalance(*wallet)};
384385
const bool has_balance{(wallet_balance.m_anonymized
385386
+ wallet_balance.m_denominated_trusted
386387
+ wallet_balance.m_denominated_untrusted_pending) > 0};

src/wallet/coinjoin.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <coinjoin/common.h>
1010
#include <coinjoin/options.h>
1111
#include <evo/dmn_types.h>
12+
#include <wallet/receive.h>
1213
#include <wallet/wallet.h>
1314
#include <wallet/transaction.h>
1415

@@ -155,9 +156,9 @@ std::vector<CompactTallyItem> CWallet::SelectCoinsGroupedByAddresses(bool fSkipD
155156

156157
const CWalletTx& wtx = (*it).second;
157158

158-
if(wtx.IsCoinBase() && wtx.GetBlocksToMaturity() > 0) continue;
159-
if(fSkipUnconfirmed && !wtx.IsTrusted()) continue;
160-
if (wtx.GetDepthInMainChain() < 0) continue;
159+
if (wtx.IsCoinBase() && GetTxBlocksToMaturity(wtx) > 0) continue;
160+
if (fSkipUnconfirmed && !CachedTxIsTrusted(*this, wtx)) continue;
161+
if (GetTxDepthInMainChain(wtx) < 0) continue;
161162

162163
for (unsigned int i = 0; i < wtx.tx->vout.size(); i++) {
163164
CTxDestination txdest;
@@ -247,7 +248,7 @@ int CWallet::CountInputsWithAmount(CAmount nInputAmount) const
247248
const auto it = mapWallet.find(outpoint.hash);
248249
if (it == mapWallet.end()) continue;
249250
if (it->second.tx->vout[outpoint.n].nValue != nInputAmount) continue;
250-
if (it->second.GetDepthInMainChain() < 0) continue;
251+
if (GetTxDepthInMainChain(it->second) < 0) continue;
251252

252253
nTotal++;
253254
}
@@ -317,7 +318,7 @@ int CWallet::GetRealOutpointCoinJoinRounds(const COutPoint& outpoint, int nRound
317318
}
318319

319320
// make sure we spent all of it with 0 fee, reset to 0 rounds otherwise
320-
if (wtx->GetDebit(ISMINE_SPENDABLE) != wtx->GetCredit(ISMINE_SPENDABLE)) {
321+
if (CachedTxGetDebit(*this, *wtx, ISMINE_SPENDABLE) != CachedTxGetCredit(*this, *wtx, ISMINE_SPENDABLE)) {
321322
*nRoundsRef = 0;
322323
WalletCJLogPrint(this, "%s UPDATED %-70s %3d\n", __func__, outpoint.ToStringShort(), *nRoundsRef);
323324
return *nRoundsRef;
@@ -327,7 +328,7 @@ int CWallet::GetRealOutpointCoinJoinRounds(const COutPoint& outpoint, int nRound
327328
bool fDenomFound = false;
328329
// only denoms here so let's look up
329330
for (const auto& txinNext : wtx->tx->vin) {
330-
if (IsMine(txinNext)) {
331+
if (InputIsMine(*this, txinNext)) {
331332
int n = GetRealOutpointCoinJoinRounds(txinNext.prevout, nRounds + 1);
332333
// denom found, find the shortest chain or initially assign nShortest with the first found value
333334
if(n >= 0 && (n < nShortest || nShortest == -10)) {
@@ -481,7 +482,7 @@ CAmount CWallet::GetNormalizedAnonymizedBalance() const
481482

482483
CAmount nValue = it->second.tx->vout[outpoint.n].nValue;
483484
if (!CoinJoin::IsDenominatedAmount(nValue)) continue;
484-
if (it->second.GetDepthInMainChain() < 0) continue;
485+
if (GetTxDepthInMainChain(it->second) < 0) continue;
485486

486487
int nRounds = GetCappedOutpointCoinJoinRounds(outpoint);
487488
nTotal += nValue * nRounds / CCoinJoinClientOptions::GetRounds();
@@ -495,7 +496,7 @@ CAmount CachedTxGetAnonymizedCredit(const CWallet& wallet, const CWalletTx& wtx,
495496
AssertLockHeld(wallet.cs_wallet);
496497

497498
// Exclude coinbase and conflicted txes
498-
if (wtx.IsCoinBase() || wtx.GetDepthInMainChain() < 0)
499+
if (wtx.IsCoinBase() || wallet.GetTxBlocksToMaturity(wtx) < 0)
499500
return 0;
500501

501502
CAmount nCredit = 0;
@@ -512,7 +513,7 @@ CAmount CachedTxGetAnonymizedCredit(const CWallet& wallet, const CWalletTx& wtx,
512513
if (wallet.IsSpent(hashTx, i) || !CoinJoin::IsDenominatedAmount(txout.nValue)) continue;
513514

514515
if (wallet.IsFullyMixed(outpoint)) {
515-
nCredit += wallet.GetCredit(txout, ISMINE_SPENDABLE);
516+
nCredit += OutputGetCredit(wallet, txout, ISMINE_SPENDABLE);
516517
if (!MoneyRange(nCredit))
517518
throw std::runtime_error(std::string(__func__) + ": value out of range");
518519
}
@@ -528,13 +529,13 @@ CoinJoinCredits CachedTxGetAvailableCoinJoinCredits(const CWallet& wallet, const
528529
AssertLockHeld(wallet.cs_wallet);
529530

530531
// Must wait until coinbase is safely deep enough in the chain before valuing it
531-
if (wtx.IsCoinBase() && wtx.GetBlocksToMaturity() > 0)
532+
if (wtx.IsCoinBase() && wallet.GetTxBlocksToMaturity(wtx) > 0)
532533
return ret;
533534

534-
int nDepth = wtx.GetDepthInMainChain();
535+
int nDepth = wallet.GetTxBlocksToMaturity(wtx);
535536
if (nDepth < 0) return ret;
536537

537-
ret.is_unconfirmed = wtx.IsTrusted() && nDepth == 0;
538+
ret.is_unconfirmed = CachedTxIsTrusted(wallet, wtx) && nDepth == 0;
538539

539540
if (wtx.m_amounts[CWalletTx::ANON_CREDIT].m_cached[ISMINE_SPENDABLE]) {
540541
if (ret.is_unconfirmed && wtx.m_amounts[CWalletTx::DENOM_UCREDIT].m_cached[ISMINE_SPENDABLE]) {
@@ -550,7 +551,7 @@ CoinJoinCredits CachedTxGetAvailableCoinJoinCredits(const CWallet& wallet, const
550551
const COutPoint outpoint = COutPoint(hashTx, i);
551552

552553
if (wallet.IsSpent(hashTx, i) || !CoinJoin::IsDenominatedAmount(txout.nValue)) continue;
553-
const CAmount credit = wallet.GetCredit(txout, ISMINE_SPENDABLE);
554+
const CAmount credit = OutputGetCredit(wallet, txout, ISMINE_SPENDABLE);
554555

555556
if (wallet.IsFullyMixed(outpoint)) {
556557
ret.m_anonymized += credit;

src/wallet/interfaces.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <wallet/fees.h>
2727
#include <wallet/ismine.h>
2828
#include <wallet/load.h>
29+
#include <wallet/receive.h>
2930
#include <wallet/rpcwallet.h>
3031
#include <wallet/wallet.h>
3132

@@ -59,7 +60,7 @@ WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
5960
result.tx = wtx.tx;
6061
result.txin_is_mine.reserve(wtx.tx->vin.size());
6162
for (const auto& txin : wtx.tx->vin) {
62-
result.txin_is_mine.emplace_back(wallet.IsMine(txin));
63+
result.txin_is_mine.emplace_back(InputIsMine(wallet, txin));
6364
if (!fInputDenomFound && result.txin_is_mine.back() && wallet.IsDenominated(txin.prevout)) {
6465
fInputDenomFound = true;
6566
}
@@ -77,9 +78,9 @@ WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
7778
fOutputDenomFound = true;
7879
}
7980
}
80-
result.credit = wtx.GetCredit(ISMINE_ALL);
81-
result.debit = wtx.GetDebit(ISMINE_ALL);
82-
result.change = wtx.GetChange();
81+
result.credit = CachedTxGetCredit(wallet, wtx, ISMINE_ALL);
82+
result.debit = CachedTxGetDebit(wallet, wtx, ISMINE_ALL);
83+
result.change = CachedTxGetChange(wallet, wtx);
8384
result.time = wtx.GetTxTime();
8485
result.value_map = wtx.mapValue;
8586
result.is_coinbase = wtx.IsCoinBase();
@@ -100,15 +101,15 @@ WalletTxStatus MakeWalletTxStatus(const CWallet& wallet, const CWalletTx& wtx)
100101

101102
WalletTxStatus result;
102103
result.block_height = wtx.m_confirm.block_height > 0 ? wtx.m_confirm.block_height : std::numeric_limits<int>::max();
103-
result.blocks_to_maturity = wtx.GetBlocksToMaturity();
104-
result.depth_in_main_chain = wtx.GetDepthInMainChain();
104+
result.blocks_to_maturity = wallet.GetTxBlocksToMaturity(wtx);
105+
result.depth_in_main_chain = wallet.GetTxDepthInMainChain(wtx);
105106
result.time_received = wtx.nTimeReceived;
106107
result.lock_time = wtx.tx->nLockTime;
107108
result.is_final = wallet.chain().checkFinalTx(*wtx.tx);
108-
result.is_trusted = wtx.IsTrusted();
109+
result.is_trusted = CachedTxIsTrusted(wallet, wtx);
109110
result.is_abandoned = wtx.isAbandoned();
110111
result.is_coinbase = wtx.IsCoinBase();
111-
result.is_in_main_chain = wtx.IsInMainChain();
112+
result.is_in_main_chain = wallet.IsTxInMainChain(wtx);
112113
result.is_chainlocked = wallet.IsTxChainLocked(wtx);
113114
result.is_islocked = wallet.IsTxLockedByInstantSend(wtx);
114115
return result;
@@ -383,7 +384,7 @@ class WalletImpl : public Wallet
383384
}
384385
WalletBalances getBalances() override
385386
{
386-
const auto bal = m_wallet->GetBalance();
387+
const auto bal = GetBalance(*m_wallet);
387388
WalletBalances result;
388389
result.balance = bal.m_mine_trusted;
389390
result.unconfirmed_balance = bal.m_mine_untrusted_pending;
@@ -409,10 +410,7 @@ class WalletImpl : public Wallet
409410
balances = getBalances();
410411
return true;
411412
}
412-
CAmount getBalance() override
413-
{
414-
return m_wallet->GetBalance().m_mine_trusted;
415-
}
413+
CAmount getBalance() override { return GetBalance(*m_wallet).m_mine_trusted; }
416414
CAmount getAnonymizableBalance(bool fSkipDenominated, bool fSkipUnconfirmed) override
417415
{
418416
return m_wallet->GetAnonymizableBalance(fSkipDenominated, fSkipUnconfirmed);
@@ -436,7 +434,7 @@ class WalletImpl : public Wallet
436434
isminetype txinIsMine(const CTxIn& txin) override
437435
{
438436
LOCK(m_wallet->cs_wallet);
439-
return m_wallet->IsMine(txin);
437+
return InputIsMine(*m_wallet, txin);
440438
}
441439
isminetype txoutIsMine(const CTxOut& txout) override
442440
{
@@ -451,7 +449,7 @@ class WalletImpl : public Wallet
451449
CAmount getCredit(const CTxOut& txout, isminefilter filter) override
452450
{
453451
LOCK(m_wallet->cs_wallet);
454-
return m_wallet->GetCredit(txout, filter);
452+
return OutputGetCredit(*m_wallet, txout, filter);
455453
}
456454
CoinsList listCoins() override
457455
{
@@ -475,7 +473,7 @@ class WalletImpl : public Wallet
475473
result.emplace_back();
476474
auto it = m_wallet->mapWallet.find(output.hash);
477475
if (it != m_wallet->mapWallet.end()) {
478-
int depth = it->second.GetDepthInMainChain();
476+
int depth = m_wallet->GetTxDepthInMainChain(it->second);
479477
if (depth >= 0) {
480478
result.back() = MakeWalletTxOut(*m_wallet, it->second, output.n, depth);
481479
}

0 commit comments

Comments
 (0)