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
46 bool hadNoDependencies; //! Not dependent on any other txs when it entered the mempool
49 CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
50 int64_t _nTime, double _dPriority, unsigned int _nHeight, bool poolHasNoInputsOf = false);
52 CTxMemPoolEntry(const CTxMemPoolEntry& other);
54 const CTransaction& GetTx() const { return this->tx; }
55 double GetPriority(unsigned int currentHeight) const;
56 CAmount GetFee() const { return nFee; }
57 size_t GetTxSize() const { return nTxSize; }
58 int64_t GetTime() const { return nTime; }
59 unsigned int GetHeight() const { return nHeight; }
60 bool WasClearAtEntry() const { return hadNoDependencies; }
63 class CBlockPolicyEstimator;
65 /** An inpoint - a combination of a transaction and an index n into its vin */
69 const CTransaction* ptx;
72 CInPoint() { SetNull(); }
73 CInPoint(const CTransaction* ptxIn, uint32_t nIn) { ptx = ptxIn; n = nIn; }
74 void SetNull() { ptx = NULL; n = (uint32_t) -1; }
75 bool IsNull() const { return (ptx == NULL && n == (uint32_t) -1); }
79 * CTxMemPool stores valid-according-to-the-current-best-chain
80 * transactions that may be included in the next block.
82 * Transactions are added when they are seen on the network
83 * (or created by the local node), but not all transactions seen
84 * are added to the pool: if a new transaction double-spends
85 * an input of a transaction in the pool, it is dropped,
86 * as are non-standard transactions.
91 bool fSanityCheck; //! Normally false, true if -checkmempool or -regtest
92 unsigned int nTransactionsUpdated;
93 CBlockPolicyEstimator* minerPolicyEstimator;
95 uint64_t totalTxSize; //! sum of all mempool tx' byte sizes
98 mutable CCriticalSection cs;
99 std::map<uint256, CTxMemPoolEntry> mapTx;
100 std::map<COutPoint, CInPoint> mapNextTx;
101 std::map<uint256, std::pair<double, CAmount> > mapDeltas;
103 CTxMemPool(const CFeeRate& _minRelayFee);
107 * If sanity-checking is turned on, check makes sure the pool is
108 * consistent (does not contain two transactions that spend the same inputs,
109 * all inputs are in the mapNextTx array). If sanity-checking is turned off,
110 * check does nothing.
112 void check(const CCoinsViewCache *pcoins) const;
113 void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }
115 bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate = true);
116 void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
117 void removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight);
118 void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
119 void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
120 std::list<CTransaction>& conflicts, bool fCurrentEstimate = true);
122 void queryHashes(std::vector<uint256>& vtxid);
123 void pruneSpent(const uint256& hash, CCoins &coins);
124 unsigned int GetTransactionsUpdated() const;
125 void AddTransactionsUpdated(unsigned int n);
127 * Check that none of this transactions inputs are in the mempool, and thus
128 * the tx is not dependent on other mempool transactions to be included in a block.
130 bool HasNoInputsOf(const CTransaction& tx) const;
132 /** Affect CreateNewBlock prioritisation of transactions */
133 void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount& nFeeDelta);
134 void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta);
135 void ClearPrioritisation(const uint256 hash);
142 uint64_t GetTotalTxSize()
148 bool exists(uint256 hash) const
151 return (mapTx.count(hash) != 0);
154 bool lookup(uint256 hash, CTransaction& result) const;
156 /** Estimate fee rate needed to get into the next nBlocks */
157 CFeeRate estimateFee(int nBlocks) const;
159 /** Estimate priority needed to get into the next nBlocks */
160 double estimatePriority(int nBlocks) const;
162 /** Write/Read estimates to disk */
163 bool WriteFeeEstimates(CAutoFile& fileout) const;
164 bool ReadFeeEstimates(CAutoFile& filein);
168 * CCoinsView that brings transactions from a memorypool into view.
169 * It does not check for spendings by memory pool transactions.
171 class CCoinsViewMemPool : public CCoinsViewBacked
177 CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn);
178 bool GetCoins(const uint256 &txid, CCoins &coins) const;
179 bool HaveCoins(const uint256 &txid) const;
182 #endif // BITCOIN_TXMEMPOOL_H