Bitcoin Core  0.20.99
P2P Digital Currency
duplicate_inputs.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2020 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 <test/util/setup_common.h>
11 #include <txmempool.h>
12 #include <validation.h>
13 
14 
15 static void DuplicateInputs(benchmark::Bench& bench)
16 {
17  TestingSetup test_setup{
19  /* extra_args */ {
20  "-nodebuglogfile",
21  "-nodebug",
22  },
23  };
24 
25  const CScript SCRIPT_PUB{CScript(OP_TRUE)};
26 
27  const CChainParams& chainparams = Params();
28 
29  CBlock block{};
30  CMutableTransaction coinbaseTx{};
31  CMutableTransaction naughtyTx{};
32 
33  LOCK(cs_main);
34  CBlockIndex* pindexPrev = ::ChainActive().Tip();
35  assert(pindexPrev != nullptr);
36  block.nBits = GetNextWorkRequired(pindexPrev, &block, chainparams.GetConsensus());
37  block.nNonce = 0;
38  auto nHeight = pindexPrev->nHeight + 1;
39 
40  // Make a coinbase TX
41  coinbaseTx.vin.resize(1);
42  coinbaseTx.vin[0].prevout.SetNull();
43  coinbaseTx.vout.resize(1);
44  coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB;
45  coinbaseTx.vout[0].nValue = GetBlockSubsidy(nHeight, chainparams.GetConsensus());
46  coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
47 
48 
49  naughtyTx.vout.resize(1);
50  naughtyTx.vout[0].nValue = 0;
51  naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB;
52 
53  uint64_t n_inputs = (((MAX_BLOCK_SERIALIZED_SIZE / WITNESS_SCALE_FACTOR) - (CTransaction(coinbaseTx).GetTotalSize() + CTransaction(naughtyTx).GetTotalSize())) / 41) - 100;
54  for (uint64_t x = 0; x < (n_inputs - 1); ++x) {
55  naughtyTx.vin.emplace_back(GetRandHash(), 0, CScript(), 0);
56  }
57  naughtyTx.vin.emplace_back(naughtyTx.vin.back());
58 
59  block.vtx.push_back(MakeTransactionRef(std::move(coinbaseTx)));
60  block.vtx.push_back(MakeTransactionRef(std::move(naughtyTx)));
61 
62  block.hashMerkleRoot = BlockMerkleRoot(block);
63 
64  bench.run([&] {
65  BlockValidationState cvstate{};
66  assert(!CheckBlock(block, cvstate, chainparams.GetConsensus(), false, false));
67  assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate");
68  });
69 }
70 
BENCHMARK(DuplicateInputs)
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
static const std::string REGTEST
Definition: block.h:62
CChain & ChainActive()
Please prefer the identical ChainstateManager::ActiveChain.
Definition: validation.cpp:113
uint256 GetRandHash() noexcept
Definition: random.cpp:602
unsigned int nHeight
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:396
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:52
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
#define LOCK(cs)
Definition: sync.h:230
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params &params)
Definition: pow.cpp:13
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:129
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:65
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
Functions for validating blocks and updating the block tree.
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:137
const CChainParams & Params()
Return the currently selected parameters.
static void DuplicateInputs(benchmark::Bench &bench)
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:390
A mutable version of CTransaction.
Definition: transaction.h:353
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
Definition: transaction.cpp:97
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:583
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
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:259
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:150
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:65
Definition: script.h:76
Definition: script.h:68