 |
Bitcoin Core
22.99.0
P2P Digital Currency
|
Go to the documentation of this file.
6 #ifndef BITCOIN_TXMEMPOOL_H
7 #define BITCOIN_TXMEMPOOL_H
28 #include <boost/multi_index/hashed_index.hpp>
29 #include <boost/multi_index/ordered_index.hpp>
30 #include <boost/multi_index/sequenced_index.hpp>
31 #include <boost/multi_index_container.hpp>
62 bool operator()(
const std::reference_wrapper<T>& a,
const std::reference_wrapper<T>& b)
const
64 return a.get().GetTx().GetHash() < b.get().GetTx().GetHash();
69 return a->GetTx().GetHash() < b->GetTx().GetHash();
90 typedef std::set<CTxMemPoolEntryRef, CompareIteratorByHash>
Parents;
91 typedef std::set<CTxMemPoolEntryRef, CompareIteratorByHash>
Children;
122 int64_t time,
unsigned int entry_height,
123 bool spends_coinbase,
131 std::chrono::seconds
GetTime()
const {
return std::chrono::seconds{
nTime}; }
179 return tx->GetHash();
194 return tx->GetWitnessHash();
208 double a_mod_fee, a_size, b_mod_fee, b_size;
214 double f1 = a_mod_fee * b_size;
215 double f2 = a_size * b_mod_fee;
281 double a_mod_fee, a_size, b_mod_fee, b_size;
287 double f1 = a_mod_fee * b_size;
288 double f2 = a_size * b_mod_fee;
291 return a.GetTx().GetHash() < b.GetTx().GetHash();
297 template <
typename T>
302 double f1 = (double)a.GetModifiedFee() * a.GetSizeWithAncestors();
303 double f2 = (double)a.GetModFeesWithAncestors() * a.GetTxSize();
306 mod_fee = a.GetModFeesWithAncestors();
307 size = a.GetSizeWithAncestors();
309 mod_fee = a.GetModifiedFee();
310 size = a.GetTxSize();
441 mutable bool blockSinceLastRollingFeeBump
GUARDED_BY(
cs);
458 typedef boost::multi_index_container<
460 boost::multi_index::indexed_by<
462 boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
464 boost::multi_index::hashed_unique<
465 boost::multi_index::tag<index_by_wtxid>,
470 boost::multi_index::ordered_non_unique<
471 boost::multi_index::tag<descendant_score>,
472 boost::multi_index::identity<CTxMemPoolEntry>,
476 boost::multi_index::ordered_non_unique<
477 boost::multi_index::tag<entry_time>,
478 boost::multi_index::identity<CTxMemPoolEntry>,
482 boost::multi_index::ordered_non_unique<
483 boost::multi_index::tag<ancestor_score>,
484 boost::multi_index::identity<CTxMemPoolEntry>,
520 using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
521 std::vector<std::pair<uint256, txiter>> vTxHashes
GUARDED_BY(
cs);
527 typedef std::map<txiter, setEntries, CompareIteratorByHash>
cacheMap;
553 uint64_t limitAncestorCount,
554 uint64_t limitAncestorSize,
555 uint64_t limitDescendantCount,
556 uint64_t limitDescendantSize,
687 uint64_t limitAncestorCount,
688 uint64_t limitAncestorSize,
689 uint64_t limitDescendantCount,
690 uint64_t limitDescendantSize,
753 return (mapTx.count(gtxid.
GetHash()) != 0);
763 std::vector<TxMempoolInfo>
infoAll()
const;
783 return m_unbroadcast_txids;
790 return m_unbroadcast_txids.count(txid) != 0;
795 return m_sequence_number++;
799 return m_sequence_number;
836 const std::set<uint256>& setExclude, std::set<uint256>& descendants_to_remove,
869 return m_epoch.visited(
it->m_epoch_marker);
898 std::unordered_map<COutPoint, Coin, SaltedOutpointHasher>
m_temp_added;
930 typedef boost::multi_index_container<
932 boost::multi_index::indexed_by<
934 boost::multi_index::hashed_unique<
935 boost::multi_index::tag<txid_index>,
940 boost::multi_index::sequenced<
941 boost::multi_index::tag<insertion_order>
978 for (
auto const &tx : vtx) {
979 auto it =
queuedTx.find(tx->GetHash());
988 void removeEntry(indexed_disconnected_transactions::index<insertion_order>::type::iterator entry)
1001 #endif // BITCOIN_TXMEMPOOL_H
bool GetSpendsCoinbase() const
int64_t nSigOpCostWithAncestors
uint64_t nCountWithDescendants
number of descendant transactions
std::set< txiter, CompareIteratorByHash > setEntries
void addTransaction(const CTransactionRef &tx)
void AddTransactionsUpdated(unsigned int n)
CFeeRate GetMinFee(size_t sizelimit) const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
TxMempoolInfo info(const GenTxid >xid) const
void removeUnchecked(txiter entry, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Before calling removeUnchecked for a given transaction, UpdateForRemoveFromMempool must be called on ...
std::set< uint256 > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
int64_t feeDelta
Used for determining the priority of the transaction for mining in a block.
unsigned int GetHeight() const
bool CheckPackageLimits(const Package &package, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Calculate all in-mempool ancestors of a set of transactions not already in the mempool and check ance...
std::set< CTxMemPoolEntryRef, CompareIteratorByHash > Children
uint64_t nCountWithAncestors
const CAmount & GetFee() const
static const int ROLLING_FEE_HALFLIFE
A generic txid reference (txid or wtxid).
size_t vsize
Virtual size of the transaction.
const int64_t nTime
Local time when entering the mempool.
DisconnectedBlockTransactions.
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
LockPoints lockPoints
Track the height and time at which tx was final.
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...
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void cs_main
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
int64_t GetModifiedFee() const
txiter get_iter_from_wtxid(const uint256 &wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
const bool spendsCoinbase
keep track of transactions that spend a coinbase
void removeEntry(indexed_disconnected_transactions::index< insertion_order >::type::iterator entry)
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.
void ClearPrioritisation(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs)
std::chrono::seconds GetTime() const
CTransactionRef GetSharedTx() const
const Parents & GetMemPoolParentsConst() const
void AddUnbroadcastTx(const uint256 &txid)
Adds a transaction to the unbroadcast set.
void SetIsLoaded(bool loaded)
Sets the current loaded state.
@ EXPIRY
Expired from mempool.
Parents & GetMemPoolParents() const
void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs)
Sever link between specified transaction and direct children.
void ApplyDelta(const uint256 &hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
bool CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, setEntries &setAncestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString, bool fSearchForParents=true) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Try to calculate all in-mempool ancestors of entry.
size_t vTxHashesIdx
Index in mempool's vTxHashes.
const size_t nTxWeight
... and avoid recomputing tx weight (also used for GetTxSize())
std::set< CTxMemPoolEntryRef, CompareIteratorByHash > Parents
static GenTxid Txid(const uint256 &hash)
std::shared_ptr< const CTransaction > CTransactionRef
void PackageAddTransaction(const CTransactionRef &tx)
Add the coins created by this transaction.
~DisconnectedBlockTransactions()
unsigned int GetTransactionsUpdated() const
boost::multi_index_container< CTxMemPoolEntry, boost::multi_index::indexed_by< boost::multi_index::hashed_unique< mempoolentry_txid, SaltedTxidHasher >, boost::multi_index::hashed_unique< boost::multi_index::tag< index_by_wtxid >, mempoolentry_wtxid, SaltedTxidHasher >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< descendant_score >, boost::multi_index::identity< CTxMemPoolEntry >, CompareTxMemPoolEntryByDescendantScore >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< entry_time >, boost::multi_index::identity< CTxMemPoolEntry >, CompareTxMemPoolEntryByEntryTime >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< ancestor_score >, boost::multi_index::identity< CTxMemPoolEntry >, CompareTxMemPoolEntryByAncestorFee > > > indexed_transaction_set
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
uint64_t nSizeWithAncestors
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
Fee rate in satoshis per kilobyte: CAmount / kB.
CTransactionRef tx
The transaction itself.
void queryHashes(std::vector< uint256 > &vtxid) const
void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Set ancestor state for an entry.
const size_t nUsageSize
... and total memory usage
The basic transaction that is broadcasted on the network and contained in blocks.
Abstract view on the open txout dataset.
uint64_t nSizeWithDescendants
... and size
CAmount nModFeesWithDescendants
... and total fees (all including us)
const CTxMemPool & mempool
bool m_is_loaded GUARDED_BY(cs)
uint64_t totalTxSize GUARDED_BY(cs)
sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discount...
int64_t GetSigOpCostWithAncestors() const
uint64_t m_sequence_number GUARDED_BY(cs)
CBlockIndex * maxInputBlock
uint64_t GetCountWithAncestors() const
int64_t nFeeDelta
The fee delta.
static size_t RecursiveDynamicUsage(const CScript &script)
CAmount GetModFeesWithAncestors() const
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Guards this internal counter for external reporting.
void UpdateForDescendants(txiter updateIt, cacheMap &cachedDescendants, const std::set< uint256 > &setExclude, std::set< uint256 > &descendants_to_remove, uint64_t ancestor_size_limit, uint64_t ancestor_count_limit) EXCLUSIVE_LOCKS_REQUIRED(cs)
UpdateForDescendants is used by UpdateTransactionsFromBlock to update the descendants for a single tr...
CTransactionRef get(const uint256 &hash) const
std::unordered_map< COutPoint, Coin, SaltedOutpointHasher > m_temp_added
Coins made available by transactions being validated.
setEntries GetIterSet(const std::set< uint256 > &hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Translate a set of hashes into a set of pool iterators to avoid repeated lookups.
int64_t GetSigOpCost() const
std::atomic< unsigned int > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
CAmount nModFeesWithAncestors
void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps)
CAmount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
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 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.
void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount)
int64_t CAmount
Amount in satoshis (Can be negative)
uint64_t GetSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
boost::multi_index_container< CTransactionRef, boost::multi_index::indexed_by< boost::multi_index::hashed_unique< boost::multi_index::tag< txid_index >, mempoolentry_txid, SaltedTxidHasher >, boost::multi_index::sequenced< boost::multi_index::tag< insertion_order > > > > indexed_disconnected_transactions
@ SIZELIMIT
Removed in size limiting.
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
bool CalculateAncestorsAndCheckLimits(size_t entry_size, size_t entry_count, setEntries &setAncestors, CTxMemPoolEntry::Parents &staged_ancestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Helper function to calculate all in-mempool ancestors of staged_ancestors and apply ancestor and desc...
const int m_check_ratio
Value n means that 1 times in n we check.
void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
void RemoveUnbroadcastTx(const uint256 &txid, const bool unchecked=false)
Removes a transaction from the unbroadcast set.
const CAmount nFee
Cached to avoid expensive parent-transaction lookups.
@ REPLACED
Removed for replacement.
bool operator()(const T &a, const T &b) const
CChainState stores and provides an API to update our local knowledge of the current best chain.
void UpdateFeeDelta(int64_t feeDelta)
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
bool operator()(const std::reference_wrapper< T > &a, const std::reference_wrapper< T > &b) const
std::optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
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.
size_t DynamicMemoryUsage() const
void removeForBlock(const std::vector< CTransactionRef > &vtx)
result_type operator()(const CTxMemPoolEntry &entry) const
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs)
For each transaction being removed, update ancestors and any direct children.
const Children & GetMemPoolChildrenConst() const
int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Expire all transaction (and their dependencies) in the mempool older than time.
void cs_main LOCKS_EXCLUDED(m_epoch)
bool exists(const GenTxid >xid) 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.
An in-memory indexed chain of blocks.
const uint256 & GetHash() const
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.
CCoinsView backed by another CCoinsView.
const LockPoints & GetLockPoints() const
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void addUnchecked(const CTxMemPoolEntry &entry, bool validFeeEstimate=true) EXCLUSIVE_LOCKS_REQUIRED(cs
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
#define EXCLUSIVE_LOCKS_REQUIRED(...)
void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
size_t DynamicMemoryUsage() const
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
indexed_disconnected_transactions queuedTx
std::vector< TxMempoolInfo > infoAll() const
const CTransaction * GetConflictTx(const COutPoint &prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Get the transaction in the pool that spends the same prevout.
bool operator()(const T &a, const T &b) const
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
CCoinsView that brings transactions from a mempool into view.
unsigned long size() 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...
Information about a mempool transaction.
void UpdateLockPoints(const LockPoints &lp)
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
bool CompareDepthAndScore(const uint256 &hasha, const uint256 &hashb, bool wtxid=false)
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
CAmount GetModFeesWithDescendants() const
void UpdateTransactionsFromBlock(const std::vector< uint256 > &vHashesToUpdate, uint64_t ancestor_size_limit, uint64_t ancestor_count_limit) EXCLUSIVE_LOCKS_REQUIRED(cs
UpdateTransactionsFromBlock is called when adding transactions from a disconnected block back to the ...
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coin to signify they are only in the memory pool (since 0....
const uint256 & GetWitnessHash() const
size_t DynamicMemoryUsage() const
size_t GetTxWeight() const
const uint256 & GetHash() const
An outpoint - a combination of a transaction hash and an index n into its vout.
Children & GetMemPoolChildren() const
@ BLOCK
Removed for block.
std::chrono::seconds m_time
Time the transaction entered the mempool.
uint64_t GetTotalTxSize() const EXCLUSIVE_LOCKS_REQUIRED(cs)
uint64_t GetSizeWithAncestors() const
CAmount fee
Fee of the transaction.
uint64_t GetCountWithDescendants() const
The block chain is a tree shaped structure starting with the genesis block at the root,...
const unsigned int entryHeight
Chain height when entering the mempool.
std::map< txiter, setEntries, CompareIteratorByHash > cacheMap
bool isSpent(const COutPoint &outpoint) const
const int64_t sigOpCost
Total sigop cost.
std::reference_wrapper< const CTxMemPoolEntry > CTxMemPoolEntryRef
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs)
result_type operator()(const CTxMemPoolEntry &entry) const
CBlockPolicyEstimator *const minerPolicyEstimator
uint64_t GetSizeWithDescendants() const
@ CONFLICT
Removed for conflict with in-block transaction.
@ REORG
Removed for reorganization.
uint64_t cachedInnerUsage
Epoch: RAII-style guard for using epoch-based graph traversal algorithms.
CTxMemPoolEntry(const CTransactionRef &tx, CAmount fee, int64_t time, unsigned int entry_height, bool spends_coinbase, int64_t sigops_cost, LockPoints lp)
Epoch::Marker m_epoch_marker
epoch when last touched, useful for graph algorithms
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of hash.
const CTransaction & GetTx() const