1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin 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 "core/transaction.h"
18 inline bool AllowFree(double dPriority)
20 // Large (in bytes) low-priority (new, small-coin) transactions
22 return dPriority > COIN * 144 / 250;
25 /** Fake height value used in CCoins to signify they are only in the memory pool (since 0.8) */
26 static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
29 * CTxMemPool stores these:
35 CAmount nFee; //! Cached to avoid expensive parent-transaction lookups
36 size_t nTxSize; //! ... and avoid recomputing tx size
37 size_t nModSize; //! ... and modified size for priority
38 int64_t nTime; //! Local time when entering the mempool
39 double dPriority; //! Priority when entering the mempool
40 unsigned int nHeight; //! Chain height when entering the mempool
43 CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
44 int64_t _nTime, double _dPriority, unsigned int _nHeight);
46 CTxMemPoolEntry(const CTxMemPoolEntry& other);
48 const CTransaction& GetTx() const { return this->tx; }
49 double GetPriority(unsigned int currentHeight) const;
50 CAmount GetFee() const { return nFee; }
51 size_t GetTxSize() const { return nTxSize; }
52 int64_t GetTime() const { return nTime; }
53 unsigned int GetHeight() const { return nHeight; }
56 class CMinerPolicyEstimator;
58 /** An inpoint - a combination of a transaction and an index n into its vin */
62 const CTransaction* ptx;
65 CInPoint() { SetNull(); }
66 CInPoint(const CTransaction* ptxIn, uint32_t nIn) { ptx = ptxIn; n = nIn; }
67 void SetNull() { ptx = NULL; n = (uint32_t) -1; }
68 bool IsNull() const { return (ptx == NULL && n == (uint32_t) -1); }
72 * CTxMemPool stores valid-according-to-the-current-best-chain
73 * transactions that may be included in the next block.
75 * Transactions are added when they are seen on the network
76 * (or created by the local node), but not all transactions seen
77 * are added to the pool: if a new transaction double-spends
78 * an input of a transaction in the pool, it is dropped,
79 * as are non-standard transactions.
84 bool fSanityCheck; //! Normally false, true if -checkmempool or -regtest
85 unsigned int nTransactionsUpdated;
86 CMinerPolicyEstimator* minerPolicyEstimator;
88 CFeeRate minRelayFee; //! Passed to constructor to avoid dependency on main
89 uint64_t totalTxSize; //! sum of all mempool tx' byte sizes
92 mutable CCriticalSection cs;
93 std::map<uint256, CTxMemPoolEntry> mapTx;
94 std::map<COutPoint, CInPoint> mapNextTx;
95 std::map<uint256, std::pair<double, CAmount> > mapDeltas;
97 CTxMemPool(const CFeeRate& _minRelayFee);
101 * If sanity-checking is turned on, check makes sure the pool is
102 * consistent (does not contain two transactions that spend the same inputs,
103 * all inputs are in the mapNextTx array). If sanity-checking is turned off,
104 * check does nothing.
106 void check(const CCoinsViewCache *pcoins) const;
107 void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }
109 bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry);
110 void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
111 void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
112 void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
113 std::list<CTransaction>& conflicts);
115 void queryHashes(std::vector<uint256>& vtxid);
116 void pruneSpent(const uint256& hash, CCoins &coins);
117 unsigned int GetTransactionsUpdated() const;
118 void AddTransactionsUpdated(unsigned int n);
120 /** Affect CreateNewBlock prioritisation of transactions */
121 void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount& nFeeDelta);
122 void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta);
123 void ClearPrioritisation(const uint256 hash);
130 uint64_t GetTotalTxSize()
136 bool exists(uint256 hash)
139 return (mapTx.count(hash) != 0);
142 bool lookup(uint256 hash, CTransaction& result) const;
144 /** Estimate fee rate needed to get into the next nBlocks */
145 CFeeRate estimateFee(int nBlocks) const;
147 /** Estimate priority needed to get into the next nBlocks */
148 double estimatePriority(int nBlocks) const;
150 /** Write/Read estimates to disk */
151 bool WriteFeeEstimates(CAutoFile& fileout) const;
152 bool ReadFeeEstimates(CAutoFile& filein);
156 * CCoinsView that brings transactions from a memorypool into view.
157 * It does not check for spendings by memory pool transactions.
159 class CCoinsViewMemPool : public CCoinsViewBacked
165 CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn);
166 bool GetCoins(const uint256 &txid, CCoins &coins) const;
167 bool HaveCoins(const uint256 &txid) const;
170 #endif // BITCOIN_TXMEMPOOL_H