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  const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
21 
22  const CScript SCRIPT_PUB{CScript(OP_TRUE)};
23 
24  const Config &config = testing_setup->m_node.chainman->GetConfig();
25  const CChainParams &chainParams = config.GetChainParams();
26  const Consensus::Params &consensusParams = chainParams.GetConsensus();
27 
28  CBlock block{};
29  CMutableTransaction coinbaseTx{};
30  CMutableTransaction naughtyTx{};
31 
32  LOCK(cs_main);
33  CBlockIndex *pindexPrev =
34  testing_setup->m_node.chainman->ActiveChain().Tip();
35  assert(pindexPrev != nullptr);
36  block.nBits = GetNextWorkRequired(pindexPrev, &block, chainParams);
37  block.nNonce = 0;
38  auto nHeight = pindexPrev->nHeight + 1;
39 
40  // Make a coinbase TX
41  coinbaseTx.vin.resize(1);
42  coinbaseTx.vin[0].prevout = COutPoint();
43  coinbaseTx.vout.resize(1);
44  coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB;
45  coinbaseTx.vout[0].nValue = GetBlockSubsidy(nHeight, consensusParams);
46  coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
47 
48  naughtyTx.vout.resize(1);
49  naughtyTx.vout[0].nValue = Amount::zero();
50  naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB;
51 
52  uint64_t n_inputs =
53  ((MAX_TX_SIZE - CTransaction(naughtyTx).GetTotalSize()) / 41) - 100;
54  for (uint64_t x = 0; x < (n_inputs - 1); ++x) {
55  naughtyTx.vin.emplace_back(TxId(GetRandHash()), 0, CScript(), 0);
56  }
57  naughtyTx.vin.emplace_back(naughtyTx.vin.back());
58 
59  block.vtx.push_back(MakeTransactionRef(std::move(coinbaseTx)));
60  block.vtx.push_back(MakeTransactionRef(std::move(naughtyTx)));
61 
62  block.hashMerkleRoot = BlockMerkleRoot(block);
63 
64  bench.run([&] {
65  BlockValidationState cvstate{};
66  assert(!CheckBlock(block, cvstate, consensusParams,
68  .withCheckPoW(false)
69  .withCheckMerkleRoot(false)));
70  assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate");
71  });
72 }
73 
Definition: block.h:60
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:25
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:38
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:80
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:92
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
Definition: config.h:19
virtual const CChainParams & GetChainParams() const =0
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
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())