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 <consensus/amount.h>7#include <consensus/validation.h>8#include <kernel/cs_main.h>9#include <policy/ephemeral_policy.h>10#include <policy/feerate.h>11#include <primitives/transaction.h>12#include <script/script.h>13#include <sync.h>14#include <test/util/setup_common.h>15#include <test/util/txmempool.h>16#include <txmempool.h>17#include <util/check.h>19#include <cstddef>20#include <cstdint>21#include <memory>22#include <vector>25static void AddTx(const CTransactionRef& tx, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)26{27int64_t nTime{0};28unsigned int nHeight{1};29uint64_t sequence{0};30bool spendsCoinbase{false};31unsigned int sigOpCost{4};32uint64_t fee{0};33LockPoints lp;34TryAddToMempool(pool, CTxMemPoolEntry(35tx, fee, nTime, nHeight, sequence,36spendsCoinbase, sigOpCost, lp));37}39static void MempoolCheckEphemeralSpends(benchmark::Bench& bench)40{41const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();43int number_outputs{1000};44if (bench.complexityN() > 1) {45number_outputs = static_cast<int>(bench.complexityN());46}48// Tx with many outputs49CMutableTransaction tx1;50tx1.vin.resize(1);51tx1.vout.resize(number_outputs);52for (size_t i = 0; i < tx1.vout.size(); i++) {53// Each output progressively larger54tx1.vout[i] = CTxOut{CAmount(i) * CENT, CScript()};55}57const auto& parent_txid = tx1.GetHash();59// Spends all outputs of tx1, other details don't matter60CMutableTransaction tx2;61tx2.vin.resize(tx1.vout.size());62for (uint32_t i{0}; i < tx2.vin.size(); ++i) {63tx2.vin[i].prevout = COutPoint{parent_txid, i};64}65tx2.vout.resize(1);67CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);68LOCK2(cs_main, pool.cs);69// Create transaction references outside the "hot loop"70const CTransactionRef tx1_r{MakeTransactionRef(tx1)};71const CTransactionRef tx2_r{MakeTransactionRef(tx2)};73AddTx(tx1_r, pool);74assert(tx2_r->vin.back().prevout == COutPoint(parent_txid, tx1_r->vout.size() - 1));76uint32_t iteration{0};78TxValidationState dummy_state;79Wtxid dummy_wtxid;81bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {83CheckEphemeralSpends({tx2_r}, /*dust_relay_rate=*/CFeeRate(iteration * COIN / 10), pool, dummy_state, dummy_wtxid);84iteration++;85});86}88BENCHMARK(MempoolCheckEphemeralSpends);
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.