mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 12:32:48 +01:00
4e46e6101c
296be8f58e02b39a58f017c52294aceed22c3ffd Get rid of unused functions CTxMemPool::GetMemPoolChildren, CTxMemPool::GetMemPoolParents (Jeremy Rubin) 46d955d196043cc297834baeebce31ff778dff80 Remove mapLinks in favor of entry inlined structs with iterator type erasure (Jeremy Rubin) Pull request description: Currently we have a peculiar data structure in the mempool called maplinks. Maplinks job is to track the in-pool children and parents of each transaction. This PR can be primarily understood and reviewed as a simple refactoring to remove this extra data structure, although it comes with a nice memory and performance improvement for free. Maplinks is particularly peculiar because removing it is not as simple as just moving it's inner structure to the owning CTxMempoolEntry. Because TxLinks (the class storing the setEntries for parents and children) store txiters to each entry in the mempool corresponding to the parent or child, it means that the TxLinks type is "aware" of the boost multiindex (mapTx) it's coming from, which is in turn, aware of the entry type stored in mapTx. Thus we used maplinks to store this entry associated data we in an entirely separate data structure just to avoid a circular type reference caused by storing a txiter inside a CTxMempoolEntry. It turns out, we can kill this circular reference by making use of iterator_to multiindex function and std::reference_wrapper. This allows us to get rid of the maplinks data structure and move the ownership of the parents/child sets to the entries themselves. The benefit of this good all around, for any of the reasons given below the change would be acceptable, and it doesn't make the code harder to reason about or worse in any respect (as far as I can tell, there's no tradeoff). ### Simpler ownership model No longer having to consistency check that mapLinks did have records for our CTxMempoolEntry, impossible to have a mapLinks entry outlive or incorrectly die before a CTxMempoolEntry. ### Memory Usage We get rid of a O(Transactions) sized map in the mempool, which is a long lived data structure. ### Performance If you have a CTxMemPoolEntry, you immediately know the address of it's children/parents, rather than having to do a O(log(Transactions)) lookup via maplinks (which we do very often). We do it in *so many* places that a true benchmark has to look at a full running node, but it is easy enough to show an improvement in this case. The ComplexMemPool shows a good coherence check that we see the expected result of it being 12.5% faster / 1.14x faster. ``` Before: # Benchmark, evals, iterations, total, min, max, median ComplexMemPool, 5, 1, 1.40462, 0.277222, 0.285339, 0.279793 After: # Benchmark, evals, iterations, total, min, max, median ComplexMemPool, 5, 1, 1.22586, 0.243831, 0.247076, 0.244596 ``` The ComplexMemPool benchmark only checks doing addUnchecked and TrimToSize for 800 transactions. While this bench does a good job of hammering the relevant types of function, it doesn't test everything. Subbing in 5000 transactions shows a that the advantage isn't completely wiped out by other asymptotic factors (this isn't the only bottleneck in growing the mempool), but it's only a bit proportionally slower (10.8%, 1.12x), which adds evidence that this will be a good change for performance minded users. ``` # Benchmark, evals, iterations, total, min, max, median ComplexMemPool, 5, 1, 59.1321, 11.5919, 12.235, 11.7068 # Benchmark, evals, iterations, total, min, max, median ComplexMemPool, 5, 1, 52.1307, 10.2641, 10.5206, 10.4306 ``` I don't think it's possible to come up with an example of where a maplinks based design would have better performance, but it's something for reviewers to consider. # Discussion ## Why maplinks in the first place? I spoke with the author of mapLinks (sdaftuar) a while back, and my recollection from our conversation was that it was implemented because he did not know how to resolve the circular dependency at the time, and there was no other reason for making it a separate map. ## Is iterator_to weird? iterator_to is expressly for this purpose, see https://www.boost.org/doc/libs/1_51_0/libs/multi_index/doc/tutorial/indices.html#iterator_to > iterator_to provides a way to retrieve an iterator to an element from a pointer to the element, thus making iterators and pointers interchangeable for the purposes of element pointing (not so for traversal) in many situations. This notwithstanding, it is not the aim of iterator_to to promote the usage of pointers as substitutes for real iterators: the latter are specifically designed for handling the elements of a container, and not only benefit from the iterator orientation of container interfaces, but are also capable of exposing many more programming bugs than raw pointers, both at compile and run time. iterator_to is thus meant to be used in scenarios where access via iterators is not suitable or desireable: > > - Interoperability with preexisting APIs based on pointers or references. > - Publication of pointer-based interfaces (for instance, when designing a C-compatible library). > - The exposure of pointers in place of iterators can act as a type erasure barrier effectively decoupling the user of the code from the implementation detail of which particular container is being used. Similar techniques, like the famous Pimpl idiom, are used in large projects to reduce dependencies and build times. > - Self-referencing contexts where an element acts upon its owner container and no iterator to itself is available. In other words, iterator_to is the perfect tool for the job by the last reason given. Under the hood it should just be a simple pointer cast and have no major runtime overhead (depending on if the function call is inlined). Edit by laanwj: removed at sign from the description ACKs for top commit: jonatack: re-ACK 296be8f per `git range-diff ab338a19 3ba1665 296be8f`, sanity check gcc 10.2 debug build is clean. hebasto: re-ACK 296be8f58e02b39a58f017c52294aceed22c3ffd, only rebased since my [previous](https://github.com/bitcoin/bitcoin/pull/19478#pullrequestreview-482400727) review (verified with `git range-diff`). Tree-SHA512: f5c30a4936fcde6ae32a02823c303b3568a747c2681d11f87df88a149f984a6d3b4c81f391859afbeb68864ef7f6a3d8779f74a58e3de701b3d51f78e498682e
972 lines
40 KiB
C++
972 lines
40 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#ifndef BITCOIN_TXMEMPOOL_H
|
|
#define BITCOIN_TXMEMPOOL_H
|
|
|
|
#include <atomic>
|
|
#include <map>
|
|
#include <optional>
|
|
#include <set>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include <addressindex.h>
|
|
#include <spentindex.h>
|
|
#include <amount.h>
|
|
#include <coins.h>
|
|
#include <indirectmap.h>
|
|
#include <policy/feerate.h>
|
|
#include <primitives/transaction.h>
|
|
#include <random.h>
|
|
#include <netaddress.h>
|
|
#include <pubkey.h>
|
|
#include <sync.h>
|
|
#include <util/epochguard.h>
|
|
#include <util/hasher.h>
|
|
|
|
#include <boost/optional.hpp>
|
|
#include <boost/multi_index_container.hpp>
|
|
#include <boost/multi_index/hashed_index.hpp>
|
|
#include <boost/multi_index/ordered_index.hpp>
|
|
#include <boost/multi_index/sequenced_index.hpp>
|
|
|
|
class CBlockIndex;
|
|
class CChainState;
|
|
extern RecursiveMutex cs_main;
|
|
|
|
// Forward declation for CBLSLazyPublicKey:
|
|
template<typename T> class CBLSLazyWrapper;
|
|
class CBLSPublicKey;
|
|
using CBLSLazyPublicKey = CBLSLazyWrapper<CBLSPublicKey>;
|
|
|
|
/** Fake height value used in Coin to signify they are only in the memory pool (since 0.8) */
|
|
static const uint32_t MEMPOOL_HEIGHT = 0x7FFFFFFF;
|
|
|
|
struct LockPoints
|
|
{
|
|
// Will be set to the blockchain height and median time past
|
|
// values that would be necessary to satisfy all relative locktime
|
|
// constraints (BIP68) of this tx given our view of block chain history
|
|
int height;
|
|
int64_t time;
|
|
// As long as the current chain descends from the highest height block
|
|
// containing one of the inputs used in the calculation, then the cached
|
|
// values are still valid even after a reorg.
|
|
CBlockIndex* maxInputBlock;
|
|
|
|
LockPoints() : height(0), time(0), maxInputBlock(nullptr) { }
|
|
};
|
|
|
|
struct CompareIteratorByHash {
|
|
// SFINAE for T where T is either a pointer type (e.g., a txiter) or a reference_wrapper<T>
|
|
// (e.g. a wrapped CTxMemPoolEntry&)
|
|
template <typename T>
|
|
bool operator()(const std::reference_wrapper<T>& a, const std::reference_wrapper<T>& b) const
|
|
{
|
|
return a.get().GetTx().GetHash() < b.get().GetTx().GetHash();
|
|
}
|
|
template <typename T>
|
|
bool operator()(const T& a, const T& b) const
|
|
{
|
|
return a->GetTx().GetHash() < b->GetTx().GetHash();
|
|
}
|
|
};
|
|
/** \class CTxMemPoolEntry
|
|
*
|
|
* CTxMemPoolEntry stores data about the corresponding transaction, as well
|
|
* as data about all in-mempool transactions that depend on the transaction
|
|
* ("descendant" transactions).
|
|
*
|
|
* When a new entry is added to the mempool, we update the descendant state
|
|
* (nCountWithDescendants, nSizeWithDescendants, and nModFeesWithDescendants) for
|
|
* all ancestors of the newly added transaction.
|
|
*
|
|
*/
|
|
|
|
class CTxMemPoolEntry
|
|
{
|
|
public:
|
|
typedef std::reference_wrapper<const CTxMemPoolEntry> CTxMemPoolEntryRef;
|
|
// two aliases, should the types ever diverge
|
|
typedef std::set<CTxMemPoolEntryRef, CompareIteratorByHash> Parents;
|
|
typedef std::set<CTxMemPoolEntryRef, CompareIteratorByHash> Children;
|
|
|
|
private:
|
|
const CTransactionRef tx;
|
|
mutable Parents m_parents;
|
|
mutable Children m_children;
|
|
const CAmount nFee; //!< Cached to avoid expensive parent-transaction lookups
|
|
const size_t nTxSize; //!< ... and avoid recomputing tx size
|
|
const size_t nUsageSize; //!< ... and total memory usage
|
|
const int64_t nTime; //!< Local time when entering the mempool
|
|
const unsigned int entryHeight; //!< Chain height when entering the mempool
|
|
const bool spendsCoinbase; //!< keep track of transactions that spend a coinbase
|
|
const unsigned int sigOpCount; //!< Legacy sig ops plus P2SH sig op count
|
|
int64_t feeDelta; //!< Used for determining the priority of the transaction for mining in a block
|
|
LockPoints lockPoints; //!< Track the height and time at which tx was final
|
|
|
|
// Information about descendants of this transaction that are in the
|
|
// mempool; if we remove this transaction we must remove all of these
|
|
// descendants as well.
|
|
uint64_t nCountWithDescendants; //!< number of descendant transactions
|
|
uint64_t nSizeWithDescendants; //!< ... and size
|
|
CAmount nModFeesWithDescendants; //!< ... and total fees (all including us)
|
|
|
|
// Analogous statistics for ancestor transactions
|
|
uint64_t nCountWithAncestors;
|
|
uint64_t nSizeWithAncestors;
|
|
CAmount nModFeesWithAncestors;
|
|
unsigned int nSigOpCountWithAncestors;
|
|
|
|
public:
|
|
CTxMemPoolEntry(const CTransactionRef& _tx, const CAmount& _nFee,
|
|
int64_t _nTime, unsigned int _entryHeight,
|
|
bool spendsCoinbase,
|
|
unsigned int nSigOps, LockPoints lp);
|
|
|
|
const CTransaction& GetTx() const { return *this->tx; }
|
|
CTransactionRef GetSharedTx() const { return this->tx; }
|
|
const CAmount& GetFee() const { return nFee; }
|
|
size_t GetTxSize() const;
|
|
std::chrono::seconds GetTime() const { return std::chrono::seconds{nTime}; }
|
|
unsigned int GetHeight() const { return entryHeight; }
|
|
unsigned int GetSigOpCount() const { return sigOpCount; }
|
|
int64_t GetModifiedFee() const { return nFee + feeDelta; }
|
|
size_t DynamicMemoryUsage() const { return nUsageSize; }
|
|
const LockPoints& GetLockPoints() const { return lockPoints; }
|
|
|
|
// Adjusts the descendant state.
|
|
void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount);
|
|
// Adjusts the ancestor state
|
|
void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps);
|
|
// Updates the fee delta used for mining priority score, and the
|
|
// modified fees with descendants.
|
|
void UpdateFeeDelta(int64_t feeDelta);
|
|
// Update the LockPoints after a reorg
|
|
void UpdateLockPoints(const LockPoints& lp);
|
|
|
|
uint64_t GetCountWithDescendants() const { return nCountWithDescendants; }
|
|
uint64_t GetSizeWithDescendants() const { return nSizeWithDescendants; }
|
|
CAmount GetModFeesWithDescendants() const { return nModFeesWithDescendants; }
|
|
|
|
bool GetSpendsCoinbase() const { return spendsCoinbase; }
|
|
|
|
uint64_t GetCountWithAncestors() const { return nCountWithAncestors; }
|
|
uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
|
|
CAmount GetModFeesWithAncestors() const { return nModFeesWithAncestors; }
|
|
unsigned int GetSigOpCountWithAncestors() const { return nSigOpCountWithAncestors; }
|
|
|
|
const Parents& GetMemPoolParentsConst() const { return m_parents; }
|
|
const Children& GetMemPoolChildrenConst() const { return m_children; }
|
|
Parents& GetMemPoolParents() const { return m_parents; }
|
|
Children& GetMemPoolChildren() const { return m_children; }
|
|
|
|
mutable size_t vTxHashesIdx; //!< Index in mempool's vTxHashes
|
|
|
|
// If this is a proTx, this will be the hash of the key for which this ProTx was valid
|
|
mutable uint256 validForProTxKey;
|
|
mutable bool isKeyChangeProTx{false};
|
|
mutable Epoch::Marker m_epoch_marker; //!< epoch when last touched, useful for graph algorithms
|
|
};
|
|
|
|
// Helpers for modifying CTxMemPool::mapTx, which is a boost multi_index.
|
|
struct update_descendant_state
|
|
{
|
|
update_descendant_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount) :
|
|
modifySize(_modifySize), modifyFee(_modifyFee), modifyCount(_modifyCount)
|
|
{}
|
|
|
|
void operator() (CTxMemPoolEntry &e)
|
|
{ e.UpdateDescendantState(modifySize, modifyFee, modifyCount); }
|
|
|
|
private:
|
|
int64_t modifySize;
|
|
CAmount modifyFee;
|
|
int64_t modifyCount;
|
|
};
|
|
|
|
struct update_ancestor_state
|
|
{
|
|
update_ancestor_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount, int _modifySigOps) :
|
|
modifySize(_modifySize), modifyFee(_modifyFee), modifyCount(_modifyCount), modifySigOps(_modifySigOps)
|
|
{}
|
|
|
|
void operator() (CTxMemPoolEntry &e)
|
|
{ e.UpdateAncestorState(modifySize, modifyFee, modifyCount, modifySigOps); }
|
|
|
|
private:
|
|
int64_t modifySize;
|
|
CAmount modifyFee;
|
|
int64_t modifyCount;
|
|
int modifySigOps;
|
|
};
|
|
|
|
struct update_fee_delta
|
|
{
|
|
explicit update_fee_delta(int64_t _feeDelta) : feeDelta(_feeDelta) { }
|
|
|
|
void operator() (CTxMemPoolEntry &e) { e.UpdateFeeDelta(feeDelta); }
|
|
|
|
private:
|
|
int64_t feeDelta;
|
|
};
|
|
|
|
struct update_lock_points
|
|
{
|
|
explicit update_lock_points(const LockPoints& _lp) : lp(_lp) { }
|
|
|
|
void operator() (CTxMemPoolEntry &e) { e.UpdateLockPoints(lp); }
|
|
|
|
private:
|
|
const LockPoints& lp;
|
|
};
|
|
|
|
// extracts a transaction hash from CTxMemPoolEntry or CTransactionRef
|
|
struct mempoolentry_txid
|
|
{
|
|
typedef uint256 result_type;
|
|
result_type operator() (const CTxMemPoolEntry &entry) const
|
|
{
|
|
return entry.GetTx().GetHash();
|
|
}
|
|
|
|
result_type operator() (const CTransactionRef& tx) const
|
|
{
|
|
return tx->GetHash();
|
|
}
|
|
};
|
|
|
|
/** \class CompareTxMemPoolEntryByDescendantScore
|
|
*
|
|
* Sort an entry by max(score/size of entry's tx, score/size with all descendants).
|
|
*/
|
|
class CompareTxMemPoolEntryByDescendantScore
|
|
{
|
|
public:
|
|
bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
|
|
{
|
|
double a_mod_fee, a_size, b_mod_fee, b_size;
|
|
|
|
GetModFeeAndSize(a, a_mod_fee, a_size);
|
|
GetModFeeAndSize(b, b_mod_fee, b_size);
|
|
|
|
// Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
|
|
double f1 = a_mod_fee * b_size;
|
|
double f2 = a_size * b_mod_fee;
|
|
|
|
if (f1 == f2) {
|
|
return a.GetTime() >= b.GetTime();
|
|
}
|
|
return f1 < f2;
|
|
}
|
|
|
|
// Return the fee/size we're using for sorting this entry.
|
|
void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
|
|
{
|
|
// Compare feerate with descendants to feerate of the transaction, and
|
|
// return the fee/size for the max.
|
|
double f1 = (double)a.GetModifiedFee() * a.GetSizeWithDescendants();
|
|
double f2 = (double)a.GetModFeesWithDescendants() * a.GetTxSize();
|
|
|
|
if (f2 > f1) {
|
|
mod_fee = a.GetModFeesWithDescendants();
|
|
size = a.GetSizeWithDescendants();
|
|
} else {
|
|
mod_fee = a.GetModifiedFee();
|
|
size = a.GetTxSize();
|
|
}
|
|
}
|
|
};
|
|
|
|
/** \class CompareTxMemPoolEntryByScore
|
|
*
|
|
* Sort by feerate of entry (fee/size) in descending order
|
|
* This is only used for transaction relay, so we use GetFee()
|
|
* instead of GetModifiedFee() to avoid leaking prioritization
|
|
* information via the sort order.
|
|
*/
|
|
class CompareTxMemPoolEntryByScore
|
|
{
|
|
public:
|
|
bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
|
|
{
|
|
double f1 = (double)a.GetFee() * b.GetTxSize();
|
|
double f2 = (double)b.GetFee() * a.GetTxSize();
|
|
if (f1 == f2) {
|
|
return b.GetTx().GetHash() < a.GetTx().GetHash();
|
|
}
|
|
return f1 > f2;
|
|
}
|
|
};
|
|
|
|
class CompareTxMemPoolEntryByEntryTime
|
|
{
|
|
public:
|
|
bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
|
|
{
|
|
return a.GetTime() < b.GetTime();
|
|
}
|
|
};
|
|
|
|
/** \class CompareTxMemPoolEntryByAncestorScore
|
|
*
|
|
* Sort an entry by min(score/size of entry's tx, score/size with all ancestors).
|
|
*/
|
|
class CompareTxMemPoolEntryByAncestorFee
|
|
{
|
|
public:
|
|
template<typename T>
|
|
bool operator()(const T& a, const T& b) const
|
|
{
|
|
double a_mod_fee, a_size, b_mod_fee, b_size;
|
|
|
|
GetModFeeAndSize(a, a_mod_fee, a_size);
|
|
GetModFeeAndSize(b, b_mod_fee, b_size);
|
|
|
|
// Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
|
|
double f1 = a_mod_fee * b_size;
|
|
double f2 = a_size * b_mod_fee;
|
|
|
|
if (f1 == f2) {
|
|
return a.GetTx().GetHash() < b.GetTx().GetHash();
|
|
}
|
|
return f1 > f2;
|
|
}
|
|
|
|
// Return the fee/size we're using for sorting this entry.
|
|
template <typename T>
|
|
void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
|
|
{
|
|
// Compare feerate with ancestors to feerate of the transaction, and
|
|
// return the fee/size for the min.
|
|
double f1 = (double)a.GetModifiedFee() * a.GetSizeWithAncestors();
|
|
double f2 = (double)a.GetModFeesWithAncestors() * a.GetTxSize();
|
|
|
|
if (f1 > f2) {
|
|
mod_fee = a.GetModFeesWithAncestors();
|
|
size = a.GetSizeWithAncestors();
|
|
} else {
|
|
mod_fee = a.GetModifiedFee();
|
|
size = a.GetTxSize();
|
|
}
|
|
}
|
|
};
|
|
|
|
// Multi_index tag names
|
|
struct descendant_score {};
|
|
struct entry_time {};
|
|
struct ancestor_score {};
|
|
|
|
class CBlockPolicyEstimator;
|
|
|
|
/**
|
|
* Information about a mempool transaction.
|
|
*/
|
|
struct TxMempoolInfo
|
|
{
|
|
/** The transaction itself */
|
|
CTransactionRef tx;
|
|
|
|
/** Time the transaction entered the mempool. */
|
|
std::chrono::seconds m_time;
|
|
|
|
/** Fee of the transaction. */
|
|
CAmount fee;
|
|
|
|
/** Virtual size of the transaction. */
|
|
size_t vsize;
|
|
|
|
/** The fee delta. */
|
|
int64_t nFeeDelta;
|
|
};
|
|
|
|
/** Reason why a transaction was removed from the mempool,
|
|
* this is passed to the notification signal.
|
|
*/
|
|
enum class MemPoolRemovalReason {
|
|
EXPIRY, //!< Expired from mempool
|
|
SIZELIMIT, //!< Removed in size limiting
|
|
REORG, //!< Removed for reorganization
|
|
BLOCK, //!< Removed for block
|
|
CONFLICT, //!< Removed for conflict with in-block transaction
|
|
MANUAL //!< Removed manually
|
|
};
|
|
|
|
/**
|
|
* 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:
|
|
* - a transaction which doesn't meet the minimum fee requirements.
|
|
* - a new transaction that double-spends an input of a transaction already in
|
|
* the pool.
|
|
* - a non-standard transaction.
|
|
*
|
|
* CTxMemPool::mapTx, and CTxMemPoolEntry bookkeeping:
|
|
*
|
|
* mapTx is a boost::multi_index that sorts the mempool on 4 criteria:
|
|
* - transaction hash
|
|
* - descendant feerate [we use max(feerate of tx, feerate of tx with all descendants)]
|
|
* - time in mempool
|
|
* - ancestor feerate [we use min(feerate of tx, feerate of tx with all unconfirmed ancestors)]
|
|
*
|
|
* 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:
|
|
* - update a new entry's setMemPoolParents to include all in-mempool parents
|
|
* - update the new entry's direct parents to include the new tx as a child
|
|
* - update all ancestors of the transaction to include the new tx's size/fee
|
|
*
|
|
* When a transaction is removed from the mempool, we must:
|
|
* - update all in-mempool parents to not track the tx in setMemPoolChildren
|
|
* - update all ancestors to not include the tx's size/fees in descendant state
|
|
* - update all in-mempool children to not include it as a parent
|
|
*
|
|
* 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.
|
|
*
|
|
*/
|
|
class CTxMemPool
|
|
{
|
|
protected:
|
|
const int m_check_ratio; //!< Value n means that 1 times in n we check.
|
|
std::atomic<unsigned int> nTransactionsUpdated{0}; //!< Used by getblocktemplate to trigger CreateNewBlock() invocation
|
|
CBlockPolicyEstimator* minerPolicyEstimator;
|
|
|
|
uint64_t totalTxSize GUARDED_BY(cs); //!< sum of all mempool tx' byte sizes
|
|
CAmount m_total_fee GUARDED_BY(cs); //!< sum of all mempool tx's fees (NOT modified fee)
|
|
uint64_t cachedInnerUsage GUARDED_BY(cs); //!< sum of dynamic memory usage of all the map elements (NOT the maps themselves)
|
|
|
|
mutable int64_t lastRollingFeeUpdate;
|
|
mutable bool blockSinceLastRollingFeeBump;
|
|
mutable double rollingMinimumFeeRate; //!< minimum fee to get into the pool, decreases exponentially
|
|
mutable Epoch m_epoch GUARDED_BY(cs);
|
|
|
|
void trackPackageRemoved(const CFeeRate& rate) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
|
|
bool m_is_loaded GUARDED_BY(cs){false};
|
|
|
|
public:
|
|
|
|
static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
|
|
|
|
typedef boost::multi_index_container<
|
|
CTxMemPoolEntry,
|
|
boost::multi_index::indexed_by<
|
|
// sorted by txid
|
|
boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
|
|
// sorted by fee rate
|
|
boost::multi_index::ordered_non_unique<
|
|
boost::multi_index::tag<descendant_score>,
|
|
boost::multi_index::identity<CTxMemPoolEntry>,
|
|
CompareTxMemPoolEntryByDescendantScore
|
|
>,
|
|
// sorted by entry time
|
|
boost::multi_index::ordered_non_unique<
|
|
boost::multi_index::tag<entry_time>,
|
|
boost::multi_index::identity<CTxMemPoolEntry>,
|
|
CompareTxMemPoolEntryByEntryTime
|
|
>,
|
|
// sorted by fee rate with ancestors
|
|
boost::multi_index::ordered_non_unique<
|
|
boost::multi_index::tag<ancestor_score>,
|
|
boost::multi_index::identity<CTxMemPoolEntry>,
|
|
CompareTxMemPoolEntryByAncestorFee
|
|
>
|
|
>
|
|
> indexed_transaction_set;
|
|
|
|
/**
|
|
* This mutex needs to be locked when accessing `mapTx` or other members
|
|
* that are guarded by it.
|
|
*
|
|
* @par Consistency guarantees
|
|
*
|
|
* By design, it is guaranteed that:
|
|
*
|
|
* 1. Locking both `cs_main` and `mempool.cs` will give a view of mempool
|
|
* that is consistent with current chain tip (`::ChainActive()` and
|
|
* `CoinsTip()`) and is fully populated. Fully populated means that if the
|
|
* current active chain is missing transactions that were present in a
|
|
* previously active chain, all the missing transactions will have been
|
|
* re-added to the mempool and should be present if they meet size and
|
|
* consistency constraints.
|
|
*
|
|
* 2. Locking `mempool.cs` without `cs_main` will give a view of a mempool
|
|
* consistent with some chain that was active since `cs_main` was last
|
|
* locked, and that is fully populated as described above. It is ok for
|
|
* code that only needs to query or remove transactions from the mempool
|
|
* to lock just `mempool.cs` without `cs_main`.
|
|
*
|
|
* To provide these guarantees, it is necessary to lock both `cs_main` and
|
|
* `mempool.cs` whenever adding transactions to the mempool and whenever
|
|
* changing the chain tip. It's necessary to keep both mutexes locked until
|
|
* the mempool is consistent with the new chain tip and fully populated.
|
|
*/
|
|
mutable RecursiveMutex cs;
|
|
indexed_transaction_set mapTx GUARDED_BY(cs);
|
|
|
|
using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
|
|
std::vector<std::pair<uint256, txiter> > vTxHashes GUARDED_BY(cs); //!< All tx hashes/entries in mapTx, in random order
|
|
|
|
typedef std::set<txiter, CompareIteratorByHash> setEntries;
|
|
|
|
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
private:
|
|
typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
|
|
|
|
|
|
typedef std::map<CMempoolAddressDeltaKey, CMempoolAddressDelta, CMempoolAddressDeltaKeyCompare> addressDeltaMap;
|
|
addressDeltaMap mapAddress;
|
|
|
|
typedef std::map<uint256, std::vector<CMempoolAddressDeltaKey> > addressDeltaMapInserted;
|
|
addressDeltaMapInserted mapAddressInserted;
|
|
|
|
typedef std::map<CSpentIndexKey, CSpentIndexValue, CSpentIndexKeyCompare> mapSpentIndex;
|
|
mapSpentIndex mapSpent;
|
|
|
|
typedef std::map<uint256, std::vector<CSpentIndexKey> > mapSpentIndexInserted;
|
|
mapSpentIndexInserted mapSpentInserted;
|
|
|
|
std::multimap<uint256, uint256> mapProTxRefs; // proTxHash -> transaction (all TXs that refer to an existing proTx)
|
|
std::map<CService, uint256> mapProTxAddresses;
|
|
std::map<CKeyID, uint256> mapProTxPubKeyIDs;
|
|
std::map<uint256, uint256> mapProTxBlsPubKeyHashes;
|
|
std::map<COutPoint, uint256> mapProTxCollaterals;
|
|
std::map<uint256, int /* expiry height */> mapAssetUnlockExpiry; // tx hash -> height
|
|
|
|
void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
|
|
std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
|
|
/**
|
|
* Track locally submitted transactions to periodically retry initial broadcast.
|
|
*/
|
|
std::set<uint256> m_unbroadcast_txids GUARDED_BY(cs);
|
|
|
|
public:
|
|
indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
|
|
std::map<uint256, CAmount> mapDeltas;
|
|
|
|
/** Create a new CTxMemPool.
|
|
* Sanity checks will be off by default for performance, because otherwise
|
|
* accepting transactions becomes O(N^2) where N is the number of transactions
|
|
* in the pool.
|
|
*
|
|
* @param[in] estimator is used to estimate appropriate transaction fees.
|
|
* @param[in] check_ratio is the ratio used to determine how often sanity checks will run.
|
|
*/
|
|
explicit CTxMemPool(CBlockPolicyEstimator* estimator = nullptr, int check_ratio = 0);
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
void check(CChainState& active_chainstate) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
|
|
|
|
// addUnchecked must updated state for all ancestors of a given transaction,
|
|
// to track size/count of descendant transactions. First version of
|
|
// addUnchecked can be used to have it call CalculateMemPoolAncestors(), and
|
|
// then invoke the second version.
|
|
// Note that addUnchecked is ONLY called from ATMP outside of tests
|
|
// and any other callers may break wallet's in-mempool tracking (due to
|
|
// lack of CValidationInterface::TransactionAddedToMempool callbacks).
|
|
void addUnchecked(const CTxMemPoolEntry& entry, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
|
|
void addUnchecked(const CTxMemPoolEntry& entry, setEntries& setAncestors, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
|
|
|
|
void addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view);
|
|
bool getAddressIndex(std::vector<std::pair<uint160, AddressType> > &addresses,
|
|
std::vector<std::pair<CMempoolAddressDeltaKey, CMempoolAddressDelta> > &results);
|
|
bool removeAddressIndex(const uint256 txhash);
|
|
|
|
void addSpentIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view);
|
|
bool getSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value);
|
|
bool removeSpentIndex(const uint256 txhash);
|
|
|
|
void removeRecursive(const CTransaction& tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
void removeForReorg(CChainState& active_chainstate, int flags) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
|
|
void removeConflicts(const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
void removeProTxPubKeyConflicts(const CTransaction &tx, const CKeyID &keyId) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
void removeProTxPubKeyConflicts(const CTransaction &tx, const CBLSLazyPublicKey &pubKey) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
void removeProTxCollateralConflicts(const CTransaction &tx, const COutPoint &collateralOutpoint) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
void removeProTxSpentCollateralConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
void removeProTxKeyChangedConflicts(const CTransaction &tx, const uint256& proTxHash, const uint256& newKeyHash) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
void removeProTxConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
void removeExpiredAssetUnlock(int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
|
|
void clear();
|
|
void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs); //lock free
|
|
bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb);
|
|
void queryHashes(std::vector<uint256>& vtxid) const;
|
|
bool isSpent(const COutPoint& outpoint) const;
|
|
unsigned int GetTransactionsUpdated() const;
|
|
void AddTransactionsUpdated(unsigned int n);
|
|
/**
|
|
* 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.
|
|
*/
|
|
bool HasNoInputsOf(const CTransaction& tx) const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
|
|
/** Affect CreateNewBlock prioritisation of transactions */
|
|
void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta);
|
|
void ApplyDelta(const uint256& hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
void ClearPrioritisation(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
|
|
/** Get the transaction in the pool that spends the same prevout */
|
|
const CTransaction* GetConflictTx(const COutPoint& prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
|
|
/** Returns an iterator to the given hash, if found */
|
|
std::optional<txiter> GetIter(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
|
|
/** Translate a set of hashes into a set of pool iterators to avoid repeated lookups */
|
|
setEntries GetIterSet(const std::set<uint256>& hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
|
|
/** 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.
|
|
*/
|
|
void RemoveStaged(setEntries& stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
|
|
/** 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.
|
|
*/
|
|
void UpdateTransactionsFromBlock(const std::vector<uint256>& vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main) LOCKS_EXCLUDED(m_epoch);
|
|
|
|
/** 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
|
|
*/
|
|
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);
|
|
|
|
/** Populate setDescendants with all in-mempool descendants of hash.
|
|
* Assumes that setDescendants includes all in-mempool descendants of anything
|
|
* already in it. */
|
|
void CalculateDescendants(txiter it, setEntries& setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
|
|
/** 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.
|
|
*/
|
|
CFeeRate GetMinFee(size_t sizelimit) const;
|
|
|
|
/** 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.
|
|
*/
|
|
void TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
|
|
/** Expire all transaction (and their dependencies) in the mempool older than time. Return the number of removed transactions. */
|
|
int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
|
|
/**
|
|
* Calculate the ancestor and descendant count for the given transaction.
|
|
* The counts include the transaction itself.
|
|
*/
|
|
void GetTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) const;
|
|
|
|
/** @returns true if the mempool is fully loaded */
|
|
bool IsLoaded() const;
|
|
|
|
/** Sets the current loaded state */
|
|
void SetIsLoaded(bool loaded);
|
|
|
|
unsigned long size() const
|
|
{
|
|
LOCK(cs);
|
|
return mapTx.size();
|
|
}
|
|
|
|
uint64_t GetTotalTxSize() const EXCLUSIVE_LOCKS_REQUIRED(cs)
|
|
{
|
|
AssertLockHeld(cs);
|
|
return totalTxSize;
|
|
}
|
|
|
|
CAmount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
|
|
{
|
|
AssertLockHeld(cs);
|
|
return m_total_fee;
|
|
}
|
|
|
|
bool exists(const uint256& hash) const
|
|
{
|
|
LOCK(cs);
|
|
return (mapTx.count(hash) != 0);
|
|
}
|
|
|
|
CTransactionRef get(const uint256& hash) const;
|
|
TxMempoolInfo info(const uint256& hash) const;
|
|
std::vector<TxMempoolInfo> infoAll() const;
|
|
|
|
bool existsProviderTxConflict(const CTransaction &tx) const;
|
|
|
|
size_t DynamicMemoryUsage() const;
|
|
|
|
/** Adds a transaction to the unbroadcast set */
|
|
void AddUnbroadcastTx(const uint256& txid)
|
|
{
|
|
LOCK(cs);
|
|
// Sanity check the transaction is in the mempool & insert into
|
|
// unbroadcast set.
|
|
if (exists(txid)) m_unbroadcast_txids.insert(txid);
|
|
}
|
|
|
|
/** Removes a transaction from the unbroadcast set */
|
|
void RemoveUnbroadcastTx(const uint256& txid, const bool unchecked = false);
|
|
|
|
/** Returns transactions in unbroadcast set */
|
|
std::set<uint256> GetUnbroadcastTxs() const
|
|
{
|
|
LOCK(cs);
|
|
return m_unbroadcast_txids;
|
|
}
|
|
|
|
/** Returns whether a txid is in the unbroadcast set */
|
|
bool IsUnbroadcastTx(const uint256& txid) const {
|
|
LOCK(cs);
|
|
return (m_unbroadcast_txids.count(txid) != 0);
|
|
}
|
|
|
|
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.
|
|
*/
|
|
void UpdateForDescendants(txiter updateIt,
|
|
cacheMap &cachedDescendants,
|
|
const std::set<uint256> &setExclude) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
/** Update ancestors of hash to add/remove it as a descendant transaction. */
|
|
void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
/** Set ancestor state for an entry */
|
|
void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
/** For each transaction being removed, update ancestors and any direct children.
|
|
* If updateDescendants is true, then also update in-mempool descendants'
|
|
* ancestor state. */
|
|
void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
/** Sever link between specified transaction and direct children. */
|
|
void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
|
|
/** 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.
|
|
*/
|
|
void removeUnchecked(txiter entry, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
public:
|
|
/** visited marks a CTxMemPoolEntry as having been traversed
|
|
* during the lifetime of the most recently created Epoch::Guard
|
|
* and returns false if we are the first visitor, true otherwise.
|
|
*
|
|
* An Epoch::Guard must be held when visited is called or an assert will be
|
|
* triggered.
|
|
*
|
|
*/
|
|
bool visited(const txiter it) const EXCLUSIVE_LOCKS_REQUIRED(cs, m_epoch)
|
|
{
|
|
return m_epoch.visited(it->m_epoch_marker);
|
|
}
|
|
|
|
bool visited(boost::optional<txiter> it) const EXCLUSIVE_LOCKS_REQUIRED(cs, m_epoch) {
|
|
assert(m_epoch.guarded());
|
|
return !it || visited(*it);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* CCoinsView that brings transactions from a mempool into view.
|
|
* It does not check for spendings by memory pool transactions.
|
|
* Instead, it provides access to all Coins which are either unspent in the
|
|
* base CCoinsView, are outputs from any mempool transaction, or are
|
|
* tracked temporarily to allow transaction dependencies in package validation.
|
|
* This allows transaction replacement to work as expected, as you want to
|
|
* have all inputs "available" to check signatures, and any cycles in the
|
|
* dependency graph are checked directly in AcceptToMemoryPool.
|
|
* It also allows you to sign a double-spend directly in
|
|
* signrawtransactionwithkey and signrawtransactionwithwallet,
|
|
* as long as the conflicting transaction is not yet confirmed.
|
|
*/
|
|
class CCoinsViewMemPool : public CCoinsViewBacked
|
|
{
|
|
/**
|
|
* Coins made available by transactions being validated. Tracking these allows for package
|
|
* validation, since we can access transaction outputs without submitting them to mempool.
|
|
*/
|
|
std::unordered_map<COutPoint, Coin, SaltedOutpointHasher> m_temp_added;
|
|
protected:
|
|
const CTxMemPool& mempool;
|
|
|
|
public:
|
|
CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
|
|
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
|
|
/** Add the coins created by this transaction. These coins are only temporarily stored in
|
|
* m_temp_added and cannot be flushed to the back end. Only used for package validation. */
|
|
void PackageAddTransaction(const CTransactionRef& tx);
|
|
};
|
|
|
|
/**
|
|
* DisconnectedBlockTransactions
|
|
|
|
* During the reorg, it's desirable to re-add previously confirmed transactions
|
|
* to the mempool, so that anything not re-confirmed in the new chain is
|
|
* available to be mined. However, it's more efficient to wait until the reorg
|
|
* is complete and process all still-unconfirmed transactions at that time,
|
|
* since we expect most confirmed transactions to (typically) still be
|
|
* confirmed in the new chain, and re-accepting to the memory pool is expensive
|
|
* (and therefore better to not do in the middle of reorg-processing).
|
|
* Instead, store the disconnected transactions (in order!) as we go, remove any
|
|
* that are included in blocks in the new chain, and then process the remaining
|
|
* still-unconfirmed transactions at the end.
|
|
*/
|
|
|
|
// multi_index tag names
|
|
struct txid_index {};
|
|
struct insertion_order {};
|
|
|
|
struct DisconnectedBlockTransactions {
|
|
typedef boost::multi_index_container<
|
|
CTransactionRef,
|
|
boost::multi_index::indexed_by<
|
|
// sorted by txid
|
|
boost::multi_index::hashed_unique<
|
|
boost::multi_index::tag<txid_index>,
|
|
mempoolentry_txid,
|
|
SaltedTxidHasher
|
|
>,
|
|
// sorted by order in the blockchain
|
|
boost::multi_index::sequenced<
|
|
boost::multi_index::tag<insertion_order>
|
|
>
|
|
>
|
|
> indexed_disconnected_transactions;
|
|
|
|
// It's almost certainly a logic bug if we don't clear out queuedTx before
|
|
// destruction, as we add to it while disconnecting blocks, and then we
|
|
// need to re-process remaining transactions to ensure mempool consistency.
|
|
// For now, assert() that we've emptied out this object on destruction.
|
|
// This assert() can always be removed if the reorg-processing code were
|
|
// to be refactored such that this assumption is no longer true (for
|
|
// instance if there was some other way we cleaned up the mempool after a
|
|
// reorg, besides draining this object).
|
|
~DisconnectedBlockTransactions() { assert(queuedTx.empty()); }
|
|
|
|
indexed_disconnected_transactions queuedTx;
|
|
uint64_t cachedInnerUsage = 0;
|
|
|
|
// Estimate the overhead of queuedTx to be 6 pointers + an allocation, as
|
|
// no exact formula for boost::multi_index_contained is implemented.
|
|
size_t DynamicMemoryUsage() const {
|
|
return memusage::MallocUsage(sizeof(CTransactionRef) + 6 * sizeof(void*)) * queuedTx.size() + cachedInnerUsage;
|
|
}
|
|
|
|
void addTransaction(const CTransactionRef& tx)
|
|
{
|
|
queuedTx.insert(tx);
|
|
cachedInnerUsage += RecursiveDynamicUsage(tx);
|
|
}
|
|
|
|
// Remove entries based on txid_index, and update memory usage.
|
|
void removeForBlock(const std::vector<CTransactionRef>& vtx)
|
|
{
|
|
// Short-circuit in the common case of a block being added to the tip
|
|
if (queuedTx.empty()) {
|
|
return;
|
|
}
|
|
for (auto const &tx : vtx) {
|
|
auto it = queuedTx.find(tx->GetHash());
|
|
if (it != queuedTx.end()) {
|
|
cachedInnerUsage -= RecursiveDynamicUsage(*it);
|
|
queuedTx.erase(it);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Remove an entry by insertion_order index, and update memory usage.
|
|
void removeEntry(indexed_disconnected_transactions::index<insertion_order>::type::iterator entry)
|
|
{
|
|
cachedInnerUsage -= RecursiveDynamicUsage(*entry);
|
|
queuedTx.get<insertion_order>().erase(entry);
|
|
}
|
|
|
|
void clear()
|
|
{
|
|
cachedInnerUsage = 0;
|
|
queuedTx.clear();
|
|
}
|
|
};
|
|
|
|
#endif // BITCOIN_TXMEMPOOL_H
|