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

Function DuplicateInputs

src/bench/duplicate_inputs.cpp:29–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27
28
29static void DuplicateInputs(benchmark::Bench& bench)
30{
31 const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
32
33 const CScript SCRIPT_PUB{CScript(OP_TRUE)};
34
35 const CChainParams& chainparams = Params();
36
37 CBlock block{};
38 CMutableTransaction coinbaseTx{};
39 CMutableTransaction naughtyTx{};
40
41 LOCK(cs_main);
42 CBlockIndex* pindexPrev = testing_setup->m_node.chainman->ActiveChain().Tip();
43 assert(pindexPrev != nullptr);
44 block.nBits = GetNextWorkRequired(pindexPrev, &block, chainparams.GetConsensus());
45 block.nNonce = 0;
46 auto nHeight = pindexPrev->nHeight + 1;
47
48 // Make a coinbase TX
49 coinbaseTx.vin.resize(1);
50 coinbaseTx.vin[0].prevout.SetNull();
51 coinbaseTx.vout.resize(1);
52 coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB;
53 coinbaseTx.vout[0].nValue = GetBlockSubsidy(nHeight, chainparams.GetConsensus());
54 coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
55
56
57 naughtyTx.vout.resize(1);
58 naughtyTx.vout[0].nValue = 0;
59 naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB;
60
61 uint64_t n_inputs = (((MAX_BLOCK_SERIALIZED_SIZE / WITNESS_SCALE_FACTOR) - (CTransaction(coinbaseTx).ComputeTotalSize() + CTransaction(naughtyTx).ComputeTotalSize())) / 41) - 100;
62 for (uint64_t x = 0; x < (n_inputs - 1); ++x) {
63 naughtyTx.vin.emplace_back(Txid::FromUint256(GetRandHash()), 0, CScript(), 0);
64 }
65 naughtyTx.vin.emplace_back(naughtyTx.vin.back());
66
67 block.vtx.push_back(MakeTransactionRef(std::move(coinbaseTx)));
68 block.vtx.push_back(MakeTransactionRef(std::move(naughtyTx)));
69
70 block.hashMerkleRoot = BlockMerkleRoot(block);
71
72 bench.run([&] {
73 BlockValidationState cvstate{};
74 assert(!CheckBlock(block, cvstate, chainparams.GetConsensus(), false, false));
75 assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate");
76 });
77}
78
79BENCHMARK(DuplicateInputs);

Callers

nothing calls this directly

Calls 15

GetNextWorkRequiredFunction · 0.85
GetBlockSubsidyFunction · 0.85
GetRandHashFunction · 0.85
MakeTransactionRefFunction · 0.85
BlockMerkleRootFunction · 0.85
CheckBlockFunction · 0.85
ComputeTotalSizeMethod · 0.80
GetRejectReasonMethod · 0.80
CScriptClass · 0.50
ParamsClass · 0.50
CTransactionClass · 0.50
TipMethod · 0.45

Tested by

no test coverage detected