2013-08-27 07:51:57 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2009-2017 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
|
|
|
|
|
2016-05-30 17:06:24 +02:00
|
|
|
#include <memory>
|
2015-07-15 20:47:45 +02:00
|
|
|
#include <set>
|
2016-08-28 01:26:41 +02:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
#include <utility>
|
|
|
|
#include <string>
|
2014-02-15 22:38:28 +01:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <amount.h>
|
|
|
|
#include <coins.h>
|
|
|
|
#include <indirectmap.h>
|
|
|
|
#include <policy/feerate.h>
|
|
|
|
#include <primitives/transaction.h>
|
|
|
|
#include <sync.h>
|
|
|
|
#include <random.h>
|
2013-08-27 07:51:57 +02:00
|
|
|
|
2017-08-26 20:09:00 +02:00
|
|
|
#include <boost/multi_index_container.hpp>
|
|
|
|
#include <boost/multi_index/hashed_index.hpp>
|
|
|
|
#include <boost/multi_index/ordered_index.hpp>
|
2016-11-03 19:45:10 +01:00
|
|
|
#include <boost/multi_index/sequenced_index.hpp>
|
2016-03-26 16:44:50 +01:00
|
|
|
#include <boost/signals2/signal.hpp>
|
|
|
|
|
2015-12-04 21:01:22 +01:00
|
|
|
class CBlockIndex;
|
2014-10-22 21:08:30 +02:00
|
|
|
|
2017-04-25 20:29:39 +02:00
|
|
|
/** 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;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2015-12-04 21:01:22 +01:00
|
|
|
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;
|
|
|
|
|
2017-08-07 07:36:37 +02:00
|
|
|
LockPoints() : height(0), time(0), maxInputBlock(nullptr) { }
|
2015-12-04 21:01:22 +01:00
|
|
|
};
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
class CTxMemPool;
|
|
|
|
|
|
|
|
/** \class CTxMemPoolEntry
|
|
|
|
*
|
2016-11-28 09:19:05 +01:00
|
|
|
* CTxMemPoolEntry stores data about the corresponding transaction, as well
|
2015-07-15 20:47:45 +02:00
|
|
|
* 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
|
2015-11-19 17:18:28 +01:00
|
|
|
* (nCountWithDescendants, nSizeWithDescendants, and nModFeesWithDescendants) for
|
2015-07-15 20:47:45 +02:00
|
|
|
* all ancestors of the newly added transaction.
|
|
|
|
*
|
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:
|
2016-11-11 02:34:17 +01:00
|
|
|
CTransactionRef tx;
|
2016-04-03 11:49:36 +02:00
|
|
|
CAmount nFee; //!< Cached to avoid expensive parent-transaction lookups
|
2016-07-18 19:28:26 +02:00
|
|
|
size_t nTxWeight; //!< ... and avoid recomputing tx weight (also used for GetTxSize())
|
2016-04-03 11:49:36 +02:00
|
|
|
size_t nUsageSize; //!< ... and total memory usage
|
|
|
|
int64_t nTime; //!< Local time when entering the mempool
|
|
|
|
unsigned int entryHeight; //!< Chain height when entering the mempool
|
|
|
|
bool spendsCoinbase; //!< keep track of transactions that spend a coinbase
|
2016-01-03 18:54:50 +01:00
|
|
|
int64_t sigOpCost; //!< Total sigop cost
|
2016-04-03 11:49:36 +02:00
|
|
|
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
|
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
|
2017-05-10 11:33:38 +02:00
|
|
|
// descendants as well.
|
2016-04-03 11:49:36 +02:00
|
|
|
uint64_t nCountWithDescendants; //!< number of descendant transactions
|
|
|
|
uint64_t nSizeWithDescendants; //!< ... and size
|
|
|
|
CAmount nModFeesWithDescendants; //!< ... and total fees (all including us)
|
2015-07-15 20:47:45 +02:00
|
|
|
|
2015-10-19 16:54:28 +02:00
|
|
|
// Analogous statistics for ancestor transactions
|
|
|
|
uint64_t nCountWithAncestors;
|
|
|
|
uint64_t nSizeWithAncestors;
|
|
|
|
CAmount nModFeesWithAncestors;
|
2016-01-03 18:54:50 +01:00
|
|
|
int64_t nSigOpCostWithAncestors;
|
2015-10-19 16:54:28 +02:00
|
|
|
|
2013-11-11 08:35:14 +01:00
|
|
|
public:
|
2016-11-11 07:29:19 +01:00
|
|
|
CTxMemPoolEntry(const CTransactionRef& _tx, const CAmount& _nFee,
|
2017-01-20 15:24:35 +01:00
|
|
|
int64_t _nTime, unsigned int _entryHeight,
|
|
|
|
bool spendsCoinbase,
|
2016-01-03 18:54:50 +01:00
|
|
|
int64_t nSigOpsCost, LockPoints lp);
|
2016-11-11 07:29:19 +01:00
|
|
|
|
2016-05-30 17:06:24 +02:00
|
|
|
const CTransaction& GetTx() const { return *this->tx; }
|
2016-11-11 02:34:17 +01:00
|
|
|
CTransactionRef GetSharedTx() const { return this->tx; }
|
2015-10-02 23:17:27 +02:00
|
|
|
const CAmount& GetFee() const { return nFee; }
|
2016-01-03 18:54:50 +01:00
|
|
|
size_t GetTxSize() const;
|
2016-07-18 19:28:26 +02:00
|
|
|
size_t GetTxWeight() const { return nTxWeight; }
|
2013-11-11 08:35:14 +01:00
|
|
|
int64_t GetTime() const { return nTime; }
|
2015-06-30 17:14:24 +02:00
|
|
|
unsigned int GetHeight() const { return entryHeight; }
|
2016-01-03 18:54:50 +01:00
|
|
|
int64_t GetSigOpCost() const { return sigOpCost; }
|
2015-10-26 19:06:06 +01:00
|
|
|
int64_t GetModifiedFee() const { return nFee + feeDelta; }
|
2015-07-09 19:56:31 +02:00
|
|
|
size_t DynamicMemoryUsage() const { return nUsageSize; }
|
2015-12-04 21:01:22 +01:00
|
|
|
const LockPoints& GetLockPoints() const { return lockPoints; }
|
2015-07-15 20:47:45 +02:00
|
|
|
|
2017-05-10 11:33:38 +02:00
|
|
|
// Adjusts the descendant state.
|
2015-10-19 16:54:28 +02:00
|
|
|
void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount);
|
|
|
|
// Adjusts the ancestor state
|
2017-09-07 20:13:01 +02:00
|
|
|
void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps);
|
2015-11-19 17:18:28 +01:00
|
|
|
// Updates the fee delta used for mining priority score, and the
|
|
|
|
// modified fees with descendants.
|
2015-10-26 19:06:06 +01:00
|
|
|
void UpdateFeeDelta(int64_t feeDelta);
|
2015-12-04 21:01:22 +01:00
|
|
|
// Update the LockPoints after a reorg
|
|
|
|
void UpdateLockPoints(const LockPoints& lp);
|
2015-07-15 20:47:45 +02:00
|
|
|
|
|
|
|
uint64_t GetCountWithDescendants() const { return nCountWithDescendants; }
|
|
|
|
uint64_t GetSizeWithDescendants() const { return nSizeWithDescendants; }
|
2015-11-19 17:18:28 +01:00
|
|
|
CAmount GetModFeesWithDescendants() const { return nModFeesWithDescendants; }
|
2015-10-29 19:06:13 +01:00
|
|
|
|
|
|
|
bool GetSpendsCoinbase() const { return spendsCoinbase; }
|
2015-10-19 16:54:28 +02:00
|
|
|
|
|
|
|
uint64_t GetCountWithAncestors() const { return nCountWithAncestors; }
|
|
|
|
uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
|
|
|
|
CAmount GetModFeesWithAncestors() const { return nModFeesWithAncestors; }
|
2016-01-03 18:54:50 +01:00
|
|
|
int64_t GetSigOpCostWithAncestors() const { return nSigOpCostWithAncestors; }
|
2016-06-15 08:59:03 +02:00
|
|
|
|
|
|
|
mutable size_t vTxHashesIdx; //!< Index in mempool's vTxHashes
|
2015-07-15 20:47:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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)
|
2015-10-19 16:54:28 +02:00
|
|
|
{ e.UpdateDescendantState(modifySize, modifyFee, modifyCount); }
|
2015-07-15 20:47:45 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
int64_t modifySize;
|
|
|
|
CAmount modifyFee;
|
|
|
|
int64_t modifyCount;
|
|
|
|
};
|
|
|
|
|
2015-10-19 16:54:28 +02:00
|
|
|
struct update_ancestor_state
|
2015-07-15 20:47:45 +02:00
|
|
|
{
|
2016-01-03 18:54:50 +01:00
|
|
|
update_ancestor_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount, int64_t _modifySigOpsCost) :
|
|
|
|
modifySize(_modifySize), modifyFee(_modifyFee), modifyCount(_modifyCount), modifySigOpsCost(_modifySigOpsCost)
|
2015-10-19 16:54:28 +02:00
|
|
|
{}
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
void operator() (CTxMemPoolEntry &e)
|
2016-01-03 18:54:50 +01:00
|
|
|
{ e.UpdateAncestorState(modifySize, modifyFee, modifyCount, modifySigOpsCost); }
|
2015-07-15 20:47:45 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
int64_t modifySize;
|
|
|
|
CAmount modifyFee;
|
|
|
|
int64_t modifyCount;
|
2016-01-03 18:54:50 +01:00
|
|
|
int64_t modifySigOpsCost;
|
2013-11-11 08:35:14 +01:00
|
|
|
};
|
|
|
|
|
2015-10-26 19:06:06 +01:00
|
|
|
struct update_fee_delta
|
|
|
|
{
|
2017-08-01 12:22:41 +02:00
|
|
|
explicit update_fee_delta(int64_t _feeDelta) : feeDelta(_feeDelta) { }
|
2015-10-26 19:06:06 +01:00
|
|
|
|
|
|
|
void operator() (CTxMemPoolEntry &e) { e.UpdateFeeDelta(feeDelta); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int64_t feeDelta;
|
|
|
|
};
|
|
|
|
|
2015-12-04 21:01:22 +01:00
|
|
|
struct update_lock_points
|
|
|
|
{
|
2017-08-01 12:22:41 +02:00
|
|
|
explicit update_lock_points(const LockPoints& _lp) : lp(_lp) { }
|
2015-12-04 21:01:22 +01:00
|
|
|
|
|
|
|
void operator() (CTxMemPoolEntry &e) { e.UpdateLockPoints(lp); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const LockPoints& lp;
|
|
|
|
};
|
|
|
|
|
2016-11-03 19:45:10 +01:00
|
|
|
// extracts a transaction hash from CTxMempoolEntry or CTransactionRef
|
2015-06-24 10:32:20 +02:00
|
|
|
struct mempoolentry_txid
|
|
|
|
{
|
|
|
|
typedef uint256 result_type;
|
|
|
|
result_type operator() (const CTxMemPoolEntry &entry) const
|
|
|
|
{
|
|
|
|
return entry.GetTx().GetHash();
|
|
|
|
}
|
2016-11-03 19:45:10 +01:00
|
|
|
|
|
|
|
result_type operator() (const CTransactionRef& tx) const
|
|
|
|
{
|
|
|
|
return tx->GetHash();
|
|
|
|
}
|
2015-06-24 10:32:20 +02:00
|
|
|
};
|
|
|
|
|
2015-11-19 17:18:28 +01:00
|
|
|
/** \class CompareTxMemPoolEntryByDescendantScore
|
2015-07-15 20:47:45 +02:00
|
|
|
*
|
2015-11-19 17:18:28 +01:00
|
|
|
* Sort an entry by max(score/size of entry's tx, score/size with all descendants).
|
2015-07-15 20:47:45 +02:00
|
|
|
*/
|
2015-11-19 17:18:28 +01:00
|
|
|
class CompareTxMemPoolEntryByDescendantScore
|
2015-06-24 10:32:20 +02:00
|
|
|
{
|
|
|
|
public:
|
2017-12-07 15:57:53 +01:00
|
|
|
bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
|
2015-06-24 10:32:20 +02:00
|
|
|
{
|
2018-01-09 17:53:40 +01:00
|
|
|
double a_mod_fee, a_size, b_mod_fee, b_size;
|
2015-07-15 20:47:45 +02:00
|
|
|
|
2018-01-09 17:53:40 +01:00
|
|
|
GetModFeeAndSize(a, a_mod_fee, a_size);
|
|
|
|
GetModFeeAndSize(b, b_mod_fee, b_size);
|
2015-07-15 20:47:45 +02:00
|
|
|
|
|
|
|
// Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
|
2018-01-09 17:53:40 +01:00
|
|
|
double f1 = a_mod_fee * b_size;
|
|
|
|
double f2 = a_size * b_mod_fee;
|
2015-07-15 20:47:45 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-09 17:53:40 +01:00
|
|
|
// Return the fee/size we're using for sorting this entry.
|
|
|
|
void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
|
2015-07-15 20:47:45 +02:00
|
|
|
{
|
2018-01-09 17:53:40 +01:00
|
|
|
// Compare feerate with descendants to feerate of the transaction, and
|
|
|
|
// return the fee/size for the max.
|
2015-11-19 17:18:28 +01:00
|
|
|
double f1 = (double)a.GetModifiedFee() * a.GetSizeWithDescendants();
|
|
|
|
double f2 = (double)a.GetModFeesWithDescendants() * a.GetTxSize();
|
2018-01-09 17:53:40 +01:00
|
|
|
|
|
|
|
if (f2 > f1) {
|
|
|
|
mod_fee = a.GetModFeesWithDescendants();
|
|
|
|
size = a.GetSizeWithDescendants();
|
|
|
|
} else {
|
|
|
|
mod_fee = a.GetModifiedFee();
|
|
|
|
size = a.GetTxSize();
|
|
|
|
}
|
2015-07-15 20:47:45 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-26 19:06:06 +01:00
|
|
|
/** \class CompareTxMemPoolEntryByScore
|
|
|
|
*
|
2018-01-19 16:53:55 +01:00
|
|
|
* 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.
|
2015-10-26 19:06:06 +01:00
|
|
|
*/
|
|
|
|
class CompareTxMemPoolEntryByScore
|
|
|
|
{
|
|
|
|
public:
|
2017-12-07 15:57:53 +01:00
|
|
|
bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
|
2015-10-26 19:06:06 +01:00
|
|
|
{
|
2018-01-19 16:53:55 +01:00
|
|
|
double f1 = (double)a.GetFee() * b.GetTxSize();
|
|
|
|
double f2 = (double)b.GetFee() * a.GetTxSize();
|
2015-10-26 19:06:06 +01:00
|
|
|
if (f1 == f2) {
|
|
|
|
return b.GetTx().GetHash() < a.GetTx().GetHash();
|
|
|
|
}
|
|
|
|
return f1 > f2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
class CompareTxMemPoolEntryByEntryTime
|
|
|
|
{
|
|
|
|
public:
|
2017-12-07 15:57:53 +01:00
|
|
|
bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
|
2015-07-15 20:47:45 +02:00
|
|
|
{
|
|
|
|
return a.GetTime() < b.GetTime();
|
2015-06-24 10:32:20 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-09 18:27:57 +01:00
|
|
|
/** \class CompareTxMemPoolEntryByAncestorScore
|
|
|
|
*
|
|
|
|
* Sort an entry by min(score/size of entry's tx, score/size with all ancestors).
|
|
|
|
*/
|
2015-10-19 21:15:12 +02:00
|
|
|
class CompareTxMemPoolEntryByAncestorFee
|
|
|
|
{
|
|
|
|
public:
|
2018-01-13 21:57:30 +01:00
|
|
|
template<typename T>
|
|
|
|
bool operator()(const T& a, const T& b) const
|
2015-10-19 21:15:12 +02:00
|
|
|
{
|
2018-01-09 18:27:57 +01:00
|
|
|
double a_mod_fee, a_size, b_mod_fee, b_size;
|
2015-10-19 21:15:12 +02:00
|
|
|
|
2018-01-09 18:27:57 +01:00
|
|
|
GetModFeeAndSize(a, a_mod_fee, a_size);
|
|
|
|
GetModFeeAndSize(b, b_mod_fee, b_size);
|
2015-10-19 21:15:12 +02:00
|
|
|
|
|
|
|
// Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
|
2018-01-09 18:27:57 +01:00
|
|
|
double f1 = a_mod_fee * b_size;
|
|
|
|
double f2 = a_size * b_mod_fee;
|
2015-10-19 21:15:12 +02:00
|
|
|
|
|
|
|
if (f1 == f2) {
|
|
|
|
return a.GetTx().GetHash() < b.GetTx().GetHash();
|
|
|
|
}
|
|
|
|
return f1 > f2;
|
|
|
|
}
|
2018-01-09 18:27:57 +01:00
|
|
|
|
|
|
|
// Return the fee/size we're using for sorting this entry.
|
2018-01-13 21:57:30 +01:00
|
|
|
template <typename T>
|
|
|
|
void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
|
2018-01-09 18:27:57 +01:00
|
|
|
{
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
2015-10-19 21:15:12 +02:00
|
|
|
};
|
|
|
|
|
2016-02-16 18:10:12 +01:00
|
|
|
// Multi_index tag names
|
|
|
|
struct descendant_score {};
|
|
|
|
struct entry_time {};
|
2015-10-19 21:15:12 +02:00
|
|
|
struct ancestor_score {};
|
2016-02-16 18:10:12 +01:00
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
class CBlockPolicyEstimator;
|
2014-03-17 13:19:54 +01:00
|
|
|
|
2016-05-30 17:06:24 +02:00
|
|
|
/**
|
|
|
|
* Information about a mempool transaction.
|
|
|
|
*/
|
|
|
|
struct TxMempoolInfo
|
|
|
|
{
|
|
|
|
/** The transaction itself */
|
2016-11-11 02:34:17 +01:00
|
|
|
CTransactionRef tx;
|
2016-05-30 17:06:24 +02:00
|
|
|
|
|
|
|
/** Time the transaction entered the mempool. */
|
|
|
|
int64_t nTime;
|
|
|
|
|
|
|
|
/** Feerate of the transaction. */
|
|
|
|
CFeeRate feeRate;
|
2016-07-31 20:53:17 +02:00
|
|
|
|
|
|
|
/** The fee delta. */
|
|
|
|
int64_t nFeeDelta;
|
2016-05-30 17:06:24 +02:00
|
|
|
};
|
|
|
|
|
2016-03-26 16:44:50 +01:00
|
|
|
/** Reason why a transaction was removed from the mempool,
|
|
|
|
* this is passed to the notification signal.
|
|
|
|
*/
|
|
|
|
enum class MemPoolRemovalReason {
|
|
|
|
UNKNOWN = 0, //! Manually removed or unknown reason
|
|
|
|
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
|
|
|
|
REPLACED //! Removed for replacement
|
|
|
|
};
|
|
|
|
|
2017-04-25 20:29:39 +02:00
|
|
|
class SaltedTxidHasher
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
/** Salt */
|
|
|
|
const uint64_t k0, k1;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SaltedTxidHasher();
|
|
|
|
|
|
|
|
size_t operator()(const uint256& txid) const {
|
|
|
|
return SipHashUint256(k0, k1, txid);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-17 03:29:09 +01:00
|
|
|
/**
|
2016-09-16 20:54:44 +02:00
|
|
|
* CTxMemPool stores valid-according-to-the-current-best-chain transactions
|
|
|
|
* that may be included in the next block.
|
2013-08-27 07:51:57 +02:00
|
|
|
*
|
2016-09-16 20:54:44 +02:00
|
|
|
* 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:
|
2017-01-18 16:15:37 +01:00
|
|
|
* - a transaction which doesn't meet the minimum fee requirements.
|
2016-09-16 20:54:44 +02:00
|
|
|
* - a new transaction that double-spends an input of a transaction already in
|
|
|
|
* the pool where the new transaction does not meet the Replace-By-Fee
|
|
|
|
* requirements as defined in BIP 125.
|
|
|
|
* - a non-standard transaction.
|
2015-07-15 20:47:45 +02:00
|
|
|
*
|
|
|
|
* CTxMemPool::mapTx, and CTxMemPoolEntry bookkeeping:
|
|
|
|
*
|
2015-10-26 19:06:06 +01:00
|
|
|
* mapTx is a boost::multi_index that sorts the mempool on 4 criteria:
|
2015-07-15 20:47:45 +02:00
|
|
|
* - transaction hash
|
2018-01-19 15:58:21 +01:00
|
|
|
* - descendant feerate [we use max(feerate of tx, feerate of tx with all descendants)]
|
2015-10-02 23:43:30 +02:00
|
|
|
* - time in mempool
|
2018-01-19 15:58:21 +01:00
|
|
|
* - ancestor feerate [we use min(feerate of tx, feerate of tx with all unconfirmed ancestors)]
|
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.
|
|
|
|
*
|
2013-08-27 07:51:57 +02:00
|
|
|
*/
|
|
|
|
class CTxMemPool
|
|
|
|
{
|
|
|
|
private:
|
2018-03-13 00:05:53 +01:00
|
|
|
uint32_t nCheckFrequency GUARDED_BY(cs); //!< Value n means that n times in 2^32 we check.
|
2017-04-03 22:31:51 +02:00
|
|
|
unsigned int nTransactionsUpdated; //!< Used by getblocktemplate to trigger CreateNewBlock() invocation
|
2014-08-26 22:28:32 +02:00
|
|
|
CBlockPolicyEstimator* minerPolicyEstimator;
|
2013-08-27 07:51:57 +02:00
|
|
|
|
2016-09-15 16:46:01 +02:00
|
|
|
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.
|
2016-04-03 11:49:36 +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-02 23:19:55 +02:00
|
|
|
mutable int64_t lastRollingFeeUpdate;
|
|
|
|
mutable bool blockSinceLastRollingFeeBump;
|
2016-04-03 11:49:36 +02:00
|
|
|
mutable double rollingMinimumFeeRate; //!< minimum fee to get into the pool, decreases exponentially
|
2015-10-02 23:19:55 +02:00
|
|
|
|
2018-04-25 22:57:18 +02:00
|
|
|
void trackPackageRemoved(const CFeeRate& rate) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
2015-10-02 23:19:55 +02:00
|
|
|
|
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
|
2016-05-06 21:36:36 +02:00
|
|
|
boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
|
2015-06-24 10:32:20 +02:00
|
|
|
// sorted by fee rate
|
|
|
|
boost::multi_index::ordered_non_unique<
|
2016-02-16 18:10:12 +01:00
|
|
|
boost::multi_index::tag<descendant_score>,
|
2015-06-24 10:32:20 +02:00
|
|
|
boost::multi_index::identity<CTxMemPoolEntry>,
|
2015-11-19 17:18:28 +01:00
|
|
|
CompareTxMemPoolEntryByDescendantScore
|
2015-10-02 23:43:30 +02:00
|
|
|
>,
|
|
|
|
// sorted by entry time
|
|
|
|
boost::multi_index::ordered_non_unique<
|
2016-02-16 18:10:12 +01:00
|
|
|
boost::multi_index::tag<entry_time>,
|
2015-10-02 23:43:30 +02:00
|
|
|
boost::multi_index::identity<CTxMemPoolEntry>,
|
|
|
|
CompareTxMemPoolEntryByEntryTime
|
2015-10-19 21:15:12 +02:00
|
|
|
>,
|
|
|
|
// sorted by fee rate with ancestors
|
|
|
|
boost::multi_index::ordered_non_unique<
|
|
|
|
boost::multi_index::tag<ancestor_score>,
|
|
|
|
boost::multi_index::identity<CTxMemPoolEntry>,
|
|
|
|
CompareTxMemPoolEntryByAncestorFee
|
2015-06-24 10:32:20 +02:00
|
|
|
>
|
|
|
|
>
|
|
|
|
> indexed_transaction_set;
|
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
mutable CCriticalSection cs;
|
2018-03-13 00:05:53 +01:00
|
|
|
indexed_transaction_set mapTx GUARDED_BY(cs);
|
2016-06-15 08:59:03 +02:00
|
|
|
|
2018-06-24 21:51:54 +02:00
|
|
|
using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
|
2016-06-25 19:17:45 +02:00
|
|
|
std::vector<std::pair<uint256, txiter> > vTxHashes; //!< All tx witness hashes/entries in mapTx, in random order
|
2016-06-15 08:59:03 +02:00
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
struct CompareIteratorByHash {
|
|
|
|
bool operator()(const txiter &a, const txiter &b) const {
|
|
|
|
return a->GetTx().GetHash() < b->GetTx().GetHash();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
typedef std::set<txiter, CompareIteratorByHash> setEntries;
|
|
|
|
|
2018-03-13 00:05:53 +01:00
|
|
|
const setEntries & GetMemPoolParents(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
|
|
|
const setEntries & GetMemPoolChildren(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
2018-05-21 05:37:44 +02:00
|
|
|
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
2015-07-15 20:47:45 +02:00
|
|
|
private:
|
|
|
|
typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
|
|
|
|
|
|
|
|
struct TxLinks {
|
|
|
|
setEntries parents;
|
|
|
|
setEntries children;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::map<txiter, TxLinks, CompareIteratorByHash> txlinksMap;
|
|
|
|
txlinksMap mapLinks;
|
|
|
|
|
|
|
|
void UpdateParent(txiter entry, txiter parent, bool add);
|
|
|
|
void UpdateChild(txiter entry, txiter child, bool add);
|
|
|
|
|
2018-04-25 22:57:18 +02:00
|
|
|
std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
2016-05-30 17:06:24 +02:00
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
public:
|
2018-03-13 00:05:53 +01:00
|
|
|
indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
|
2017-01-20 05:37:15 +01:00
|
|
|
std::map<uint256, CAmount> mapDeltas;
|
2013-08-27 07:51:57 +02:00
|
|
|
|
2015-10-13 09:57:41 +02:00
|
|
|
/** Create a new CTxMemPool.
|
|
|
|
*/
|
2017-08-01 12:22:41 +02:00
|
|
|
explicit CTxMemPool(CBlockPolicyEstimator* estimator = nullptr);
|
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;
|
2017-11-16 09:54:10 +01:00
|
|
|
void setSanityCheck(double dFrequency = 1.0) { LOCK(cs); nCheckFrequency = static_cast<uint32_t>(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.
|
2017-01-20 21:08:14 +01:00
|
|
|
// 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).
|
2018-07-27 08:22:42 +02:00
|
|
|
void addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool validFeeEstimate = true);
|
|
|
|
void addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, setEntries &setAncestors, bool validFeeEstimate = true);
|
2015-07-15 20:47:45 +02:00
|
|
|
|
2016-03-26 16:44:50 +01:00
|
|
|
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason = MemPoolRemovalReason::UNKNOWN);
|
2015-11-23 22:06:12 +01:00
|
|
|
void removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags);
|
2018-02-22 22:16:54 +01:00
|
|
|
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
2016-11-11 19:14:45 +01:00
|
|
|
void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight);
|
2016-03-26 16:44:50 +01:00
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
void clear();
|
2018-03-13 00:05:53 +01:00
|
|
|
void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs); //lock free
|
2016-04-04 04:36:47 +02:00
|
|
|
bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb);
|
2013-08-27 07:51:57 +02:00
|
|
|
void queryHashes(std::vector<uint256>& vtxid);
|
2018-03-22 12:55:22 +01:00
|
|
|
bool isSpent(const COutPoint& outpoint) const;
|
2013-08-27 07:51:57 +02:00
|
|
|
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 */
|
2017-01-20 05:37:15 +01:00
|
|
|
void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta);
|
|
|
|
void ApplyDelta(const uint256 hash, 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
|
2015-10-19 16:54:28 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
2018-04-25 22:57:18 +02:00
|
|
|
void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason = MemPoolRemovalReason::UNKNOWN) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
2015-07-15 20:47:45 +02:00
|
|
|
|
|
|
|
/** 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
|
2017-03-18 21:32:14 +01:00
|
|
|
* 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
|
2015-07-15 20:47:45 +02:00
|
|
|
* disconnected block that have been accepted back into the mempool.
|
|
|
|
*/
|
2017-03-18 21:32:14 +01:00
|
|
|
void UpdateTransactionsFromBlock(const std::vector<uint256> &vHashesToUpdate);
|
2015-07-15 20:47:45 +02:00
|
|
|
|
|
|
|
/** 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
|
|
|
*/
|
2016-03-08 21:49:26 +01: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) const;
|
2015-07-15 20:47:45 +02:00
|
|
|
|
2015-10-30 03:49:00 +01:00
|
|
|
/** Populate setDescendants with all in-mempool descendants of hash.
|
|
|
|
* Assumes that setDescendants includes all in-mempool descendants of anything
|
|
|
|
* already in it. */
|
2018-03-13 00:05:53 +01:00
|
|
|
void CalculateDescendants(txiter it, setEntries& setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
2015-10-30 03:49:00 +01: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.
|
2016-12-13 19:43:35 +01:00
|
|
|
* The incrementalRelayFee policy variable is used to bound the time it
|
2015-10-14 21:46:20 +02:00
|
|
|
* 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;
|
|
|
|
|
2015-10-22 02:44:00 +02:00
|
|
|
/** Remove transactions from the mempool until its dynamic size is <= sizelimit.
|
2017-04-25 20:29:39 +02:00
|
|
|
* pvNoSpendsRemaining, if set, will be populated with the list of outpoints
|
2015-10-22 02:44:00 +02:00
|
|
|
* which are not in mempool which no longer have any spends in this mempool.
|
|
|
|
*/
|
2017-08-07 07:36:37 +02:00
|
|
|
void TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining=nullptr);
|
2015-10-02 23:19:55 +02:00
|
|
|
|
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);
|
|
|
|
|
2018-03-08 18:15:42 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
unsigned long size()
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
return mapTx.size();
|
|
|
|
}
|
2015-07-09 19:56:31 +02:00
|
|
|
|
2017-03-09 13:34:54 +01:00
|
|
|
uint64_t GetTotalTxSize() const
|
2014-08-07 05:58:19 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-11-11 02:34:17 +01:00
|
|
|
CTransactionRef get(const uint256& hash) const;
|
2016-05-30 17:06:24 +02:00
|
|
|
TxMempoolInfo info(const uint256& hash) const;
|
|
|
|
std::vector<TxMempoolInfo> infoAll() const;
|
2014-03-17 13:19:54 +01:00
|
|
|
|
2015-07-09 19:56:31 +02:00
|
|
|
size_t DynamicMemoryUsage() const;
|
2015-07-15 20:47:45 +02:00
|
|
|
|
2016-03-26 16:44:50 +01:00
|
|
|
boost::signals2::signal<void (CTransactionRef)> NotifyEntryAdded;
|
|
|
|
boost::signals2::signal<void (CTransactionRef, MemPoolRemovalReason)> NotifyEntryRemoved;
|
|
|
|
|
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).
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-10-21 16:18:24 +02:00
|
|
|
void UpdateForDescendants(txiter updateIt,
|
2015-07-15 20:47:45 +02:00
|
|
|
cacheMap &cachedDescendants,
|
2018-03-13 00:05:53 +01:00
|
|
|
const std::set<uint256> &setExclude) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
2015-07-15 20:47:45 +02:00
|
|
|
/** Update ancestors of hash to add/remove it as a descendant transaction. */
|
2018-03-13 00:05:53 +01:00
|
|
|
void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
2015-10-19 16:54:28 +02:00
|
|
|
/** Set ancestor state for an entry */
|
2018-03-13 00:05:53 +01:00
|
|
|
void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
2015-10-19 16:54:28 +02:00
|
|
|
/** For each transaction being removed, update ancestors and any direct children.
|
|
|
|
* If updateDescendants is true, then also update in-mempool descendants'
|
|
|
|
* ancestor state. */
|
2018-03-13 00:05:53 +01:00
|
|
|
void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
2015-07-15 20:47:45 +02:00
|
|
|
/** Sever link between specified transaction and direct children. */
|
2018-03-13 00:05:53 +01:00
|
|
|
void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
2015-07-15 20:47:45 +02:00
|
|
|
|
|
|
|
/** 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.
|
|
|
|
*/
|
2018-03-13 00:05:53 +01:00
|
|
|
void removeUnchecked(txiter entry, MemPoolRemovalReason reason = MemPoolRemovalReason::UNKNOWN) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
2013-08-27 07:51:57 +02:00
|
|
|
};
|
|
|
|
|
2018-07-24 17:59:49 +02:00
|
|
|
/**
|
2018-03-18 15:26:45 +01:00
|
|
|
* CCoinsView that brings transactions from a mempool into view.
|
2014-11-17 03:29:09 +01:00
|
|
|
* It does not check for spendings by memory pool transactions.
|
2017-06-27 20:46:19 +02:00
|
|
|
* Instead, it provides access to all Coins which are either unspent in the
|
|
|
|
* base CCoinsView, or are outputs from any mempool transaction!
|
|
|
|
* 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 signrawtransaction,
|
|
|
|
* as long as the conflicting transaction is not yet confirmed.
|
2014-11-17 03:29:09 +01:00
|
|
|
*/
|
2013-11-05 02:47:07 +01:00
|
|
|
class CCoinsViewMemPool : public CCoinsViewBacked
|
|
|
|
{
|
|
|
|
protected:
|
2016-04-30 19:25:00 +02:00
|
|
|
const CTxMemPool& mempool;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
|
|
|
public:
|
2016-04-30 19:25:00 +02:00
|
|
|
CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
|
2017-06-13 21:17:30 +02:00
|
|
|
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
|
2013-11-05 02:47:07 +01:00
|
|
|
};
|
|
|
|
|
2016-11-03 19:45:10 +01:00
|
|
|
/**
|
|
|
|
* 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);
|
2016-11-21 17:47:12 +01:00
|
|
|
cachedInnerUsage += RecursiveDynamicUsage(tx);
|
2016-11-03 19:45:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()) {
|
2016-11-21 17:47:12 +01:00
|
|
|
cachedInnerUsage -= RecursiveDynamicUsage(*it);
|
2016-11-03 19:45:10 +01:00
|
|
|
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)
|
|
|
|
{
|
2016-11-21 17:47:12 +01:00
|
|
|
cachedInnerUsage -= RecursiveDynamicUsage(*entry);
|
2016-11-03 19:45:10 +01:00
|
|
|
queuedTx.get<insertion_order>().erase(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
cachedInnerUsage = 0;
|
|
|
|
queuedTx.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_TXMEMPOOL_H
|