Bitcoin Core  29.1.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 <chain.h>
7 #include <chainparams.h>
8 #include <consensus/consensus.h>
9 #include <consensus/merkle.h>
10 #include <consensus/validation.h>
11 #include <pow.h>
12 #include <primitives/block.h>
13 #include <primitives/transaction.h>
14 #include <random.h>
15 #include <script/script.h>
16 #include <sync.h>
17 #include <test/util/setup_common.h>
18 #include <uint256.h>
19 #include <validation.h>
20 
21 #include <cassert>
22 #include <cstdint>
23 #include <memory>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 
29 static 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).GetTotalSize() + CTransaction(naughtyTx).GetTotalSize())) / 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 
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
assert(!tx.IsCoinBase())
Definition: block.h:68
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:80
Definition: script.h:76
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
Definition: script.h:84
#define LOCK(cs)
Definition: sync.h:257
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params &params)
Definition: pow.cpp:14
BENCHMARK(DuplicateInputs, benchmark::PriorityLevel::HIGH)
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:66
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
Functions for validating blocks and updating the block tree.
unsigned int nHeight
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:140
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:414
static transaction_identifier FromUint256(const uint256 &id)
uint256 GetRandHash() noexcept
Generate a random uint256.
Definition: random.h:454
A mutable version of CTransaction.
Definition: transaction.h:377
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:627
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:295
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:153
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:93
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8