Bitcoin Core 28.99.0
P2P Digital Currency
Classes | Public Types | Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Protected Attributes | Private Types | Private Member Functions | Friends | List of all members
CTxMemPool Class Reference

CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the next block. More...

#include <txmempool.h>

Inheritance diagram for CTxMemPool:
[legend]
Collaboration diagram for CTxMemPool:
[legend]

Classes

class  ChangeSet
 
struct  CTxMemPoolEntry_Indices
 
struct  delta_info
 

Public Types

typedef boost::multi_index_container< CTxMemPoolEntry, CTxMemPoolEntry_Indicesindexed_transaction_set
 
using txiter = indexed_transaction_set::nth_index< 0 >::type::const_iterator
 
typedef std::set< txiter, CompareIteratorByHashsetEntries
 
using Limits = kernel::MemPoolLimits
 
using Options = kernel::MemPoolOptions
 

Public Member Functions

indexed_transaction_set mapTx GUARDED_BY (cs)
 
std::vector< CTransactionRef > txns_randomized GUARDED_BY (cs)
 All transactions in mapTx, in random order. More...
 
uint64_t CalculateDescendantMaximum (txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 
indirectmap< COutPoint, const CTransaction * > mapNextTx GUARDED_BY (cs)
 
std::map< uint256, CAmount > mapDeltas GUARDED_BY (cs)
 
 CTxMemPool (Options opts, bilingual_str &error)
 Create a new CTxMemPool. More...
 
void removeForReorg (CChain &chain, std::function< bool(txiter)> filter_final_and_mature) EXCLUSIVE_LOCKS_REQUIRED(cs
 After reorg, filter the entries that would no longer be valid in the next block, and update the entries' cached LockPoints if needed. More...
 
void removeConflicts (const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
void removeForBlock (const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Called when a block is connected. More...
 
bool CompareDepthAndScore (const uint256 &hasha, const uint256 &hashb, bool wtxid=false)
 
bool isSpent (const COutPoint &outpoint) const
 
unsigned int GetTransactionsUpdated () const
 
void AddTransactionsUpdated (unsigned int n)
 
bool HasNoInputsOf (const CTransaction &tx) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Check that none of this transactions inputs are in the mempool, and thus the tx is not dependent on other mempool transactions to be included in a block. More...
 
void PrioritiseTransaction (const uint256 &hash, const CAmount &nFeeDelta)
 Affect CreateNewBlock prioritisation of transactions. More...
 
void ApplyDelta (const uint256 &hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 
void ClearPrioritisation (const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
std::vector< delta_infoGetPrioritisedTransactions () const EXCLUSIVE_LOCKS_REQUIRED(!cs)
 Return a vector of all entries in mapDeltas with their corresponding delta_info. More...
 
const CTransactionGetConflictTx (const COutPoint &prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Get the transaction in the pool that spends the same prevout. More...
 
std::optional< txiterGetIter (const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Returns an iterator to the given hash, if found. More...
 
setEntries GetIterSet (const std::set< Txid > &hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Translate a set of hashes into a set of pool iterators to avoid repeated lookups. More...
 
std::vector< txiterGetIterVec (const std::vector< uint256 > &txids) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Translate a list of hashes into a list of mempool iterators to avoid repeated lookups. More...
 
void UpdateTransactionsFromBlock (const std::vector< uint256 > &vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs
 UpdateTransactionsFromBlock is called when adding transactions from a disconnected block back to the mempool, new mempool entries may have children in the mempool (which is generally not the case when otherwise adding transactions). More...
 
void cs_main LOCKS_EXCLUDED (m_epoch)
 
util::Result< setEntriesCalculateMemPoolAncestors (const CTxMemPoolEntry &entry, const Limits &limits, bool fSearchForParents=true) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Try to calculate all in-mempool ancestors of entry. More...
 
setEntries AssumeCalculateMemPoolAncestors (std::string_view calling_fn_name, const CTxMemPoolEntry &entry, const Limits &limits, bool fSearchForParents=true) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Same as CalculateMemPoolAncestors, but always returns a (non-optional) setEntries. More...
 
std::vector< txiterGatherClusters (const std::vector< uint256 > &txids) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Collect the entire cluster of connected transactions for each transaction in txids. More...
 
util::Result< void > CheckPackageLimits (const Package &package, int64_t total_vsize) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Calculate all in-mempool ancestors of a set of transactions not already in the mempool and check ancestor and descendant limits. More...
 
void CalculateDescendants (txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Populate setDescendants with all in-mempool descendants of hash. More...
 
CFeeRate GetMinFee () const
 The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions. More...
 
void TrimToSize (size_t sizelimit, std::vector< COutPoint > *pvNoSpendsRemaining=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Remove transactions from the mempool until its dynamic size is <= sizelimit. More...
 
int Expire (std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Expire all transaction (and their dependencies) in the mempool older than time. More...
 
void GetTransactionAncestry (const uint256 &txid, size_t &ancestors, size_t &descendants, size_t *ancestorsize=nullptr, CAmount *ancestorfees=nullptr) const
 Calculate the ancestor and descendant count for the given transaction. More...
 
bool GetLoadTried () const
 
void SetLoadTried (bool load_tried)
 Set whether or not an initial attempt to load the persisted mempool was made (regardless of whether the attempt was successful or not) More...
 
unsigned long size () const
 
uint64_t GetTotalTxSize () const EXCLUSIVE_LOCKS_REQUIRED(cs)
 
CAmount GetTotalFee () const EXCLUSIVE_LOCKS_REQUIRED(cs)
 
bool exists (const GenTxid &gtxid) const
 
const CTxMemPoolEntryGetEntry (const Txid &txid) const LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(cs)
 
CTransactionRef get (const uint256 &hash) const
 
txiter get_iter_from_wtxid (const uint256 &wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 
TxMempoolInfo info (const GenTxid &gtxid) const
 
TxMempoolInfo info_for_relay (const GenTxid &gtxid, uint64_t last_sequence) const
 Returns info for a transaction if its entry_sequence < last_sequence. More...
 
std::vector< CTxMemPoolEntryRefentryAll () const EXCLUSIVE_LOCKS_REQUIRED(cs)
 
std::vector< TxMempoolInfoinfoAll () const
 
size_t DynamicMemoryUsage () const
 
void AddUnbroadcastTx (const uint256 &txid)
 Adds a transaction to the unbroadcast set. More...
 
void RemoveUnbroadcastTx (const uint256 &txid, const bool unchecked=false)
 Removes a transaction from the unbroadcast set. More...
 
std::set< uint256GetUnbroadcastTxs () const
 Returns transactions in unbroadcast set. More...
 
bool IsUnbroadcastTx (const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Returns whether a txid is in the unbroadcast set. More...
 
uint64_t GetAndIncrementSequence () const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Guards this internal counter for external reporting. More...
 
uint64_t GetSequence () const EXCLUSIVE_LOCKS_REQUIRED(cs)
 
std::optional< std::string > CheckConflictTopology (const setEntries &direct_conflicts)
 
bool visited (const txiter it) const EXCLUSIVE_LOCKS_REQUIRED(cs
 visited marks a CTxMemPoolEntry as having been traversed during the lifetime of the most recently created Epoch::Guard and returns false if we are the first visitor, true otherwise. More...
 
bool visited (std::optional< txiter > it) const EXCLUSIVE_LOCKS_REQUIRED(cs
 
std::unique_ptr< ChangeSetGetChangeSet () EXCLUSIVE_LOCKS_REQUIRED(cs)
 
bool m_have_changeset GUARDED_BY (cs)
 

Public Attributes

RecursiveMutex cs
 This mutex needs to be locked when accessing mapTx or other members that are guarded by it. More...
 
const Options m_opts
 
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void removeRecursive (const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
 If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transactions that spend the same inputs, all inputs are in the mapNextTx array). More...
 
void cs_main
 
bool m_epoch
 
return !it visitedit
 

Static Public Attributes

static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12
 

Protected Member Functions

uint64_t totalTxSize GUARDED_BY (cs)
 
CAmount m_total_fee GUARDED_BY (cs)
 sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discounted. Defined in BIP 141. More...
 
uint64_t cachedInnerUsage GUARDED_BY (cs)
 sum of all mempool tx's fees (NOT modified fee) More...
 
int64_t lastRollingFeeUpdate GUARDED_BY (cs)
 sum of dynamic memory usage of all the map elements (NOT the maps themselves) More...
 
bool blockSinceLastRollingFeeBump GUARDED_BY (cs)
 
double rollingMinimumFeeRate GUARDED_BY (cs)
 
Epoch m_epoch GUARDED_BY (cs)
 minimum fee to get into the pool, decreases exponentially More...
 
uint64_t m_sequence_number GUARDED_BY (cs)
 
void trackPackageRemoved (const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
bool m_load_tried GUARDED_BY (cs)
 
CFeeRate GetMinFee (size_t sizelimit) const
 

Protected Attributes

std::atomic< unsigned int > nTransactionsUpdated {0}
 Used by getblocktemplate to trigger CreateNewBlock() invocation. More...
 

Private Types

typedef std::map< txiter, setEntries, CompareIteratorByHashcacheMap
 

Private Member Functions

void UpdateParent (txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
void UpdateChild (txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
std::vector< indexed_transaction_set::const_iterator > GetSortedDepthAndScore () const EXCLUSIVE_LOCKS_REQUIRED(cs)
 
std::set< uint256 > m_unbroadcast_txids GUARDED_BY (cs)
 Track locally submitted transactions to periodically retry initial broadcast. More...
 
util::Result< setEntriesCalculateAncestorsAndCheckLimits (int64_t entry_size, size_t entry_count, CTxMemPoolEntry::Parents &staged_ancestors, const Limits &limits) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Helper function to calculate all in-mempool ancestors of staged_ancestors and apply ancestor and descendant limits (including staged_ancestors themselves, entry_size and entry_count). More...
 
void RemoveStaged (setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Remove a set of transactions from the mempool. More...
 
void UpdateForDescendants (txiter updateIt, cacheMap &cachedDescendants, const std::set< uint256 > &setExclude, std::set< uint256 > &descendants_to_remove) EXCLUSIVE_LOCKS_REQUIRED(cs)
 UpdateForDescendants is used by UpdateTransactionsFromBlock to update the descendants for a single transaction that has been added to the mempool but may have child transactions in the mempool, eg during a chain reorg. More...
 
void UpdateAncestorsOf (bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Update ancestors of hash to add/remove it as a descendant transaction. More...
 
void UpdateEntryForAncestors (txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Set ancestor state for an entry. More...
 
void UpdateForRemoveFromMempool (const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs)
 For each transaction being removed, update ancestors and any direct children. More...
 
void UpdateChildrenForRemoval (txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Sever link between specified transaction and direct children. More...
 
void removeUnchecked (txiter entry, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Before calling removeUnchecked for a given transaction, UpdateForRemoveFromMempool must be called on the entire (dependent) set of transactions being removed at the same time. More...
 
void Apply (CTxMemPool::ChangeSet *changeset) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
void addNewTransaction (CTxMemPool::txiter it) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
void addNewTransaction (CTxMemPool::txiter it, CTxMemPool::setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
 

Friends

class CTxMemPool::ChangeSet
 

Detailed Description

CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the next block.

Transactions are added when they are seen on the network (or created by the local node), but not all transactions seen are added to the pool. For example, the following new transactions will not be added to the mempool:

CTxMemPool::mapTx, and CTxMemPoolEntry bookkeeping:

mapTx is a boost::multi_index that sorts the mempool on 5 criteria:

Note: the term "descendant" refers to in-mempool transactions that depend on this one, while "ancestor" refers to in-mempool transactions that a given transaction depends on.

In order for the feerate sort to remain correct, we must update transactions in the mempool when new descendants arrive. To facilitate this, we track the set of in-mempool direct parents and direct children in mapLinks. Within each CTxMemPoolEntry, we track the size and fees of all descendants.

Usually when a new transaction is added to the mempool, it has no in-mempool children (because any such children would be an orphan). So in addNewTransaction(), we:

When a transaction is removed from the mempool, we must:

These happen in UpdateForRemoveFromMempool(). (Note that when removing a transaction along with its descendants, we must calculate that set of transactions to be removed before doing the removal, or else the mempool can be in an inconsistent state where it's impossible to walk the ancestors of a transaction.)

In the event of a reorg, the assumption that a newly added tx has no in-mempool children is false. In particular, the mempool is in an inconsistent state while new transactions are being added, because there may be descendant transactions of a tx coming from a disconnected block that are unreachable from just looking at transactions in the mempool (the linking transactions may also be in the disconnected block, waiting to be added). Because of this, there's not much benefit in trying to search for in-mempool children in addNewTransaction(). Instead, in the special case of transactions being added from a disconnected block, we require the caller to clean up the state, to account for in-mempool, out-of-block descendants for all the in-block transactions by calling UpdateTransactionsFromBlock(). Note that until this is called, the mempool state is not consistent, and in particular mapLinks may not be correct (and therefore functions like CalculateMemPoolAncestors() and CalculateDescendants() that rely on them to walk the mempool are not generally safe to use).

Computational limits:

Updating all in-mempool ancestors of a newly added transaction can be slow, if no bound exists on how many in-mempool ancestors there may be. CalculateMemPoolAncestors() takes configurable limits that are designed to prevent these calculations from being too CPU intensive.

Definition at line 303 of file txmempool.h.

Member Typedef Documentation

◆ cacheMap

Definition at line 402 of file txmempool.h.

◆ indexed_transaction_set

Definition at line 364 of file txmempool.h.

◆ Limits

Definition at line 398 of file txmempool.h.

◆ Options

Definition at line 437 of file txmempool.h.

◆ setEntries

Definition at line 396 of file txmempool.h.

◆ txiter

using CTxMemPool::txiter = indexed_transaction_set::nth_index<0>::type::const_iterator

Definition at line 393 of file txmempool.h.

Constructor & Destructor Documentation

◆ CTxMemPool()

CTxMemPool::CTxMemPool ( Options  opts,
bilingual_str error 
)
explicit

Create a new CTxMemPool.

Sanity checks will be off by default for performance, because otherwise accepting transactions becomes O(N^2) where N is the number of transactions in the pool.

Definition at line 414 of file txmempool.cpp.

Member Function Documentation

◆ addNewTransaction() [1/2]

void CTxMemPool::addNewTransaction ( CTxMemPool::txiter  it)
private

Definition at line 471 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addNewTransaction() [2/2]

void CTxMemPool::addNewTransaction ( CTxMemPool::txiter  it,
CTxMemPool::setEntries setAncestors 
)
private

Definition at line 477 of file txmempool.cpp.

Here is the call graph for this function:

◆ AddTransactionsUpdated()

void CTxMemPool::AddTransactionsUpdated ( unsigned int  n)

Definition at line 430 of file txmempool.cpp.

◆ AddUnbroadcastTx()

void CTxMemPool::AddUnbroadcastTx ( const uint256 txid)
inline

Adds a transaction to the unbroadcast set.

Definition at line 675 of file txmempool.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Apply()

void CTxMemPool::Apply ( CTxMemPool::ChangeSet changeset)
private

Definition at line 435 of file txmempool.cpp.

Here is the call graph for this function:

◆ ApplyDelta()

void CTxMemPool::ApplyDelta ( const uint256 hash,
CAmount nFeeDelta 
) const

Definition at line 949 of file txmempool.cpp.

Here is the call graph for this function:

◆ AssumeCalculateMemPoolAncestors()

CTxMemPool::setEntries CTxMemPool::AssumeCalculateMemPoolAncestors ( std::string_view  calling_fn_name,
const CTxMemPoolEntry entry,
const Limits limits,
bool  fSearchForParents = true 
) const

Same as CalculateMemPoolAncestors, but always returns a (non-optional) setEntries.

Should only be used when it is assumed CalculateMemPoolAncestors would not fail. If CalculateMemPoolAncestors does unexpectedly fail, an empty setEntries is returned and the error is logged to BCLog::MEMPOOL with level BCLog::Level::Error. In debug builds, failure of CalculateMemPoolAncestors will lead to shutdown due to assertion failure.

Parameters
[in]calling_fn_nameName of calling function so we can properly log the call site
Returns
a setEntries corresponding to the result of CalculateMemPoolAncestors or an empty setEntries if it failed
See also
CTXMemPool::CalculateMemPoolAncestors()

Definition at line 275 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CalculateAncestorsAndCheckLimits()

util::Result< CTxMemPool::setEntries > CTxMemPool::CalculateAncestorsAndCheckLimits ( int64_t  entry_size,
size_t  entry_count,
CTxMemPoolEntry::Parents staged_ancestors,
const Limits limits 
) const
private

Helper function to calculate all in-mempool ancestors of staged_ancestors and apply ancestor and descendant limits (including staged_ancestors themselves, entry_size and entry_count).

Parameters
[in]entry_sizeVirtual size to include in the limits.
[in]entry_countHow many entries to include in the limits.
[in]staged_ancestorsShould contain entries in the mempool.
[in]limitsMaximum number and size of ancestors and descendants
Returns
all in-mempool ancestors, or an error if any ancestor or descendant limits were hit

Definition at line 163 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CalculateDescendantMaximum()

uint64_t CTxMemPool::CalculateDescendantMaximum ( txiter  entry) const

Definition at line 1202 of file txmempool.cpp.

Here is the caller graph for this function:

◆ CalculateDescendants()

void CTxMemPool::CalculateDescendants ( txiter  it,
setEntries setDescendants 
) const

Populate setDescendants with all in-mempool descendants of hash.

Assumes that setDescendants includes all in-mempool descendants of anything already in it.

Definition at line 571 of file txmempool.cpp.

Here is the caller graph for this function:

◆ CalculateMemPoolAncestors()

util::Result< CTxMemPool::setEntries > CTxMemPool::CalculateMemPoolAncestors ( const CTxMemPoolEntry entry,
const Limits limits,
bool  fSearchForParents = true 
) const

Try to calculate all in-mempool ancestors of entry.

(these are all calculated including the tx itself)

Parameters
[in]entryCTxMemPoolEntry of which all in-mempool ancestors are calculated
[in]limitsMaximum number and size of ancestors and descendants
[in]fSearchForParentsWhether to search a tx's vin for in-mempool parents, or look up parents from mapLinks. Must be true for entries not in the mempool
Returns
all in-mempool ancestors, or an error if any ancestor or descendant limits were hit

Definition at line 243 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CheckConflictTopology()

std::optional< std::string > CTxMemPool::CheckConflictTopology ( const setEntries direct_conflicts)

Definition at line 1275 of file txmempool.cpp.

Here is the caller graph for this function:

◆ CheckPackageLimits()

util::Result< void > CTxMemPool::CheckPackageLimits ( const Package package,
int64_t  total_vsize 
) const

Calculate all in-mempool ancestors of a set of transactions not already in the mempool and check ancestor and descendant limits.

Heuristics are used to estimate the ancestor and descendant count of all entries if the package were to be added to the mempool. The limits are applied to the union of all package transactions. For example, if the package has 3 transactions and limits.ancestor_count = 25, the union of all 3 sets of ancestors (including the transactions themselves) must be <= 22.

Parameters
[in]packageTransaction package being evaluated for acceptance to mempool. The transactions need not be direct ancestors/descendants of each other.
[in]total_vsizeSum of virtual sizes for all transactions in package.
Returns
{} or the error reason if a limit is hit.

Definition at line 205 of file txmempool.cpp.

Here is the call graph for this function:

◆ ClearPrioritisation()

void CTxMemPool::ClearPrioritisation ( const uint256 hash)

Definition at line 959 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CompareDepthAndScore()

bool CTxMemPool::CompareDepthAndScore ( const uint256 hasha,
const uint256 hashb,
bool  wtxid = false 
)

Definition at line 797 of file txmempool.cpp.

Here is the call graph for this function:

◆ DynamicMemoryUsage()

size_t CTxMemPool::DynamicMemoryUsage ( ) const

Definition at line 1063 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ entryAll()

std::vector< CTxMemPoolEntryRef > CTxMemPool::entryAll ( ) const

Definition at line 851 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ exists()

bool CTxMemPool::exists ( const GenTxid gtxid) const
inline

Definition at line 647 of file txmempool.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Expire()

int CTxMemPool::Expire ( std::chrono::seconds  time)

Expire all transaction (and their dependencies) in the mempool older than time.

Return the number of removed transactions.

Definition at line 1086 of file txmempool.cpp.

Here is the call graph for this function:

◆ GatherClusters()

std::vector< CTxMemPool::txiter > CTxMemPool::GatherClusters ( const std::vector< uint256 > &  txids) const

Collect the entire cluster of connected transactions for each transaction in txids.

All txids must correspond to transaction entries in the mempool, otherwise this returns an empty vector. This call will also exit early and return an empty vector if it collects 500 or more transactions as a DoS protection.

Definition at line 1248 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get()

CTransactionRef CTxMemPool::get ( const uint256 hash) const

Definition at line 884 of file txmempool.cpp.

Here is the caller graph for this function:

◆ get_iter_from_wtxid()

txiter CTxMemPool::get_iter_from_wtxid ( const uint256 wtxid) const
inline

Definition at line 659 of file txmempool.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetAndIncrementSequence()

uint64_t CTxMemPool::GetAndIncrementSequence ( ) const
inline

Guards this internal counter for external reporting.

Definition at line 701 of file txmempool.h.

Here is the caller graph for this function:

◆ GetChangeSet()

std::unique_ptr< ChangeSet > CTxMemPool::GetChangeSet ( )
inline

Definition at line 884 of file txmempool.h.

Here is the caller graph for this function:

◆ GetConflictTx()

const CTransaction * CTxMemPool::GetConflictTx ( const COutPoint prevout) const

Get the transaction in the pool that spends the same prevout.

Definition at line 981 of file txmempool.cpp.

Here is the caller graph for this function:

◆ GetEntry()

const CTxMemPoolEntry * CTxMemPool::GetEntry ( const Txid txid) const

Definition at line 877 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetIter()

std::optional< CTxMemPool::txiter > CTxMemPool::GetIter ( const uint256 txid) const

Returns an iterator to the given hash, if found.

Definition at line 987 of file txmempool.cpp.

Here is the caller graph for this function:

◆ GetIterSet()

CTxMemPool::setEntries CTxMemPool::GetIterSet ( const std::set< Txid > &  hashes) const

Translate a set of hashes into a set of pool iterators to avoid repeated lookups.

Does not require that all of the hashes correspond to actual transactions in the mempool, only returns the ones that exist.

Definition at line 994 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetIterVec()

std::vector< CTxMemPool::txiter > CTxMemPool::GetIterVec ( const std::vector< uint256 > &  txids) const

Translate a list of hashes into a list of mempool iterators to avoid repeated lookups.

The nth element in txids becomes the nth element in the returned vector. If any of the txids don't actually exist in the mempool, returns an empty vector.

Definition at line 1004 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetLoadTried()

bool CTxMemPool::GetLoadTried ( ) const
Returns
true if an initial attempt to load the persisted mempool was made, regardless of whether the attempt was successful or not

Definition at line 1236 of file txmempool.cpp.

Here is the caller graph for this function:

◆ GetMinFee() [1/2]

CFeeRate CTxMemPool::GetMinFee ( ) const
inline

The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions.

The m_incremental_relay_feerate policy variable is used to bound the time it takes the fee rate to go back down all the way to 0. When the feerate would otherwise be half of this, it is set to 0 instead.

Definition at line 596 of file txmempool.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetMinFee() [2/2]

CFeeRate CTxMemPool::GetMinFee ( size_t  sizelimit) const
protected

Definition at line 1126 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetPrioritisedTransactions()

std::vector< CTxMemPool::delta_info > CTxMemPool::GetPrioritisedTransactions ( ) const

Return a vector of all entries in mapDeltas with their corresponding delta_info.

Definition at line 965 of file txmempool.cpp.

Here is the caller graph for this function:

◆ GetSequence()

uint64_t CTxMemPool::GetSequence ( ) const
inline

Definition at line 705 of file txmempool.h.

Here is the caller graph for this function:

◆ GetSortedDepthAndScore()

std::vector< CTxMemPool::indexed_transaction_set::const_iterator > CTxMemPool::GetSortedDepthAndScore ( ) const
private

Definition at line 833 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetTotalFee()

CAmount CTxMemPool::GetTotalFee ( ) const
inline

Definition at line 641 of file txmempool.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetTotalTxSize()

uint64_t CTxMemPool::GetTotalTxSize ( ) const
inline

Definition at line 635 of file txmempool.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetTransactionAncestry()

void CTxMemPool::GetTransactionAncestry ( const uint256 txid,
size_t &  ancestors,
size_t &  descendants,
size_t *  ancestorsize = nullptr,
CAmount ancestorfees = nullptr 
) const

Calculate the ancestor and descendant count for the given transaction.

The counts include the transaction itself. When ancestors is non-zero (ie, the transaction itself is in the mempool), ancestorsize and ancestorfees will also be set to the appropriate values.

Definition at line 1224 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetTransactionsUpdated()

unsigned int CTxMemPool::GetTransactionsUpdated ( ) const

Definition at line 425 of file txmempool.cpp.

Here is the caller graph for this function:

◆ GetUnbroadcastTxs()

std::set< uint256 > CTxMemPool::GetUnbroadcastTxs ( ) const
inline

Returns transactions in unbroadcast set.

Definition at line 687 of file txmempool.h.

Here is the caller graph for this function:

◆ GUARDED_BY() [1/15]

uint64_t totalTxSize CTxMemPool::GUARDED_BY ( cs  )
inlineprotected

Definition at line 308 of file txmempool.h.

◆ GUARDED_BY() [2/15]

CAmount m_total_fee CTxMemPool::GUARDED_BY ( cs  )
inlineprotected

sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discounted. Defined in BIP 141.

Definition at line 309 of file txmempool.h.

◆ GUARDED_BY() [3/15]

uint64_t cachedInnerUsage CTxMemPool::GUARDED_BY ( cs  )
inlineprotected

sum of all mempool tx's fees (NOT modified fee)

Definition at line 310 of file txmempool.h.

◆ GUARDED_BY() [4/15]

int64_t lastRollingFeeUpdate CTxMemPool::GUARDED_BY ( cs  )
inlinemutableprotected

sum of dynamic memory usage of all the map elements (NOT the maps themselves)

Definition at line 312 of file txmempool.h.

Here is the call graph for this function:

◆ GUARDED_BY() [5/15]

bool blockSinceLastRollingFeeBump CTxMemPool::GUARDED_BY ( cs  )
inlinemutableprotected

Definition at line 313 of file txmempool.h.

◆ GUARDED_BY() [6/15]

double rollingMinimumFeeRate CTxMemPool::GUARDED_BY ( cs  )
inlinemutableprotected

Definition at line 314 of file txmempool.h.

◆ GUARDED_BY() [7/15]

Epoch m_epoch CTxMemPool::GUARDED_BY ( cs  )
inlinemutableprotected

minimum fee to get into the pool, decreases exponentially

Definition at line 315 of file txmempool.h.

◆ GUARDED_BY() [8/15]

uint64_t m_sequence_number CTxMemPool::GUARDED_BY ( cs  )
inlinemutableprotected

Definition at line 320 of file txmempool.h.

◆ GUARDED_BY() [9/15]

bool m_load_tried CTxMemPool::GUARDED_BY ( cs  )
inlineprotected

Definition at line 324 of file txmempool.h.

◆ GUARDED_BY() [10/15]

indexed_transaction_set mapTx CTxMemPool::GUARDED_BY ( cs  )

◆ GUARDED_BY() [11/15]

std::vector< CTransactionRef > txns_randomized CTxMemPool::GUARDED_BY ( cs  )

All transactions in mapTx, in random order.

◆ GUARDED_BY() [12/15]

std::set< uint256 > m_unbroadcast_txids CTxMemPool::GUARDED_BY ( cs  )
private

Track locally submitted transactions to periodically retry initial broadcast.

◆ GUARDED_BY() [13/15]

indirectmap< COutPoint, const CTransaction * > mapNextTx CTxMemPool::GUARDED_BY ( cs  )

◆ GUARDED_BY() [14/15]

std::map< uint256, CAmount > mapDeltas CTxMemPool::GUARDED_BY ( cs  )

◆ GUARDED_BY() [15/15]

bool m_have_changeset CTxMemPool::GUARDED_BY ( cs  )
inline

Definition at line 890 of file txmempool.h.

◆ HasNoInputsOf()

bool CTxMemPool::HasNoInputsOf ( const CTransaction tx) const

Check that none of this transactions inputs are in the mempool, and thus the tx is not dependent on other mempool transactions to be included in a block.

Definition at line 1017 of file txmempool.cpp.

Here is the call graph for this function:

◆ info()

TxMempoolInfo CTxMemPool::info ( const GenTxid gtxid) const

Definition at line 893 of file txmempool.cpp.

Here is the call graph for this function:

◆ info_for_relay()

TxMempoolInfo CTxMemPool::info_for_relay ( const GenTxid gtxid,
uint64_t  last_sequence 
) const

Returns info for a transaction if its entry_sequence < last_sequence.

Definition at line 902 of file txmempool.cpp.

Here is the call graph for this function:

◆ infoAll()

std::vector< TxMempoolInfo > CTxMemPool::infoAll ( ) const

Definition at line 863 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isSpent()

bool CTxMemPool::isSpent ( const COutPoint outpoint) const

Definition at line 419 of file txmempool.cpp.

Here is the caller graph for this function:

◆ IsUnbroadcastTx()

bool CTxMemPool::IsUnbroadcastTx ( const uint256 txid) const
inline

Returns whether a txid is in the unbroadcast set.

Definition at line 694 of file txmempool.h.

Here is the call graph for this function:

◆ LOCKS_EXCLUDED()

void cs_main CTxMemPool::LOCKS_EXCLUDED ( m_epoch  )

◆ PrioritiseTransaction()

void CTxMemPool::PrioritiseTransaction ( const uint256 hash,
const CAmount nFeeDelta 
)

Affect CreateNewBlock prioritisation of transactions.

Definition at line 913 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeConflicts()

void CTxMemPool::removeConflicts ( const CTransaction tx)

Definition at line 647 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeForBlock()

void CTxMemPool::removeForBlock ( const std::vector< CTransactionRef > &  vtx,
unsigned int  nBlockHeight 
)

Called when a block is connected.

Removes from mempool.

Definition at line 667 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeForReorg()

void CTxMemPool::removeForReorg ( CChain chain,
std::function< bool(txiter)>  filter_final_and_mature 
)

After reorg, filter the entries that would no longer be valid in the next block, and update the entries' cached LockPoints if needed.

The mempool does not have any knowledge of consensus rules. It just applies the callable function and removes the ones for which it returns true.

Parameters
[in]filter_final_and_maturePredicate that checks the relevant validation rules and updates an entry's LockPoints.

Definition at line 626 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RemoveStaged()

void CTxMemPool::RemoveStaged ( setEntries stage,
bool  updateDescendants,
MemPoolRemovalReason  reason 
)
private

Remove a set of transactions from the mempool.

If a transaction is in this set, then all in-mempool descendants must also be in the set, unless this transaction is being removed for being in a block. Set updateDescendants to true when removing a tx that was in a block, so that any in-mempool descendants have their ancestor state updated.

Definition at line 1078 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RemoveUnbroadcastTx()

void CTxMemPool::RemoveUnbroadcastTx ( const uint256 txid,
const bool  unchecked = false 
)

Removes a transaction from the unbroadcast set.

Definition at line 1069 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeUnchecked()

void CTxMemPool::removeUnchecked ( txiter  entry,
MemPoolRemovalReason  reason 
)
private

Before calling removeUnchecked for a given transaction, UpdateForRemoveFromMempool must be called on the entire (dependent) set of transactions being removed at the same time.

We use each CTxMemPoolEntry's m_parents in order to walk ancestors of a given transaction that is removed, so we can't remove intermediate transactions in a chain before we've updated all the state for the removal.

Definition at line 520 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetLoadTried()

void CTxMemPool::SetLoadTried ( bool  load_tried)

Set whether or not an initial attempt to load the persisted mempool was made (regardless of whether the attempt was successful or not)

Definition at line 1242 of file txmempool.cpp.

◆ size()

unsigned long CTxMemPool::size ( ) const
inline

Definition at line 629 of file txmempool.h.

Here is the caller graph for this function:

◆ trackPackageRemoved()

void CTxMemPool::trackPackageRemoved ( const CFeeRate rate)
protected

Definition at line 1150 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TrimToSize()

void CTxMemPool::TrimToSize ( size_t  sizelimit,
std::vector< COutPoint > *  pvNoSpendsRemaining = nullptr 
)

Remove transactions from the mempool until its dynamic size is <= sizelimit.

pvNoSpendsRemaining, if set, will be populated with the list of outpoints which are not in mempool which no longer have any spends in this mempool.

Definition at line 1158 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdateAncestorsOf()

void CTxMemPool::UpdateAncestorsOf ( bool  add,
txiter  hash,
setEntries setAncestors 
)
private

Update ancestors of hash to add/remove it as a descendant transaction.

Definition at line 289 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdateChild()

void CTxMemPool::UpdateChild ( txiter  entry,
txiter  child,
bool  add 
)
private

Definition at line 1104 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdateChildrenForRemoval()

void CTxMemPool::UpdateChildrenForRemoval ( txiter  entry)
private

Sever link between specified transaction and direct children.

Definition at line 318 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdateEntryForAncestors()

void CTxMemPool::UpdateEntryForAncestors ( txiter  it,
const setEntries setAncestors 
)
private

Set ancestor state for an entry.

Definition at line 304 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdateForDescendants()

void CTxMemPool::UpdateForDescendants ( txiter  updateIt,
cacheMap cachedDescendants,
const std::set< uint256 > &  setExclude,
std::set< uint256 > &  descendants_to_remove 
)
private

UpdateForDescendants is used by UpdateTransactionsFromBlock to update the descendants for a single transaction that has been added to the mempool but may have child transactions in the mempool, eg during a chain reorg.

Precondition
CTxMemPoolEntry::m_children is correct for the given tx and all descendants.
cachedDescendants is an accurate cache where each entry has all descendants of the corresponding key, including those that should be removed for violation of ancestor limits.
Postcondition
if updateIt has any non-excluded descendants, cachedDescendants has a new cache line for updateIt.
descendants_to_remove has a new entry for any descendant which exceeded ancestor limits relative to updateIt.
Parameters
[in]updateItthe entry to update for its descendants
[in,out]cachedDescendantsa cache where each line corresponds to all descendants. It will be updated with the descendants of the transaction being updated, so that future invocations don't need to walk the same transaction again, if encountered in another transaction chain.
[in]setExcludethe set of descendant transactions in the mempool that must not be accounted for (because any descendants in setExclude were added to the mempool after the transaction being updated and hence their state is already reflected in the parent state).
[out]descendants_to_removePopulated with the txids of entries that exceed ancestor limits. It's the responsibility of the caller to removeRecursive them.

Definition at line 57 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdateForRemoveFromMempool()

void CTxMemPool::UpdateForRemoveFromMempool ( const setEntries entriesToRemove,
bool  updateDescendants 
)
private

For each transaction being removed, update ancestors and any direct children.

If updateDescendants is true, then also update in-mempool descendants' ancestor state.

Definition at line 326 of file txmempool.cpp.

Here is the caller graph for this function:

◆ UpdateParent()

void CTxMemPool::UpdateParent ( txiter  entry,
txiter  parent,
bool  add 
)
private

Definition at line 1115 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdateTransactionsFromBlock()

void CTxMemPool::UpdateTransactionsFromBlock ( const std::vector< uint256 > &  vHashesToUpdate)

UpdateTransactionsFromBlock is called when adding transactions from a disconnected block back to the mempool, new mempool entries may have children in the mempool (which is generally not the case when otherwise adding transactions).

Postcondition
updated descendant state for descendants of each transaction in vHashesToUpdate (excluding any child transactions present in vHashesToUpdate, which are already accounted for). Updated state includes add fee/size information for such descendants to the parent and updated ancestor state to include the parent.
Parameters
[in]vHashesToUpdateThe set of txids from the disconnected block that have been accepted back into the mempool.

Definition at line 108 of file txmempool.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ visited() [1/2]

bool CTxMemPool::visited ( const txiter  it) const

visited marks a CTxMemPoolEntry as having been traversed during the lifetime of the most recently created Epoch::Guard and returns false if we are the first visitor, true otherwise.

An Epoch::Guard must be held when visited is called or an assert will be triggered.

Here is the caller graph for this function:

◆ visited() [2/2]

bool CTxMemPool::visited ( std::optional< txiter it) const
Here is the call graph for this function:

Friends And Related Function Documentation

◆ CTxMemPool::ChangeSet

friend class CTxMemPool::ChangeSet
friend

Definition at line 892 of file txmempool.h.

Member Data Documentation

◆ cs

RecursiveMutex CTxMemPool::cs
mutable

This mutex needs to be locked when accessing mapTx or other members that are guarded by it.

Consistency guarantees
By design, it is guaranteed that:
  1. Locking both cs_main and mempool.cs will give a view of mempool that is consistent with current chain tip (ActiveChain() and CoinsTip()) and is fully populated. Fully populated means that if the current active chain is missing transactions that were present in a previously active chain, all the missing transactions will have been re-added to the mempool and should be present if they meet size and consistency constraints.
  2. Locking mempool.cs without cs_main will give a view of a mempool consistent with some chain that was active since cs_main was last locked, and that is fully populated as described above. It is ok for code that only needs to query or remove transactions from the mempool to lock just mempool.cs without cs_main.

To provide these guarantees, it is necessary to lock both cs_main and mempool.cs whenever adding transactions to the mempool and whenever changing the chain tip. It's necessary to keep both mutexes locked until the mempool is consistent with the new chain tip and fully populated.

Definition at line 390 of file txmempool.h.

◆ cs_main

void CTxMemPool::cs_main

Definition at line 465 of file txmempool.h.

◆ it

return !it visited* CTxMemPool::it

Definition at line 791 of file txmempool.h.

◆ m_epoch

bool CTxMemPool::m_epoch
Initial value:
{
return m_epoch.visited(it->m_epoch_marker)
return !it visited * it
Definition: txmempool.h:791
bool m_epoch
Definition: txmempool.h:784

Definition at line 783 of file txmempool.h.

◆ m_opts

const Options CTxMemPool::m_opts

Definition at line 439 of file txmempool.h.

◆ nTransactionsUpdated

std::atomic<unsigned int> CTxMemPool::nTransactionsUpdated {0}
protected

Used by getblocktemplate to trigger CreateNewBlock() invocation.

Definition at line 306 of file txmempool.h.

◆ removeRecursive

void CTxMemPool::removeRecursive

If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transactions that spend the same inputs, all inputs are in the mapNextTx array).

If sanity-checking is turned off, check does nothing.

Definition at line 457 of file txmempool.h.

◆ ROLLING_FEE_HALFLIFE

const int CTxMemPool::ROLLING_FEE_HALFLIFE = 60 * 60 * 12
static

Definition at line 330 of file txmempool.h.


The documentation for this class was generated from the following files: