Bitcoin ABC  0.26.3
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  TestingSetup test_setup{
22  /* extra_args */
23  {
24  "-nodebuglogfile",
25  "-nodebug",
26  },
27  };
28 
29  const CScript SCRIPT_PUB{CScript(OP_TRUE)};
30 
31  const CChainParams &chainParams = Params();
32  const Consensus::Params &consensusParams = chainParams.GetConsensus();
33 
34  CBlock block{};
35  CMutableTransaction coinbaseTx{};
36  CMutableTransaction naughtyTx{};
37 
38  LOCK(cs_main);
39  CBlockIndex *pindexPrev = test_setup.m_node.chainman->ActiveChain().Tip();
40  assert(pindexPrev != nullptr);
41  block.nBits = GetNextWorkRequired(pindexPrev, &block, chainParams);
42  block.nNonce = 0;
43  auto nHeight = pindexPrev->nHeight + 1;
44 
45  // Make a coinbase TX
46  coinbaseTx.vin.resize(1);
47  coinbaseTx.vin[0].prevout = COutPoint();
48  coinbaseTx.vout.resize(1);
49  coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB;
50  coinbaseTx.vout[0].nValue = GetBlockSubsidy(nHeight, consensusParams);
51  coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
52 
53  naughtyTx.vout.resize(1);
54  naughtyTx.vout[0].nValue = Amount::zero();
55  naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB;
56 
57  uint64_t n_inputs =
58  ((MAX_TX_SIZE - CTransaction(naughtyTx).GetTotalSize()) / 41) - 100;
59  for (uint64_t x = 0; x < (n_inputs - 1); ++x) {
60  naughtyTx.vin.emplace_back(TxId(GetRandHash()), 0, CScript(), 0);
61  }
62  naughtyTx.vin.emplace_back(naughtyTx.vin.back());
63 
64  block.vtx.push_back(MakeTransactionRef(std::move(coinbaseTx)));
65  block.vtx.push_back(MakeTransactionRef(std::move(naughtyTx)));
66 
67  block.hashMerkleRoot = BlockMerkleRoot(block);
68 
69  bench.run([&] {
70  BlockValidationState cvstate{};
71  assert(!CheckBlock(block, cvstate, consensusParams,
73  .withCheckPoW(false)
74  .withCheckMerkleRoot(false)));
75  assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate");
76  });
77 }
78 
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:113
const CChainParams & Params()
Return the currently selected parameters.
static const std::string REGTEST
Definition: block.h:55
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:280
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
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:194
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
Definition: transaction.cpp:91
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
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:322
uint256 GetRandHash() noexcept
Definition: random.cpp:658
@ OP_TRUE
Definition: script.h:57
@ OP_0
Definition: script.h:49
static constexpr Amount zero()
Definition: amount.h:32
Parameters that influence chain consensus.
Definition: params.h:33
A TxId is the identifier of a transaction.
Definition: txid.h:14
#define LOCK(cs)
Definition: sync.h:243
Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
assert(!tx.IsCoinBase())