Bitcoin Core 22.99.0
P2P Digital Currency
duplicate_inputs.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2020 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 <chainparams.h>
7#include <consensus/merkle.h>
9#include <pow.h>
11#include <txmempool.h>
12#include <validation.h>
13
14
16{
17 const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
18
19 const CScript SCRIPT_PUB{CScript(OP_TRUE)};
20
21 const CChainParams& chainparams = Params();
22
23 CBlock block{};
24 CMutableTransaction coinbaseTx{};
25 CMutableTransaction naughtyTx{};
26
28 CBlockIndex* pindexPrev = testing_setup->m_node.chainman->ActiveChain().Tip();
29 assert(pindexPrev != nullptr);
30 block.nBits = GetNextWorkRequired(pindexPrev, &block, chainparams.GetConsensus());
31 block.nNonce = 0;
32 auto nHeight = pindexPrev->nHeight + 1;
33
34 // Make a coinbase TX
35 coinbaseTx.vin.resize(1);
36 coinbaseTx.vin[0].prevout.SetNull();
37 coinbaseTx.vout.resize(1);
38 coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB;
39 coinbaseTx.vout[0].nValue = GetBlockSubsidy(nHeight, chainparams.GetConsensus());
40 coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
41
42
43 naughtyTx.vout.resize(1);
44 naughtyTx.vout[0].nValue = 0;
45 naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB;
46
47 uint64_t n_inputs = (((MAX_BLOCK_SERIALIZED_SIZE / WITNESS_SCALE_FACTOR) - (CTransaction(coinbaseTx).GetTotalSize() + CTransaction(naughtyTx).GetTotalSize())) / 41) - 100;
48 for (uint64_t x = 0; x < (n_inputs - 1); ++x) {
49 naughtyTx.vin.emplace_back(GetRandHash(), 0, CScript(), 0);
50 }
51 naughtyTx.vin.emplace_back(naughtyTx.vin.back());
52
53 block.vtx.push_back(MakeTransactionRef(std::move(coinbaseTx)));
54 block.vtx.push_back(MakeTransactionRef(std::move(naughtyTx)));
55
56 block.hashMerkleRoot = BlockMerkleRoot(block);
57
58 bench.run([&] {
59 BlockValidationState cvstate{};
60 assert(!CheckBlock(block, cvstate, chainparams.GetConsensus(), false, false));
61 assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate");
62 });
63}
64
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:118
const CChainParams & Params()
Return the currently selected parameters.
Definition: block.h:63
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:146
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:158
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:70
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:82
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:260
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
Definition: transaction.cpp:96
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 unsigned int MAX_BLOCK_SERIALIZED_SIZE
The maximum allowed size for a serialized block, in bytes (only for buffer size limits)
Definition: consensus.h:13
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
BENCHMARK(DuplicateInputs)
static void DuplicateInputs(benchmark::Bench &bench)
unsigned int nHeight
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:65
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params &params)
Definition: pow.cpp:13
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:387
uint256 GetRandHash() noexcept
Definition: random.cpp:601
@ OP_TRUE
Definition: script.h:77
@ OP_0
Definition: script.h:69
A mutable version of CTransaction.
Definition: transaction.h:345
#define LOCK(cs)
Definition: sync.h:226
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
Functions for validating blocks and updating the block tree.
assert(!tx.IsCoinBase())