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 size_t nUsageSize; //! ... and total memory usage
44 int64_t nTime; //! Local time when entering the mempool
45 double dPriority; //! Priority when entering the mempool
46 unsigned int nHeight; //! Chain height when entering the mempool
47 bool hadNoDependencies; //! Not dependent on any other txs when it entered the mempool
50 CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
51 int64_t _nTime, double _dPriority, unsigned int _nHeight, bool poolHasNoInputsOf = false);
53 CTxMemPoolEntry(const CTxMemPoolEntry& other);
55 const CTransaction& GetTx() const { return this->tx; }
56 double GetPriority(unsigned int currentHeight) const;
57 CAmount GetFee() const { return nFee; }
58 size_t GetTxSize() const { return nTxSize; }
59 int64_t GetTime() const { return nTime; }
60 unsigned int GetHeight() const { return nHeight; }
61 bool WasClearAtEntry() const { return hadNoDependencies; }
62 size_t DynamicMemoryUsage() const { return nUsageSize; }
65 class CBlockPolicyEstimator;
67 /** An inpoint - a combination of a transaction and an index n into its vin */
71 const CTransaction* ptx;
74 CInPoint() { SetNull(); }
75 CInPoint(const CTransaction* ptxIn, uint32_t nIn) { ptx = ptxIn; n = nIn; }
76 void SetNull() { ptx = NULL; n = (uint32_t) -1; }
77 bool IsNull() const { return (ptx == NULL && n == (uint32_t) -1); }
78 size_t DynamicMemoryUsage() const { return 0; }
82 * CTxMemPool stores valid-according-to-the-current-best-chain
83 * transactions that may be included in the next block.
85 * Transactions are added when they are seen on the network
86 * (or created by the local node), but not all transactions seen
87 * are added to the pool: if a new transaction double-spends
88 * an input of a transaction in the pool, it is dropped,
89 * as are non-standard transactions.
94 bool fSanityCheck; //! Normally false, true if -checkmempool or -regtest
95 unsigned int nTransactionsUpdated;
96 CBlockPolicyEstimator* minerPolicyEstimator;
98 uint64_t totalTxSize = 0; //! sum of all mempool tx' byte sizes
99 uint64_t cachedInnerUsage; //! sum of dynamic memory usage of all the map elements (NOT the maps themselves)
102 mutable CCriticalSection cs;
103 std::map<uint256, CTxMemPoolEntry> mapTx;
104 std::map<COutPoint, CInPoint> mapNextTx;
105 std::map<uint256, const CTransaction*> mapNullifiers;
106 std::map<uint256, std::pair<double, CAmount> > mapDeltas;
108 CTxMemPool(const CFeeRate& _minRelayFee);
112 * If sanity-checking is turned on, check makes sure the pool is
113 * consistent (does not contain two transactions that spend the same inputs,
114 * all inputs are in the mapNextTx array). If sanity-checking is turned off,
115 * check does nothing.
117 void check(const CCoinsViewCache *pcoins) const;
118 void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }
120 bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate = true);
121 void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
122 void removeWithAnchor(const uint256 &invalidRoot);
123 void removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight);
124 void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
125 void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
126 std::list<CTransaction>& conflicts, bool fCurrentEstimate = true);
128 void queryHashes(std::vector<uint256>& vtxid);
129 void pruneSpent(const uint256& hash, CCoins &coins);
130 unsigned int GetTransactionsUpdated() const;
131 void AddTransactionsUpdated(unsigned int n);
133 * Check that none of this transactions inputs are in the mempool, and thus
134 * the tx is not dependent on other mempool transactions to be included in a block.
136 bool HasNoInputsOf(const CTransaction& tx) const;
138 /** Affect CreateNewBlock prioritisation of transactions */
139 void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount& nFeeDelta);
140 void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta);
141 void ClearPrioritisation(const uint256 hash);
149 uint64_t GetTotalTxSize()
155 bool exists(uint256 hash) const
158 return (mapTx.count(hash) != 0);
161 bool lookup(uint256 hash, CTransaction& result) const;
163 /** Estimate fee rate needed to get into the next nBlocks */
164 CFeeRate estimateFee(int nBlocks) const;
166 /** Estimate priority needed to get into the next nBlocks */
167 double estimatePriority(int nBlocks) const;
169 /** Write/Read estimates to disk */
170 bool WriteFeeEstimates(CAutoFile& fileout) const;
171 bool ReadFeeEstimates(CAutoFile& filein);
173 size_t DynamicMemoryUsage() const;
177 * CCoinsView that brings transactions from a memorypool into view.
178 * It does not check for spendings by memory pool transactions.
180 class CCoinsViewMemPool : public CCoinsViewBacked
186 CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn);
187 bool GetNullifier(const uint256 &txid) const;
188 bool GetCoins(const uint256 &txid, CCoins &coins) const;
189 bool HaveCoins(const uint256 &txid) const;
192 #endif // BITCOIN_TXMEMPOOL_H