MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / BOOST_FIXTURE_TEST_CASE

Function BOOST_FIXTURE_TEST_CASE

src/test/txvalidationcache_tests.cpp:31–110  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

29BOOST_AUTO_TEST_SUITE(txvalidationcache_tests)
30
31BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, Dersig100Setup)
32{
33 // Make sure skipping validation of transactions that were
34 // validated going into the memory pool does not allow
35 // double-spends in blocks to pass validation when they should not.
36
37 CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
38
39 const auto ToMemPool = [this](const CMutableTransaction& tx) {
40 LOCK(cs_main);
41
42 const MempoolAcceptResult result = m_node.chainman->ProcessTransaction(MakeTransactionRef(tx));
43 return result.m_result_type == MempoolAcceptResult::ResultType::VALID;
44 };
45
46 // Create a double-spend of mature coinbase txn:
47 std::vector<CMutableTransaction> spends;
48 spends.resize(2);
49 for (int i = 0; i < 2; i++)
50 {
51 spends[i].version = 1;
52 spends[i].vin.resize(1);
53 spends[i].vin[0].prevout.hash = m_coinbase_txns[0]->GetHash();
54 spends[i].vin[0].prevout.n = 0;
55 spends[i].vout.resize(1);
56 spends[i].vout[0].nValue = 11*CENT;
57 spends[i].vout[0].scriptPubKey = scriptPubKey;
58
59 // Sign:
60 std::vector<unsigned char> vchSig;
61 uint256 hash = SignatureHash(scriptPubKey, spends[i], 0, SIGHASH_ALL, 0, SigVersion::BASE);
62 BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
63 vchSig.push_back((unsigned char)SIGHASH_ALL);
64 spends[i].vin[0].scriptSig << vchSig;
65 }
66
67 CBlock block;
68
69 // Test 1: block with both of those transactions should be rejected.
70 block = CreateAndProcessBlock(spends, scriptPubKey);
71 {
72 LOCK(cs_main);
73 BOOST_CHECK(m_node.chainman->ActiveChain().Tip()->GetBlockHash() != block.GetHash());
74 }
75
76 // Test 2: ... and should be rejected if spend1 is in the memory pool
77 BOOST_CHECK(ToMemPool(spends[0]));
78 block = CreateAndProcessBlock(spends, scriptPubKey);
79 {
80 LOCK(cs_main);
81 BOOST_CHECK(m_node.chainman->ActiveChain().Tip()->GetBlockHash() != block.GetHash());
82 }
83 BOOST_CHECK_EQUAL(m_node.mempool->size(), 1U);
84 WITH_LOCK(m_node.mempool->cs, m_node.mempool->removeRecursive(CTransaction{spends[0]}, MemPoolRemovalReason::CONFLICT));
85 BOOST_CHECK_EQUAL(m_node.mempool->size(), 0U);
86
87 // Test 3: ... and should be rejected if spend2 is in the memory pool
88 BOOST_CHECK(ToMemPool(spends[1]));

Callers

nothing calls this directly

Calls 15

ToByteVectorFunction · 0.85
MakeTransactionRefFunction · 0.85
SignatureHashFunction · 0.85
GetScriptForDestinationFunction · 0.85
ScriptHashClass · 0.85
PKHashClass · 0.85
WitnessV0KeyHashClass · 0.85
CheckInputScriptsFunction · 0.85
ProduceSignatureFunction · 0.85
UpdateInputFunction · 0.85
ProcessTransactionMethod · 0.80

Tested by

no test coverage detected