6 #ifndef BITCOIN_TXMEMPOOL_H
7 #define BITCOIN_TXMEMPOOL_H
18 #include <boost/multi_index/hashed_index.hpp>
19 #include <boost/multi_index/ordered_index.hpp>
20 #include <boost/multi_index/sequenced_index.hpp>
21 #include <boost/multi_index_container.hpp>
28 #include <unordered_map>
69 const std::reference_wrapper<T> &b)
const {
70 return a.get().GetTx().GetId() < b.get().GetTx().GetId();
72 template <
typename T>
bool operator()(
const T &a,
const T &b)
const {
73 return a->GetTx().GetId() < b->GetTx().GetId();
92 typedef std::set<CTxMemPoolEntryRef, CompareIteratorById>
Parents;
93 typedef std::set<CTxMemPoolEntryRef, CompareIteratorById>
Children;
146 unsigned int entry_height,
bool spends_coinbase,
160 std::chrono::seconds
GetTime()
const {
return std::chrono::seconds{
nTime}; }
172 int64_t modifyCount, int64_t modifySigChecks);
175 int64_t modifyCount, int64_t modifySigChecks);
392 typedef boost::multi_index_container<
395 boost::multi_index::hashed_unique<
398 boost::multi_index::ordered_non_unique<
399 boost::multi_index::tag<modified_feerate>,
400 boost::multi_index::identity<CTxMemPoolEntry>,
403 boost::multi_index::ordered_non_unique<
404 boost::multi_index::tag<entry_time>,
405 boost::multi_index::identity<CTxMemPoolEntry>,
408 boost::multi_index::ordered_unique<
409 boost::multi_index::tag<entry_id>,
410 boost::multi_index::identity<CTxMemPoolEntry>,
444 using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
475 size_t entry_size,
size_t entry_count,
setEntries &setAncestors,
477 uint64_t limitAncestorSize, uint64_t limitDescendantCount,
478 uint64_t limitDescendantSize, std::string &errString)
const
597 uint64_t limitAncestorCount, uint64_t limitAncestorSize,
598 uint64_t limitDescendantCount, uint64_t limitDescendantSize,
599 std::
string &errString,
bool fSearchForParents = true) const
623 uint64_t limitAncestorSize,
624 uint64_t limitDescendantCount,
625 uint64_t limitDescendantSize,
626 std::
string &errString) const
653 std::vector<
COutPoint> *pvNoSpendsRemaining =
nullptr)
666 std::chrono::seconds age)
683 size_t *ancestorsize =
nullptr,
684 Amount *ancestorfees =
nullptr) const;
709 return mapTx.count(txid) != 0;
714 std::vector<TxMempoolInfo>
infoAll()
const;
726 m_unbroadcast_txids.insert(txid);
736 return m_unbroadcast_txids;
742 return (m_unbroadcast_txids.count(txid) != 0);
747 return m_sequence_number++;
751 return m_sequence_number;
772 bool updateDescendants)
808 std::unordered_map<COutPoint, Coin, SaltedOutpointHasher>
m_temp_added;
815 bool GetCoin(
const COutPoint &outpoint,
Coin &coin)
const override;
848 typedef boost::multi_index_container<
851 boost::multi_index::hashed_unique<
852 boost::multi_index::tag<txid_index>,
855 boost::multi_index::sequenced<
856 boost::multi_index::tag<insertion_order>>>>
860 uint64_t cachedInnerUsage = 0;
863 const std::chrono::seconds
time;
867 unsigned height_) noexcept
868 : time(time_), feeDelta(feeDelta_), height(height_) {}
871 using TxInfoMap = std::unordered_map<TxId, TxInfo, SaltedTxIdHasher>;
900 6 *
sizeof(
void *)) *
916 void addForBlock(
const std::vector<CTransactionRef> &vtx,
CTxMemPool &pool)
922 if (queuedTx.empty()) {
925 for (
auto const &tx : vtx) {
926 auto it = queuedTx.find(tx->GetId());
927 if (it != queuedTx.end()) {
930 txInfo.erase(tx->GetId());
939 txInfo.erase((*entry)->GetId());
943 bool isEmpty()
const {
return queuedTx.empty(); }
946 cachedInnerUsage = 0;
964 void updateMempoolForReorg(
const Config &config,
The block chain is a tree shaped structure starting with the genesis block at the root,...
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.
std::unordered_map< COutPoint, Coin, SaltedOutpointHasher > m_temp_added
Coins made available by transactions being validated.
const CTxMemPool & mempool
Fee rate in satoshis per kilobyte: Amount / kB.
An outpoint - a combination of a transaction hash and an index n into its vout.
The basic transaction that is broadcasted on the network and contained in blocks.
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
int64_t GetSigChecksWithDescendants() const
std::set< CTxMemPoolEntryRef, CompareIteratorById > Parents
uint64_t GetVirtualSizeWithDescendants() const
const bool spendsCoinbase
keep track of transactions that spend a coinbase
int64_t nSigChecksWithDescendants
... and sichecks
const CTransaction & GetTx() const
unsigned int GetHeight() const
const Parents & GetMemPoolParentsConst() const
std::chrono::seconds GetTime() const
int64_t GetSigChecksWithAncestors() const
uint64_t GetEntryId() const
std::reference_wrapper< const CTxMemPoolEntry > CTxMemPoolEntryRef
bool GetSpendsCoinbase() const
const int64_t nTime
Local time when entering the mempool.
const Amount nFee
Cached to avoid expensive parent-transaction lookups.
uint64_t GetCountWithDescendants() const
void UpdateAncestorState(int64_t modifySize, Amount modifyFee, int64_t modifyCount, int64_t modifySigChecks)
const size_t nTxSize
... and avoid recomputing tx size
const size_t nUsageSize
... and total memory usage
void UpdateLockPoints(const LockPoints &lp)
uint64_t GetVirtualSizeWithAncestors() const
Amount nModFeesWithAncestors
const LockPoints & GetLockPoints() const
int64_t GetSigChecks() const
void UpdateFeeDelta(Amount feeDelta)
void SetEntryId(uint64_t eid)
This should only be set by addUnchecked() before entry insertion into mempool.
int64_t nSigChecksWithAncestors
uint64_t nCountWithDescendants
number of descendant transactions
Amount feeDelta
Used for determining the priority of the transaction for mining in a block.
CTxMemPoolEntry(const CTransactionRef &_tx, const Amount fee, int64_t time, unsigned int entry_height, bool spends_coinbase, int64_t sigchecks, LockPoints lp)
Amount nModFeesWithDescendants
... and total fees (all including us)
uint64_t GetSizeWithAncestors() const
CTransactionRef GetSharedTx() const
Amount GetModifiedFee() const
uint64_t nSizeWithDescendants
... and size
CFeeRate GetModifiedFeeRate() const
Amount GetModFeesWithDescendants() const
size_t DynamicMemoryUsage() const
uint64_t nCountWithAncestors
size_t GetTxVirtualSize() const
void UpdateDescendantState(int64_t modifySize, Amount modifyFee, int64_t modifyCount, int64_t modifySigChecks)
uint64_t entryId
Unique identifier – used for topological sorting.
Parents & GetMemPoolParents() const
uint64_t GetSizeWithDescendants() const
uint64_t GetCountWithAncestors() const
LockPoints lockPoints
Track the height and time at which tx was final.
uint64_t nSizeWithAncestors
std::set< CTxMemPoolEntryRef, CompareIteratorById > Children
Amount GetModFeesWithAncestors() const
const Children & GetMemPoolChildrenConst() const
const unsigned int entryHeight
Chain height when entering the mempool.
Children & GetMemPoolChildren() const
const int64_t sigChecks
Total sigChecks.
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)
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
void RemoveUnbroadcastTx(const TxId &txid, const bool unchecked=false)
Removes a transaction from the unbroadcast set.
void UpdateParentsOf(bool add, txiter it, const setEntries *setAncestors=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Update parents of it to add/remove it as a child transaction.
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)
void UpdateEntryForAncestors(txiter it, const setEntries *setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Set ancestor state for an entry.
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)
CFeeRate GetMinFee(size_t sizelimit) const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
Amount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
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.
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
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.
boost::multi_index_container< CTxMemPoolEntry, 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< CTxMemPoolEntry >, CompareTxMemPoolEntryByModifiedFeeRate >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< entry_time >, boost::multi_index::identity< CTxMemPoolEntry >, CompareTxMemPoolEntryByEntryTime >, boost::multi_index::ordered_unique< boost::multi_index::tag< entry_id >, boost::multi_index::identity< CTxMemPoolEntry >, CompareTxMemPoolEntryByEntryId > > > indexed_transaction_set
size_t DynamicMemoryUsage() const
std::vector< TxMempoolInfo > infoAll() const
indexed_transaction_set mapTx GUARDED_BY(cs)
void SetIsLoaded(bool loaded)
Sets the current loaded state.
void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
indirectmap< COutPoint, const CTransaction * > mapNextTx 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 totalTxSize GUARDED_BY(cs)
sum of all mempool tx's sizes.
void removeForBlock(const std::vector< CTransactionRef > &vtx) EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Expire all transaction (and their dependencies) in the mempool older than time.
bool exists(const TxId &txid) 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::map< TxId, Amount > mapDeltas GUARDED_BY(cs)
int64_t lastRollingFeeUpdate GUARDED_BY(cs)
CTxMemPool(int check_ratio=0)
Create a new CTxMemPool.
void LimitSize(CCoinsViewCache &coins_cache, size_t limit, std::chrono::seconds age) EXCLUSIVE_LOCKS_REQUIRED(cs
Reduce the size of the mempool by expiring and then trimming the mempool.
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.
static const int ROLLING_FEE_HALFLIFE
void GetTransactionAncestry(const TxId &txid, size_t &ancestors, size_t &descendants, size_t *ancestorsize=nullptr, Amount *ancestorfees=nullptr) const
Calculate the ancestor and descendant count for the given transaction.
CTransactionRef get(const TxId &txid) const
void PrioritiseTransaction(const TxId &txid, const Amount nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
std::set< TxId > m_unbroadcast_txids GUARDED_BY(cs)
Track locally submitted transactions to periodically retry initial broadcast.
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)
double rollingMinimumFeeRate GUARDED_BY(cs)
minimum fee to get into the pool, decreases exponentially
std::atomic< bool > wellingtonLatched
Wellington activation latch.
const CTransaction * GetConflictTx(const COutPoint &prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Get the transaction in the pool that spends the same prevout.
std::set< TxId > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
void RemoveStaged(const setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of hash.
unsigned long size() const
bool m_is_loaded GUARDED_BY(cs)
uint64_t m_sequence_number GUARDED_BY(cs)
void ApplyDelta(const TxId &txid, Amount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs)
For each transaction being removed, update ancestors and any direct children.
std::optional< txiter > GetIter(const TxId &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given txid, if found.
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void cs_main
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...
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove after wellington activates as this will be inaccurate.
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 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...
void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs)
Chainstate stores and provides an API to update our local knowledge of the current best chain.
std::unordered_map< TxId, TxInfo, SaltedTxIdHasher > TxInfoMap
void removeEntry(indexed_disconnected_transactions::index< insertion_order >::type::iterator entry)
indexed_disconnected_transactions queuedTx
TxInfoMap txInfo
populated by importMempool(); the original tx entry times and feeDeltas
~DisconnectedBlockTransactions()
void removeForBlock(const std::vector< CTransactionRef > &vtx)
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
size_t DynamicMemoryUsage() const
void addTransaction(const CTransactionRef &tx)
const indexed_disconnected_transactions & GetQueuedTx() const
Map whose keys are pointers, but are compared by their dereferenced values.
static size_t RecursiveDynamicUsage(const CScript &script)
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
std::shared_ptr< const CTransaction > CTransactionRef
static constexpr Amount zero()
bool operator()(const std::reference_wrapper< T > &a, const std::reference_wrapper< T > &b) const
bool operator()(const T &a, const T &b) const
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Sort by feerate of entry (modfee/vsize) in descending order.
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
TxInfo(const std::chrono::seconds &time_, Amount feeDelta_, unsigned height_) noexcept
const std::chrono::seconds time
CBlockIndex * maxInputBlock
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.
result_type operator()(const CTxMemPoolEntry &entry) const
result_type operator()(const CTransactionRef &tx) const
DisconnectedBlockTransactions.
#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.
@ BLOCK
Removed for block.
@ EXPIRY
Expired from mempool.
@ REPLACED
Removed for replacement.
@ 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....
RecursiveMutex cs_main
Global state.
bool TestLockPointValidity(const 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.