6 #ifndef BITCOIN_TXMEMPOOL_H 7 #define BITCOIN_TXMEMPOOL_H 24 #include <boost/multi_index_container.hpp> 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/signals2/signal.hpp> 34 static const uint32_t MEMPOOL_HEIGHT = 0x7FFFFFFF;
94 int64_t _nTime,
unsigned int _entryHeight,
197 return tx->GetHash();
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();
461 typedef boost::multi_index_container<
463 boost::multi_index::indexed_by<
465 boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
467 boost::multi_index::ordered_non_unique<
468 boost::multi_index::tag<descendant_score>,
469 boost::multi_index::identity<CTxMemPoolEntry>,
473 boost::multi_index::ordered_non_unique<
474 boost::multi_index::tag<entry_time>,
475 boost::multi_index::identity<CTxMemPoolEntry>,
479 boost::multi_index::ordered_non_unique<
480 boost::multi_index::tag<ancestor_score>,
481 boost::multi_index::identity<CTxMemPoolEntry>,
490 using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
495 return a->GetTx().GetHash() < b->GetTx().GetHash();
504 typedef std::map<txiter, setEntries, CompareIteratorByHash>
cacheMap;
511 typedef std::map<txiter, TxLinks, CompareIteratorByHash>
txlinksMap;
534 void setSanityCheck(
double dFrequency = 1.0) {
LOCK(
cs); nCheckFrequency =
static_cast<uint32_t
>(dFrequency * 4294967295.0); }
653 return (mapTx.count(hash) != 0);
658 std::vector<TxMempoolInfo>
infoAll()
const;
745 typedef boost::multi_index_container<
747 boost::multi_index::indexed_by<
749 boost::multi_index::hashed_unique<
750 boost::multi_index::tag<txid_index>,
755 boost::multi_index::sequenced<
756 boost::multi_index::tag<insertion_order>
793 for (
auto const &tx : vtx) {
794 auto it =
queuedTx.find(tx->GetHash());
803 void removeEntry(indexed_disconnected_transactions::index<insertion_order>::type::iterator entry)
816 #endif // BITCOIN_TXMEMPOOL_H
CAmount GetModFeesWithAncestors() const
size_t vTxHashesIdx
Index in mempool's vTxHashes.
const bool spendsCoinbase
keep track of transactions that spend a coinbase
Information about a mempool transaction.
int Expire(int64_t time)
Expire all transaction (and their dependencies) in the mempool older than time.
update_fee_delta(int64_t _feeDelta)
uint64_t GetSizeWithAncestors() const
CAmount nModFeesWithDescendants
... and total fees (all including us)
void UpdateLockPoints(const LockPoints &lp)
std::map< txiter, TxLinks, CompareIteratorByHash > txlinksMap
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
std::vector< TxMempoolInfo > infoAll() const
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN)
uint32_t nCheckFrequency GUARDED_BY(cs)
Value n means that n times in 2^32 we check.
void removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
~DisconnectedBlockTransactions()
size_t GetTxWeight() const
boost::signals2::signal< void(CTransactionRef)> NotifyEntryAdded
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
Retrieve the Coin (unspent transaction output) for a given outpoint.
Manually removed or unknown reason.
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
TxMempoolInfo info(const uint256 &hash) 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
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
uint64_t GetTotalTxSize() const
bool operator()(const txiter &a, const txiter &b) const
bool HasNoInputsOf(const CTransaction &tx) const
Check that none of this transactions inputs are in the mempool, and thus the tx is not dependent on o...
uint64_t GetSizeWithDescendants() const
std::set< txiter, CompareIteratorByHash > setEntries
void queryHashes(std::vector< uint256 > &vtxid)
void addTransaction(const CTransactionRef &tx)
void operator()(CTxMemPoolEntry &e)
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
const int64_t nTime
Local time when entering the mempool.
void TrimToSize(size_t sizelimit, std::vector< COutPoint > *pvNoSpendsRemaining=nullptr)
Remove transactions from the mempool until its dynamic size is <= sizelimit.
void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps)
std::shared_ptr< const CTransaction > CTransactionRef
uint64_t nCountWithDescendants
number of descendant transactions
int64_t lastRollingFeeUpdate
void operator()(CTxMemPoolEntry &e)
int64_t nFeeDelta
The fee delta.
bool isSpent(const COutPoint &outpoint) const
update_descendant_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount)
void UpdateTransactionsFromBlock(const std::vector< uint256 > &vHashesToUpdate)
When adding transactions from a disconnected block back to the mempool, new mempool entries may have ...
CTransactionRef tx
The transaction itself.
void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
void check(const CCoinsViewCache *pcoins) const
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
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)
bool operator()(const T &a, const T &b) const
bool blockSinceLastRollingFeeBump
Removed for reorganization.
indexed_disconnected_transactions queuedTx
int64_t nSigOpCostWithAncestors
void setSanityCheck(double dFrequency=1.0)
const size_t nTxWeight
... and avoid recomputing tx weight (also used for GetTxSize())
result_type operator()(const CTxMemPoolEntry &entry) const
std::vector< std::pair< uint256, txiter > > vTxHashes
All tx witness hashes/entries in mapTx, in random order.
void UpdateFeeDelta(int64_t feeDelta)
void removeUnchecked(txiter entry, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN) EXCLUSIVE_LOCKS_REQUIRED(cs)
Before calling removeUnchecked for a given transaction, UpdateForRemoveFromMempool must be called on ...
uint64_t nSizeWithAncestors
int64_t feeDelta
Used for determining the priority of the transaction for mining in a block.
Abstract view on the open txout dataset.
size_t DynamicMemoryUsage() const
unsigned int GetHeight() const
void ApplyDelta(const uint256 hash, CAmount &nFeeDelta) const
bool exists(const uint256 &hash) const
void removeForBlock(const std::vector< CTransactionRef > &vtx)
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.
CTransactionRef GetSharedTx() const
void UpdateParent(txiter entry, txiter parent, bool add)
std::map< uint256, CAmount > mapDeltas
uint64_t cachedInnerUsage
sum of dynamic memory usage of all the map elements (NOT the maps themselves)
Sort an entry by max(score/size of entry's tx, score/size with all descendants).
static const int ROLLING_FEE_HALFLIFE
CAmount nModFeesWithAncestors
const setEntries & GetMemPoolChildren(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
unsigned int nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
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
CFeeRate GetMinFee(size_t sizelimit) const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
const size_t nUsageSize
... and total memory usage
uint64_t totalTxSize
sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discount...
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
int64_t GetSigOpCostWithAncestors() const
const int64_t sigOpCost
Total sigop cost.
uint64_t GetCountWithAncestors() const
int64_t nTime
Time the transaction entered the mempool.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
std::vector< indexed_transaction_set::const_iterator > GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs)
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
bool CompareDepthAndScore(const uint256 &hasha, const uint256 &hashb)
void operator()(CTxMemPoolEntry &e)
std::map< txiter, setEntries, CompareIteratorByHash > cacheMap
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.
update_ancestor_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount, int64_t _modifySigOpsCost)
int64_t GetModifiedFee() const
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. ...
boost::optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
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< 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 nCountWithAncestors
Fee rate in satoshis per kilobyte: CAmount / kB.
int64_t GetSigOpCost() const
void UpdateChild(txiter entry, txiter child, bool add)
unsigned int GetTransactionsUpdated() const
const CAmount nFee
Cached to avoid expensive parent-transaction lookups.
size_t operator()(const uint256 &txid) const
const setEntries & GetMemPoolParents(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
update_lock_points(const LockPoints &_lp)
void ClearPrioritisation(const uint256 hash)
boost::signals2::signal< void(CTransactionRef, MemPoolRemovalReason)> NotifyEntryRemoved
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight)
Called when a block is connected.
uint64_t cachedInnerUsage
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
size_t DynamicMemoryUsage() const
CFeeRate feeRate
Feerate of the transaction.
The basic transaction that is broadcasted on the network and contained in blocks. ...
CCoinsView backed by another CCoinsView.
const LockPoints & GetLockPoints() const
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...
const CTxMemPool & mempool
void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs)
Sever link between specified transaction and direct children.
void UpdateForDescendants(txiter updateIt, cacheMap &cachedDescendants, const std::set< uint256 > &setExclude) EXCLUSIVE_LOCKS_REQUIRED(cs)
UpdateForDescendants is used by UpdateTransactionsFromBlock to update the descendants for a single tr...
CBlockPolicyEstimator * minerPolicyEstimator
CBlockIndex * maxInputBlock
void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
double rollingMinimumFeeRate
minimum fee to get into the pool, decreases exponentially
CCoinsView that brings transactions from a mempool into view.
CTxMemPoolEntry(const CTransactionRef &_tx, const CAmount &_nFee, int64_t _nTime, unsigned int _entryHeight, bool spendsCoinbase, int64_t nSigOpsCost, LockPoints lp)
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
void addUnchecked(const CTxMemPoolEntry &entry, bool validFeeEstimate=true) EXCLUSIVE_LOCKS_REQUIRED(cs)
void removeEntry(indexed_disconnected_transactions::index< insertion_order >::type::iterator entry)
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.
void operator()(CTxMemPoolEntry &e)
void GetTransactionAncestry(const uint256 &txid, size_t &ancestors, size_t &descendants) const
Calculate the ancestor and descendant count for the given transaction.