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
34
35 const CChainParams& chainparams = Params();
36
37 CBlock block{};
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
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([&] {
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.
Definition block.h:74
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition chain.h:94
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.
Main entry point to nanobench's benchmarking facility.
Definition nanobench.h:627
void resize(size_type new_size)
Definition prevector.h:276
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
A mutable version of CTransaction.
#define LOCK(cs)
Definition sync.h:258
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:73
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())