Bitcoin Core  0.19.99
P2P Digital Currency
duplicate_inputs.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2019 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 <txmempool.h>
11 #include <validation.h>
12 
13 
14 
15 static void DuplicateInputs(benchmark::State& state)
16 {
17  const CScript SCRIPT_PUB{CScript(OP_TRUE)};
18 
19  const CChainParams& chainparams = Params();
20 
21  CBlock block{};
22  CMutableTransaction coinbaseTx{};
23  CMutableTransaction naughtyTx{};
24 
25  LOCK(cs_main);
26  CBlockIndex* pindexPrev = ::ChainActive().Tip();
27  assert(pindexPrev != nullptr);
28  block.nBits = GetNextWorkRequired(pindexPrev, &block, chainparams.GetConsensus());
29  block.nNonce = 0;
30  auto nHeight = pindexPrev->nHeight + 1;
31 
32  // Make a coinbase TX
33  coinbaseTx.vin.resize(1);
34  coinbaseTx.vin[0].prevout.SetNull();
35  coinbaseTx.vout.resize(1);
36  coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB;
37  coinbaseTx.vout[0].nValue = GetBlockSubsidy(nHeight, chainparams.GetConsensus());
38  coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
39 
40 
41  naughtyTx.vout.resize(1);
42  naughtyTx.vout[0].nValue = 0;
43  naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB;
44 
45  uint64_t n_inputs = (((MAX_BLOCK_SERIALIZED_SIZE / WITNESS_SCALE_FACTOR) - (CTransaction(coinbaseTx).GetTotalSize() + CTransaction(naughtyTx).GetTotalSize())) / 41) - 100;
46  for (uint64_t x = 0; x < (n_inputs - 1); ++x) {
47  naughtyTx.vin.emplace_back(GetRandHash(), 0, CScript(), 0);
48  }
49  naughtyTx.vin.emplace_back(naughtyTx.vin.back());
50 
51  block.vtx.push_back(MakeTransactionRef(std::move(coinbaseTx)));
52  block.vtx.push_back(MakeTransactionRef(std::move(naughtyTx)));
53 
54  block.hashMerkleRoot = BlockMerkleRoot(block);
55 
56  while (state.KeepRunning()) {
57  BlockValidationState cvstate{};
58  assert(!CheckBlock(block, cvstate, chainparams.GetConsensus(), false, false));
59  assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate");
60  }
61 }
62 
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
Definition: block.h:72
CChain & ChainActive()
Definition: validation.cpp:91
uint256 GetRandHash() noexcept
Definition: random.cpp:603
unsigned int nHeight
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:409
bool KeepRunning()
Definition: bench.h:72
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:47
#define LOCK(cs)
Definition: sync.h:179
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:106
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.
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:390
static void DuplicateInputs(benchmark::State &state)
A mutable version of CTransaction.
Definition: transaction.h:366
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
Definition: transaction.cpp:94
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:270
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:60
BENCHMARK(DuplicateInputs, 10)
Definition: script.h:65
Definition: script.h:57