test: refactor: simplify tx vin/vout creation in txvalidationcache_tests.cpp

This commit is contained in:
Sebastian Falbesoner
2026-06-07 01:51:27 +02:00
parent 27472a542c
commit e78a2a0d00

View File

@@ -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<unsigned char> 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<unsigned char> 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<unsigned char> 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<unsigned char> 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) {