From e78a2a0d00cf67637eae4caf4db1735f110aff14 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Sun, 7 Jun 2026 01:51:27 +0200 Subject: [PATCH] test: refactor: simplify tx vin/vout creation in txvalidationcache_tests.cpp --- src/test/txvalidationcache_tests.cpp | 74 +++++++++------------------- 1 file changed, 22 insertions(+), 52 deletions(-) diff --git a/src/test/txvalidationcache_tests.cpp b/src/test/txvalidationcache_tests.cpp index d695e08f5f5..5ace00880a6 100644 --- a/src/test/txvalidationcache_tests.cpp +++ b/src/test/txvalidationcache_tests.cpp @@ -49,12 +49,8 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, Dersig100Setup) for (int i = 0; i < 2; i++) { spends[i].version = 1; - spends[i].vin.resize(1); - spends[i].vin[0].prevout.hash = m_coinbase_txns[0]->GetHash(); - spends[i].vin[0].prevout.n = 0; - spends[i].vout.resize(1); - spends[i].vout[0].nValue = 11*CENT; - spends[i].vout[0].scriptPubKey = scriptPubKey; + spends[i].vin = {CTxIn{m_coinbase_txns[0]->GetHash(), 0}}; + spends[i].vout = {CTxOut{11*CENT, scriptPubKey}}; // Sign: std::vector vchSig; @@ -184,18 +180,13 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, Dersig100Setup) CMutableTransaction spend_tx; spend_tx.version = 1; - spend_tx.vin.resize(1); - spend_tx.vin[0].prevout.hash = m_coinbase_txns[0]->GetHash(); - spend_tx.vin[0].prevout.n = 0; - spend_tx.vout.resize(4); - spend_tx.vout[0].nValue = 11*CENT; - spend_tx.vout[0].scriptPubKey = p2sh_scriptPubKey; - spend_tx.vout[1].nValue = 11*CENT; - spend_tx.vout[1].scriptPubKey = p2wpkh_scriptPubKey; - spend_tx.vout[2].nValue = 11*CENT; - spend_tx.vout[2].scriptPubKey = CScript() << OP_CHECKLOCKTIMEVERIFY << OP_DROP << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG; - spend_tx.vout[3].nValue = 11*CENT; - spend_tx.vout[3].scriptPubKey = CScript() << OP_CHECKSEQUENCEVERIFY << OP_DROP << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG; + spend_tx.vin = {CTxIn{m_coinbase_txns[0]->GetHash(), 0}}; + spend_tx.vout = { + CTxOut{11*CENT, p2sh_scriptPubKey}, + CTxOut{11*CENT, p2wpkh_scriptPubKey}, + CTxOut{11*CENT, CScript() << OP_CHECKLOCKTIMEVERIFY << OP_DROP << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG}, + CTxOut{11*CENT, CScript() << OP_CHECKSEQUENCEVERIFY << OP_DROP << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG}, + }; // Sign, with a non-DER signature { @@ -246,12 +237,8 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, Dersig100Setup) { CMutableTransaction invalid_under_p2sh_tx; invalid_under_p2sh_tx.version = 1; - invalid_under_p2sh_tx.vin.resize(1); - invalid_under_p2sh_tx.vin[0].prevout.hash = spend_tx.GetHash(); - invalid_under_p2sh_tx.vin[0].prevout.n = 0; - invalid_under_p2sh_tx.vout.resize(1); - invalid_under_p2sh_tx.vout[0].nValue = 11*CENT; - invalid_under_p2sh_tx.vout[0].scriptPubKey = p2pk_scriptPubKey; + invalid_under_p2sh_tx.vin = {CTxIn{spend_tx.GetHash(), 0}}; + invalid_under_p2sh_tx.vout = {CTxOut{11*CENT, p2pk_scriptPubKey}}; std::vector vchSig2(p2pk_scriptPubKey.begin(), p2pk_scriptPubKey.end()); invalid_under_p2sh_tx.vin[0].scriptSig << vchSig2; @@ -263,13 +250,8 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, Dersig100Setup) CMutableTransaction invalid_with_cltv_tx; invalid_with_cltv_tx.version = 1; invalid_with_cltv_tx.nLockTime = 100; - invalid_with_cltv_tx.vin.resize(1); - invalid_with_cltv_tx.vin[0].prevout.hash = spend_tx.GetHash(); - invalid_with_cltv_tx.vin[0].prevout.n = 2; - invalid_with_cltv_tx.vin[0].nSequence = 0; - invalid_with_cltv_tx.vout.resize(1); - invalid_with_cltv_tx.vout[0].nValue = 11*CENT; - invalid_with_cltv_tx.vout[0].scriptPubKey = p2pk_scriptPubKey; + invalid_with_cltv_tx.vin = {CTxIn{spend_tx.GetHash(), 2, {}, /*nSequenceIn=*/0}}; + invalid_with_cltv_tx.vout = {CTxOut{11*CENT, p2pk_scriptPubKey}}; // Sign std::vector vchSig; @@ -291,13 +273,8 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, Dersig100Setup) { CMutableTransaction invalid_with_csv_tx; invalid_with_csv_tx.version = 2; - invalid_with_csv_tx.vin.resize(1); - invalid_with_csv_tx.vin[0].prevout.hash = spend_tx.GetHash(); - invalid_with_csv_tx.vin[0].prevout.n = 3; - invalid_with_csv_tx.vin[0].nSequence = 100; - invalid_with_csv_tx.vout.resize(1); - invalid_with_csv_tx.vout[0].nValue = 11*CENT; - invalid_with_csv_tx.vout[0].scriptPubKey = p2pk_scriptPubKey; + invalid_with_csv_tx.vin = {CTxIn{spend_tx.GetHash(), 3, {}, /*nSequenceIn=*/100}}; + invalid_with_csv_tx.vout = {CTxOut{11*CENT, p2pk_scriptPubKey}}; // Sign std::vector vchSig; @@ -322,12 +299,8 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, Dersig100Setup) { CMutableTransaction valid_with_witness_tx; valid_with_witness_tx.version = 1; - valid_with_witness_tx.vin.resize(1); - valid_with_witness_tx.vin[0].prevout.hash = spend_tx.GetHash(); - valid_with_witness_tx.vin[0].prevout.n = 1; - valid_with_witness_tx.vout.resize(1); - valid_with_witness_tx.vout[0].nValue = 11*CENT; - valid_with_witness_tx.vout[0].scriptPubKey = p2pk_scriptPubKey; + valid_with_witness_tx.vin = {CTxIn{spend_tx.GetHash(), 1}}; + valid_with_witness_tx.vout = {CTxOut{11*CENT, p2pk_scriptPubKey}}; // Sign SignatureData sigdata; @@ -347,14 +320,11 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, Dersig100Setup) CMutableTransaction tx; tx.version = 1; - tx.vin.resize(2); - tx.vin[0].prevout.hash = spend_tx.GetHash(); - tx.vin[0].prevout.n = 0; - tx.vin[1].prevout.hash = spend_tx.GetHash(); - tx.vin[1].prevout.n = 1; - tx.vout.resize(1); - tx.vout[0].nValue = 22*CENT; - tx.vout[0].scriptPubKey = p2pk_scriptPubKey; + tx.vin = { + CTxIn{spend_tx.GetHash(), 0}, + CTxIn{spend_tx.GetHash(), 1}, + }; + tx.vout = {CTxOut{22*CENT, p2pk_scriptPubKey}}; // Sign for (int i = 0; i < 2; ++i) {