Bitcoin ABC  0.24.11
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::Bench &bench) {
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  bench.run([&] {
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 
OP_0
@ OP_0
Definition: script.h:48
GetBlockSubsidy
Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
Definition: validation.cpp:849
MakeTransactionRef
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:320
pow.h
nHeight
unsigned int nHeight
Definition: mempool_eviction.cpp:13
ChainActive
CChain & ChainActive()
Please prefer the identical ChainstateManager::ActiveChain.
Definition: validation.cpp:86
BlockValidationState
Definition: validation.h:138
CBlockIndex::nHeight
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:36
BENCHMARK
BENCHMARK(DuplicateInputs)
validation.h
CChainParams
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:47
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:583
CChainParams::GetConsensus
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:59
GetNextWorkRequired
uint32_t GetNextWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const CChainParams &chainParams)
Definition: pow.cpp:21
chainparams.h
BlockValidationOptions
Definition: validation.h:180
CChain::Tip
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:168
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:194
Amount::zero
static constexpr Amount zero()
Definition: amount.h:42
txmempool.h
Consensus::Params
Parameters that influence chain consensus.
Definition: params.h:59
random.h
MAX_TX_SIZE
static const uint64_t MAX_TX_SIZE
The maximum allowed size for a transaction, in bytes.
Definition: consensus.h:14
cs_main
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:103
GetRandHash
uint256 GetRandHash() noexcept
Definition: random.cpp:658
CBaseChainParams::REGTEST
static const std::string REGTEST
Definition: chainparamsbase.h:23
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
CTransaction::GetTotalSize
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
Definition: transaction.cpp:90
TxId
A TxId is the identifier of a transaction.
Definition: txid.h:14
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
bench.h
merkle.h
CBlock
Definition: block.h:55
DuplicateInputs
static void DuplicateInputs(benchmark::Bench &bench)
Definition: duplicate_inputs.cpp:18
LOCK
#define LOCK(cs)
Definition: sync.h:241
OP_TRUE
@ OP_TRUE
Definition: script.h:56
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:508
scriptcache.h
config.h
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:23
GetConfig
const Config & GetConfig()
Definition: config.cpp:34
Checkpoints::CheckBlock
bool CheckBlock(const CCheckpointData &data, int nHeight, const BlockHash &hash)
Returns true if block passes checkpoint checks.
Definition: checkpoints.cpp:17
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:278
BlockMerkleRoot
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Compute the Merkle root of the transactions in a block.
Definition: merkle.cpp:69