Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
duplicate_inputs.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-present 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>
9#include <consensus/merkle.h>
11#include <pow.h>
12#include <primitives/block.h>
14#include <random.h>
15#include <script/script.h>
16#include <sync.h>
18#include <uint256.h>
19#include <validation.h>
20
21#include <cassert>
22#include <cstdint>
23#include <memory>
24#include <string>
25#include <utility>
26#include <vector>
27
28
30{
31 const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
32
33 const CScript SCRIPT_PUB{CScript(OP_TRUE)};
34
35 const CChainParams& chainparams = Params();
36
37 CBlock block{};
38 CMutableTransaction coinbaseTx{};
39 CMutableTransaction naughtyTx{};
40
42 CBlockIndex* pindexPrev = testing_setup->m_node.chainman->ActiveChain().Tip();
43 assert(pindexPrev != nullptr);
44 block.nBits = GetNextWorkRequired(pindexPrev, &block, chainparams.GetConsensus());
45 block.nNonce = 0;
46 auto nHeight = pindexPrev->nHeight + 1;
47
48 // Make a coinbase TX
49 coinbaseTx.vin.resize(1);
50 coinbaseTx.vin[0].prevout.SetNull();
51 coinbaseTx.vout.resize(1);
52 coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB;
53 coinbaseTx.vout[0].nValue = GetBlockSubsidy(nHeight, chainparams.GetConsensus());
54 coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
55
56
57 naughtyTx.vout.resize(1);
58 naughtyTx.vout[0].nValue = 0;
59 naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB;
60
61 uint64_t n_inputs = (((MAX_BLOCK_SERIALIZED_SIZE / WITNESS_SCALE_FACTOR) - (CTransaction(coinbaseTx).ComputeTotalSize() + CTransaction(naughtyTx).ComputeTotalSize())) / 41) - 100;
62 for (uint64_t x = 0; x < (n_inputs - 1); ++x) {
63 naughtyTx.vin.emplace_back(Txid::FromUint256(GetRandHash()), 0, CScript(), 0);
64 }
65 naughtyTx.vin.emplace_back(naughtyTx.vin.back());
66
67 block.vtx.push_back(MakeTransactionRef(std::move(coinbaseTx)));
68 block.vtx.push_back(MakeTransactionRef(std::move(naughtyTx)));
69
70 block.hashMerkleRoot = BlockMerkleRoot(block);
71
72 bench.run([&] {
73 BlockValidationState cvstate{};
74 assert(!CheckBlock(block, cvstate, chainparams.GetConsensus(), false, false));
75 assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate");
76 });
77}
78
#define BENCHMARK(n)
Definition bench.h:68
const CChainParams & Params()
Return the currently selected parameters.
uint32_t nNonce
Definition block.h:35
uint32_t nBits
Definition block.h:34
uint256 hashMerkleRoot
Definition block.h:32
Definition block.h:74
std::vector< CTransactionRef > vtx
Definition block.h:77
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition chain.h:94
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition chain.h:106
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition chainparams.h:77
const Consensus::Params & GetConsensus() const
Definition chainparams.h:89
Serialized script, used inside transaction inputs and outputs.
Definition script.h:405
The basic transaction that is broadcasted on the network and contained in blocks.
unsigned int ComputeTotalSize() const
Calculate the total transaction size in bytes, including witness data.
std::string GetRejectReason() const
Definition validation.h:109
Main entry point to nanobench's benchmarking facility.
Definition nanobench.h:627
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition nanobench.h:1234
static transaction_identifier FromUint256(const uint256 &id)
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition merkle.cpp:66
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
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition cs_main.cpp:8
static void DuplicateInputs(benchmark::Bench &bench)
unsigned int nHeight
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params &params)
Definition pow.cpp:14
static CTransactionRef MakeTransactionRef(Tx &&txIn)
uint256 GetRandHash() noexcept
Generate a random uint256.
Definition random.h:463
@ OP_TRUE
Definition script.h:84
@ OP_0
Definition script.h:76
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
A mutable version of CTransaction.
std::vector< CTxOut > vout
std::vector< CTxIn > vin
#define LOCK(cs)
Definition sync.h:258
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())