1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_TXMEMPOOL_H
7 #define BITCOIN_TXMEMPOOL_H
13 #include "primitives/transaction.h"
18 inline double AllowFreeThreshold()
20 return COIN * 144 / 250;
23 inline bool AllowFree(double dPriority)
25 // Large (in bytes) low-priority (new, small-coin) transactions
27 return dPriority > AllowFreeThreshold();
30 /** Fake height value used in CCoins to signify they are only in the memory pool (since 0.8) */
31 static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
34 * CTxMemPool stores these:
40 CAmount nFee; //! Cached to avoid expensive parent-transaction lookups
41 size_t nTxSize; //! ... and avoid recomputing tx size
42 size_t nModSize; //! ... and modified size for priority
43 int64_t nTime; //! Local time when entering the mempool
44 double dPriority; //! Priority when entering the mempool
45 unsigned int nHeight; //! Chain height when entering the mempool
48 CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
49 int64_t _nTime, double _dPriority, unsigned int _nHeight);
51 CTxMemPoolEntry(const CTxMemPoolEntry& other);
53 const CTransaction& GetTx() const { return this->tx; }
54 double GetPriority(unsigned int currentHeight) const;
55 CAmount GetFee() const { return nFee; }
56 size_t GetTxSize() const { return nTxSize; }
57 int64_t GetTime() const { return nTime; }
58 unsigned int GetHeight() const { return nHeight; }
61 class CMinerPolicyEstimator;
63 /** An inpoint - a combination of a transaction and an index n into its vin */
67 const CTransaction* ptx;
70 CInPoint() { SetNull(); }
71 CInPoint(const CTransaction* ptxIn, uint32_t nIn) { ptx = ptxIn; n = nIn; }
72 void SetNull() { ptx = NULL; n = (uint32_t) -1; }
73 bool IsNull() const { return (ptx == NULL && n == (uint32_t) -1); }
77 * CTxMemPool stores valid-according-to-the-current-best-chain
78 * transactions that may be included in the next block.
80 * Transactions are added when they are seen on the network
81 * (or created by the local node), but not all transactions seen
82 * are added to the pool: if a new transaction double-spends
83 * an input of a transaction in the pool, it is dropped,
84 * as are non-standard transactions.
89 bool fSanityCheck; //! Normally false, true if -checkmempool or -regtest
90 unsigned int nTransactionsUpdated;
91 CMinerPolicyEstimator* minerPolicyEstimator;
93 CFeeRate minRelayFee; //! Passed to constructor to avoid dependency on main
94 uint64_t totalTxSize; //! sum of all mempool tx' byte sizes
97 mutable CCriticalSection cs;
98 std::map<uint256, CTxMemPoolEntry> mapTx;
99 std::map<COutPoint, CInPoint> mapNextTx;
100 std::map<uint256, std::pair<double, CAmount> > mapDeltas;
102 CTxMemPool(const CFeeRate& _minRelayFee);
106 * If sanity-checking is turned on, check makes sure the pool is
107 * consistent (does not contain two transactions that spend the same inputs,
108 * all inputs are in the mapNextTx array). If sanity-checking is turned off,
109 * check does nothing.
111 void check(const CCoinsViewCache *pcoins) const;
112 void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }
114 bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry);
115 void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
116 void removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight);
117 void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
118 void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
119 std::list<CTransaction>& conflicts);
121 void queryHashes(std::vector<uint256>& vtxid);
122 void pruneSpent(const uint256& hash, CCoins &coins);
123 unsigned int GetTransactionsUpdated() const;
124 void AddTransactionsUpdated(unsigned int n);
126 /** Affect CreateNewBlock prioritisation of transactions */
127 void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount& nFeeDelta);
128 void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta);
129 void ClearPrioritisation(const uint256 hash);
136 uint64_t GetTotalTxSize()
142 bool exists(uint256 hash)
145 return (mapTx.count(hash) != 0);
148 bool lookup(uint256 hash, CTransaction& result) const;
150 /** Estimate fee rate needed to get into the next nBlocks */
151 CFeeRate estimateFee(int nBlocks) const;
153 /** Estimate priority needed to get into the next nBlocks */
154 double estimatePriority(int nBlocks) const;
156 /** Write/Read estimates to disk */
157 bool WriteFeeEstimates(CAutoFile& fileout) const;
158 bool ReadFeeEstimates(CAutoFile& filein);
162 * CCoinsView that brings transactions from a memorypool into view.
163 * It does not check for spendings by memory pool transactions.
165 class CCoinsViewMemPool : public CCoinsViewBacked
171 CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn);
172 bool GetCoins(const uint256 &txid, CCoins &coins) const;
173 bool HaveCoins(const uint256 &txid) const;
176 #endif // BITCOIN_TXMEMPOOL_H