Skip to content

Commit 14d04d5

Browse files
committed
wallet: Replace CWalletTx in COutput with COutPoint and CTxOut
Instead of having a pointer to the CWalletTx in COutput, we can just store the COutPoint and the CTxOut as those are the only things we need from the CWalletTx. Other things CWalletTx used to provide were time and fIsFromMe but these are also being stored by COutput.
1 parent 0ba4d19 commit 14d04d5

File tree

6 files changed

+30
-22
lines changed

6 files changed

+30
-22
lines changed

src/wallet/interfaces.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ WalletTxOut MakeWalletTxOut(const CWallet& wallet,
115115
const COutput& output) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
116116
{
117117
WalletTxOut result;
118-
result.txout = output.tx->tx->vout[output.i];
118+
result.txout = output.txout;
119119
result.time = output.time;
120120
result.depth_in_main_chain = output.depth;
121-
result.is_spent = wallet.IsSpent(output.tx->GetHash(), output.i);
121+
result.is_spent = wallet.IsSpent(output.outpoint.hash, output.outpoint.n);
122122
return result;
123123
}
124124

@@ -430,7 +430,7 @@ class WalletImpl : public Wallet
430430
for (const auto& entry : ListCoins(*m_wallet)) {
431431
auto& group = result[entry.first];
432432
for (const auto& coin : entry.second) {
433-
group.emplace_back(COutPoint(coin.tx->GetHash(), coin.i),
433+
group.emplace_back(coin.outpoint,
434434
MakeWalletTxOut(*m_wallet, coin));
435435
}
436436
}

src/wallet/rpc/coins.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -648,16 +648,16 @@ RPCHelpMan listunspent()
648648

649649
for (const COutput& out : vecOutputs) {
650650
CTxDestination address;
651-
const CScript& scriptPubKey = out.tx->tx->vout[out.i].scriptPubKey;
651+
const CScript& scriptPubKey = out.txout.scriptPubKey;
652652
bool fValidAddress = ExtractDestination(scriptPubKey, address);
653-
bool reused = avoid_reuse && pwallet->IsSpentKey(out.tx->GetHash(), out.i);
653+
bool reused = avoid_reuse && pwallet->IsSpentKey(out.outpoint.hash, out.outpoint.n);
654654

655655
if (destinations.size() && (!fValidAddress || !destinations.count(address)))
656656
continue;
657657

658658
UniValue entry(UniValue::VOBJ);
659-
entry.pushKV("txid", out.tx->GetHash().GetHex());
660-
entry.pushKV("vout", out.i);
659+
entry.pushKV("txid", out.outpoint.hash.GetHex());
660+
entry.pushKV("vout", (int)out.outpoint.n);
661661

662662
if (fValidAddress) {
663663
entry.pushKV("address", EncodeDestination(address));
@@ -702,12 +702,12 @@ RPCHelpMan listunspent()
702702
}
703703

704704
entry.pushKV("scriptPubKey", HexStr(scriptPubKey));
705-
entry.pushKV("amount", ValueFromAmount(out.tx->tx->vout[out.i].nValue));
705+
entry.pushKV("amount", ValueFromAmount(out.txout.nValue));
706706
entry.pushKV("confirmations", out.depth);
707707
if (!out.depth) {
708708
size_t ancestor_count, descendant_count, ancestor_size;
709709
CAmount ancestor_fees;
710-
pwallet->chain().getTransactionAncestry(out.tx->GetHash(), ancestor_count, descendant_count, &ancestor_size, &ancestor_fees);
710+
pwallet->chain().getTransactionAncestry(out.outpoint.hash, ancestor_count, descendant_count, &ancestor_size, &ancestor_fees);
711711
if (ancestor_count) {
712712
entry.pushKV("ancestorcount", uint64_t(ancestor_count));
713713
entry.pushKV("ancestorsize", uint64_t(ancestor_size));

src/wallet/spend.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ int GetTxSpendSize(const CWallet& wallet, const CWalletTx& wtx, unsigned int out
3131

3232
std::string COutput::ToString() const
3333
{
34-
return strprintf("COutput(%s, %d, %d) [%s]", tx->GetHash().ToString(), i, depth, FormatMoney(tx->tx->vout[i].nValue));
34+
return strprintf("COutput(%s, %d, %d) [%s]", outpoint.hash.ToString(), outpoint.n, depth, FormatMoney(txout.nValue));
3535
}
3636

3737
int CalculateMaximumSignedInputSize(const CTxOut& txout, const SigningProvider* provider, bool use_max_sig)
@@ -221,7 +221,7 @@ CAmount GetAvailableBalance(const CWallet& wallet, const CCoinControl* coinContr
221221
AvailableCoins(wallet, vCoins, coinControl);
222222
for (const COutput& out : vCoins) {
223223
if (out.spendable) {
224-
balance += out.tx->tx->vout[out.i].nValue;
224+
balance += out.txout.nValue;
225225
}
226226
}
227227
return balance;
@@ -245,6 +245,12 @@ const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const CTransactio
245245
return ptx->vout[n];
246246
}
247247

248+
const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const COutPoint& outpoint)
249+
{
250+
AssertLockHeld(wallet.cs_wallet);
251+
return FindNonChangeParentOutput(wallet, *wallet.GetWalletTx(outpoint.hash)->tx, outpoint.n);
252+
}
253+
248254
std::map<CTxDestination, std::vector<COutput>> ListCoins(const CWallet& wallet)
249255
{
250256
AssertLockHeld(wallet.cs_wallet);
@@ -257,7 +263,7 @@ std::map<CTxDestination, std::vector<COutput>> ListCoins(const CWallet& wallet)
257263
for (const COutput& coin : availableCoins) {
258264
CTxDestination address;
259265
if ((coin.spendable || (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && coin.solvable)) &&
260-
ExtractDestination(FindNonChangeParentOutput(wallet, *coin.tx->tx, coin.i).scriptPubKey, address)) {
266+
ExtractDestination(FindNonChangeParentOutput(wallet, coin.outpoint).scriptPubKey, address)) {
261267
result[address].emplace_back(std::move(coin));
262268
}
263269
}
@@ -298,7 +304,7 @@ std::vector<OutputGroup> GroupOutputs(const CWallet& wallet, const std::vector<C
298304
if (!output.spendable) continue;
299305

300306
size_t ancestors, descendants;
301-
wallet.chain().getTransactionAncestry(output.tx->GetHash(), ancestors, descendants);
307+
wallet.chain().getTransactionAncestry(output.outpoint.hash, ancestors, descendants);
302308
CInputCoin input_coin = output.GetInputCoin();
303309

304310
// Make an OutputGroup containing just this output
@@ -324,7 +330,7 @@ std::vector<OutputGroup> GroupOutputs(const CWallet& wallet, const std::vector<C
324330
if (!output.spendable) continue;
325331

326332
size_t ancestors, descendants;
327-
wallet.chain().getTransactionAncestry(output.tx->GetHash(), ancestors, descendants);
333+
wallet.chain().getTransactionAncestry(output.outpoint.hash, ancestors, descendants);
328334
CInputCoin input_coin = output.GetInputCoin();
329335
CScript spk = input_coin.txout.scriptPubKey;
330336

src/wallet/spend.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ int GetTxSpendSize(const CWallet& wallet, const CWalletTx& wtx, unsigned int out
1919
class COutput
2020
{
2121
public:
22-
const CWalletTx *tx;
22+
/** The outpoint identifying this UTXO */
23+
COutPoint outpoint;
2324

24-
/** Index in tx->vout. */
25-
int i;
25+
/** The output itself */
26+
CTxOut txout;
2627

2728
/**
2829
* Depth in block chain.
@@ -54,8 +55,8 @@ class COutput
5455
bool from_me;
5556

5657
COutput(const CWallet& wallet, const CWalletTx& wtx, int iIn, int depth, int input_bytes, bool spendable, bool solvable, bool safe, int64_t time, bool from_me)
57-
: tx(&wtx),
58-
i(iIn),
58+
: outpoint(COutPoint(wtx.GetHash(), iIn)),
59+
txout(wtx.tx->vout.at(iIn)),
5960
depth(depth),
6061
input_bytes(input_bytes),
6162
spendable(spendable),
@@ -69,7 +70,7 @@ class COutput
6970

7071
inline CInputCoin GetInputCoin() const
7172
{
72-
return CInputCoin(tx->tx, i, input_bytes);
73+
return CInputCoin(outpoint, txout, input_bytes);
7374
}
7475
};
7576

@@ -100,6 +101,7 @@ CAmount GetAvailableBalance(const CWallet& wallet, const CCoinControl* coinContr
100101
* Find non-change parent output.
101102
*/
102103
const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const CTransaction& tx, int output) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
104+
const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const COutPoint& outpoint) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
103105

104106
/**
105107
* Return list of available coins and locked coins grouped by non-change output address.

src/wallet/test/coinselector_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
331331
add_coin(coins, *wallet, 2 * CENT, 6 * 24, false, 0, true);
332332
CCoinControl coin_control;
333333
coin_control.fAllowOtherInputs = true;
334-
coin_control.Select(COutPoint(coins.at(0).tx->GetHash(), coins.at(0).i));
334+
coin_control.Select(coins.at(0).outpoint);
335335
coin_selection_params_bnb.m_effective_feerate = CFeeRate(0);
336336
const auto result10 = SelectCoins(*wallet, coins, 10 * CENT, coin_control, coin_selection_params_bnb);
337337
BOOST_CHECK(result10);

src/wallet/test/wallet_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ BOOST_FIXTURE_TEST_CASE(ListCoinsTest, ListCoinsTestingSetup)
588588
for (const auto& group : list) {
589589
for (const auto& coin : group.second) {
590590
LOCK(wallet->cs_wallet);
591-
wallet->LockCoin(COutPoint(coin.tx->GetHash(), coin.i));
591+
wallet->LockCoin(coin.outpoint);
592592
}
593593
}
594594
{

0 commit comments

Comments
 (0)