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 <kernel/cs_main.h>8#include <policy/policy.h>9#include <primitives/transaction.h>10#include <script/script.h>11#include <sync.h>12#include <test/util/setup_common.h>13#include <test/util/txmempool.h>14#include <txmempool.h>15#include <util/check.h>17#include <cstdint>18#include <memory>19#include <vector>22static void AddTx(const CTransactionRef& tx, const CAmount& nFee, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)23{24int64_t nTime = 0;25unsigned int nHeight = 1;26uint64_t sequence = 0;27bool spendsCoinbase = false;28unsigned int sigOpCost = 4;29LockPoints lp;30TryAddToMempool(pool, CTxMemPoolEntry(31tx, nFee, nTime, nHeight, sequence,32spendsCoinbase, sigOpCost, lp));33}35// Right now this is only testing eviction performance in an extremely small36// mempool. Code needs to be written to generate a much wider variety of37// unique transactions for a more meaningful performance measurement.38static void MempoolEviction(benchmark::Bench& bench)39{40const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();42CMutableTransaction tx1 = CMutableTransaction();43tx1.vin.resize(1);44tx1.vin[0].scriptSig = CScript() << OP_1;45tx1.vin[0].scriptWitness.stack.push_back({1});46tx1.vout.resize(1);47tx1.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;48tx1.vout[0].nValue = 10 * COIN;50CMutableTransaction tx2 = CMutableTransaction();51tx2.vin.resize(1);52tx2.vin[0].scriptSig = CScript() << OP_2;53tx2.vin[0].scriptWitness.stack.push_back({2});54tx2.vout.resize(1);55tx2.vout[0].scriptPubKey = CScript() << OP_2 << OP_EQUAL;56tx2.vout[0].nValue = 10 * COIN;58CMutableTransaction tx3 = CMutableTransaction();59tx3.vin.resize(1);60tx3.vin[0].prevout = COutPoint(tx2.GetHash(), 0);61tx3.vin[0].scriptSig = CScript() << OP_2;62tx3.vin[0].scriptWitness.stack.push_back({3});63tx3.vout.resize(1);64tx3.vout[0].scriptPubKey = CScript() << OP_3 << OP_EQUAL;65tx3.vout[0].nValue = 10 * COIN;67CMutableTransaction tx4 = CMutableTransaction();68tx4.vin.resize(2);69tx4.vin[0].prevout.SetNull();70tx4.vin[0].scriptSig = CScript() << OP_4;71tx4.vin[0].scriptWitness.stack.push_back({4});72tx4.vin[1].prevout.SetNull();73tx4.vin[1].scriptSig = CScript() << OP_4;74tx4.vin[1].scriptWitness.stack.push_back({4});75tx4.vout.resize(2);76tx4.vout[0].scriptPubKey = CScript() << OP_4 << OP_EQUAL;77tx4.vout[0].nValue = 10 * COIN;78tx4.vout[1].scriptPubKey = CScript() << OP_4 << OP_EQUAL;79tx4.vout[1].nValue = 10 * COIN;81CMutableTransaction tx5 = CMutableTransaction();82tx5.vin.resize(2);83tx5.vin[0].prevout = COutPoint(tx4.GetHash(), 0);84tx5.vin[0].scriptSig = CScript() << OP_4;85tx5.vin[0].scriptWitness.stack.push_back({4});86tx5.vin[1].prevout.SetNull();87tx5.vin[1].scriptSig = CScript() << OP_5;88tx5.vin[1].scriptWitness.stack.push_back({5});89tx5.vout.resize(2);90tx5.vout[0].scriptPubKey = CScript() << OP_5 << OP_EQUAL;91tx5.vout[0].nValue = 10 * COIN;92tx5.vout[1].scriptPubKey = CScript() << OP_5 << OP_EQUAL;93tx5.vout[1].nValue = 10 * COIN;95CMutableTransaction tx6 = CMutableTransaction();96tx6.vin.resize(2);97tx6.vin[0].prevout = COutPoint(tx4.GetHash(), 1);98tx6.vin[0].scriptSig = CScript() << OP_4;99tx6.vin[0].scriptWitness.stack.push_back({4});100tx6.vin[1].prevout.SetNull();101tx6.vin[1].scriptSig = CScript() << OP_6;102tx6.vin[1].scriptWitness.stack.push_back({6});103tx6.vout.resize(2);104tx6.vout[0].scriptPubKey = CScript() << OP_6 << OP_EQUAL;105tx6.vout[0].nValue = 10 * COIN;106tx6.vout[1].scriptPubKey = CScript() << OP_6 << OP_EQUAL;107tx6.vout[1].nValue = 10 * COIN;109CMutableTransaction tx7 = CMutableTransaction();110tx7.vin.resize(2);111tx7.vin[0].prevout = COutPoint(tx5.GetHash(), 0);112tx7.vin[0].scriptSig = CScript() << OP_5;113tx7.vin[0].scriptWitness.stack.push_back({5});114tx7.vin[1].prevout = COutPoint(tx6.GetHash(), 0);115tx7.vin[1].scriptSig = CScript() << OP_6;116tx7.vin[1].scriptWitness.stack.push_back({6});117tx7.vout.resize(2);118tx7.vout[0].scriptPubKey = CScript() << OP_7 << OP_EQUAL;119tx7.vout[0].nValue = 10 * COIN;120tx7.vout[1].scriptPubKey = CScript() << OP_7 << OP_EQUAL;121tx7.vout[1].nValue = 10 * COIN;123CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);124LOCK2(cs_main, pool.cs);125// Create transaction references outside the "hot loop"126const CTransactionRef tx1_r{MakeTransactionRef(tx1)};127const CTransactionRef tx2_r{MakeTransactionRef(tx2)};128const CTransactionRef tx3_r{MakeTransactionRef(tx3)};129const CTransactionRef tx4_r{MakeTransactionRef(tx4)};130const CTransactionRef tx5_r{MakeTransactionRef(tx5)};131const CTransactionRef tx6_r{MakeTransactionRef(tx6)};132const CTransactionRef tx7_r{MakeTransactionRef(tx7)};134bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {135AddTx(tx1_r, 10000LL, pool);136AddTx(tx2_r, 5000LL, pool);137AddTx(tx3_r, 20000LL, pool);138AddTx(tx4_r, 7000LL, pool);139AddTx(tx5_r, 1000LL, pool);140AddTx(tx6_r, 1100LL, pool);141AddTx(tx7_r, 9000LL, pool);142pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);143pool.TrimToSize(GetVirtualTransactionSize(*tx1_r));144});145}147BENCHMARK(MempoolEviction);
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.