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