Bitcoin ABC 0.34.5
P2P Digital Currency
Loading...
Searching...
No Matches
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/amount.h>
10#include <consensus/merkle.h>
12#include <pow/pow.h>
13#include <random.h>
14#include <script/scriptcache.h>
15#include <test/util/setup_common.h>
16#include <txmempool.h>
17#include <validation.h>
18
19static void DuplicateInputs(benchmark::Bench &bench) {
20 const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
21
23
24 const Config &config = testing_setup->m_node.chainman->GetConfig();
25 const CChainParams &chainParams = config.GetChainParams();
26 const Consensus::Params &consensusParams = chainParams.GetConsensus();
27
28 CBlock block{};
29 CMutableTransaction coinbaseTx{};
30 CMutableTransaction naughtyTx{};
31
33 CBlockIndex *pindexPrev =
34 testing_setup->m_node.chainman->ActiveChain().Tip();
35 assert(pindexPrev != nullptr);
36 block.nBits = GetNextWorkRequired(pindexPrev, &block, chainParams);
37 block.nNonce = 0;
38 auto nHeight = pindexPrev->nHeight + 1;
39
40 // Make a coinbase TX
41 coinbaseTx.vin.resize(1);
42 coinbaseTx.vin[0].prevout = COutPoint();
43 coinbaseTx.vout.resize(1);
44 coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB;
45 coinbaseTx.vout[0].nValue = GetBlockSubsidy(nHeight, consensusParams);
46 coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
47
48 naughtyTx.vout.resize(1);
49 naughtyTx.vout[0].nValue = Amount::zero();
50 naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB;
51
52 uint64_t n_inputs =
53 ((MAX_TX_SIZE - CTransaction(naughtyTx).GetTotalSize()) / 41) - 100;
54 for (uint64_t x = 0; x < (n_inputs - 1); ++x) {
55 naughtyTx.vin.emplace_back(TxId(GetRandHash()), 0, CScript(), 0);
56 }
57 naughtyTx.vin.emplace_back(naughtyTx.vin.back());
58
59 block.vtx.push_back(MakeTransactionRef(std::move(coinbaseTx)));
60 block.vtx.push_back(MakeTransactionRef(std::move(naughtyTx)));
61
62 block.hashMerkleRoot = BlockMerkleRoot(block);
63
64 bench.run([&] {
65 BlockValidationState cvstate{};
66 assert(!CheckBlock(block, cvstate, consensusParams,
68 .withCheckPoW(false)
69 .withCheckMerkleRoot(false)));
70 assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate");
71 });
72}
73
#define BENCHMARK(n)
Definition bench.h:65
const CScript SCRIPT_PUB
Definition block.h:60
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition blockindex.h:25
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition blockindex.h:38
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition chainparams.h:86
const Consensus::Params & GetConsensus() const
Definition chainparams.h:98
A mutable version of CTransaction.
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition transaction.h:20
Serialized script, used inside transaction inputs and outputs.
Definition script.h:434
The basic transaction that is broadcasted on the network and contained in blocks.
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
virtual const CChainParams & GetChainParams() const =0
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
void emplace_back(Args &&...args)
Definition prevector.h:526
void resize(size_type new_size)
Definition prevector.h:428
static const uint64_t MAX_TX_SIZE
The maximum allowed size for a transaction, in bytes.
Definition consensus.h:14
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition cs_main.cpp:7
static void DuplicateInputs(benchmark::Bench &bench)
unsigned int nHeight
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Compute the Merkle root of the transactions in a block.
Definition merkle.cpp:69
uint32_t GetNextWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const CChainParams &chainParams)
Definition pow.cpp:21
static CTransactionRef MakeTransactionRef()
uint256 GetRandHash() noexcept
========== CONVENIENCE FUNCTIONS FOR COMMONLY USED RANDOMNESS ==========
Definition random.h:494
@ OP_TRUE
Definition script.h:63
@ OP_0
Definition script.h:55
static constexpr Amount zero() noexcept
Definition amount.h:36
Parameters that influence chain consensus.
Definition params.h:34
A TxId is the identifier of a transaction.
Definition txid.h:14
#define LOCK(cs)
Definition sync.h:295
Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params &params, BlockValidationOptions validationOptions)
Functions for validating blocks and updating the block tree.
assert(!tx.IsCoinBase())