Bitcoin ABC  0.22.13
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/merkle.h>
10 #include <consensus/validation.h>
11 #include <pow/pow.h>
12 #include <random.h>
13 #include <script/scriptcache.h>
14 #include <test/util/setup_common.h>
15 #include <txmempool.h>
16 #include <validation.h>
17 
18 static void DuplicateInputs(benchmark::State &state) {
19  TestingSetup test_setup{
21  /* extra_args */
22  {
23  "-nodebuglogfile",
24  "-nodebug",
25  },
26  };
27 
28  const CScript SCRIPT_PUB{CScript(OP_TRUE)};
29 
30  const CChainParams &chainParams = Params();
31  const Consensus::Params &consensusParams = chainParams.GetConsensus();
32 
33  CBlock block{};
34  CMutableTransaction coinbaseTx{};
35  CMutableTransaction naughtyTx{};
36 
37  LOCK(cs_main);
38  CBlockIndex *pindexPrev = ::ChainActive().Tip();
39  assert(pindexPrev != nullptr);
40  block.nBits = GetNextWorkRequired(pindexPrev, &block, chainParams);
41  block.nNonce = 0;
42  auto nHeight = pindexPrev->nHeight + 1;
43 
44  // Make a coinbase TX
45  coinbaseTx.vin.resize(1);
46  coinbaseTx.vin[0].prevout = COutPoint();
47  coinbaseTx.vout.resize(1);
48  coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB;
49  coinbaseTx.vout[0].nValue = GetBlockSubsidy(nHeight, consensusParams);
50  coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
51 
52  naughtyTx.vout.resize(1);
53  naughtyTx.vout[0].nValue = Amount::zero();
54  naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB;
55 
56  uint64_t n_inputs =
57  ((MAX_TX_SIZE - CTransaction(naughtyTx).GetTotalSize()) / 41) - 100;
58  for (uint64_t x = 0; x < (n_inputs - 1); ++x) {
59  naughtyTx.vin.emplace_back(TxId(GetRandHash()), 0, CScript(), 0);
60  }
61  naughtyTx.vin.emplace_back(naughtyTx.vin.back());
62 
63  block.vtx.push_back(MakeTransactionRef(std::move(coinbaseTx)));
64  block.vtx.push_back(MakeTransactionRef(std::move(naughtyTx)));
65 
66  block.hashMerkleRoot = BlockMerkleRoot(block);
67 
68  while (state.KeepRunning()) {
69  BlockValidationState cvstate{};
70  assert(!CheckBlock(block, cvstate, consensusParams,
72  .withCheckPoW(false)
73  .withCheckMerkleRoot(false)));
74  assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate");
75  }
76 }
77 
static constexpr Amount zero()
Definition: amount.h:35
static const std::string REGTEST
Definition: block.h:62
CChain & ChainActive()
Definition: validation.cpp:78
uint256 GetRandHash() noexcept
Definition: random.cpp:659
unsigned int nHeight
static const uint64_t MAX_TX_SIZE
The maximum allowed size for a transaction, in bytes.
Definition: consensus.h:14
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:337
bool KeepRunning()
Definition: bench.h:71
Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
Definition: validation.cpp:854
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:47
#define LOCK(cs)
Definition: sync.h:230
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:95
const Config & GetConfig()
Definition: config.cpp:34
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Compute the Merkle root of the transactions in a block.
Definition: merkle.cpp:69
Parameters that influence chain consensus.
Definition: params.h:59
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
uint32_t GetNextWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const CChainParams &chainParams)
Definition: pow.cpp:21
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: blockindex.h:23
const CChainParams & Params()
Return the currently selected parameters.
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
A TxId is the identifier of a transaction.
Definition: txid.h:14
A mutable version of CTransaction.
Definition: transaction.h:295
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:168
static void DuplicateInputs(benchmark::State &state)
bool CheckBlock(const CCheckpointData &data, int nHeight, const BlockHash &hash)
Returns true if block passes checkpoint checks.
Definition: checkpoints.cpp:17
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
Definition: transaction.cpp:86
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:211
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:36
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:59
BENCHMARK(DuplicateInputs, 10)
Definition: script.h:56
Definition: script.h:48