Bitcoin Core  27.99.0
P2P Digital Currency
duplicate_inputs.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <bench/bench.h>
6 #include <chainparams.h>
7 #include <consensus/merkle.h>
8 #include <consensus/validation.h>
9 #include <pow.h>
10 #include <random.h>
11 #include <test/util/setup_common.h>
12 #include <txmempool.h>
13 #include <validation.h>
14 
15 
16 static void DuplicateInputs(benchmark::Bench& bench)
17 {
18  const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
19 
20  const CScript SCRIPT_PUB{CScript(OP_TRUE)};
21 
22  const CChainParams& chainparams = Params();
23 
24  CBlock block{};
25  CMutableTransaction coinbaseTx{};
26  CMutableTransaction naughtyTx{};
27 
28  LOCK(cs_main);
29  CBlockIndex* pindexPrev = testing_setup->m_node.chainman->ActiveChain().Tip();
30  assert(pindexPrev != nullptr);
31  block.nBits = GetNextWorkRequired(pindexPrev, &block, chainparams.GetConsensus());
32  block.nNonce = 0;
33  auto nHeight = pindexPrev->nHeight + 1;
34 
35  // Make a coinbase TX
36  coinbaseTx.vin.resize(1);
37  coinbaseTx.vin[0].prevout.SetNull();
38  coinbaseTx.vout.resize(1);
39  coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB;
40  coinbaseTx.vout[0].nValue = GetBlockSubsidy(nHeight, chainparams.GetConsensus());
41  coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
42 
43 
44  naughtyTx.vout.resize(1);
45  naughtyTx.vout[0].nValue = 0;
46  naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB;
47 
48  uint64_t n_inputs = (((MAX_BLOCK_SERIALIZED_SIZE / WITNESS_SCALE_FACTOR) - (CTransaction(coinbaseTx).GetTotalSize() + CTransaction(naughtyTx).GetTotalSize())) / 41) - 100;
49  for (uint64_t x = 0; x < (n_inputs - 1); ++x) {
50  naughtyTx.vin.emplace_back(Txid::FromUint256(GetRandHash()), 0, CScript(), 0);
51  }
52  naughtyTx.vin.emplace_back(naughtyTx.vin.back());
53 
54  block.vtx.push_back(MakeTransactionRef(std::move(coinbaseTx)));
55  block.vtx.push_back(MakeTransactionRef(std::move(naughtyTx)));
56 
57  block.hashMerkleRoot = BlockMerkleRoot(block);
58 
59  bench.run([&] {
60  BlockValidationState cvstate{};
61  assert(!CheckBlock(block, cvstate, chainparams.GetConsensus(), false, false));
62  assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate");
63  });
64 }
65 
const CChainParams & Params()
Return the currently selected parameters.
Definition: block.h:69
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:141
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:153
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:81
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:93
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:414
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:627
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
static transaction_identifier FromUint256(const uint256 &id)
static const unsigned int MAX_BLOCK_SERIALIZED_SIZE
The maximum allowed size for a serialized block, in bytes (only for buffer size limits)
Definition: consensus.h:13
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
BENCHMARK(DuplicateInputs, benchmark::PriorityLevel::HIGH)
static void DuplicateInputs(benchmark::Bench &bench)
unsigned int nHeight
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:65
@ HIGH
Definition: bench.h:47
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params &params)
Definition: pow.cpp:13
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
uint256 GetRandHash() noexcept
Definition: random.cpp:648
@ OP_TRUE
Definition: script.h:83
@ OP_0
Definition: script.h:75
A mutable version of CTransaction.
Definition: transaction.h:378
#define LOCK(cs)
Definition: sync.h:257
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
Functions for validating blocks and updating the block tree.
assert(!tx.IsCoinBase())