2013-08-27 07:51:57 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-11-17 03:29:09 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin developers
|
|
|
|
// 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>
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
|
2014-11-17 03:29:09 +01:00
|
|
|
/**
|
2013-11-11 08:35:14 +01:00
|
|
|
* CTxMemPool stores these:
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
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
|
2013-11-11 08:35:14 +01:00
|
|
|
|
|
|
|
public:
|
2014-04-23 00:46:19 +02:00
|
|
|
CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
|
2013-11-11 08:35:14 +01:00
|
|
|
int64_t _nTime, double _dPriority, unsigned int _nHeight);
|
|
|
|
CTxMemPoolEntry();
|
|
|
|
CTxMemPoolEntry(const CTxMemPoolEntry& other);
|
|
|
|
|
|
|
|
const CTransaction& GetTx() const { return this->tx; }
|
|
|
|
double GetPriority(unsigned int currentHeight) const;
|
2014-04-23 00:46:19 +02:00
|
|
|
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-03-17 13:19:54 +01:00
|
|
|
class CMinerPolicyEstimator;
|
|
|
|
|
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); }
|
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
class CTxMemPool
|
|
|
|
{
|
|
|
|
private:
|
2014-11-17 03:29:09 +01:00
|
|
|
bool fSanityCheck; //! Normally false, true if -checkmempool or -regtest
|
2013-08-27 07:51:57 +02:00
|
|
|
unsigned int nTransactionsUpdated;
|
2014-03-17 13:19:54 +01:00
|
|
|
CMinerPolicyEstimator* minerPolicyEstimator;
|
2013-08-27 07:51:57 +02:00
|
|
|
|
2014-11-17 03:29:09 +01:00
|
|
|
CFeeRate minRelayFee; //! Passed to constructor to avoid dependency on main
|
|
|
|
uint64_t totalTxSize; //! sum of all mempool tx' byte sizes
|
2014-07-03 20:25:32 +02:00
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
public:
|
|
|
|
mutable CCriticalSection cs;
|
2013-11-11 08:35:14 +01:00
|
|
|
std::map<uint256, CTxMemPoolEntry> mapTx;
|
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
|
|
|
|
2014-07-03 20:25:32 +02:00
|
|
|
CTxMemPool(const CFeeRate& _minRelayFee);
|
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;
|
2013-08-27 07:51:57 +02:00
|
|
|
void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }
|
|
|
|
|
2013-11-11 08:35:14 +01:00
|
|
|
bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry);
|
2014-02-15 22:38:28 +01:00
|
|
|
void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
|
|
|
|
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,
|
|
|
|
std::list<CTransaction>& conflicts);
|
2013-08-27 07:51:57 +02:00
|
|
|
void clear();
|
|
|
|
void queryHashes(std::vector<uint256>& vtxid);
|
|
|
|
void pruneSpent(const uint256& hash, CCoins &coins);
|
|
|
|
unsigned int GetTransactionsUpdated() const;
|
|
|
|
void AddTransactionsUpdated(unsigned int n);
|
|
|
|
|
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);
|
|
|
|
void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta);
|
2012-07-11 20:52:41 +02:00
|
|
|
void ClearPrioritisation(const uint256 hash);
|
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
unsigned long size()
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
return mapTx.size();
|
|
|
|
}
|
2014-08-07 05:58:19 +02:00
|
|
|
uint64_t GetTotalTxSize()
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
return totalTxSize;
|
|
|
|
}
|
2013-08-27 07:51:57 +02:00
|
|
|
|
|
|
|
bool exists(uint256 hash)
|
|
|
|
{
|
|
|
|
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);
|
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
|