Merge bitcoin/bitcoin#36606: test, bench: construct inputs/outputs declaratively in context-dependent cases
What changed, and why it matters
This commit is a code cleanup inside Bitcoin Core's tests and benchmarks. It replaces a manual, field-by-field way of building fake transactions with a more direct constructor style. The author explicitly states that the actual transaction data being built is unchanged, and the diff confirms the values and structure stay the same. There is no security issue here.
No action required; this is a benign test/bench refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors test/bench-only code to construct COutPoint, CTxIn, and CTxOut objects declaratively instead of mutating fields after resize(). It touches src/bench/ccoins_caching.cpp, src/test/sigopcount_tests.cpp, src/test/transaction_tests.cpp, src/test/util/mining.cpp, and src/test/util/transaction_utils.cpp. The PR description and diff show equivalent construction; no consensus, networking, wallet, or production code is modified.
Changed components
src/bench/ccoins_caching.cppsrc/test/sigopcount_tests.cppsrc/test/transaction_tests.cppsrc/test/util/mining.cppsrc/test/util/transaction_utils.cppInspect captured patch +60 / −72
### src/bench/ccoins_caching.cpp
@@ -34,19 +34,15 @@ static void CCoinsCaching(benchmark::Bench& bench)
SetupDummyInputs(keystore, coins, {11 * COIN, 50 * COIN, 21 * COIN, 22 * COIN});
CMutableTransaction t1;
- t1.vin.resize(3);
- t1.vin[0].prevout.hash = dummyTransactions[0].GetHash();
- t1.vin[0].prevout.n = 1;
- t1.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
- t1.vin[1].prevout.hash = dummyTransactions[1].GetHash();
- t1.vin[1].prevout.n = 0;
- t1.vin[1].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
- t1.vin[2].prevout.hash = dummyTransactions[1].GetHash();
- t1.vin[2].prevout.n = 1;
- t1.vin[2].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
- t1.vout.resize(2);
- t1.vout[0].nValue = 90 * COIN;
- t1.vout[0].scriptPubKey << OP_1;
+ t1.vin = {
+ CTxIn{COutPoint{dummyTransactions[0].GetHash(), 1}, CScript{} << std::vector<unsigned char>(65, 0)},
+ CTxIn{COutPoint{dummyTransactions[1].GetHash(), 0}, CScript{} << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4)},
+ CTxIn{COutPoint{dummyTransactions[1].GetHash(), 1}, CScript{} << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4)},
+ };
+ t1.vout = {
+ CTxOut{90 * COIN, CScript{} << OP_1},
+ CTxOut{},
+ };
// Benchmark.
const CTransaction tx_1(t1);
### src/test/sigopcount_tests.cpp
@@ -87,22 +87,21 @@ static ScriptError VerifyWithFlag(const CTransaction& output, const CMutableTran
static void BuildTxs(CMutableTransaction& spendingTx, CCoinsViewCache& coins, CMutableTransaction& creationTx, const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& witness)
{
creationTx.version = 1;
- creationTx.vin.resize(1);
- creationTx.vin[0].prevout.SetNull();
- creationTx.vin[0].scriptSig = CScript();
- creationTx.vout.resize(1);
- creationTx.vout[0].nValue = 1;
- creationTx.vout[0].scriptPubKey = scriptPubKey;
+ creationTx.vin = {
+ CTxIn{COutPoint{}},
+ };
+ creationTx.vout = {
+ CTxOut{1, scriptPubKey},
+ };
spendingTx.version = 1;
- spendingTx.vin.resize(1);
- spendingTx.vin[0].prevout.hash = creationTx.GetHash();
- spendingTx.vin[0].prevout.n = 0;
- spendingTx.vin[0].scriptSig = scriptSig;
+ spendingTx.vin = {
+ CTxIn{COutPoint{creationTx.GetHash(), 0}, scriptSig},
+ };
spendingTx.vin[0].scriptWitness = witness;
- spendingTx.vout.resize(1);
- spendingTx.vout[0].nValue = 1;
- spendingTx.vout[0].scriptPubKey = CScript();
+ spendingTx.vout = {
+ CTxOut{1, CScript{}},
+ };
AddCoins(coins, CTransaction(creationTx), 0);
}
### src/test/transaction_tests.cpp
@@ -399,19 +399,15 @@ BOOST_AUTO_TEST_CASE(test_Get)
SetupDummyInputs(keystore, coins, {11*CENT, 50*CENT, 21*CENT, 22*CENT});
CMutableTransaction t1;
- t1.vin.resize(3);
- t1.vin[0].prevout.hash = dummyTransactions[0].GetHash();
- t1.vin[0].prevout.n = 1;
- t1.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
- t1.vin[1].prevout.hash = dummyTransactions[1].GetHash();
- t1.vin[1].prevout.n = 0;
- t1.vin[1].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
- t1.vin[2].prevout.hash = dummyTransactions[1].GetHash();
- t1.vin[2].prevout.n = 1;
- t1.vin[2].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
- t1.vout.resize(2);
- t1.vout[0].nValue = 90*CENT;
- t1.vout[0].scriptPubKey << OP_1;
+ t1.vin = {
+ CTxIn{COutPoint{dummyTransactions[0].GetHash(), 1}, CScript{} << std::vector<unsigned char>(65, 0)},
+ CTxIn{COutPoint{dummyTransactions[1].GetHash(), 0}, CScript{} << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4)},
+ CTxIn{COutPoint{dummyTransactions[1].GetHash(), 1}, CScript{} << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4)},
+ };
+ t1.vout = {
+ CTxOut{90 * CENT, CScript{} << OP_1},
+ CTxOut{},
+ };
BOOST_CHECK(ValidateInputsStandardness(CTransaction(t1), coins).IsValid());
}
### src/test/util/mining.cpp
@@ -52,14 +52,16 @@ std::vector<std::shared_ptr<CBlock>> CreateBlockChain(size_t total_height, const
CMutableTransaction coinbase_tx;
coinbase_tx.nLockTime = static_cast<uint32_t>(height);
- coinbase_tx.vin.resize(1);
- coinbase_tx.vin[0].prevout.SetNull();
- coinbase_tx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL; // Make sure timelock is enforced.
- coinbase_tx.vout.resize(1);
- coinbase_tx.vout[0].scriptPubKey = P2WSH_OP_TRUE;
- coinbase_tx.vout[0].nValue = GetBlockSubsidy(height + 1, params.GetConsensus());
- // Always include OP_0 as a dummy extraNonce.
- coinbase_tx.vin[0].scriptSig = CScript() << (height + 1) << OP_0;
+ coinbase_tx.vin = {
+ CTxIn{
+ COutPoint{},
+ CScript{} << (height + 1) << OP_0, // Always include OP_0 as a dummy extraNonce.
+ CTxIn::MAX_SEQUENCE_NONFINAL, // Make sure timelock is enforced.
+ },
+ };
+ coinbase_tx.vout = {
+ CTxOut{GetBlockSubsidy(height + 1, params.GetConsensus()), P2WSH_OP_TRUE},
+ };
block.vtx = {MakeTransactionRef(std::move(coinbase_tx))};
block.nVersion = VERSIONBITS_LAST_OLD_BLOCK_VERSION;
### src/test/util/transaction_utils.cpp
@@ -12,13 +12,12 @@ CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey, CAmou
CMutableTransaction txCredit;
txCredit.version = 1;
txCredit.nLockTime = 0;
- txCredit.vin.resize(1);
- txCredit.vout.resize(1);
- txCredit.vin[0].prevout.SetNull();
- txCredit.vin[0].scriptSig = CScript() << CScriptNum(0) << CScriptNum(0);
- txCredit.vin[0].nSequence = CTxIn::SEQUENCE_FINAL;
- txCredit.vout[0].scriptPubKey = scriptPubKey;
- txCredit.vout[0].nValue = nValue;
+ txCredit.vin = {
+ CTxIn{COutPoint{}, CScript{} << CScriptNum(0) << CScriptNum(0), CTxIn::SEQUENCE_FINAL},
+ };
+ txCredit.vout = {
+ CTxOut{nValue, scriptPubKey},
+ };
return txCredit;
}
@@ -28,15 +27,13 @@ CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CSc
CMutableTransaction txSpend;
txSpend.version = 1;
txSpend.nLockTime = 0;
- txSpend.vin.resize(1);
- txSpend.vout.resize(1);
+ txSpend.vin = {
+ CTxIn{COutPoint{txCredit.GetHash(), 0}, scriptSig, CTxIn::SEQUENCE_FINAL},
+ };
txSpend.vin[0].scriptWitness = scriptWitness;
- txSpend.vin[0].prevout.hash = txCredit.GetHash();
- txSpend.vin[0].prevout.n = 0;
- txSpend.vin[0].scriptSig = scriptSig;
- txSpend.vin[0].nSequence = CTxIn::SEQUENCE_FINAL;
- txSpend.vout[0].scriptPubKey = CScript();
- txSpend.vout[0].nValue = txCredit.vout[0].nValue;
+ txSpend.vout = {
+ CTxOut{txCredit.vout[0].nValue, CScript{}},
+ };
return txSpend;
}
@@ -54,18 +51,16 @@ std::vector<CMutableTransaction> SetupDummyInputs(FillableSigningProvider& keyst
}
// Create some dummy input transactions
- dummyTransactions[0].vout.resize(2);
- dummyTransactions[0].vout[0].nValue = nValues[0];
- dummyTransactions[0].vout[0].scriptPubKey << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG;
- dummyTransactions[0].vout[1].nValue = nValues[1];
- dummyTransactions[0].vout[1].scriptPubKey << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG;
+ dummyTransactions[0].vout = {
+ CTxOut{nValues[0], CScript{} << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG},
+ CTxOut{nValues[1], CScript{} << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG},
+ };
AddCoins(coinsRet, CTransaction(dummyTransactions[0]), 0);
- dummyTransactions[1].vout.resize(2);
- dummyTransactions[1].vout[0].nValue = nValues[2];
- dummyTransactions[1].vout[0].scriptPubKey = GetScriptForDestination(PKHash(key[2].GetPubKey()));
- dummyTransactions[1].vout[1].nValue = nValues[3];
- dummyTransactions[1].vout[1].scriptPubKey = GetScriptForDestination(PKHash(key[3].GetPubKey()));
+ dummyTransactions[1].vout = {
+ CTxOut{nValues[2], GetScriptForDestination(PKHash(key[2].GetPubKey()))},
+ CTxOut{nValues[3], GetScriptForDestination(PKHash(key[3].GetPubKey()))},
+ };
AddCoins(coinsRet, CTransaction(dummyTransactions[1]), 0);
return dummyTransactions;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.