Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2011-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <bench/bench.h>6#include <chain.h>7#include <chainparams.h>8#include <consensus/consensus.h>9#include <consensus/merkle.h>10#include <consensus/validation.h>11#include <pow.h>12#include <primitives/block.h>13#include <primitives/transaction.h>14#include <random.h>15#include <script/script.h>16#include <sync.h>17#include <test/util/setup_common.h>18#include <uint256.h>19#include <util/check.h>20#include <validation.h>22#include <cstdint>23#include <memory>24#include <string>25#include <utility>26#include <vector>29static void DuplicateInputs(benchmark::Bench& bench)30{31const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();33const CScript SCRIPT_PUB{CScript(OP_TRUE)};35const CChainParams& chainparams = Params();37CBlock block{};38CMutableTransaction coinbaseTx{};39CMutableTransaction naughtyTx{};41LOCK(cs_main);42CBlockIndex* pindexPrev = testing_setup->m_node.chainman->ActiveChain().Tip();43assert(pindexPrev != nullptr);44block.nBits = GetNextWorkRequired(pindexPrev, &block, chainparams.GetConsensus());45block.nNonce = 0;46auto nHeight = pindexPrev->nHeight + 1;48// Make a coinbase TX49coinbaseTx.vin.resize(1);50coinbaseTx.vin[0].prevout.SetNull();51coinbaseTx.vout.resize(1);52coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB;53coinbaseTx.vout[0].nValue = GetBlockSubsidy(nHeight, chainparams.GetConsensus());54coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;57naughtyTx.vout.resize(1);58naughtyTx.vout[0].nValue = 0;59naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB;61uint64_t n_inputs = (((MAX_BLOCK_SERIALIZED_SIZE / WITNESS_SCALE_FACTOR) - (CTransaction(coinbaseTx).ComputeTotalSize() + CTransaction(naughtyTx).ComputeTotalSize())) / 41) - 100;62for (uint64_t x = 0; x < (n_inputs - 1); ++x) {63naughtyTx.vin.emplace_back(Txid::FromUint256(GetRandHash()), 0, CScript(), 0);64}65naughtyTx.vin.emplace_back(naughtyTx.vin.back());67block.vtx.push_back(MakeTransactionRef(std::move(coinbaseTx)));68block.vtx.push_back(MakeTransactionRef(std::move(naughtyTx)));70block.hashMerkleRoot = BlockMerkleRoot(block);72bench.run([&] {73BlockValidationState cvstate{};74assert(!CheckBlock(block, cvstate, chainparams.GetConsensus(), false, false));75assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate");76});77}79BENCHMARK(DuplicateInputs);
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.