Bitcoin ABC  0.28.12
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 <chain.h>
7 #include <chainparams.h>
8 #include <config.h>
9 #include <consensus/amount.h>
10 #include <consensus/merkle.h>
11 #include <consensus/validation.h>
12 #include <pow/pow.h>
13 #include <random.h>
14 #include <script/scriptcache.h>
15 #include <test/util/setup_common.h>
16 #include <txmempool.h>
17 #include <validation.h>
18 
19 static void DuplicateInputs(benchmark::Bench &bench) {
20  const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
21 
22  const CScript SCRIPT_PUB{CScript(OP_TRUE)};
23 
24  const CChainParams &chainParams = Params();
25  const Consensus::Params &consensusParams = chainParams.GetConsensus();
26 
27  CBlock block{};
28  CMutableTransaction coinbaseTx{};
29  CMutableTransaction naughtyTx{};
30 
31  LOCK(cs_main);
32  CBlockIndex *pindexPrev =
33  testing_setup->m_node.chainman->ActiveChain().Tip();
34  assert(pindexPrev != nullptr);
35  block.nBits = GetNextWorkRequired(pindexPrev, &block, chainParams);
36  block.nNonce = 0;
37  auto nHeight = pindexPrev->nHeight + 1;
38 
39  // Make a coinbase TX
40  coinbaseTx.vin.resize(1);
41  coinbaseTx.vin[0].prevout = COutPoint();
42  coinbaseTx.vout.resize(1);
43  coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB;
44  coinbaseTx.vout[0].nValue = GetBlockSubsidy(nHeight, consensusParams);
45  coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
46 
47  naughtyTx.vout.resize(1);
48  naughtyTx.vout[0].nValue = Amount::zero();
49  naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB;
50 
51  uint64_t n_inputs =
52  ((MAX_TX_SIZE - CTransaction(naughtyTx).GetTotalSize()) / 41) - 100;
53  for (uint64_t x = 0; x < (n_inputs - 1); ++x) {
54  naughtyTx.vin.emplace_back(TxId(GetRandHash()), 0, CScript(), 0);
55  }
56  naughtyTx.vin.emplace_back(naughtyTx.vin.back());
57 
58  block.vtx.push_back(MakeTransactionRef(std::move(coinbaseTx)));
59  block.vtx.push_back(MakeTransactionRef(std::move(naughtyTx)));
60 
61  block.hashMerkleRoot = BlockMerkleRoot(block);
62 
63  bench.run([&] {
64  BlockValidationState cvstate{};
65  assert(!CheckBlock(block, cvstate, consensusParams,
67  .withCheckPoW(false)
68  .withCheckMerkleRoot(false)));
69  assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate");
70  });
71 }
72 
const CChainParams & Params()
Return the currently selected parameters.
Definition: block.h:60
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:26
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:39
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:74
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:86
A mutable version of CTransaction.
Definition: transaction.h:274
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
Definition: transaction.cpp:92
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
const Config & GetConfig()
Definition: config.cpp:34
static const uint64_t MAX_TX_SIZE
The maximum allowed size for a transaction, in bytes.
Definition: consensus.h:14
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
BENCHMARK(DuplicateInputs)
static void DuplicateInputs(benchmark::Bench &bench)
unsigned int nHeight
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Compute the Merkle root of the transactions in a block.
Definition: merkle.cpp:69
bool CheckBlock(const CCheckpointData &data, int nHeight, const BlockHash &hash)
Returns true if block passes checkpoint checks.
Definition: checkpoints.cpp:11
uint32_t GetNextWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const CChainParams &chainParams)
Definition: pow.cpp:21
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:316
uint256 GetRandHash() noexcept
Definition: random.cpp:659
@ OP_TRUE
Definition: script.h:57
@ OP_0
Definition: script.h:49
static constexpr Amount zero() noexcept
Definition: amount.h:32
Parameters that influence chain consensus.
Definition: params.h:34
A TxId is the identifier of a transaction.
Definition: txid.h:14
#define LOCK(cs)
Definition: sync.h:306
Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
assert(!tx.IsCoinBase())