Merge bitcoin/bitcoin#36830: test: make `tx_mempool_block_doublespend` cases independent
What changed, and why it matters
This commit only changes a test file. It reorganizes an existing test into four separate, independent test cases and makes the two test transactions slightly different so the test correctly checks that a mempool transaction is rejected as a conflict when a block contains a competing spend. No production code is changed, so this cannot affect real Bitcoin Core behavior or introduce a security vulnerability.
No action needed. This is a test-only refactor that improves test clarity and correctness. Review as normal code quality/test change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors txvalidationcache_tests.cpp. It splits the monolithic tx_mempool_block_doublespend test into four fixture-based BOOST_FIXTURE_TEST_CASE tests using a new DoubleSpendSetup helper. It also changes the two spend transactions to use different output values (11 CENT vs 12 CENT) so they have distinct txids, ensuring the final case verifies mempool removal due to conflict rather than the transaction being treated as mined. The actual validation checks remain unchanged.
Changed components
src/test/txvalidationcache_tests.cppInspect captured patch +54 / −50
### src/test/txvalidationcache_tests.cpp
@@ -22,78 +22,82 @@ struct Dersig100Setup : public TestChain100Setup {
BOOST_AUTO_TEST_SUITE(txvalidationcache_tests)
-BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, Dersig100Setup)
-{
- // Make sure skipping validation of transactions that were
- // validated going into the memory pool does not allow
- // double-spends in blocks to pass validation when they should not.
-
- CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
-
- const auto ToMemPool = [this](const CMutableTransaction& tx) {
- LOCK(cs_main);
-
- const MempoolAcceptResult result = m_node.chainman->ProcessTransaction(MakeTransactionRef(tx));
- return result.m_result_type == MempoolAcceptResult::ResultType::VALID;
- };
-
- // Create a double-spend of mature coinbase txn:
- std::vector<CMutableTransaction> spends;
- spends.resize(2);
- for (int i = 0; i < 2; i++)
+// Make sure skipping validation of transactions that were
+// validated going into the memory pool does not allow
+// double-spends in blocks to pass validation when they should not.
+struct DoubleSpendSetup : public Dersig100Setup {
+ const CScript scriptPubKey{CScript{} << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
+
+ // Returns a signed spend of the first mature coinbase txn, paying the given value.
+ CMutableTransaction MakeSpend(CAmount value) const
{
- spends[i].version = 1;
- spends[i].vin = {CTxIn{m_coinbase_txns[0]->GetHash(), 0}};
- spends[i].vout = {CTxOut{11*CENT, scriptPubKey}};
+ CMutableTransaction spend;
+ spend.version = 1;
+ spend.vin = {CTxIn{m_coinbase_txns[0]->GetHash(), 0}};
+ spend.vout = {CTxOut{value, scriptPubKey}};
// Sign:
std::vector<unsigned char> vchSig;
- uint256 hash = SignatureHash(scriptPubKey, spends[i], 0, SIGHASH_ALL, 0, SigVersion::BASE);
+ uint256 hash = SignatureHash(scriptPubKey, spend, /*nIn=*/0, SIGHASH_ALL, /*amount=*/0, SigVersion::BASE);
BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
vchSig.push_back((unsigned char)SIGHASH_ALL);
- spends[i].vin[0].scriptSig << vchSig;
+ spend.vin[0].scriptSig << vchSig;
+ return spend;
}
- CBlock block;
+ // A double-spend of a mature coinbase txn.
+ const std::vector<CMutableTransaction> spends{MakeSpend(11 * CENT), MakeSpend(12 * CENT)};
- // Test 1: block with both of those transactions should be rejected.
- block = CreateAndProcessBlock(spends, scriptPubKey);
+ bool ToMemPool(const CMutableTransaction& tx)
{
LOCK(cs_main);
- BOOST_CHECK(m_node.chainman->ActiveChain().Tip()->GetBlockHash() != block.GetHash());
+
+ const MempoolAcceptResult result = m_node.chainman->ProcessTransaction(MakeTransactionRef(tx));
+ return result.m_result_type == MempoolAcceptResult::ResultType::VALID;
}
- // Test 2: ... and should be rejected if spend1 is in the memory pool
- BOOST_CHECK(ToMemPool(spends[0]));
- block = CreateAndProcessBlock(spends, scriptPubKey);
+ bool IsTip(const CBlock& block)
{
LOCK(cs_main);
- BOOST_CHECK(m_node.chainman->ActiveChain().Tip()->GetBlockHash() != block.GetHash());
+ return m_node.chainman->ActiveChain().Tip()->GetBlockHash() == block.GetHash();
}
+};
+
+BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend_both_in_block, DoubleSpendSetup)
+{
+ // A block with both of those transactions should be rejected.
+ const CBlock block{CreateAndProcessBlock(spends, scriptPubKey)};
+ BOOST_CHECK(!IsTip(block));
+}
+
+BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend_first_in_mempool, DoubleSpendSetup)
+{
+ // ... and should be rejected if spend1 is in the memory pool
+ BOOST_CHECK(ToMemPool(spends[0]));
+ const CBlock block{CreateAndProcessBlock(spends, scriptPubKey)};
+ BOOST_CHECK(!IsTip(block));
BOOST_CHECK_EQUAL(m_node.mempool->size(), 1U);
- WITH_LOCK(m_node.mempool->cs, m_node.mempool->removeRecursive(CTransaction{spends[0]}, MemPoolRemovalReason::CONFLICT));
- BOOST_CHECK_EQUAL(m_node.mempool->size(), 0U);
+}
- // Test 3: ... and should be rejected if spend2 is in the memory pool
+BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend_second_in_mempool, DoubleSpendSetup)
+{
+ // ... and should be rejected if spend2 is in the memory pool
BOOST_CHECK(ToMemPool(spends[1]));
- block = CreateAndProcessBlock(spends, scriptPubKey);
- {
- LOCK(cs_main);
- BOOST_CHECK(m_node.chainman->ActiveChain().Tip()->GetBlockHash() != block.GetHash());
- }
+ const CBlock block{CreateAndProcessBlock(spends, scriptPubKey)};
+ BOOST_CHECK(!IsTip(block));
BOOST_CHECK_EQUAL(m_node.mempool->size(), 1U);
- WITH_LOCK(m_node.mempool->cs, m_node.mempool->removeRecursive(CTransaction{spends[1]}, MemPoolRemovalReason::CONFLICT));
- BOOST_CHECK_EQUAL(m_node.mempool->size(), 0U);
+}
+
+BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend_one_in_mempool_other_in_block, DoubleSpendSetup)
+{
+ // The spends must be distinct transactions, so that the one in the
+ // memory pool is removed as a conflict rather than as mined:
+ BOOST_REQUIRE(spends[0].GetHash() != spends[1].GetHash());
- // Final sanity test: first spend in *m_node.mempool, second in block, that's OK:
- std::vector<CMutableTransaction> oneSpend;
- oneSpend.push_back(spends[0]);
+ // Final sanity test: second spend in the memory pool, first in block, that's OK:
BOOST_CHECK(ToMemPool(spends[1]));
- block = CreateAndProcessBlock(oneSpend, scriptPubKey);
- {
- LOCK(cs_main);
- BOOST_CHECK(m_node.chainman->ActiveChain().Tip()->GetBlockHash() == block.GetHash());
- }
+ const CBlock block{CreateAndProcessBlock({spends[0]}, scriptPubKey)};
+ BOOST_CHECK(IsTip(block));
// spends[1] should have been removed from the mempool when the
// block with spends[0] is accepted:
BOOST_CHECK_EQUAL(m_node.mempool->size(), 0U);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.