Fabcoin Core
0.16.2
P2P Digital Currency
|
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the next block. More...
#include <txmempool.h>
Classes | |
struct | CompareIteratorByHash |
struct | TxLinks |
Public Types | |
typedef 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 >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< ancestor_score_or_gas_price >, boost::multi_index::identity< CTxMemPoolEntry >, CompareTxMemPoolEntryByAncestorFeeOrGasPrice > > > | indexed_transaction_set |
typedef indexed_transaction_set::nth_index< 0 >::type::iterator | txiter |
typedef std::set< txiter, CompareIteratorByHash > | setEntries |
Public Member Functions | |
const setEntries & | GetMemPoolParents (txiter entry) const |
const setEntries & | GetMemPoolChildren (txiter entry) const |
CTxMemPool (CBlockPolicyEstimator *estimator=nullptr) | |
Create a new CTxMemPool. More... | |
void | check (const CCoinsViewCache *pcoins) const |
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transactions that spend the same inputs, all inputs are in the mapNextTx array). More... | |
void | setSanityCheck (double dFrequency=1.0) |
bool | addUnchecked (const uint256 &hash, const CTxMemPoolEntry &entry, bool validFeeEstimate=true) |
bool | addUnchecked (const uint256 &hash, const CTxMemPoolEntry &entry, setEntries &setAncestors, bool validFeeEstimate=true) |
void | removeRecursive (const CTransaction &tx, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN) |
void | removeForReorg (const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags) |
void | removeConflicts (const CTransaction &tx) |
void | removeForBlock (const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) |
Called when a block is connected. More... | |
void | clear () |
void | _clear () |
bool | CompareDepthAndScore (const uint256 &hasha, const uint256 &hashb) |
void | queryHashes (std::vector< uint256 > &vtxid) |
bool | isSpent (const COutPoint &outpoint) |
unsigned int | GetTransactionsUpdated () const |
void | AddTransactionsUpdated (unsigned int n) |
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 other mempool transactions to be included in a block. More... | |
void | PrioritiseTransaction (const uint256 &hash, const CAmount &nFeeDelta) |
Affect CreateNewBlock prioritisation of transactions. More... | |
void | ApplyDelta (const uint256 hash, CAmount &nFeeDelta) const |
void | ClearPrioritisation (const uint256 hash) |
void | RemoveStaged (setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN) |
Remove a set of transactions from the mempool. More... | |
void | UpdateTransactionsFromBlock (const std::vector< uint256 > &vHashesToUpdate) |
When adding transactions from a disconnected block back to the mempool, new mempool entries may have children in the mempool (which is generally not the case when otherwise adding transactions). More... | |
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. More... | |
void | CalculateDescendants (txiter it, setEntries &setDescendants) |
Populate setDescendants with all in-mempool descendants of hash. More... | |
CFeeRate | GetMinFee (size_t sizelimit) const |
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions. More... | |
void | TrimToSize (size_t sizelimit, std::vector< COutPoint > *pvNoSpendsRemaining=nullptr) |
Remove transactions from the mempool until its dynamic size is <= sizelimit. More... | |
int | Expire (int64_t time) |
Expire all transaction (and their dependencies) in the mempool older than time. More... | |
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. More... | |
unsigned long | size () |
uint64_t | GetTotalTxSize () |
bool | exists (uint256 hash) const |
bool | exists (const COutPoint &outpoint) const |
CTransactionRef | get (const uint256 &hash) const |
TxMempoolInfo | info (const uint256 &hash) const |
std::vector< TxMempoolInfo > | infoAll () const |
size_t | DynamicMemoryUsage () const |
Public Attributes | |
CCriticalSection | cs |
indexed_transaction_set | mapTx |
std::vector< std::pair< uint256, txiter > > | vTxHashes |
All tx witness hashes/entries in mapTx, in random order. More... | |
indirectmap< COutPoint, const CTransaction * > | mapNextTx |
std::map< uint256, CAmount > | mapDeltas |
boost::signals2::signal< void(CTransactionRef)> | NotifyEntryAdded |
boost::signals2::signal< void(CTransactionRef, MemPoolRemovalReason)> | NotifyEntryRemoved |
Static Public Attributes | |
static const int | ROLLING_FEE_HALFLIFE = 60 * 60 * 12 |
Private Types | |
typedef std::map< txiter, setEntries, CompareIteratorByHash > | cacheMap |
typedef std::map< txiter, TxLinks, CompareIteratorByHash > | txlinksMap |
Private Member Functions | |
void | trackPackageRemoved (const CFeeRate &rate) |
void | UpdateParent (txiter entry, txiter parent, bool add) |
void | UpdateChild (txiter entry, txiter child, bool add) |
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 transaction that has been added to the mempool but may have child transactions in the mempool, eg during a chain reorg. More... | |
void | UpdateAncestorsOf (bool add, txiter hash, setEntries &setAncestors) |
Update ancestors of hash to add/remove it as a descendant transaction. More... | |
void | UpdateEntryForAncestors (txiter it, const setEntries &setAncestors) |
Set ancestor state for an entry. More... | |
void | UpdateForRemoveFromMempool (const setEntries &entriesToRemove, bool updateDescendants) |
For each transaction being removed, update ancestors and any direct children. More... | |
void | UpdateChildrenForRemoval (txiter entry) |
Sever link between specified transaction and direct children. More... | |
void | removeUnchecked (txiter entry, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN) |
Before calling removeUnchecked for a given transaction, UpdateForRemoveFromMempool must be called on the entire (dependent) set of transactions being removed at the same time. More... | |
Private Attributes | |
uint32_t | nCheckFrequency |
Value n means that n times in 2^32 we check. More... | |
unsigned int | nTransactionsUpdated |
Used by getblocktemplate to trigger CreateNewBlock() invocation. More... | |
CBlockPolicyEstimator * | minerPolicyEstimator |
uint64_t | totalTxSize |
sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discounted. Defined in BIP 141. More... | |
uint64_t | cachedInnerUsage |
sum of dynamic memory usage of all the map elements (NOT the maps themselves) More... | |
int64_t | lastRollingFeeUpdate |
bool | blockSinceLastRollingFeeBump |
double | rollingMinimumFeeRate |
minimum fee to get into the pool, decreases exponentially More... | |
txlinksMap | mapLinks |
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the next block.
Transactions are added when they are seen on the network (or created by the local node), but not all transactions seen are added to the pool. For example, the following new transactions will not be added to the mempool:
CTxMemPool::mapTx, and CTxMemPoolEntry bookkeeping:
mapTx is a boost::multi_index that sorts the mempool on 4 criteria:
Note: the term "descendant" refers to in-mempool transactions that depend on this one, while "ancestor" refers to in-mempool transactions that a given transaction depends on.
In order for the feerate sort to remain correct, we must update transactions in the mempool when new descendants arrive. To facilitate this, we track the set of in-mempool direct parents and direct children in mapLinks. Within each CTxMemPoolEntry, we track the size and fees of all descendants.
Usually when a new transaction is added to the mempool, it has no in-mempool children (because any such children would be an orphan). So in addUnchecked(), we:
When a transaction is removed from the mempool, we must:
These happen in UpdateForRemoveFromMempool(). (Note that when removing a transaction along with its descendants, we must calculate that set of transactions to be removed before doing the removal, or else the mempool can be in an inconsistent state where it's impossible to walk the ancestors of a transaction.)
In the event of a reorg, the assumption that a newly added tx has no in-mempool children is false. In particular, the mempool is in an inconsistent state while new transactions are being added, because there may be descendant transactions of a tx coming from a disconnected block that are unreachable from just looking at transactions in the mempool (the linking transactions may also be in the disconnected block, waiting to be added). Because of this, there's not much benefit in trying to search for in-mempool children in addUnchecked(). Instead, in the special case of transactions being added from a disconnected block, we require the caller to clean up the state, to account for in-mempool, out-of-block descendants for all the in-block transactions by calling UpdateTransactionsFromBlock(). Note that until this is called, the mempool state is not consistent, and in particular mapLinks may not be correct (and therefore functions like CalculateMemPoolAncestors() and CalculateDescendants() that rely on them to walk the mempool are not generally safe to use).
Computational limits:
Updating all in-mempool ancestors of a newly added transaction can be slow, if no bound exists on how many in-mempool ancestors there may be. CalculateMemPoolAncestors() takes configurable limits that are designed to prevent these calculations from being too CPU intensive.
Definition at line 465 of file txmempool.h.
|
private |
Definition at line 539 of file txmempool.h.
typedef 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 >, boost::multi_index::ordered_non_unique< boost::multi_index::tag<ancestor_score_or_gas_price>, boost::multi_index::identity<CTxMemPoolEntry>, CompareTxMemPoolEntryByAncestorFeeOrGasPrice > > > CTxMemPool::indexed_transaction_set |
Definition at line 521 of file txmempool.h.
typedef std::set<txiter, CompareIteratorByHash> CTxMemPool::setEntries |
Definition at line 534 of file txmempool.h.
typedef indexed_transaction_set::nth_index<0>::type::iterator CTxMemPool::txiter |
Definition at line 526 of file txmempool.h.
|
private |
Definition at line 546 of file txmempool.h.
CTxMemPool::CTxMemPool | ( | CBlockPolicyEstimator * | estimator = nullptr | ) |
Create a new CTxMemPool.
Definition at line 337 of file txmempool.cpp.
void CTxMemPool::_clear | ( | ) |
Definition at line 602 of file txmempool.cpp.
void CTxMemPool::AddTransactionsUpdated | ( | unsigned int | n | ) |
bool CTxMemPool::addUnchecked | ( | const uint256 & | hash, |
const CTxMemPoolEntry & | entry, | ||
bool | validFeeEstimate = true |
||
) |
bool CTxMemPool::addUnchecked | ( | const uint256 & | hash, |
const CTxMemPoolEntry & | entry, | ||
setEntries & | setAncestors, | ||
bool | validFeeEstimate = true |
||
) |
void CTxMemPool::CalculateDescendants | ( | txiter | it, |
setEntries & | setDescendants | ||
) |
Populate setDescendants with all in-mempool descendants of hash.
Assumes that setDescendants includes all in-mempool descendants of anything already in it.
Definition at line 456 of file txmempool.cpp.
bool CTxMemPool::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.
(these are all calculated including the tx itself) limitAncestorCount = max number of ancestors limitAncestorSize = max size of ancestors limitDescendantCount = max number of descendants any ancestor can have limitDescendantSize = max size of descendants any ancestor can have errString = populated with error reason if any limits are hit fSearchForParents = whether to search a tx's vin for in-mempool parents, or look up parents from mapLinks. Must be true for entries not in the mempool
Definition at line 158 of file txmempool.cpp.
void CTxMemPool::check | ( | const CCoinsViewCache * | pcoins | ) | const |
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transactions that spend the same inputs, all inputs are in the mapNextTx array).
If sanity-checking is turned off, check does nothing.
Definition at line 621 of file txmempool.cpp.
void CTxMemPool::clear | ( | ) |
Definition at line 615 of file txmempool.cpp.
void CTxMemPool::ClearPrioritisation | ( | const uint256 | hash | ) |
size_t CTxMemPool::DynamicMemoryUsage | ( | ) | const |
|
inline |
|
inline |
Definition at line 677 of file txmempool.h.
int CTxMemPool::Expire | ( | int64_t | time | ) |
Expire all transaction (and their dependencies) in the mempool older than time.
Return the number of removed transactions.
Definition at line 934 of file txmempool.cpp.
CTransactionRef CTxMemPool::get | ( | const uint256 & | hash | ) | const |
const CTxMemPool::setEntries & CTxMemPool::GetMemPoolChildren | ( | txiter | entry | ) | const |
Definition at line 988 of file txmempool.cpp.
const CTxMemPool::setEntries & CTxMemPool::GetMemPoolParents | ( | txiter | entry | ) | const |
Definition at line 980 of file txmempool.cpp.
CFeeRate CTxMemPool::GetMinFee | ( | size_t | sizelimit | ) | const |
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions.
The incrementalRelayFee policy variable is used to bound the time it takes the fee rate to go back down all the way to 0. When the feerate would otherwise be half of this, it is set to 0 instead.
Definition at line 996 of file txmempool.cpp.
|
private |
|
inline |
unsigned int CTxMemPool::GetTransactionsUpdated | ( | ) | const |
bool CTxMemPool::HasNoInputsOf | ( | const CTransaction & | tx | ) | const |
Check that none of this transactions inputs are in the mempool, and thus the tx is not dependent on other mempool transactions to be included in a block.
Definition at line 890 of file txmempool.cpp.
TxMempoolInfo CTxMemPool::info | ( | const uint256 & | hash | ) | const |
std::vector< TxMempoolInfo > CTxMemPool::infoAll | ( | ) | const |
Definition at line 812 of file txmempool.cpp.
bool CTxMemPool::isSpent | ( | const COutPoint & | outpoint | ) |
Definition at line 348 of file txmempool.cpp.
Affect CreateNewBlock prioritisation of transactions.
Definition at line 844 of file txmempool.cpp.
void CTxMemPool::queryHashes | ( | std::vector< uint256 > & | vtxid | ) |
Definition at line 795 of file txmempool.cpp.
void CTxMemPool::removeConflicts | ( | const CTransaction & | tx | ) |
Definition at line 553 of file txmempool.cpp.
void CTxMemPool::removeForBlock | ( | const std::vector< CTransactionRef > & | vtx, |
unsigned int | nBlockHeight | ||
) |
Called when a block is connected.
Removes from mempool and updates the miner fee estimator.
Definition at line 573 of file txmempool.cpp.
void CTxMemPool::removeForReorg | ( | const CCoinsViewCache * | pcoins, |
unsigned int | nMemPoolHeight, | ||
int | flags | ||
) |
Definition at line 511 of file txmempool.cpp.
void CTxMemPool::removeRecursive | ( | const CTransaction & | tx, |
MemPoolRemovalReason | reason = MemPoolRemovalReason::UNKNOWN |
||
) |
Definition at line 479 of file txmempool.cpp.
void CTxMemPool::RemoveStaged | ( | setEntries & | stage, |
bool | updateDescendants, | ||
MemPoolRemovalReason | reason = MemPoolRemovalReason::UNKNOWN |
||
) |
Remove a set of transactions from the mempool.
If a transaction is in this set, then all in-mempool descendants must also be in the set, unless this transaction is being removed for being in a block. Set updateDescendants to true when removing a tx that was in a block, so that any in-mempool descendants have their ancestor state updated.
Definition at line 926 of file txmempool.cpp.
|
private |
Before calling removeUnchecked for a given transaction, UpdateForRemoveFromMempool must be called on the entire (dependent) set of transactions being removed at the same time.
We use each CTxMemPoolEntry's setMemPoolParents in order to walk ancestors of a given transaction that is removed, so we can't remove intermediate transactions in a chain before we've updated all the state for the removal.
Definition at line 425 of file txmempool.cpp.
|
inline |
|
inline |
|
private |
bool CTxMemPool::TransactionWithinChainLimit | ( | const uint256 & | txid, |
size_t | chainLimit | ||
) | const |
Returns false if the transaction is in the mempool and not within the chain limit specified.
Definition at line 1073 of file txmempool.cpp.
void CTxMemPool::TrimToSize | ( | size_t | sizelimit, |
std::vector< COutPoint > * | pvNoSpendsRemaining = nullptr |
||
) |
Remove transactions from the mempool until its dynamic size is <= sizelimit.
pvNoSpendsRemaining, if set, will be populated with the list of outpoints which are not in mempool which no longer have any spends in this mempool.
Definition at line 1028 of file txmempool.cpp.
|
private |
Update ancestors of hash to add/remove it as a descendant transaction.
Definition at line 222 of file txmempool.cpp.
Definition at line 960 of file txmempool.cpp.
|
private |
Sever link between specified transaction and direct children.
Definition at line 251 of file txmempool.cpp.
|
private |
Set ancestor state for an entry.
Definition at line 237 of file txmempool.cpp.
|
private |
UpdateForDescendants is used by UpdateTransactionsFromBlock to update the descendants for a single transaction that has been added to the mempool but may have child transactions in the mempool, eg during a chain reorg.
setExclude is the set of descendant transactions in the mempool that must not be accounted for (because any descendants in setExclude were added to the mempool after the transaction being updated and hence their state is already reflected in the parent state).
cachedDescendants will be updated with the descendants of the transaction being updated, so that future invocations don't need to walk the same transaction again, if encountered in another transaction chain.
Definition at line 68 of file txmempool.cpp.
|
private |
For each transaction being removed, update ancestors and any direct children.
If updateDescendants is true, then also update in-mempool descendants' ancestor state.
Definition at line 259 of file txmempool.cpp.
void CTxMemPool::UpdateTransactionsFromBlock | ( | const std::vector< uint256 > & | vHashesToUpdate | ) |
When adding transactions from a disconnected block back to the mempool, new mempool entries may have children in the mempool (which is generally not the case when otherwise adding transactions).
UpdateTransactionsFromBlock() will find child transactions and update the descendant state for each transaction in vHashesToUpdate (excluding any child transactions present in vHashesToUpdate, which are already accounted for). Note: vHashesToUpdate should be the set of transactions from the disconnected block that have been accepted back into the mempool.
Definition at line 115 of file txmempool.cpp.
|
mutableprivate |
Definition at line 476 of file txmempool.h.
|
private |
sum of dynamic memory usage of all the map elements (NOT the maps themselves)
Definition at line 473 of file txmempool.h.
|
mutable |
Definition at line 523 of file txmempool.h.
|
mutableprivate |
Definition at line 475 of file txmempool.h.
Definition at line 556 of file txmempool.h.
|
private |
Definition at line 547 of file txmempool.h.
indirectmap<COutPoint, const CTransaction*> CTxMemPool::mapNextTx |
Definition at line 555 of file txmempool.h.
indexed_transaction_set CTxMemPool::mapTx |
Definition at line 524 of file txmempool.h.
|
private |
Definition at line 470 of file txmempool.h.
|
private |
Value n means that n times in 2^32 we check.
Definition at line 468 of file txmempool.h.
boost::signals2::signal<void (CTransactionRef)> CTxMemPool::NotifyEntryAdded |
Definition at line 690 of file txmempool.h.
boost::signals2::signal<void (CTransactionRef, MemPoolRemovalReason)> CTxMemPool::NotifyEntryRemoved |
Definition at line 691 of file txmempool.h.
|
private |
Used by getblocktemplate to trigger CreateNewBlock() invocation.
Definition at line 469 of file txmempool.h.
|
static |
Definition at line 483 of file txmempool.h.
|
mutableprivate |
minimum fee to get into the pool, decreases exponentially
Definition at line 477 of file txmempool.h.
|
private |
sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discounted. Defined in BIP 141.
Definition at line 472 of file txmempool.h.
All tx witness hashes/entries in mapTx, in random order.
Definition at line 527 of file txmempool.h.