From 764016eb2259676d834cf6829f5b0e04b135d407 Mon Sep 17 00:00:00 2001 From: Ava Chow Date: Tue, 20 Feb 2024 11:50:09 -0500 Subject: [PATCH] wallet: Retrieve TXO directly in FetchSelectedInputs Instead of searching mapWallet for the preselected inputs, search m_txos. wallet_fundrawtransaction.py spends external inputs and needs the change output to also belong to the test wallet for the oversized tx test. --- src/wallet/spend.cpp | 8 ++------ src/wallet/wallet.cpp | 10 ++++++++++ src/wallet/wallet.h | 1 + 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp index 9d6cf41a428..fe3f4b57b95 100644 --- a/src/wallet/spend.cpp +++ b/src/wallet/spend.cpp @@ -277,12 +277,8 @@ util::Result FetchSelectedInputs(const CWallet& wallet, const input_bytes = GetVirtualTransactionSize(input_bytes, 0, 0); } CTxOut txout; - if (auto ptr_wtx = wallet.GetWalletTx(outpoint.hash)) { - // Clearly invalid input, fail - if (ptr_wtx->tx->vout.size() <= outpoint.n) { - return util::Error{strprintf(_("Invalid pre-selected input %s"), outpoint.ToString())}; - } - txout = ptr_wtx->tx->vout.at(outpoint.n); + if (auto txo = wallet.GetTXO(outpoint)) { + txout = txo->GetTxOut(); if (input_bytes == -1) { input_bytes = CalculateMaximumSignedInputSize(txout, &wallet, &coin_control); } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index ecbddf341f4..7d2e9eec766 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4459,4 +4459,14 @@ void CWallet::RefreshAllTXOs() RefreshTXOsFromTx(wtx); } } + +std::optional CWallet::GetTXO(const COutPoint& outpoint) const +{ + AssertLockHeld(cs_wallet); + const auto& it = m_txos.find(outpoint); + if (it == m_txos.end()) { + return std::nullopt; + } + return it->second; +} } // namespace wallet diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 3d6f0508869..2db522c4f4d 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -524,6 +524,7 @@ public: std::set GetTxConflicts(const CWalletTx& wtx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); const std::unordered_map& GetTXOs() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(cs_wallet); return m_txos; }; + std::optional GetTXO(const COutPoint& outpoint) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); /** Cache outputs that belong to the wallet from a single transaction */ void RefreshTXOsFromTx(const CWalletTx& wtx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);