2013-08-27 07:51:57 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-12-17 02:47:57 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
2014-11-17 03:29:09 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-08-27 07:51:57 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2014-08-28 22:21:03 +02:00
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
#ifndef BITCOIN_TXMEMPOOL_H
|
|
|
|
#define BITCOIN_TXMEMPOOL_H
|
|
|
|
|
2014-02-15 22:38:28 +01:00
|
|
|
#include <list>
|
2015-07-15 20:47:45 +02:00
|
|
|
#include <set>
|
2014-02-15 22:38:28 +01:00
|
|
|
|
2014-10-23 02:05:11 +02:00
|
|
|
#include "amount.h"
|
2013-11-05 02:47:07 +01:00
|
|
|
#include "coins.h"
|
2014-11-18 22:03:02 +01:00
|
|
|
#include "primitives/transaction.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "sync.h"
|
2013-08-27 07:51:57 +02:00
|
|
|
|
2015-06-24 10:32:20 +02:00
|
|
|
#undef foreach
|
|
|
|
#include "boost/multi_index_container.hpp"
|
|
|
|
#include "boost/multi_index/ordered_index.hpp"
|
|
|
|
|
2014-10-22 21:08:30 +02:00
|
|
|
class CAutoFile;
|
|
|
|
|
2014-11-02 00:14:47 +01:00
|
|
|
inline double AllowFreeThreshold()
|
|
|
|
{
|
|
|
|
return COIN * 144 / 250;
|
|
|
|
}
|
|
|
|
|
2014-03-17 13:19:54 +01:00
|
|
|
inline bool AllowFree(double dPriority)
|
|
|
|
{
|
|
|
|
// Large (in bytes) low-priority (new, small-coin) transactions
|
|
|
|
// need a fee.
|
2014-11-02 00:14:47 +01:00
|
|
|
return dPriority > AllowFreeThreshold();
|
2014-03-17 13:19:54 +01:00
|
|
|
}
|
|
|
|
|
2013-11-05 02:47:07 +01:00
|
|
|
/** Fake height value used in CCoins to signify they are only in the memory pool (since 0.8) */
|
|
|
|
static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
class CTxMemPool;
|
|
|
|
|
|
|
|
/** \class CTxMemPoolEntry
|
|
|
|
*
|
|
|
|
* CTxMemPoolEntry stores data about the correponding 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 nFeesWithDescendants) for
|
|
|
|
* all ancestors of the newly added transaction.
|
|
|
|
*
|
|
|
|
* If updating the descendant state is skipped, we can mark the entry as
|
|
|
|
* "dirty", and set nSizeWithDescendants/nFeesWithDescendants to equal nTxSize/
|
|
|
|
* nTxFee. (This can potentially happen during a reorg, where we limit the
|
|
|
|
* amount of work we're willing to do to avoid consuming too much CPU.)
|
|
|
|
*
|
2013-11-11 08:35:14 +01:00
|
|
|
*/
|
2015-07-15 20:47:45 +02:00
|
|
|
|
2013-11-11 08:35:14 +01:00
|
|
|
class CTxMemPoolEntry
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
CTransaction tx;
|
2014-11-17 03:29:09 +01:00
|
|
|
CAmount nFee; //! Cached to avoid expensive parent-transaction lookups
|
|
|
|
size_t nTxSize; //! ... and avoid recomputing tx size
|
|
|
|
size_t nModSize; //! ... and modified size for priority
|
2015-07-09 19:56:31 +02:00
|
|
|
size_t nUsageSize; //! ... and total memory usage
|
2014-11-17 03:29:09 +01:00
|
|
|
int64_t nTime; //! Local time when entering the mempool
|
|
|
|
double dPriority; //! Priority when entering the mempool
|
|
|
|
unsigned int nHeight; //! Chain height when entering the mempool
|
2014-08-26 22:28:32 +02:00
|
|
|
bool hadNoDependencies; //! Not dependent on any other txs when it entered the mempool
|
2013-11-11 08:35:14 +01:00
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
// 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. if nCountWithDescendants is 0, treat this entry as
|
|
|
|
// dirty, and nSizeWithDescendants and nFeesWithDescendants will not be
|
|
|
|
// correct.
|
|
|
|
uint64_t nCountWithDescendants; //! number of descendant transactions
|
|
|
|
uint64_t nSizeWithDescendants; //! ... and size
|
|
|
|
CAmount nFeesWithDescendants; //! ... and total fees (all including us)
|
|
|
|
|
2013-11-11 08:35:14 +01:00
|
|
|
public:
|
2014-04-23 00:46:19 +02:00
|
|
|
CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
|
2014-08-26 22:28:32 +02:00
|
|
|
int64_t _nTime, double _dPriority, unsigned int _nHeight, bool poolHasNoInputsOf = false);
|
2013-11-11 08:35:14 +01:00
|
|
|
CTxMemPoolEntry(const CTxMemPoolEntry& other);
|
|
|
|
|
|
|
|
const CTransaction& GetTx() const { return this->tx; }
|
|
|
|
double GetPriority(unsigned int currentHeight) const;
|
2015-10-02 23:17:27 +02:00
|
|
|
const CAmount& GetFee() const { return nFee; }
|
2013-11-11 08:35:14 +01:00
|
|
|
size_t GetTxSize() const { return nTxSize; }
|
|
|
|
int64_t GetTime() const { return nTime; }
|
|
|
|
unsigned int GetHeight() const { return nHeight; }
|
2014-08-26 22:28:32 +02:00
|
|
|
bool WasClearAtEntry() const { return hadNoDependencies; }
|
2015-07-09 19:56:31 +02:00
|
|
|
size_t DynamicMemoryUsage() const { return nUsageSize; }
|
2015-07-15 20:47:45 +02:00
|
|
|
|
|
|
|
// Adjusts the descendant state, if this entry is not dirty.
|
|
|
|
void UpdateState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount);
|
|
|
|
|
|
|
|
/** We can set the entry to be dirty if doing the full calculation of in-
|
|
|
|
* mempool descendants will be too expensive, which can potentially happen
|
|
|
|
* when re-adding transactions from a block back to the mempool.
|
|
|
|
*/
|
|
|
|
void SetDirty();
|
|
|
|
bool IsDirty() const { return nCountWithDescendants == 0; }
|
|
|
|
|
|
|
|
uint64_t GetCountWithDescendants() const { return nCountWithDescendants; }
|
|
|
|
uint64_t GetSizeWithDescendants() const { return nSizeWithDescendants; }
|
|
|
|
CAmount GetFeesWithDescendants() const { return nFeesWithDescendants; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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.UpdateState(modifySize, modifyFee, modifyCount); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int64_t modifySize;
|
|
|
|
CAmount modifyFee;
|
|
|
|
int64_t modifyCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct set_dirty
|
|
|
|
{
|
|
|
|
void operator() (CTxMemPoolEntry &e)
|
|
|
|
{ e.SetDirty(); }
|
2013-11-11 08:35:14 +01:00
|
|
|
};
|
|
|
|
|
2015-06-24 10:32:20 +02:00
|
|
|
// extracts a TxMemPoolEntry's transaction hash
|
|
|
|
struct mempoolentry_txid
|
|
|
|
{
|
|
|
|
typedef uint256 result_type;
|
|
|
|
result_type operator() (const CTxMemPoolEntry &entry) const
|
|
|
|
{
|
|
|
|
return entry.GetTx().GetHash();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
/** \class CompareTxMemPoolEntryByFee
|
|
|
|
*
|
|
|
|
* Sort an entry by max(feerate of entry's tx, feerate with all descendants).
|
|
|
|
*/
|
2015-06-24 10:32:20 +02:00
|
|
|
class CompareTxMemPoolEntryByFee
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b)
|
|
|
|
{
|
2015-07-15 20:47:45 +02:00
|
|
|
bool fUseADescendants = UseDescendantFeeRate(a);
|
|
|
|
bool fUseBDescendants = UseDescendantFeeRate(b);
|
|
|
|
|
|
|
|
double aFees = fUseADescendants ? a.GetFeesWithDescendants() : a.GetFee();
|
|
|
|
double aSize = fUseADescendants ? a.GetSizeWithDescendants() : a.GetTxSize();
|
|
|
|
|
|
|
|
double bFees = fUseBDescendants ? b.GetFeesWithDescendants() : b.GetFee();
|
|
|
|
double bSize = fUseBDescendants ? b.GetSizeWithDescendants() : b.GetTxSize();
|
|
|
|
|
|
|
|
// Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
|
|
|
|
double f1 = aFees * bSize;
|
|
|
|
double f2 = aSize * bFees;
|
|
|
|
|
|
|
|
if (f1 == f2) {
|
2015-09-24 19:21:31 +02:00
|
|
|
return a.GetTime() >= b.GetTime();
|
2015-07-15 20:47:45 +02:00
|
|
|
}
|
2015-09-24 19:21:31 +02:00
|
|
|
return f1 < f2;
|
2015-07-15 20:47:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate which feerate to use for an entry (avoiding division).
|
|
|
|
bool UseDescendantFeeRate(const CTxMemPoolEntry &a)
|
|
|
|
{
|
|
|
|
double f1 = (double)a.GetFee() * a.GetSizeWithDescendants();
|
|
|
|
double f2 = (double)a.GetFeesWithDescendants() * a.GetTxSize();
|
|
|
|
return f2 > f1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CompareTxMemPoolEntryByEntryTime
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b)
|
|
|
|
{
|
|
|
|
return a.GetTime() < b.GetTime();
|
2015-06-24 10:32:20 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
class CBlockPolicyEstimator;
|
2014-03-17 13:19:54 +01:00
|
|
|
|
2014-10-17 01:58:43 +02:00
|
|
|
/** An inpoint - a combination of a transaction and an index n into its vin */
|
|
|
|
class CInPoint
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
const CTransaction* ptx;
|
|
|
|
uint32_t n;
|
|
|
|
|
|
|
|
CInPoint() { SetNull(); }
|
|
|
|
CInPoint(const CTransaction* ptxIn, uint32_t nIn) { ptx = ptxIn; n = nIn; }
|
|
|
|
void SetNull() { ptx = NULL; n = (uint32_t) -1; }
|
|
|
|
bool IsNull() const { return (ptx == NULL && n == (uint32_t) -1); }
|
2015-07-09 19:56:31 +02:00
|
|
|
size_t DynamicMemoryUsage() const { return 0; }
|
2014-10-17 01:58:43 +02:00
|
|
|
};
|
|
|
|
|
2014-11-17 03:29:09 +01:00
|
|
|
/**
|
2013-08-27 07:51:57 +02:00
|
|
|
* 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: if a new transaction double-spends
|
|
|
|
* an input of a transaction in the pool, it is dropped,
|
|
|
|
* as are non-standard transactions.
|
2015-07-15 20:47:45 +02:00
|
|
|
*
|
|
|
|
* CTxMemPool::mapTx, and CTxMemPoolEntry bookkeeping:
|
|
|
|
*
|
2015-10-02 23:43:30 +02:00
|
|
|
* mapTx is a boost::multi_index that sorts the mempool on 3 criteria:
|
2015-07-15 20:47:45 +02:00
|
|
|
* - transaction hash
|
|
|
|
* - feerate [we use max(feerate of tx, feerate of tx with all descendants)]
|
2015-10-02 23:43:30 +02:00
|
|
|
* - time in mempool
|
2015-07-15 20:47:45 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Adding transactions from a disconnected block can be very time consuming,
|
|
|
|
* because we don't have a way to limit the number of in-mempool descendants.
|
|
|
|
* To bound CPU processing, we limit the amount of work we're willing to do
|
|
|
|
* to properly update the descendant information for a tx being added from
|
|
|
|
* a disconnected block. If we would exceed the limit, then we instead mark
|
|
|
|
* the entry as "dirty", and set the feerate for sorting purposes to be equal
|
|
|
|
* the feerate of the transaction without any descendants.
|
|
|
|
*
|
2013-08-27 07:51:57 +02:00
|
|
|
*/
|
|
|
|
class CTxMemPool
|
|
|
|
{
|
|
|
|
private:
|
2015-10-07 23:34:55 +02:00
|
|
|
uint32_t nCheckFrequency; //! Value n means that n times in 2^32 we check.
|
2013-08-27 07:51:57 +02:00
|
|
|
unsigned int nTransactionsUpdated;
|
2014-08-26 22:28:32 +02:00
|
|
|
CBlockPolicyEstimator* minerPolicyEstimator;
|
2013-08-27 07:51:57 +02:00
|
|
|
|
2014-11-17 03:29:09 +01:00
|
|
|
uint64_t totalTxSize; //! sum of all mempool tx' byte sizes
|
2015-07-09 19:56:31 +02:00
|
|
|
uint64_t cachedInnerUsage; //! sum of dynamic memory usage of all the map elements (NOT the maps themselves)
|
2014-07-03 20:25:32 +02:00
|
|
|
|
2015-10-13 09:57:41 +02:00
|
|
|
CFeeRate minReasonableRelayFee;
|
|
|
|
|
2015-10-02 23:19:55 +02:00
|
|
|
mutable int64_t lastRollingFeeUpdate;
|
|
|
|
mutable bool blockSinceLastRollingFeeBump;
|
|
|
|
mutable double rollingMinimumFeeRate; //! minimum fee to get into the pool, decreases exponentially
|
|
|
|
|
|
|
|
void trackPackageRemoved(const CFeeRate& rate);
|
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
public:
|
2015-10-13 09:43:15 +02:00
|
|
|
|
|
|
|
static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
|
|
|
|
|
2015-06-24 10:32:20 +02:00
|
|
|
typedef boost::multi_index_container<
|
|
|
|
CTxMemPoolEntry,
|
|
|
|
boost::multi_index::indexed_by<
|
|
|
|
// sorted by txid
|
|
|
|
boost::multi_index::ordered_unique<mempoolentry_txid>,
|
|
|
|
// sorted by fee rate
|
|
|
|
boost::multi_index::ordered_non_unique<
|
|
|
|
boost::multi_index::identity<CTxMemPoolEntry>,
|
|
|
|
CompareTxMemPoolEntryByFee
|
2015-10-02 23:43:30 +02:00
|
|
|
>,
|
|
|
|
// sorted by entry time
|
|
|
|
boost::multi_index::ordered_non_unique<
|
|
|
|
boost::multi_index::identity<CTxMemPoolEntry>,
|
|
|
|
CompareTxMemPoolEntryByEntryTime
|
2015-06-24 10:32:20 +02:00
|
|
|
>
|
|
|
|
>
|
|
|
|
> indexed_transaction_set;
|
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
mutable CCriticalSection cs;
|
2015-06-24 10:32:20 +02:00
|
|
|
indexed_transaction_set mapTx;
|
2015-07-15 20:47:45 +02:00
|
|
|
typedef indexed_transaction_set::nth_index<0>::type::iterator txiter;
|
|
|
|
struct CompareIteratorByHash {
|
|
|
|
bool operator()(const txiter &a, const txiter &b) const {
|
|
|
|
return a->GetTx().GetHash() < b->GetTx().GetHash();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
typedef std::set<txiter, CompareIteratorByHash> setEntries;
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
|
|
|
|
|
|
|
|
struct TxLinks {
|
|
|
|
setEntries parents;
|
|
|
|
setEntries children;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::map<txiter, TxLinks, CompareIteratorByHash> txlinksMap;
|
|
|
|
txlinksMap mapLinks;
|
|
|
|
|
|
|
|
const setEntries & GetMemPoolParents(txiter entry) const;
|
|
|
|
const setEntries & GetMemPoolChildren(txiter entry) const;
|
|
|
|
void UpdateParent(txiter entry, txiter parent, bool add);
|
|
|
|
void UpdateChild(txiter entry, txiter child, bool add);
|
|
|
|
|
|
|
|
public:
|
2013-08-27 07:51:57 +02:00
|
|
|
std::map<COutPoint, CInPoint> mapNextTx;
|
2014-04-23 00:46:19 +02:00
|
|
|
std::map<uint256, std::pair<double, CAmount> > mapDeltas;
|
2013-08-27 07:51:57 +02:00
|
|
|
|
2015-10-13 09:57:41 +02:00
|
|
|
/** Create a new CTxMemPool.
|
|
|
|
* minReasonableRelayFee should be a feerate which is, roughly, somewhere
|
|
|
|
* around what it "costs" to relay a transaction around the network and
|
|
|
|
* below which we would reasonably say a transaction has 0-effective-fee.
|
|
|
|
*/
|
|
|
|
CTxMemPool(const CFeeRate& _minReasonableRelayFee);
|
2014-03-17 13:19:54 +01:00
|
|
|
~CTxMemPool();
|
2013-08-27 07:51:57 +02:00
|
|
|
|
2014-11-17 03:29:09 +01:00
|
|
|
/**
|
2013-08-27 07:51:57 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-07-19 17:14:23 +02:00
|
|
|
void check(const CCoinsViewCache *pcoins) const;
|
2015-10-28 03:07:59 +01:00
|
|
|
void setSanityCheck(double dFrequency = 1.0) { nCheckFrequency = dFrequency * 4294967295.0; }
|
2013-08-27 07:51:57 +02:00
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
// 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.
|
2014-08-26 22:28:32 +02:00
|
|
|
bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate = true);
|
2015-07-15 20:47:45 +02:00
|
|
|
bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, setEntries &setAncestors, bool fCurrentEstimate = true);
|
|
|
|
|
2014-02-15 22:38:28 +01:00
|
|
|
void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
|
2014-11-12 05:57:54 +01:00
|
|
|
void removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight);
|
2014-02-15 22:38:28 +01:00
|
|
|
void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
|
2014-03-17 13:19:54 +01:00
|
|
|
void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
|
2014-08-26 22:28:32 +02:00
|
|
|
std::list<CTransaction>& conflicts, bool fCurrentEstimate = true);
|
2013-08-27 07:51:57 +02:00
|
|
|
void clear();
|
2015-10-26 14:55:17 +01:00
|
|
|
void _clear(); //lock free
|
2013-08-27 07:51:57 +02:00
|
|
|
void queryHashes(std::vector<uint256>& vtxid);
|
|
|
|
void pruneSpent(const uint256& hash, CCoins &coins);
|
|
|
|
unsigned int GetTransactionsUpdated() const;
|
|
|
|
void AddTransactionsUpdated(unsigned int n);
|
2014-08-26 22:28:32 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
2013-08-27 07:51:57 +02:00
|
|
|
|
2012-07-11 20:52:41 +02:00
|
|
|
/** Affect CreateNewBlock prioritisation of transactions */
|
2014-04-23 00:46:19 +02:00
|
|
|
void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount& nFeeDelta);
|
2015-10-02 23:17:27 +02:00
|
|
|
void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta) const;
|
2012-07-11 20:52:41 +02:00
|
|
|
void ClearPrioritisation(const uint256 hash);
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
public:
|
|
|
|
/** 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.*/
|
|
|
|
void RemoveStaged(setEntries &stage);
|
|
|
|
|
|
|
|
/** 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 hashesToUpdate (excluding any
|
|
|
|
* child transactions present in hashesToUpdate, which are already accounted
|
|
|
|
* for). Note: hashesToUpdate should be the set of transactions from the
|
|
|
|
* disconnected block that have been accepted back into the mempool.
|
|
|
|
*/
|
|
|
|
void UpdateTransactionsFromBlock(const std::vector<uint256> &hashesToUpdate);
|
|
|
|
|
|
|
|
/** 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
|
2015-09-23 19:37:32 +02:00
|
|
|
* 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
|
2015-07-15 20:47:45 +02:00
|
|
|
*/
|
2015-09-23 19:37:32 +02:00
|
|
|
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);
|
2015-07-15 20:47:45 +02:00
|
|
|
|
2015-10-02 23:19:55 +02:00
|
|
|
/** The minimum fee to get into the mempool, which may itself not be enough
|
2015-10-14 21:46:20 +02:00
|
|
|
* for larger-sized transactions.
|
|
|
|
* The minReasonableRelayFee constructor arg 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.
|
|
|
|
*/
|
2015-10-02 23:19:55 +02:00
|
|
|
CFeeRate GetMinFee(size_t sizelimit) const;
|
|
|
|
|
|
|
|
/** Remove transactions from the mempool until its dynamic size is <= sizelimit. */
|
|
|
|
void TrimToSize(size_t sizelimit);
|
|
|
|
|
2015-10-02 23:43:30 +02:00
|
|
|
/** Expire all transaction (and their dependencies) in the mempool older than time. Return the number of removed transactions. */
|
|
|
|
int Expire(int64_t time);
|
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
unsigned long size()
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
return mapTx.size();
|
|
|
|
}
|
2015-07-09 19:56:31 +02:00
|
|
|
|
2014-08-07 05:58:19 +02:00
|
|
|
uint64_t GetTotalTxSize()
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
return totalTxSize;
|
|
|
|
}
|
2013-08-27 07:51:57 +02:00
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
bool exists(uint256 hash) const
|
2013-08-27 07:51:57 +02:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
return (mapTx.count(hash) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool lookup(uint256 hash, CTransaction& result) const;
|
2014-03-17 13:19:54 +01:00
|
|
|
|
2014-11-17 03:29:09 +01:00
|
|
|
/** Estimate fee rate needed to get into the next nBlocks */
|
2014-03-17 13:19:54 +01:00
|
|
|
CFeeRate estimateFee(int nBlocks) const;
|
2014-11-17 03:29:09 +01:00
|
|
|
|
|
|
|
/** Estimate priority needed to get into the next nBlocks */
|
2014-03-17 13:19:54 +01:00
|
|
|
double estimatePriority(int nBlocks) const;
|
2014-11-17 03:29:09 +01:00
|
|
|
|
|
|
|
/** Write/Read estimates to disk */
|
2014-03-17 13:19:54 +01:00
|
|
|
bool WriteFeeEstimates(CAutoFile& fileout) const;
|
|
|
|
bool ReadFeeEstimates(CAutoFile& filein);
|
2015-07-09 19:56:31 +02:00
|
|
|
|
|
|
|
size_t DynamicMemoryUsage() const;
|
2015-07-15 20:47:45 +02:00
|
|
|
|
|
|
|
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).
|
|
|
|
*
|
|
|
|
* If updating an entry requires looking at more than maxDescendantsToVisit
|
|
|
|
* transactions, outside of the ones in setExclude, then give up.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
bool UpdateForDescendants(txiter updateIt,
|
|
|
|
int maxDescendantsToVisit,
|
|
|
|
cacheMap &cachedDescendants,
|
|
|
|
const std::set<uint256> &setExclude);
|
|
|
|
/** Update ancestors of hash to add/remove it as a descendant transaction. */
|
|
|
|
void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors);
|
|
|
|
/** For each transaction being removed, update ancestors and any direct children. */
|
|
|
|
void UpdateForRemoveFromMempool(const setEntries &entriesToRemove);
|
|
|
|
/** Sever link between specified transaction and direct children. */
|
|
|
|
void UpdateChildrenForRemoval(txiter entry);
|
|
|
|
/** 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);
|
|
|
|
|
|
|
|
/** 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);
|
2013-08-27 07:51:57 +02:00
|
|
|
};
|
|
|
|
|
2014-11-17 03:29:09 +01:00
|
|
|
/**
|
|
|
|
* CCoinsView that brings transactions from a memorypool into view.
|
|
|
|
* It does not check for spendings by memory pool transactions.
|
|
|
|
*/
|
2013-11-05 02:47:07 +01:00
|
|
|
class CCoinsViewMemPool : public CCoinsViewBacked
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
CTxMemPool &mempool;
|
|
|
|
|
|
|
|
public:
|
2014-09-24 03:19:04 +02:00
|
|
|
CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn);
|
2014-07-19 16:42:48 +02:00
|
|
|
bool GetCoins(const uint256 &txid, CCoins &coins) const;
|
|
|
|
bool HaveCoins(const uint256 &txid) const;
|
2013-11-05 02:47:07 +01:00
|
|
|
};
|
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_TXMEMPOOL_H
|