6#ifndef BITCOIN_TXMEMPOOL_H
7#define BITCOIN_TXMEMPOOL_H
25#include <boost/multi_index/hashed_index.hpp>
26#include <boost/multi_index/ordered_index.hpp>
27#include <boost/multi_index/sequenced_index.hpp>
28#include <boost/multi_index_container.hpp>
36#include <unordered_map>
55 return entry->GetTx().GetId();
76 return a->GetTime() < b->GetTime();
84 return a->GetEntryId() < b->GetEntryId();
257 typedef boost::multi_index_container<
259 boost::multi_index::indexed_by<
264 boost::multi_index::ordered_non_unique<
265 boost::multi_index::tag<modified_feerate>,
266 boost::multi_index::identity<CTxMemPoolEntryRef>,
269 boost::multi_index::ordered_non_unique<
270 boost::multi_index::tag<entry_time>,
271 boost::multi_index::identity<CTxMemPoolEntryRef>,
274 boost::multi_index::ordered_unique<
275 boost::multi_index::tag<entry_id>,
276 boost::multi_index::identity<CTxMemPoolEntryRef>,
310 using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
387 bool isSpent(const COutPoint &outpoint) const;
436 bool fSearchForParents = true) const
461 std::vector<COutPoint> *pvNoSpendsRemaining =
nullptr)
505 return mapTx.count(txid) != 0;
520 std::vector<TxMempoolInfo>
infoAll()
const;
532 m_unbroadcast_txids.insert(txid);
542 return m_unbroadcast_txids;
548 return (m_unbroadcast_txids.count(txid) != 0);
553 return m_sequence_number++;
557 return m_sequence_number;
560 template <
typename Callable>
565 return func(*m_orphanage);
568 template <
typename Callable>
573 return func(*m_conflicting);
624 std::unordered_map<COutPoint, Coin, SaltedOutpointHasher>
m_temp_added;
631 mutable std::unordered_set<COutPoint, SaltedOutpointHasher>
643 bool GetCoin(
const COutPoint &outpoint,
Coin &coin)
const override;
651 std::unordered_set<COutPoint, SaltedOutpointHasher>
An in-memory indexed chain of blocks.
CCoinsView backed by another CCoinsView.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Abstract view on the open txout dataset.
CCoinsView that brings transactions from a mempool into view.
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
GetCoin, returning whether it exists and is not spent.
std::unordered_set< COutPoint, SaltedOutpointHasher > GetNonBaseCoins() const
Get all coins in m_non_base_coins.
void Reset()
Clear m_temp_added and m_non_base_coins.
std::unordered_map< COutPoint, Coin, SaltedOutpointHasher > m_temp_added
Coins made available by transactions being validated.
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
std::unordered_set< COutPoint, SaltedOutpointHasher > m_non_base_coins
Set of all coins that have been fetched from mempool or created using PackageAddTransaction (not base...
void PackageAddTransaction(const CTransactionRef &tx)
Add the coins created by this transaction.
const CTxMemPool & mempool
Fee rate in satoshis per kilobyte: Amount / kB.
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
uint64_t GetEntryId() const
CTransactionRef GetSharedTx() const
CFeeRate GetModifiedFeeRate() const
std::set< std::reference_wrapper< const CTxMemPoolEntryRef >, CompareIteratorById > Parents
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
bool isAvalancheFinalized(const TxId &txid) const
CFeeRate estimateFee() const
uint64_t nextEntryId GUARDED_BY(cs)
Used by addUnchecked to generate ever-increasing CTxMemPoolEntry::entryId's.
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 o...
void ClearPrioritisation(const TxId &txid) EXCLUSIVE_LOCKS_REQUIRED(cs)
std::set< txiter, CompareIteratorById > setEntries
const bool m_require_standard
void RemoveUnbroadcastTx(const TxId &txid, const bool unchecked=false)
Removes a transaction from the unbroadcast set.
uint64_t cachedInnerUsage GUARDED_BY(cs)
sum of dynamic memory usage of all the map elements (NOT the maps themselves)
Amount m_total_fee GUARDED_BY(cs)
sum of all mempool tx's fees (NOT modified fee)
bool GetLoadTried() const
bool CalculateAncestors(setEntries &setAncestors, CTxMemPoolEntry::Parents &staged_ancestors) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Helper function to calculate all in-mempool ancestors of staged_ancestors param@[in] staged_ancestors...
void updateFeeForBlock() EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
void UpdateEntryForAncestors(txiter it, const setEntries *setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Set ancestor state for an entry.
CFeeRate GetMinFee() const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
Amount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
void UpdateForRemoveFromMempool(const setEntries &entriesToRemove) EXCLUSIVE_LOCKS_REQUIRED(cs)
For each transaction being removed, update ancestors and any direct children.
bool blockSinceLastRollingFeeBump GUARDED_BY(cs)
const int m_check_ratio
Value n means that 1 times in n we check.
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.
const std::chrono::seconds m_expiry
const std::optional< unsigned > m_max_datacarrier_bytes
void AddTransactionsUpdated(unsigned int n)
void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs)
Sever link between specified transaction and direct children.
bool CompareTopologically(const TxId &txida, const TxId &txidb) const
TxMempoolInfo info(const TxId &txid) const
std::map< TxId, Amount > mapDeltas GUARDED_BY(cs)
const int64_t m_max_size_bytes
void getAllTxIds(std::vector< TxId > &vtxid) const
std::atomic< uint32_t > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
setEntries GetIterSet(const std::set< TxId > &txids) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Translate a set of txids into a set of pool iterators to avoid repeated lookups.
std::unique_ptr< TxConflicting > m_conflicting GUARDED_BY(cs_conflicting)
Storage for conflicting txs information.
size_t DynamicMemoryUsage() const
std::vector< TxMempoolInfo > infoAll() const
indexed_transaction_set mapTx GUARDED_BY(cs)
void LimitSize(CCoinsViewCache &coins_cache) EXCLUSIVE_LOCKS_REQUIRED(cs
Reduce the size of the mempool by expiring and then trimming the mempool.
bool setAvalancheFinalized(const CTxMemPoolEntryRef &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
CTransactionRef GetConflictTx(const COutPoint &prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Get the transaction in the pool that spends the same prevout.
void removeUnchecked(txiter entry, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Before calling removeUnchecked for a given transaction, UpdateForRemoveFromMempool must be called on ...
uint64_t totalTxSize GUARDED_BY(cs)
sum of all mempool tx's sizes.
int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Expire all transaction (and their dependencies) in the mempool older than time.
std::set< txiter, CompareIteratorByRevEntryId > setRevTopoEntries
bool exists(const TxId &txid) const
std::set< TxId > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
int64_t lastRollingFeeUpdate GUARDED_BY(cs)
const bool m_permit_bare_multisig
bool m_load_tried GUARDED_BY(cs)
static const int ROLLING_FEE_HALFLIFE
auto withOrphanage(Callable &&func) const EXCLUSIVE_LOCKS_REQUIRED(!cs_orphanage)
CTransactionRef get(const TxId &txid) const
boost::multi_index_container< CTxMemPoolEntryRef, boost::multi_index::indexed_by< boost::multi_index::hashed_unique< mempoolentry_txid, SaltedTxIdHasher >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< modified_feerate >, boost::multi_index::identity< CTxMemPoolEntryRef >, CompareTxMemPoolEntryByModifiedFeeRate >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< entry_time >, boost::multi_index::identity< CTxMemPoolEntryRef >, CompareTxMemPoolEntryByEntryTime >, boost::multi_index::ordered_unique< boost::multi_index::tag< entry_id >, boost::multi_index::identity< CTxMemPoolEntryRef >, CompareTxMemPoolEntryByEntryId > > > indexed_transaction_set
const CFeeRate m_min_relay_feerate
void PrioritiseTransaction(const TxId &txid, const Amount nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
uint64_t GetSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
bool IsUnbroadcastTx(const TxId &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns whether a txid is in the unbroadcast set.
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Guards this internal counter for external reporting.
void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void addUnchecked(CTxMemPoolEntryRef entry) EXCLUSIVE_LOCKS_REQUIRED(cs
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
RadixTree< CTxMemPoolEntry, MemPoolEntryRadixTreeAdapter > finalizedTxs
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void cs_main
double rollingMinimumFeeRate GUARDED_BY(cs)
minimum fee to get into the pool, decreases exponentially
std::unique_ptr< TxOrphanage > m_orphanage GUARDED_BY(cs_orphanage)
Storage for orphan information.
CTxMemPool(const Options &opts)
Create a new CTxMemPool.
auto withConflicting(Callable &&func) const EXCLUSIVE_LOCKS_REQUIRED(!cs_conflicting)
indirectmap< COutPoint, CTransactionRef > mapNextTx GUARDED_BY(cs)
bool CalculateMemPoolAncestors(const CTxMemPoolEntryRef &entry, setEntries &setAncestors, bool fSearchForParents=true) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Try to calculate all in-mempool ancestors of entry.
void removeForFinalizedBlock(const std::vector< CTransactionRef > &vtx) EXCLUSIVE_LOCKS_REQUIRED(cs)
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of hash.
void RemoveStaged(const setEntries &stage, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
unsigned long size() const
void UpdateParentsOf(bool add, txiter it) EXCLUSIVE_LOCKS_REQUIRED(cs)
Update parents of it to add/remove it as a child transaction.
uint64_t m_sequence_number GUARDED_BY(cs)
void ApplyDelta(const TxId &txid, Amount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
void SetLoadTried(bool load_tried)
Set whether or not we've made an attempt to load the mempool (regardless of whether the attempt was s...
const CFeeRate m_dust_relay_feerate
std::optional< txiter > GetIter(const TxId &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given txid, if found.
std::set< TxId > m_unbroadcast_txids GUARDED_BY(cs)
Track locally submitted transactions to periodically retry initial broadcast.
uint64_t GetTotalTxSize() const EXCLUSIVE_LOCKS_REQUIRED(cs)
bool isSpent(const COutPoint &outpoint) const
void AddUnbroadcastTx(const TxId &txid)
Adds a transaction to the unbroadcast set.
unsigned int GetTransactionsUpdated() const
void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs)
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Map whose keys are pointers, but are compared by their dereferenced values.
RCUPtr< CTxMemPoolEntry > CTxMemPoolEntryRef
Implement std::hash so RCUPtr can be used as a key for maps or sets.
std::shared_ptr< const CTransaction > CTransactionRef
bool operator()(const CTxMemPoolEntryRef &a, const CTxMemPoolEntryRef &b) const
bool operator()(const CTxMemPoolEntryRef &a, const CTxMemPoolEntryRef &b) const
Sort by feerate of entry (modfee/vsize) in descending order.
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
bool operator()(const CTxMemPoolEntryRef &a, const CTxMemPoolEntryRef &b) const
Radix tree adapter for storing a CTxMemPoolEntry as a tree element.
Uint256RadixKey getId(const CTxMemPoolEntry &entry) const
RCUPtr< T > get(const KeyType &key)
Get the value corresponding to a key.
bool insert(const RCUPtr< T > &value)
Insert a value into the tree.
A TxId is the identifier of a transaction.
Information about a mempool transaction.
Amount fee
Fee of the transaction.
Amount nFeeDelta
The fee delta.
CTransactionRef tx
The transaction itself.
std::chrono::seconds m_time
Time the transaction entered the mempool.
size_t vsize
Virtual size of the transaction.
Facility for using an uint256 as a radix tree key.
Options struct containing options for constructing a CTxMemPool.
result_type operator()(const CTxMemPoolEntryRef &entry) const
result_type operator()(const CTransactionRef &tx) const
#define EXCLUSIVE_LOCKS_REQUIRED(...)
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
@ SIZELIMIT
Removed in size limiting.
@ EXPIRY
Expired from mempool.
@ CONFLICT
Removed for conflict with in-block transaction.
@ REORG
Removed for reorganization.
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coins to signify they are only in the memory pool(since 0....
const std::string RemovalReasonToString(const MemPoolRemovalReason &r) noexcept