Inside Bitcoin
Opening the selected file…
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) 2009-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#ifndef BITCOIN_KERNEL_MEMPOOL_ENTRY_H6#define BITCOIN_KERNEL_MEMPOOL_ENTRY_H8#include <consensus/amount.h>9#include <consensus/validation.h>10#include <core_memusage.h>11#include <policy/policy.h>12#include <policy/settings.h>13#include <primitives/transaction.h>14#include <txgraph.h>15#include <util/overflow.h>16#include <util/time.h>18#include <cstddef>19#include <cstdint>20#include <functional>21#include <memory>22#include <set>24class CBlockIndex;26struct LockPoints {27// Will be set to the blockchain height and median time past28// values that would be necessary to satisfy all relative locktime29// constraints (BIP68) of this tx given our view of block chain history30int height{0};31int64_t time{0};32// As long as the current chain descends from the highest height block33// containing one of the inputs used in the calculation, then the cached34// values are still valid even after a reorg.35CBlockIndex* maxInputBlock{nullptr};36};38struct CompareIteratorByHash {39// SFINAE for T where T is either a pointer type (e.g., a txiter) or a reference_wrapper<T>40// (e.g. a wrapped CTxMemPoolEntry&)41template <typename T>42bool operator()(const std::reference_wrapper<T>& a, const std::reference_wrapper<T>& b) const43{44return a.get().GetTx().GetHash() < b.get().GetTx().GetHash();45}46template <typename T>47bool operator()(const T& a, const T& b) const48{49return a->GetTx().GetHash() < b->GetTx().GetHash();50}51};53/** \class CTxMemPoolEntry54*55* CTxMemPoolEntry stores data about the corresponding transaction, as well56* as data about all in-mempool transactions that depend on the transaction57* ("descendant" transactions).58*59* When a new entry is added to the mempool, we update the descendant state60* (m_count_with_descendants, nSizeWithDescendants, and nModFeesWithDescendants) for61* all ancestors of the newly added transaction.62*63*/65class CTxMemPoolEntry : public TxGraph::Ref66{67public:68typedef std::reference_wrapper<const CTxMemPoolEntry> CTxMemPoolEntryRef;70private:71CTxMemPoolEntry(const CTxMemPoolEntry&) = delete;73const CTransactionRef tx;74const CAmount nFee; //!< Cached to avoid expensive parent-transaction lookups75const int32_t nTxWeight; //!< ... and avoid recomputing tx weight (also used for GetTxSize())76const size_t nUsageSize; //!< ... and total memory usage77const int64_t nTime; //!< Local time when entering the mempool78const uint64_t entry_sequence; //!< Sequence number used to determine whether this transaction is too recent for relay79const unsigned int entryHeight; //!< Chain height when entering the mempool80const bool spendsCoinbase; //!< keep track of transactions that spend a coinbase81const int64_t sigOpCost; //!< Total sigop cost82mutable CAmount m_modified_fee; //!< Used for determining the priority of the transaction for mining in a block83mutable LockPoints lockPoints; //!< Track the height and time at which tx was final85public:86virtual ~CTxMemPoolEntry() = default;87CTxMemPoolEntry(const CTransactionRef& tx, CAmount fee,88int64_t time, unsigned int entry_height, uint64_t entry_sequence,89bool spends_coinbase,90int64_t sigops_cost, LockPoints lp)91: tx{tx},92nFee{fee},93nTxWeight{GetTransactionWeight(*tx)},94nUsageSize{RecursiveDynamicUsage(tx)},95nTime{time},96entry_sequence{entry_sequence},97entryHeight{entry_height},98spendsCoinbase{spends_coinbase},99sigOpCost{sigops_cost},100m_modified_fee{nFee},101lockPoints{lp} {}103CTxMemPoolEntry& operator=(const CTxMemPoolEntry&) = delete;104CTxMemPoolEntry(CTxMemPoolEntry&&) = default;105CTxMemPoolEntry& operator=(CTxMemPoolEntry&&) = delete;107const CTransaction& GetTx() const { return *this->tx; }108CTransactionRef GetSharedTx() const { return this->tx; }109const CAmount& GetFee() const { return nFee; }110int32_t GetTxSize() const111{112return GetVirtualTransactionSize(nTxWeight, sigOpCost, ::nBytesPerSigOp);113}114int32_t GetAdjustedWeight() const { return GetSigOpsAdjustedWeight(nTxWeight, sigOpCost, ::nBytesPerSigOp); }115int32_t GetTxWeight() const { return nTxWeight; }116std::chrono::seconds GetTime() const { return std::chrono::seconds{nTime}; }117unsigned int GetHeight() const { return entryHeight; }118uint64_t GetSequence() const { return entry_sequence; }119int64_t GetSigOpCost() const { return sigOpCost; }120CAmount GetModifiedFee() const { return m_modified_fee; }121size_t DynamicMemoryUsage() const { return nUsageSize; }122const LockPoints& GetLockPoints() const { return lockPoints; }124// Updates the modified fees with descendants/ancestors.125void UpdateModifiedFee(CAmount fee_diff) const126{127m_modified_fee = SaturatingAdd(m_modified_fee, fee_diff);128}130// Update the LockPoints after a reorg131void UpdateLockPoints(const LockPoints& lp) const132{133lockPoints = lp;134}136bool GetSpendsCoinbase() const { return spendsCoinbase; }138mutable size_t idx_randomized; //!< Index in mempool's txns_randomized139};141using CTxMemPoolEntryRef = CTxMemPoolEntry::CTxMemPoolEntryRef;143struct TransactionInfo {144const CTransactionRef m_tx;145/* The fee the transaction paid */146const CAmount m_fee;147/**148* The virtual transaction size.149*150* This is a policy field which considers the sigop cost of the151* transaction as well as its weight, and reinterprets it as bytes.152*153* It is the primary metric by which the mining algorithm selects154* transactions.155*/156const int64_t m_virtual_transaction_size;157/* The block height the transaction entered the mempool */158const unsigned int txHeight;160TransactionInfo(const CTransactionRef& tx, const CAmount& fee, const int64_t vsize, const unsigned int height)161: m_tx{tx},162m_fee{fee},163m_virtual_transaction_size{vsize},164txHeight{height} {}165};167struct RemovedMempoolTransactionInfo {168TransactionInfo info;169explicit RemovedMempoolTransactionInfo(const CTxMemPoolEntry& entry)170: info{entry.GetSharedTx(), entry.GetFee(), entry.GetTxSize(), entry.GetHeight()} {}171};173struct NewMempoolTransactionInfo {174TransactionInfo info;175/*176* This boolean indicates whether the transaction was added177* without enforcing mempool fee limits.178*/179const bool m_mempool_limit_bypassed;180/* This boolean indicates whether the transaction is part of a package. */181const bool m_submitted_in_package;182/*183* This boolean indicates whether the blockchain is up to date when the184* transaction is added to the mempool.185*/186const bool m_chainstate_is_current;187/* Indicates whether the transaction has unconfirmed parents. */188const bool m_has_no_mempool_parents;190explicit NewMempoolTransactionInfo(const CTransactionRef& tx, const CAmount& fee,191const int64_t vsize, const unsigned int height,192const bool mempool_limit_bypassed, const bool submitted_in_package,193const bool chainstate_is_current,194const bool has_no_mempool_parents)195: info{tx, fee, vsize, height},196m_mempool_limit_bypassed{mempool_limit_bypassed},197m_submitted_in_package{submitted_in_package},198m_chainstate_is_current{chainstate_is_current},199m_has_no_mempool_parents{has_no_mempool_parents} {}200};202#endif // BITCOIN_KERNEL_MEMPOOL_ENTRY_H
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.