6 #ifndef BITCOIN_TXMEMPOOL_H
7 #define BITCOIN_TXMEMPOOL_H
24 #include "boost/multi_index_container.hpp"
25 #include "boost/multi_index/ordered_index.hpp"
26 #include "boost/multi_index/hashed_index.hpp"
28 #include <boost/signals2/signal.hpp>
35 return COIN * 144 / 250;
46 static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
116 int64_t _nTime,
double _entryPriority,
unsigned int _entryHeight,
128 double GetPriority(
unsigned int currentHeight)
const;
244 double f1 = aModFee * bSize;
245 double f2 = aSize * bModFee;
301 double f1 = aFees * bSize;
302 double f2 = aSize * bFees;
451 typedef boost::multi_index_container<
453 boost::multi_index::indexed_by<
455 boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
457 boost::multi_index::ordered_non_unique<
458 boost::multi_index::tag<descendant_score>,
459 boost::multi_index::identity<CTxMemPoolEntry>,
463 boost::multi_index::ordered_non_unique<
464 boost::multi_index::tag<entry_time>,
465 boost::multi_index::identity<CTxMemPoolEntry>,
469 boost::multi_index::ordered_unique<
470 boost::multi_index::tag<mining_score>,
471 boost::multi_index::identity<CTxMemPoolEntry>,
475 boost::multi_index::ordered_non_unique<
476 boost::multi_index::tag<ancestor_score>,
477 boost::multi_index::identity<CTxMemPoolEntry>,
486 typedef indexed_transaction_set::nth_index<0>::type::iterator
txiter;
491 return a->GetTx().GetHash() < b->GetTx().GetHash();
499 typedef std::map<txiter, setEntries, CompareIteratorByHash>
cacheMap;
506 typedef std::map<txiter, TxLinks, CompareIteratorByHash>
txlinksMap;
516 std::map<uint256, std::pair<double, CAmount> >
mapDeltas;
542 void removeForBlock(
const std::vector<CTransactionRef>& vtx,
unsigned int nBlockHeight);
593 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;
612 void TrimToSize(
size_t sizelimit, std::vector<uint256>* pvNoSpendsRemaining=NULL);
635 return (
mapTx.count(hash) != 0);
640 std::vector<TxMempoolInfo>
infoAll()
const;
685 const std::set<uint256> &setExclude);
730 if (a.first == b.first)
732 return a.first < b.first;
int64_t CAmount
Amount in satoshis (Can be negative)
Non-refcounted RAII wrapper for FILE*.
The block chain is a tree shaped structure starting with the genesis block at the root,...
We want to be able to estimate feerates that are needed on tx's to be included in a certain number of...
Pruned version of CTransaction: only retains metadata and unspent transaction outputs.
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 memorypool into view.
bool GetCoins(const uint256 &txid, CCoins &coins) const
Retrieve the CCoins (unspent transaction outputs) for a given txid.
bool HaveCoins(const uint256 &txid) const
Just check whether we have data for a given txid.
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
const CTxMemPool & mempool
Wrapped boost mutex: supports recursive locking, but no waiting TODO: We should move away from using ...
Fee rate in satoshis per kilobyte: CAmount / kB.
The basic transaction that is broadcasted on the network and contained in blocks.
const uint256 & GetHash() const
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
size_t nTxWeight
... and avoid recomputing tx weight (also used for GetTxSize())
int64_t GetSigOpCostWithAncestors() const
double GetPriority(unsigned int currentHeight) const
Fast calculation of lower bound of current priority as update from entry priority.
void UpdateFeeDelta(int64_t feeDelta)
int64_t nSigOpCostWithAncestors
unsigned int entryHeight
Chain height when entering the mempool.
const CTransaction & GetTx() const
int64_t feeDelta
Used for determining the priority of the transaction for mining in a block.
unsigned int GetHeight() const
bool spendsCoinbase
keep track of transactions that spend a coinbase
void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount)
size_t nUsageSize
... and total memory usage
bool GetSpendsCoinbase() const
int64_t nTime
Local time when entering the mempool.
uint64_t GetCountWithDescendants() const
void UpdateLockPoints(const LockPoints &lp)
int64_t sigOpCost
Total sigop cost.
void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int modifySigOps)
CAmount nModFeesWithAncestors
const LockPoints & GetLockPoints() const
CAmount GetModFeesWithDescendants() const
int64_t GetSigOpCost() const
uint64_t nCountWithDescendants
number of descendant transactions
double entryPriority
Priority when entering the mempool.
CTxMemPoolEntry(const CTransactionRef &_tx, const CAmount &_nFee, int64_t _nTime, double _entryPriority, unsigned int _entryHeight, CAmount _inChainInputValue, bool spendsCoinbase, int64_t nSigOpsCost, LockPoints lp)
uint64_t GetSizeWithAncestors() const
CTransactionRef GetSharedTx() const
uint64_t nSizeWithDescendants
... and size
size_t DynamicMemoryUsage() const
CAmount nModFeesWithDescendants
... and total fees (all including us)
uint64_t nCountWithAncestors
CAmount GetModFeesWithAncestors() const
uint64_t GetSizeWithDescendants() const
uint64_t GetCountWithAncestors() const
LockPoints lockPoints
Track the height and time at which tx was final.
uint64_t nSizeWithAncestors
int64_t GetModifiedFee() const
const CAmount & GetFee() const
CAmount inChainInputValue
Sum of all txin values that are already in blockchain.
size_t nModSize
... and modified size for priority
size_t vTxHashesIdx
Index in mempool's vTxHashes.
size_t GetTxWeight() const
CAmount nFee
Cached to avoid expensive parent-transaction lookups.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
bool TransactionWithinChainLimit(const uint256 &txid, size_t chainLimit) const
Returns false if the transaction is in the mempool and not within the chain limit specified.
bool ReadFeeEstimates(CAutoFile &filein)
const setEntries & GetMemPoolChildren(txiter entry) const
void ClearPrioritisation(const uint256 hash)
void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN)
void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors)
Update ancestors of hash to add/remove it as a descendant transaction.
double estimatePriority(int nBlocks) const
Estimate priority needed to get into the next nBlocks.
unsigned int nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
CFeeRate GetMinFee(size_t sizelimit) const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
bool WriteFeeEstimates(CAutoFile &fileout) const
Write/Read estimates to disk.
int Expire(int64_t time)
Expire all transaction (and their dependencies) in the mempool older than time.
boost::signals2::signal< void(CTransactionRef)> NotifyEntryAdded
CFeeRate estimateSmartFee(int nBlocks, int *answerFoundAtBlocks=NULL) const
Estimate fee rate needed to get into the next nBlocks If no answer can be given at nBlocks,...
uint32_t nCheckFrequency
Value n means that n times in 2^32 we check.
void AddTransactionsUpdated(unsigned int n)
CFeeRate estimateFee(int nBlocks) const
Estimate fee rate needed to get into the next nBlocks.
std::map< uint256, std::pair< double, CAmount > > mapDeltas
void setSanityCheck(double dFrequency=1.0)
double rollingMinimumFeeRate
minimum fee to get into the pool, decreases exponentially
void queryHashes(std::vector< uint256 > &vtxid)
std::vector< indexed_transaction_set::const_iterator > GetSortedDepthAndScore() const
void UpdateForDescendants(txiter updateIt, cacheMap &cachedDescendants, const std::set< uint256 > &setExclude)
UpdateForDescendants is used by UpdateTransactionsFromBlock to update the descendants for a single tr...
void removeUnchecked(txiter entry, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN)
Before calling removeUnchecked for a given transaction, UpdateForRemoveFromMempool must be called on ...
CTransactionRef get(const uint256 &hash) const
bool addUnchecked(const uint256 &hash, const CTxMemPoolEntry &entry, bool validFeeEstimate=true)
size_t DynamicMemoryUsage() const
std::vector< TxMempoolInfo > infoAll() const
std::vector< std::pair< uint256, txiter > > vTxHashes
All tx witness hashes/entries in mapTx, in random order.
indirectmap< COutPoint, const CTransaction * > mapNextTx
int64_t lastRollingFeeUpdate
void trackPackageRemoved(const CFeeRate &rate)
void TrimToSize(size_t sizelimit, std::vector< uint256 > *pvNoSpendsRemaining=NULL)
Remove transactions from the mempool until its dynamic size is <= sizelimit.
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...
void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants)
For each transaction being removed, update ancestors and any direct children.
void UpdateTransactionsFromBlock(const std::vector< uint256 > &hashesToUpdate)
When adding transactions from a disconnected block back to the mempool, new mempool entries may have ...
bool CompareDepthAndScore(const uint256 &hasha, const uint256 &hashb)
void UpdateChildrenForRemoval(txiter entry)
Sever link between specified transaction and direct children.
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
Try to calculate all in-mempool ancestors of entry.
bool exists(uint256 hash) const
static const int ROLLING_FEE_HALFLIFE
void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta) const
std::set< txiter, CompareIteratorByHash > setEntries
void removeConflicts(const CTransaction &tx)
void UpdateChild(txiter entry, txiter child, bool add)
uint64_t cachedInnerUsage
sum of dynamic memory usage of all the map elements (NOT the maps themselves)
indexed_transaction_set::nth_index< 0 >::type::iterator txiter
double estimateSmartPriority(int nBlocks, int *answerFoundAtBlocks=NULL) const
Estimate priority needed to get into the next nBlocks If no answer can be given at nBlocks,...
void check(const CCoinsViewCache *pcoins) const
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
std::map< txiter, setEntries, CompareIteratorByHash > cacheMap
CTxMemPool(const CFeeRate &_minReasonableRelayFee)
Create a new CTxMemPool.
const setEntries & GetMemPoolParents(txiter entry) const
uint64_t totalTxSize
sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discount...
void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN)
Remove a set of transactions from the mempool.
indexed_transaction_set mapTx
void removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags)
TxMempoolInfo info(const uint256 &hash) const
boost::signals2::signal< void(CTransactionRef, MemPoolRemovalReason)> NotifyEntryRemoved
uint64_t GetTotalTxSize()
void pruneSpent(const uint256 &hash, CCoins &coins)
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_unique< boost::multi_index::tag< mining_score >, boost::multi_index::identity< CTxMemPoolEntry >, CompareTxMemPoolEntryByScore >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< ancestor_score >, boost::multi_index::identity< CTxMemPoolEntry >, CompareTxMemPoolEntryByAncestorFee > > > indexed_transaction_set
void UpdateParent(txiter entry, txiter parent, bool add)
void CalculateDescendants(txiter it, setEntries &setDescendants)
Populate setDescendants with all in-mempool descendants of hash.
void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors)
Set ancestor state for an entry.
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight)
Called when a block is connected.
bool blockSinceLastRollingFeeBump
CBlockPolicyEstimator * minerPolicyEstimator
std::map< txiter, TxLinks, CompareIteratorByHash > txlinksMap
unsigned int GetTransactionsUpdated() const
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Sort an entry by max(score/size of entry's tx, score/size with all descendants).
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
bool UseDescendantScore(const CTxMemPoolEntry &a) const
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Sort by score of entry ((fee+delta)/size) in descending order.
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
bool operator()(const txiter &a, const txiter &b) const
CBlockIndex * maxInputBlock
bool operator()(const TxCoinAgePriority &a, const TxCoinAgePriority &b)
Information about a mempool transaction.
int64_t nFeeDelta
The fee delta.
int64_t nTime
Time the transaction entered the mempool.
CTransactionRef tx
The transaction itself.
CFeeRate feeRate
Feerate of the transaction.
result_type operator()(const CTxMemPoolEntry &entry) const
void operator()(CTxMemPoolEntry &e)
update_ancestor_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount, int64_t _modifySigOpsCost)
void operator()(CTxMemPoolEntry &e)
update_descendant_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount)
update_fee_delta(int64_t _feeDelta)
void operator()(CTxMemPoolEntry &e)
void operator()(CTxMemPoolEntry &e)
update_lock_points(const LockPoints &_lp)
std::shared_ptr< const CTransaction > CTransactionRef
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
@ SIZELIMIT
Expired from mempool.
@ BLOCK
Removed for reorganization.
@ EXPIRY
Manually removed or unknown reason.
@ REPLACED
Removed for conflict with in-block transaction.
@ CONFLICT
Removed for block.
@ REORG
Removed in size limiting.
double AllowFreeThreshold()
bool AllowFree(double dPriority)
std::pair< double, CTxMemPool::txiter > TxCoinAgePriority