From a2ac6f9582c4c996fa36e4801fa0aac756235754 Mon Sep 17 00:00:00 2001 From: furszy Date: Wed, 20 Jul 2022 13:24:05 -0300 Subject: [PATCH] wallet: unify FindNonChangeParentOutput functions The function is only used in ListCoins. --- src/wallet/spend.cpp | 34 +++++++++++++++------------------- src/wallet/spend.h | 1 - 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp index 28f939f0fd3..0e573badc23 100644 --- a/src/wallet/spend.cpp +++ b/src/wallet/spend.cpp @@ -362,28 +362,24 @@ CAmount GetAvailableBalance(const CWallet& wallet, const CCoinControl* coinContr return AvailableCoins(wallet, coinControl).GetTotalAmount(); } -const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const CTransaction& tx, int output) -{ - AssertLockHeld(wallet.cs_wallet); - const CTransaction* ptx = &tx; - int n = output; - while (OutputIsChange(wallet, ptx->vout[n]) && ptx->vin.size() > 0) { - const COutPoint& prevout = ptx->vin[0].prevout; - auto it = wallet.mapWallet.find(prevout.hash); - if (it == wallet.mapWallet.end() || it->second.tx->vout.size() <= prevout.n || - !wallet.IsMine(it->second.tx->vout[prevout.n])) { - break; - } - ptx = it->second.tx.get(); - n = prevout.n; - } - return ptx->vout[n]; -} - const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const COutPoint& outpoint) { AssertLockHeld(wallet.cs_wallet); - return FindNonChangeParentOutput(wallet, *wallet.GetWalletTx(outpoint.hash)->tx, outpoint.n); + const CWalletTx* wtx{Assert(wallet.GetWalletTx(outpoint.hash))}; + + const CTransaction* ptx = wtx->tx.get(); + int n = outpoint.n; + while (OutputIsChange(wallet, ptx->vout[n]) && ptx->vin.size() > 0) { + const COutPoint& prevout = ptx->vin[0].prevout; + const CWalletTx* it = wallet.GetWalletTx(prevout.hash); + if (!it || it->tx->vout.size() <= prevout.n || + !wallet.IsMine(it->tx->vout[prevout.n])) { + break; + } + ptx = it->tx.get(); + n = prevout.n; + } + return ptx->vout[n]; } std::map> ListCoins(const CWallet& wallet) diff --git a/src/wallet/spend.h b/src/wallet/spend.h index 281a6ca9e81..14a057b1ea5 100644 --- a/src/wallet/spend.h +++ b/src/wallet/spend.h @@ -99,7 +99,6 @@ CAmount GetAvailableBalance(const CWallet& wallet, const CCoinControl* coinContr /** * Find non-change parent output. */ -const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const CTransaction& tx, int output) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet); const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const COutPoint& outpoint) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet); /**