From 092de4f1f6c2fa32dfab206839abb10a31698a81 Mon Sep 17 00:00:00 2001 From: Ava Chow Date: Mon, 22 Jul 2024 17:14:15 -0400 Subject: [PATCH] Replace PSBT::GetInputUTXO with PSBTInput::GetUTXO Now that PSBTInput's track their own prevouts, there's no need for a PSBT global function to fetch input specific data. --- src/node/psbt.cpp | 4 ++-- src/psbt.cpp | 22 ++++++++++------------ src/psbt.h | 15 +++++++-------- src/test/fuzz/psbt.cpp | 4 ++-- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/node/psbt.cpp b/src/node/psbt.cpp index cbcbfa0ef5d..3827290902d 100644 --- a/src/node/psbt.cpp +++ b/src/node/psbt.cpp @@ -35,7 +35,7 @@ PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx) // Check for a UTXO CTxOut utxo; - if (psbtx.GetInputUTXO(utxo, i)) { + if (input.GetUTXO(utxo)) { if (!MoneyRange(utxo.nValue) || !MoneyRange(in_amt + utxo.nValue)) { result.SetInvalid(strprintf("PSBT is not valid. Input %u has invalid value", i)); return result; @@ -123,7 +123,7 @@ PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx) PSBTInput& input = psbtx.inputs[i]; Coin newcoin; - if (SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, nullptr, /*options=*/{}) != PSBTError::OK || !psbtx.GetInputUTXO(newcoin.out, i)) { + if (SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, nullptr, /*options=*/{}) != PSBTError::OK || !input.GetUTXO(newcoin.out)) { success = false; break; } else { diff --git a/src/psbt.cpp b/src/psbt.cpp index cff76611cb8..2c67af233eb 100644 --- a/src/psbt.cpp +++ b/src/psbt.cpp @@ -100,20 +100,18 @@ bool PartiallySignedTransaction::AddOutput(const PSBTOutput& psbtout) return false; } -bool PartiallySignedTransaction::GetInputUTXO(CTxOut& utxo, int input_index) const +bool PSBTInput::GetUTXO(CTxOut& utxo) const { - const PSBTInput& input = inputs[input_index]; - uint32_t prevout_index = tx->vin[input_index].prevout.n; - if (input.non_witness_utxo) { - if (prevout_index >= input.non_witness_utxo->vout.size()) { + if (non_witness_utxo) { + if (prev_out >= non_witness_utxo->vout.size()) { return false; } - if (input.non_witness_utxo->GetHash() != tx->vin[input_index].prevout.hash) { + if (non_witness_utxo->GetHash() != prev_txid) { return false; } - utxo = input.non_witness_utxo->vout[prevout_index]; - } else if (!input.witness_utxo.IsNull()) { - utxo = input.witness_utxo; + utxo = non_witness_utxo->vout[prev_out]; + } else if (!witness_utxo.IsNull()) { + utxo = witness_utxo; } else { return false; } @@ -435,9 +433,9 @@ PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction& { const CMutableTransaction& tx = *psbt.tx; bool have_all_spent_outputs = true; - std::vector utxos(tx.vin.size()); - for (size_t idx = 0; idx < tx.vin.size(); ++idx) { - if (!psbt.GetInputUTXO(utxos[idx], idx)) have_all_spent_outputs = false; + std::vector utxos; + for (const PSBTInput& input : psbt.inputs) { + if (!input.GetUTXO(utxos.emplace_back())) have_all_spent_outputs = false; } PrecomputedTransactionData txdata; if (have_all_spent_outputs) { diff --git a/src/psbt.h b/src/psbt.h index 70bdae96f41..6e7e0cb925a 100644 --- a/src/psbt.h +++ b/src/psbt.h @@ -310,6 +310,13 @@ public: void Merge(const PSBTInput& input); uint32_t GetVersion() const { return m_psbt_version; } COutPoint GetOutPoint() const; + /** + * Retrieves the UTXO for this input + * + * @param[out] utxo The UTXO of this input + * @return Whether the UTXO could be retrieved + */ + bool GetUTXO(CTxOut& utxo) const; explicit PSBTInput(uint32_t psbt_version, const Txid& prev_txid, uint32_t prev_out, std::optional sequence = std::nullopt) : m_psbt_version(psbt_version), @@ -1087,14 +1094,6 @@ public: bool AddInput(const PSBTInput& psbtin); bool AddOutput(const PSBTOutput& psbtout); explicit PartiallySignedTransaction(const CMutableTransaction& tx); - /** - * Finds the UTXO for a given input index - * - * @param[out] utxo The UTXO of the input if found - * @param[in] input_index Index of the input to retrieve the UTXO of - * @return Whether the UTXO for the specified input was found - */ - bool GetInputUTXO(CTxOut& utxo, int input_index) const; template inline void Serialize(Stream& s) const { diff --git a/src/test/fuzz/psbt.cpp b/src/test/fuzz/psbt.cpp index a1b1a5e9289..7481d02e5a5 100644 --- a/src/test/fuzz/psbt.cpp +++ b/src/test/fuzz/psbt.cpp @@ -68,9 +68,9 @@ FUZZ_TARGET(psbt) (void)output.IsNull(); } - for (size_t i = 0; i < psbt.inputs.size(); ++i) { + for (const PSBTInput& input : psbt.inputs) { CTxOut tx_out; - if (psbt.GetInputUTXO(tx_out, i)) { + if (input.GetUTXO(tx_out)) { (void)tx_out.IsNull(); (void)tx_out.ToString(); }