6 #ifndef BITCOIN_TXMEMPOOL_H 7 #define BITCOIN_TXMEMPOOL_H 31 #include <boost/multi_index/hashed_index.hpp> 32 #include <boost/multi_index/ordered_index.hpp> 33 #include <boost/multi_index/sequenced_index.hpp> 34 #include <boost/multi_index_container.hpp> 65 bool operator()(
const std::reference_wrapper<T>& a,
const std::reference_wrapper<T>& b)
const 67 return a.get().GetTx().GetHash() < b.get().GetTx().GetHash();
72 return a->GetTx().GetHash() < b->GetTx().GetHash();
93 typedef std::set<CTxMemPoolEntryRef, CompareIteratorByHash>
Parents;
94 typedef std::set<CTxMemPoolEntryRef, CompareIteratorByHash>
Children;
125 int64_t time,
unsigned int entry_height,
126 bool spends_coinbase,
134 std::chrono::seconds
GetTime()
const {
return std::chrono::seconds{
nTime}; }
181 return tx->GetHash();
196 return tx->GetWitnessHash();
210 double a_mod_fee, a_size, b_mod_fee, b_size;
216 double f1 = a_mod_fee * b_size;
217 double f2 = a_size * b_mod_fee;
283 double a_mod_fee, a_size, b_mod_fee, b_size;
289 double f1 = a_mod_fee * b_size;
290 double f2 = a_size * b_mod_fee;
293 return a.GetTx().GetHash() < b.GetTx().GetHash();
299 template <
typename T>
304 double f1 = (double)a.GetModifiedFee() * a.GetSizeWithAncestors();
305 double f2 = (double)a.GetModFeesWithAncestors() * a.GetTxSize();
308 mod_fee = a.GetModFeesWithAncestors();
309 size = a.GetSizeWithAncestors();
311 mod_fee = a.GetModifiedFee();
312 size = a.GetTxSize();
443 mutable bool blockSinceLastRollingFeeBump
GUARDED_BY(
cs);
462 typedef boost::multi_index_container<
464 boost::multi_index::indexed_by<
466 boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
468 boost::multi_index::hashed_unique<
469 boost::multi_index::tag<index_by_wtxid>,
474 boost::multi_index::ordered_non_unique<
475 boost::multi_index::tag<descendant_score>,
476 boost::multi_index::identity<CTxMemPoolEntry>,
480 boost::multi_index::ordered_non_unique<
481 boost::multi_index::tag<entry_time>,
482 boost::multi_index::identity<CTxMemPoolEntry>,
486 boost::multi_index::ordered_non_unique<
487 boost::multi_index::tag<ancestor_score>,
488 boost::multi_index::identity<CTxMemPoolEntry>,
524 using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
525 std::vector<std::pair<uint256, txiter>> vTxHashes
GUARDED_BY(
cs);
531 typedef std::map<txiter, setEntries, CompareIteratorByHash>
cacheMap;
557 uint64_t limitAncestorCount,
558 uint64_t limitAncestorSize,
559 uint64_t limitDescendantCount,
560 uint64_t limitDescendantSize,
699 uint64_t limitAncestorCount,
700 uint64_t limitAncestorSize,
701 uint64_t limitDescendantCount,
702 uint64_t limitDescendantSize,
773 return (mapTx.count(gtxid.
GetHash()) != 0);
783 std::vector<TxMempoolInfo>
infoAll()
const;
803 return m_unbroadcast_txids;
810 return m_unbroadcast_txids.count(txid) != 0;
815 return m_sequence_number++;
819 return m_sequence_number;
884 return m_epoch.visited(
it->m_epoch_marker);
913 std::unordered_map<COutPoint, Coin, SaltedOutpointHasher>
m_temp_added;
945 typedef boost::multi_index_container<
947 boost::multi_index::indexed_by<
949 boost::multi_index::hashed_unique<
950 boost::multi_index::tag<txid_index>,
955 boost::multi_index::sequenced<
956 boost::multi_index::tag<insertion_order>
993 for (
auto const &tx : vtx) {
994 auto it =
queuedTx.find(tx->GetHash());
1003 void removeEntry(indexed_disconnected_transactions::index<insertion_order>::type::iterator entry)
1016 #endif // BITCOIN_TXMEMPOOL_H
std::shared_ptr< const CTransaction > CTransactionRef
CAmount GetModFeesWithAncestors() const
size_t vTxHashesIdx
Index in mempool's vTxHashes.
const bool spendsCoinbase
keep track of transactions that spend a coinbase
void queryHashes(std::vector< uint256 > &vtxid) const
Information about a mempool transaction.
uint64_t GetSizeWithAncestors() const
CAmount nModFeesWithDescendants
... and total fees (all including us)
void UpdateLockPoints(const LockPoints &lp)
void UpdateModifiedFee(CAmount fee_diff)
CAmount m_modified_fee
Used for determining the priority of the transaction for mining in a block.
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
std::vector< TxMempoolInfo > infoAll() const
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Children & GetMemPoolChildren() const
CTxMemPoolEntry(const CTransactionRef &tx, CAmount fee, int64_t time, unsigned int entry_height, bool spends_coinbase, int64_t sigops_cost, LockPoints lp)
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 ...
~DisconnectedBlockTransactions()
size_t GetTxWeight() const
Options struct containing limit options for a CTxMemPool.
An in-memory indexed chain of blocks.
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of hash.
const int64_t m_max_size_bytes
size_t DynamicMemoryUsage() const
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Removed in size limiting.
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
const unsigned int entryHeight
Chain height when entering the mempool.
CAmount GetModFeesWithDescendants() const
void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
const uint256 & GetHash() const
unsigned long size() const
uint64_t GetSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
uint64_t GetSizeWithDescendants() const
const std::optional< unsigned > m_max_datacarrier_bytes
bool CompareDepthAndScore(const uint256 &hasha, const uint256 &hashb, bool wtxid=false)
std::set< txiter, CompareIteratorByHash > setEntries
void addTransaction(const CTransactionRef &tx)
const Children & GetMemPoolChildrenConst() const
size_t vsize
Virtual size of the transaction.
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal...
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
bool GetSpendsCoinbase() const
std::atomic< unsigned int > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
const int64_t nTime
Local time when entering the mempool.
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason) 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...
CFeeRate GetMinFee() const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps)
uint64_t nCountWithDescendants
number of descendant transactions
CAmount GetModifiedFee() const
bool operator()(const std::reference_wrapper< T > &a, const std::reference_wrapper< T > &b) 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...
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...
bool operator()(const T &a, const T &b) const
CTransactionRef tx
The transaction itself.
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
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...
const int m_check_ratio
Value n means that 1 times in n we check.
void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
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
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)
Parents & GetMemPoolParents() const
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
const CFeeRate m_dust_relay_feerate
Removed for reorganization.
indexed_disconnected_transactions queuedTx
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coin to signify they are only in the memory pool (since 0...
int64_t nSigOpCostWithAncestors
const size_t nTxWeight
... and avoid recomputing tx weight (also used for GetTxSize())
std::set< CTxMemPoolEntryRef, CompareIteratorByHash > Parents
result_type operator()(const CTxMemPoolEntry &entry) const
void AddUnbroadcastTx(const uint256 &txid)
Adds a transaction to the unbroadcast set.
Chainstate stores and provides an API to update our local knowledge of the current best chain...
const std::chrono::seconds m_expiry
uint64_t nSizeWithAncestors
Abstract view on the open txout dataset.
size_t DynamicMemoryUsage() const
unsigned int GetHeight() const
int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Expire all transaction (and their dependencies) in the mempool older than time.
void removeForBlock(const std::vector< CTransactionRef > &vtx)
const uint256 & GetWitnessHash() const
bool m_load_tried GUARDED_BY(cs)
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
const uint256 & GetHash() const
const CAmount & GetFee() const
Removed for conflict with in-block transaction.
DisconnectedBlockTransactions.
result_type operator()(const CTxMemPoolEntry &entry) const
CTransactionRef GetSharedTx() const
void removeUnchecked(txiter entry, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Before calling removeUnchecked for a given transaction, UpdateForRemoveFromMempool must be called on ...
const CFeeRate m_min_relay_feerate
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)
CAmount nModFeesWithAncestors
uint64_t m_sequence_number GUARDED_BY(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...
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.
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.
An outpoint - a combination of a transaction hash and an index n into its vout.
uint64_t nSizeWithDescendants
... and size
void AddTransactionsUpdated(unsigned int n)
uint64_t GetCountWithDescendants() const
void ApplyDelta(const uint256 &hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
const size_t nUsageSize
... and total memory usage
const CFeeRate m_incremental_relay_feerate
const bool m_require_standard
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
bool GetLoadTried() const
int64_t GetSigOpCostWithAncestors() const
const int64_t sigOpCost
Total sigop cost.
std::optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
uint64_t GetCountWithAncestors() const
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void cs_main
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...
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
std::map< txiter, setEntries, CompareIteratorByHash > cacheMap
std::set< uint256 > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
The block chain is a tree shaped structure starting with the genesis block at the root...
LockPoints lockPoints
Track the height and time at which tx was final.
void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount)
const CTransaction & GetTx() const
void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs)
void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Set ancestor state for an entry.
CAmount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
std::reference_wrapper< const CTxMemPoolEntry > CTxMemPoolEntryRef
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)
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. ...
CBlockPolicyEstimator *const minerPolicyEstimator
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::set< CTxMemPoolEntryRef, CompareIteratorByHash > Children
uint64_t nCountWithAncestors
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
int64_t GetSigOpCost() const
unsigned int GetTransactionsUpdated() const
Epoch::Marker m_epoch_marker
epoch when last touched, useful for graph algorithms
const CAmount nFee
Cached to avoid expensive parent-transaction lookups.
CAmount fee
Fee of the transaction.
static size_t RecursiveDynamicUsage(const CScript &script)
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.
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)
const bool m_permit_bare_multisig
uint64_t cachedInnerUsage
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.
size_t DynamicMemoryUsage() const
The basic transaction that is broadcasted on the network and contained in blocks. ...
CCoinsView backed by another CCoinsView.
const LockPoints & GetLockPoints() 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.
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
void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs)
Sever link between specified transaction and direct children.
const Parents & GetMemPoolParentsConst() const
std::chrono::seconds m_time
Time the transaction entered the mempool.
CBlockIndex * maxInputBlock
void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
CCoinsView that brings transactions from a mempool into view.
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
A generic txid reference (txid or wtxid).
void removeEntry(indexed_disconnected_transactions::index< insertion_order >::type::iterator entry)
std::unordered_map< COutPoint, Coin, SaltedOutpointHasher > m_temp_added
Coins made available by transactions being validated.
static GenTxid Txid(const uint256 &hash)
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.
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)
Epoch: RAII-style guard for using epoch-based graph traversal algorithms.
uint64_t totalTxSize GUARDED_BY(cs)
sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discount...