6 #ifndef BITCOIN_TXMEMPOOL_H 7 #define BITCOIN_TXMEMPOOL_H 26 #include <boost/multi_index/hashed_index.hpp> 27 #include <boost/multi_index/identity.hpp> 28 #include <boost/multi_index/indexed_by.hpp> 29 #include <boost/multi_index/ordered_index.hpp> 30 #include <boost/multi_index/sequenced_index.hpp> 31 #include <boost/multi_index/tag.hpp> 32 #include <boost/multi_index_container.hpp> 39 #include <string_view> 82 return tx->GetWitnessHash();
96 double a_mod_fee, a_size, b_mod_fee, b_size;
102 double f1 = a_mod_fee * b_size;
103 double f2 = a_size * b_mod_fee;
169 double a_mod_fee, a_size, b_mod_fee, b_size;
175 double f1 = a_mod_fee * b_size;
176 double f2 = a_size * b_mod_fee;
179 return a.GetTx().GetHash() < b.GetTx().GetHash();
185 template <
typename T>
190 double f1 = (double)a.GetModifiedFee() * a.GetSizeWithAncestors();
191 double f2 = (double)a.GetModFeesWithAncestors() * a.GetTxSize();
194 mod_fee = a.GetModFeesWithAncestors();
195 size = a.GetSizeWithAncestors();
197 mod_fee = a.GetModifiedFee();
198 size = a.GetTxSize();
334 boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
336 boost::multi_index::hashed_unique<
337 boost::multi_index::tag<index_by_wtxid>,
342 boost::multi_index::ordered_non_unique<
343 boost::multi_index::tag<descendant_score>,
344 boost::multi_index::identity<CTxMemPoolEntry>,
345 CompareTxMemPoolEntryByDescendantScore
348 boost::multi_index::ordered_non_unique<
349 boost::multi_index::tag<entry_time>,
350 boost::multi_index::identity<CTxMemPoolEntry>,
351 CompareTxMemPoolEntryByEntryTime
354 boost::multi_index::ordered_non_unique<
355 boost::multi_index::tag<ancestor_score>,
356 boost::multi_index::identity<CTxMemPoolEntry>,
357 CompareTxMemPoolEntryByAncestorFee
361 typedef boost::multi_index_container<
393 using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
394 std::vector<CTransactionRef> txns_randomized
GUARDED_BY(
cs);
402 typedef std::map<txiter, setEntries, CompareIteratorByHash>
cacheMap;
577 std::string_view calling_fn_name,
671 return (mapTx.count(gtxid.
GetHash()) != 0);
708 return m_unbroadcast_txids;
715 return m_unbroadcast_txids.count(txid) != 0;
720 return m_sequence_number++;
724 return m_sequence_number;
811 return m_epoch.visited(
it->m_epoch_marker);
840 std::unordered_map<COutPoint, Coin, SaltedOutpointHasher>
m_temp_added;
863 #endif // BITCOIN_TXMEMPOOL_H
std::shared_ptr< const CTransaction > CTransactionRef
int64_t lastRollingFeeUpdate GUARDED_BY(cs)
sum of dynamic memory usage of all the map elements (NOT the maps themselves)
const bool in_mempool
Whether this transaction is in the mempool.
Information about a mempool transaction.
std::vector< txiter > GetIterVec(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.
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...
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
std::vector< TxMempoolInfo > infoAll() const
int32_t GetTxSize() const
bool exists(const GenTxid >xid) const
void UpdateTransactionsFromBlock(const std::vector< uint256 > &vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs
UpdateTransactionsFromBlock is called when adding transactions from a disconnected block back to the ...
const uint256 txid
The prioritised transaction's txid.
Options struct containing limit options for a CTxMemPool.
An in-memory indexed chain of blocks.
Epoch m_epoch GUARDED_BY(cs)
minimum fee to get into the pool, decreases exponentially
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of hash.
size_t DynamicMemoryUsage() const
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
GetCoin, returning whether it exists and is not spent.
util::Result< std::pair< std::vector< FeeFrac >, std::vector< FeeFrac > > > CalculateChunksForRBF(CAmount replacement_fees, int64_t replacement_vsize, const setEntries &direct_conflicts, const setEntries &all_conflicts) EXCLUSIVE_LOCKS_REQUIRED(cs)
Calculate the sorted chunks for the old and new mempool relating to the clusters that would be affect...
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal...
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
CAmount GetModFeesWithDescendants() const
void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
unsigned long size() const
uint64_t GetSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
bool CompareDepthAndScore(const uint256 &hasha, const uint256 &hashb, bool wtxid=false)
std::set< txiter, CompareIteratorByHash > setEntries
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
std::unordered_set< COutPoint, SaltedOutpointHasher > GetNonBaseCoins() const
Get all coins in m_non_base_coins.
std::atomic< unsigned int > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
void SetLoadTried(bool load_tried)
Set whether or not an initial attempt to load the persisted mempool was made (regardless of whether t...
CFeeRate GetMinFee() const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
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.
CAmount GetModifiedFee() const
util::Result< setEntries > CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, const Limits &limits, bool fSearchForParents=true) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Try to calculate all in-mempool ancestors of entry.
int64_t nFeeDelta
The fee delta.
bool isSpent(const COutPoint &outpoint) const
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 entri...
std::vector< txiter > GatherClusters(const std::vector< uint256 > &txids) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Collect the entire cluster of connected transactions for each transaction in txids.
const CAmount delta
The fee delta added using PrioritiseTransaction().
CTransactionRef tx
The transaction itself.
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Update ancestors of hash to add/remove it as a descendant transaction.
int64_t CAmount
Amount in satoshis (Can be negative)
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 tr...
bool operator()(const T &a, const T &b) const
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coin to signify they are only in the memory pool (since 0...
bool blockSinceLastRollingFeeBump GUARDED_BY(cs)
result_type operator()(const CTxMemPoolEntry &entry) const
void AddUnbroadcastTx(const uint256 &txid)
Adds a transaction to the unbroadcast set.
Abstract view on the open txout dataset.
int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Expire all transaction (and their dependencies) in the mempool older than time.
bool m_load_tried GUARDED_BY(cs)
const CAmount & GetFee() const
result_type operator()(const CTxMemPoolEntry &entry) const
double rollingMinimumFeeRate GUARDED_BY(cs)
void removeUnchecked(txiter entry, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Before calling removeUnchecked for a given transaction, UpdateForRemoveFromMempool must be called on ...
uint64_t cachedInnerUsage GUARDED_BY(cs)
sum of all mempool tx's fees (NOT modified fee)
std::vector< delta_info > GetPrioritisedTransactions() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Return a vector of all entries in mapDeltas with their corresponding delta_info.
Sort an entry by max(score/size of entry's tx, score/size with all descendants).
static const int ROLLING_FEE_HALFLIFE
uint64_t GetTotalTxSize() const EXCLUSIVE_LOCKS_REQUIRED(cs)
uint64_t m_sequence_number GUARDED_BY(cs)
boost::multi_index_container< CTxMemPoolEntry, CTxMemPoolEntry_Indices > indexed_transaction_set
std::chrono::seconds GetTime() const
const CTransaction * GetConflictTx(const COutPoint &prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Get the transaction in the pool that spends the same prevout.
An outpoint - a combination of a transaction hash and an index n into its vout.
void AddTransactionsUpdated(unsigned int n)
void ApplyDelta(const uint256 &hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
bool GetLoadTried() const
TxMempoolInfo info_for_relay(const GenTxid >xid, uint64_t last_sequence) const
Returns info for a transaction if its entry_sequence < last_sequence.
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void addUnchecked(const CTxMemPoolEntry &entry) EXCLUSIVE_LOCKS_REQUIRED(cs
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
std::optional< CAmount > modified_fee
The modified fee (base fee + delta) of this entry.
std::optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void cs_main
const CTxMemPoolEntry * GetEntry(const Txid &txid) const LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(cs)
int32_t vsize
Virtual size of the transaction.
uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Guards this internal counter for external reporting.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
std::vector< indexed_transaction_set::const_iterator > GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs)
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 cre...
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
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 Reset()
Clear m_temp_added and m_non_base_coins.
std::map< txiter, setEntries, CompareIteratorByHash > cacheMap
std::set< uint256 > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
const CTransaction & GetTx() const
void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Set ancestor state for an entry.
CAmount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
TxMempoolInfo info(const GenTxid >xid) const
txiter get_iter_from_wtxid(const uint256 &wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
void ClearPrioritisation(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs)
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.
std::optional< std::string > CheckConflictTopology(const setEntries &direct_conflicts)
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
unsigned int GetTransactionsUpdated() const
CAmount fee
Fee of the transaction.
void PackageAddTransaction(const CTransactionRef &tx)
Add the coins created by this transaction.
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.
util::Result< setEntries > CalculateAncestorsAndCheckLimits(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 desc...
void RemoveUnbroadcastTx(const uint256 &txid, const bool unchecked=false)
Removes a transaction from the unbroadcast set.
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
bool TestLockPointValidity(CChain &active_chain, const LockPoints &lp) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Test whether the LockPoints height and time are still valid on the current chain. ...
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
Options struct containing options for constructing a CTxMemPool.
CAmount m_total_fee GUARDED_BY(cs)
sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discount...
The basic transaction that is broadcasted on the network and contained in blocks. ...
CCoinsView backed by another CCoinsView.
bool IsUnbroadcastTx(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns whether a txid is in the unbroadcast set.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Sort by feerate of entry (fee/size) in descending order This is only used for transaction relay...
void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
const CTxMemPool & mempool
int64_t GetTime()
DEPRECATED, see GetTime.
void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs)
Sever link between specified transaction and direct children.
std::chrono::seconds m_time
Time the transaction entered the mempool.
void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
CCoinsView that brings transactions from a mempool into view.
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 ance...
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
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. ...
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
A generic txid reference (txid or wtxid).
std::unordered_map< COutPoint, Coin, SaltedOutpointHasher > m_temp_added
Coins made available by transactions being validated.
static GenTxid Txid(const uint256 &hash)
const Wtxid & GetWitnessHash() const LIFETIMEBOUND
std::vector< CTxMemPoolEntryRef > entryAll() const EXCLUSIVE_LOCKS_REQUIRED(cs)
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs)
For each transaction being removed, update ancestors and any direct children.
const uint256 & GetHash() const LIFETIMEBOUND
int64_t GetSizeWithDescendants() const
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it...
void cs_main LOCKS_EXCLUDED(m_epoch)
const Txid & GetHash() const LIFETIMEBOUND
Epoch: RAII-style guard for using epoch-based graph traversal algorithms.
uint64_t totalTxSize GUARDED_BY(cs)