Merge bitcoin/bitcoin#36696: CTransaction: Add constructor taking inputs and outputs
What changed, and why it matters
This is a routine code cleanup in Bitcoin Core. It adds a new, more convenient way to create transaction objects directly, instead of always going through a mutable intermediate. It also lets transaction inputs carry their witness data in the constructor. The changes are purely structural and do not alter how transactions are validated, serialized, or accepted by the network.
No security action required. This is a benign refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit introduces a value constructor for CTransaction taking vectors of CTxIn and CTxOut plus version/locktime, and refactors the existing CMutableTransaction conversion constructors to delegate to it. CTxIn constructors gain an optional CScriptWitness parameter. A unit test is updated to use the new constructor. The transaction serialization methods and consensus-critical hashing logic are unchanged.
Changed components
src/primitives/transaction.cppsrc/primitives/transaction.hsrc/test/sigopcount_tests.cppInspect captured patch +72 / −64
### src/primitives/transaction.cpp
@@ -23,18 +23,17 @@ std::string COutPoint::ToString() const
return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
}
-CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
+CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn, CScriptWitness scriptWitnessIn)
+ : prevout{std::move(prevoutIn)},
+ scriptSig{std::move(scriptSigIn)},
+ nSequence{nSequenceIn},
+ scriptWitness{std::move(scriptWitnessIn)}
{
- prevout = prevoutIn;
- scriptSig = scriptSigIn;
- nSequence = nSequenceIn;
}
-CTxIn::CTxIn(Txid hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
+CTxIn::CTxIn(Txid hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn, CScriptWitness scriptWitnessIn)
+ : CTxIn{COutPoint{hashPrevTx, nOut}, std::move(scriptSigIn), nSequenceIn, std::move(scriptWitnessIn)}
{
- prevout = COutPoint(hashPrevTx, nOut);
- scriptSig = scriptSigIn;
- nSequence = nSequenceIn;
}
std::string CTxIn::ToString() const
@@ -92,8 +91,26 @@ Wtxid CTransaction::ComputeWitnessHash() const
return Wtxid::FromUint256((HashWriter{} << TX_WITH_WITNESS(*this)).GetHash());
}
-CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), version{tx.version}, nLockTime{tx.nLockTime}, m_has_witness{ComputeHasWitness()}, hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
-CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), version{tx.version}, nLockTime{tx.nLockTime}, m_has_witness{ComputeHasWitness()}, hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
+CTransaction::CTransaction(std::vector<CTxIn> inputs, std::vector<CTxOut> outputs, uint32_t version, uint32_t locktime)
+ : vin(std::move(inputs)),
+ vout(std::move(outputs)),
+ version{version},
+ nLockTime{locktime},
+ m_has_witness{ComputeHasWitness()},
+ hash{ComputeHash()},
+ m_witness_hash{ComputeWitnessHash()}
+{
+}
+
+CTransaction::CTransaction(const CMutableTransaction& tx)
+ : CTransaction{tx.vin, tx.vout, tx.version, tx.nLockTime}
+{
+}
+
+CTransaction::CTransaction(CMutableTransaction&& tx)
+ : CTransaction{std::move(tx.vin), std::move(tx.vout), tx.version, tx.nLockTime}
+{
+}
CAmount CTransaction::GetValueOut() const
{
### src/primitives/transaction.h
@@ -119,8 +119,8 @@ class CTxIn
nSequence = SEQUENCE_FINAL;
}
- explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
- CTxIn(Txid hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
+ explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn = {}, uint32_t nSequenceIn = SEQUENCE_FINAL, CScriptWitness scriptWitnessIn = {});
+ CTxIn(Txid hashPrevTx, uint32_t nOut, CScript scriptSigIn = {}, uint32_t nSequenceIn = SEQUENCE_FINAL, CScriptWitness scriptWitnessIn = {});
SERIALIZE_METHODS(CTxIn, obj) { READWRITE(obj.prevout, obj.scriptSig, obj.nSequence); }
@@ -311,6 +311,8 @@ class CTransaction
bool ComputeHasWitness() const;
public:
+ CTransaction(std::vector<CTxIn> inputs, std::vector<CTxOut> outputs, uint32_t version = CURRENT_VERSION, uint32_t locktime = 0);
+
/** Convert a CMutableTransaction into a CTransaction. */
explicit CTransaction(const CMutableTransaction& tx);
explicit CTransaction(CMutableTransaction&& tx);
### src/test/sigopcount_tests.cpp
@@ -69,11 +69,10 @@ BOOST_AUTO_TEST_CASE(GetSigOpCount)
* Verifies script execution of the zeroth scriptPubKey of tx output and
* zeroth scriptSig and witness of tx input.
*/
-static ScriptError VerifyWithFlag(const CTransaction& output, const CMutableTransaction& input, script_verify_flags flags)
+static ScriptError VerifyWithFlag(const CTransaction& output, const CTransaction& input, script_verify_flags flags)
{
ScriptError error;
- CTransaction inputi(input);
- bool ret = VerifyScript(inputi.vin[0].scriptSig, output.vout[0].scriptPubKey, &inputi.vin[0].scriptWitness, flags, TransactionSignatureChecker(&inputi, 0, output.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &error);
+ bool ret = VerifyScript(input.vin[0].scriptSig, output.vout[0].scriptPubKey, &input.vin[0].scriptWitness, flags, TransactionSignatureChecker(&input, 0, output.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &error);
BOOST_CHECK((ret == true) == (error == SCRIPT_ERR_OK));
return error;
@@ -84,36 +83,25 @@ static ScriptError VerifyWithFlag(const CTransaction& output, const CMutableTran
* and witness such that spendingTx spends output zero of creationTx.
* Also inserts creationTx's output into the coins view.
*/
-static void BuildTxs(CMutableTransaction& spendingTx, CCoinsViewCache& coins, CMutableTransaction& creationTx, const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& witness)
+static std::pair<CTransaction, CTransaction> BuildTxs(CCoinsViewCache& coins, const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& witness)
{
- creationTx.version = 1;
- creationTx.vin = {
- CTxIn{COutPoint{}},
+ CTransaction creationTx{
+ /*inputs=*/{CTxIn{COutPoint{}}},
+ /*outputs=*/{CTxOut{1, scriptPubKey}},
+ /*version=*/1,
};
- creationTx.vout = {
- CTxOut{1, scriptPubKey},
+ CTransaction spendingTx{
+ /*inputs=*/{CTxIn{COutPoint{creationTx.GetHash(), 0}, scriptSig, CTxIn::SEQUENCE_FINAL, witness}},
+ /*outputs=*/{CTxOut{1, CScript{}}},
+ /*version=*/1,
};
- spendingTx.version = 1;
- spendingTx.vin = {
- CTxIn{COutPoint{creationTx.GetHash(), 0}, scriptSig},
- };
- spendingTx.vin[0].scriptWitness = witness;
- spendingTx.vout = {
- CTxOut{1, CScript{}},
- };
-
- AddCoins(coins, CTransaction(creationTx), 0);
+ AddCoins(coins, creationTx, /*nHeight=*/0);
+ return {std::move(creationTx), std::move(spendingTx)};
}
BOOST_AUTO_TEST_CASE(GetTxSigOpCost)
{
- // Transaction creates outputs
- CMutableTransaction creationTx;
- // Transaction that spends outputs and whose
- // sig op cost is going to be tested
- CMutableTransaction spendingTx;
-
// Create utxo set
CCoinsViewCache coins{&CoinsViewEmpty::Get()};
// Create key
@@ -128,16 +116,16 @@ BOOST_AUTO_TEST_CASE(GetTxSigOpCost)
// Do not use a valid signature to avoid using wallet operations.
CScript scriptSig = CScript() << OP_0 << OP_0;
- BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, CScriptWitness());
+ const auto [creationTx, spendingTx] = BuildTxs(coins, scriptPubKey, scriptSig, CScriptWitness());
// Legacy counting only includes signature operations in scriptSigs and scriptPubKeys
// of a transaction and does not take the actual executed sig operations into account.
// spendingTx in itself does not contain a signature operation.
- assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
+ assert(GetTransactionSigOpCost(spendingTx, coins, flags) == 0);
// creationTx contains two signature operations in its scriptPubKey, but legacy counting
// is not accurate.
- assert(GetTransactionSigOpCost(CTransaction(creationTx), coins, flags) == MAX_PUBKEYS_PER_MULTISIG * WITNESS_SCALE_FACTOR);
+ assert(GetTransactionSigOpCost(creationTx, coins, flags) == MAX_PUBKEYS_PER_MULTISIG * WITNESS_SCALE_FACTOR);
// Sanity check: script verification fails because of an invalid signature.
- assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
+ assert(VerifyWithFlag(creationTx, spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
}
// Multisig nested in P2SH
@@ -146,12 +134,12 @@ BOOST_AUTO_TEST_CASE(GetTxSigOpCost)
CScript scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript));
CScript scriptSig = CScript() << OP_0 << OP_0 << ToByteVector(redeemScript);
- BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, CScriptWitness());
- assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 2 * WITNESS_SCALE_FACTOR);
- assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
+ const auto [creationTx, spendingTx] = BuildTxs(coins, scriptPubKey, scriptSig, CScriptWitness());
+ assert(GetTransactionSigOpCost(spendingTx, coins, flags) == 2 * WITNESS_SCALE_FACTOR);
+ assert(VerifyWithFlag(creationTx, spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
// P2SH sigops are not counted if we don't set the SCRIPT_VERIFY_P2SH flag
- assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, /*flags=*/0) == 0);
+ assert(GetTransactionSigOpCost(spendingTx, coins, /*flags=*/0) == 0);
}
// P2WPKH witness program
@@ -161,23 +149,24 @@ BOOST_AUTO_TEST_CASE(GetTxSigOpCost)
CScriptWitness scriptWitness{{{}, {}}};
- BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
- assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 1);
+ const auto [creationTx, spendingTx] = BuildTxs(coins, scriptPubKey, scriptSig, scriptWitness);
+ assert(GetTransactionSigOpCost(spendingTx, coins, flags) == 1);
// No signature operations if we don't verify the witness.
- assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags & ~SCRIPT_VERIFY_WITNESS) == 0);
- assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_EQUALVERIFY);
+ assert(GetTransactionSigOpCost(spendingTx, coins, flags & ~SCRIPT_VERIFY_WITNESS) == 0);
+ assert(VerifyWithFlag(creationTx, spendingTx, flags) == SCRIPT_ERR_EQUALVERIFY);
// The sig op cost for witness version != 0 is zero.
assert(scriptPubKey[0] == 0x00);
scriptPubKey[0] = 0x51;
- BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
- assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
+ const auto [creationTx2, spendingTx2] = BuildTxs(coins, scriptPubKey, scriptSig, scriptWitness);
+ assert(GetTransactionSigOpCost(spendingTx2, coins, flags) == 0);
scriptPubKey[0] = 0x00;
- BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
+ const auto [creationTx3, spendingTx3] = BuildTxs(coins, scriptPubKey, scriptSig, scriptWitness);
// The witness of a coinbase transaction is not taken into account.
- spendingTx.vin[0].prevout.SetNull();
- assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
+ CMutableTransaction spendingTx4{spendingTx3};
+ spendingTx4.vin[0].prevout.SetNull();
+ assert(GetTransactionSigOpCost(CTransaction{spendingTx4}, coins, flags) == 0);
}
// P2WPKH nested in P2SH
@@ -187,9 +176,9 @@ BOOST_AUTO_TEST_CASE(GetTxSigOpCost)
scriptSig = CScript() << ToByteVector(scriptSig);
CScriptWitness scriptWitness{{{}, {}}};
- BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
- assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 1);
- assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_EQUALVERIFY);
+ const auto [creationTx, spendingTx] = BuildTxs(coins, scriptPubKey, scriptSig, scriptWitness);
+ assert(GetTransactionSigOpCost(spendingTx, coins, flags) == 1);
+ assert(VerifyWithFlag(creationTx, spendingTx, flags) == SCRIPT_ERR_EQUALVERIFY);
}
// P2WSH witness program
@@ -199,10 +188,10 @@ BOOST_AUTO_TEST_CASE(GetTxSigOpCost)
CScript scriptSig = CScript();
CScriptWitness scriptWitness{{{}, {}, ToByteVector(witnessScript)}};
- BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
- assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 2);
- assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags & ~SCRIPT_VERIFY_WITNESS) == 0);
- assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
+ const auto [creationTx, spendingTx] = BuildTxs(coins, scriptPubKey, scriptSig, scriptWitness);
+ assert(GetTransactionSigOpCost(spendingTx, coins, flags) == 2);
+ assert(GetTransactionSigOpCost(spendingTx, coins, flags & ~SCRIPT_VERIFY_WITNESS) == 0);
+ assert(VerifyWithFlag(creationTx, spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
}
// P2WSH nested in P2SH
@@ -213,9 +202,9 @@ BOOST_AUTO_TEST_CASE(GetTxSigOpCost)
CScript scriptSig = CScript() << ToByteVector(redeemScript);
CScriptWitness scriptWitness{{{}, {}, ToByteVector(witnessScript)}};
- BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
- assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 2);
- assert(VerifyWithFlag(CTransaction(creationTx), spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
+ const auto [creationTx, spendingTx] = BuildTxs(coins, scriptPubKey, scriptSig, scriptWitness);
+ assert(GetTransactionSigOpCost(spendingTx, coins, flags) == 2);
+ assert(VerifyWithFlag(creationTx, spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
}
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.