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.
This commit is contained in:
Ava Chow 2024-07-22 17:14:15 -04:00
parent f4da139c60
commit bb4da6631e
4 changed files with 20 additions and 23 deletions

View file

@ -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;
@ -124,7 +124,7 @@ PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx)
PSBTInput& input = psbtx.inputs[i];
Coin newcoin;
if (!SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, nullptr, 1) || !psbtx.GetInputUTXO(newcoin.out, i)) {
if (!SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, nullptr, 1) || !input.GetUTXO(newcoin.out)) {
success = false;
break;
} else {

View file

@ -67,20 +67,18 @@ bool PartiallySignedTransaction::AddOutput(const CTxOut& txout, const PSBTOutput
return true;
}
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;
}
@ -360,9 +358,9 @@ PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction&
{
const CMutableTransaction& tx = *psbt.tx;
bool have_all_spent_outputs = true;
std::vector<CTxOut> 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<CTxOut> 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) {

View file

@ -301,6 +301,13 @@ struct PSBTInput
void FillSignatureData(SignatureData& sigdata) const;
void FromSignatureData(const SignatureData& sigdata);
void Merge(const PSBTInput& input);
/**
* 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;
PSBTInput(uint32_t psbt_version) : m_psbt_version(psbt_version) {}
template <typename Stream>
@ -1322,14 +1329,6 @@ struct PartiallySignedTransaction
void CacheUnsignedTxPieces();
PartiallySignedTransaction() = default;
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 <typename Stream>
inline void Serialize(Stream& s) const {

View file

@ -59,7 +59,7 @@ FUZZ_TARGET(psbt)
for (size_t i = 0; i < psbt.tx->vin.size(); ++i) {
CTxOut tx_out;
if (psbt.GetInputUTXO(tx_out, i)) {
if (psbt.inputs.at(i).GetUTXO(tx_out)) {
(void)tx_out.IsNull();
(void)tx_out.ToString();
}