]>
Commit | Line | Data |
---|---|---|
319b1160 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
7329fdd1 | 3 | // Distributed under the MIT software license, see the accompanying |
319b1160 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
093303a8 | 5 | |
319b1160 GA |
6 | #ifndef BITCOIN_TXMEMPOOL_H |
7 | #define BITCOIN_TXMEMPOOL_H | |
8 | ||
93a18a36 GA |
9 | #include <list> |
10 | ||
eda37330 | 11 | #include "amount.h" |
a0fa20a1 | 12 | #include "coins.h" |
d2270111 | 13 | #include "primitives/transaction.h" |
51ed9ec9 | 14 | #include "sync.h" |
319b1160 | 15 | |
fa736190 CF |
16 | class CAutoFile; |
17 | ||
c1c9d5b4 CL |
18 | inline double AllowFreeThreshold() |
19 | { | |
20 | return COIN * 144 / 250; | |
21 | } | |
22 | ||
171ca774 GA |
23 | inline bool AllowFree(double dPriority) |
24 | { | |
25 | // Large (in bytes) low-priority (new, small-coin) transactions | |
26 | // need a fee. | |
c1c9d5b4 | 27 | return dPriority > AllowFreeThreshold(); |
171ca774 GA |
28 | } |
29 | ||
a0fa20a1 PW |
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; | |
32 | ||
7329fdd1 | 33 | /** |
4d707d51 GA |
34 | * CTxMemPool stores these: |
35 | */ | |
36 | class CTxMemPoolEntry | |
37 | { | |
38 | private: | |
39 | CTransaction tx; | |
7329fdd1 MF |
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 | |
b649e039 | 46 | bool hadNoDependencies; //! Not dependent on any other txs when it entered the mempool |
4d707d51 GA |
47 | |
48 | public: | |
a372168e | 49 | CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee, |
b649e039 | 50 | int64_t _nTime, double _dPriority, unsigned int _nHeight, bool poolHasNoInputsOf = false); |
4d707d51 GA |
51 | CTxMemPoolEntry(); |
52 | CTxMemPoolEntry(const CTxMemPoolEntry& other); | |
53 | ||
54 | const CTransaction& GetTx() const { return this->tx; } | |
55 | double GetPriority(unsigned int currentHeight) const; | |
a372168e | 56 | CAmount GetFee() const { return nFee; } |
4d707d51 GA |
57 | size_t GetTxSize() const { return nTxSize; } |
58 | int64_t GetTime() const { return nTime; } | |
59 | unsigned int GetHeight() const { return nHeight; } | |
b649e039 | 60 | bool WasClearAtEntry() const { return hadNoDependencies; } |
4d707d51 GA |
61 | }; |
62 | ||
b649e039 | 63 | class CBlockPolicyEstimator; |
171ca774 | 64 | |
e8ea0fd1 | 65 | /** An inpoint - a combination of a transaction and an index n into its vin */ |
66 | class CInPoint | |
67 | { | |
68 | public: | |
69 | const CTransaction* ptx; | |
70 | uint32_t n; | |
71 | ||
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); } | |
76 | }; | |
77 | ||
7329fdd1 | 78 | /** |
319b1160 GA |
79 | * CTxMemPool stores valid-according-to-the-current-best-chain |
80 | * transactions that may be included in the next block. | |
81 | * | |
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. | |
87 | */ | |
88 | class CTxMemPool | |
89 | { | |
90 | private: | |
7329fdd1 | 91 | bool fSanityCheck; //! Normally false, true if -checkmempool or -regtest |
319b1160 | 92 | unsigned int nTransactionsUpdated; |
b649e039 | 93 | CBlockPolicyEstimator* minerPolicyEstimator; |
319b1160 | 94 | |
43873535 | 95 | uint64_t totalTxSize = 0; //! sum of all mempool tx' byte sizes |
13fc83c7 | 96 | |
319b1160 GA |
97 | public: |
98 | mutable CCriticalSection cs; | |
4d707d51 | 99 | std::map<uint256, CTxMemPoolEntry> mapTx; |
319b1160 | 100 | std::map<COutPoint, CInPoint> mapNextTx; |
bb64be52 | 101 | std::map<uint256, const CTransaction*> mapNullifiers; |
a372168e | 102 | std::map<uint256, std::pair<double, CAmount> > mapDeltas; |
319b1160 | 103 | |
13fc83c7 | 104 | CTxMemPool(const CFeeRate& _minRelayFee); |
171ca774 | 105 | ~CTxMemPool(); |
319b1160 | 106 | |
7329fdd1 | 107 | /** |
319b1160 GA |
108 | * If sanity-checking is turned on, check makes sure the pool is |
109 | * consistent (does not contain two transactions that spend the same inputs, | |
110 | * all inputs are in the mapNextTx array). If sanity-checking is turned off, | |
111 | * check does nothing. | |
112 | */ | |
d0867acb | 113 | void check(const CCoinsViewCache *pcoins) const; |
319b1160 GA |
114 | void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; } |
115 | ||
b649e039 | 116 | bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate = true); |
93a18a36 | 117 | void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false); |
a8ac403d | 118 | void removeWithAnchor(const uint256 &invalidRoot); |
723d12c0 | 119 | void removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight); |
93a18a36 | 120 | void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed); |
171ca774 | 121 | void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight, |
b649e039 | 122 | std::list<CTransaction>& conflicts, bool fCurrentEstimate = true); |
319b1160 GA |
123 | void clear(); |
124 | void queryHashes(std::vector<uint256>& vtxid); | |
125 | void pruneSpent(const uint256& hash, CCoins &coins); | |
126 | unsigned int GetTransactionsUpdated() const; | |
127 | void AddTransactionsUpdated(unsigned int n); | |
b649e039 AM |
128 | /** |
129 | * Check that none of this transactions inputs are in the mempool, and thus | |
130 | * the tx is not dependent on other mempool transactions to be included in a block. | |
131 | */ | |
132 | bool HasNoInputsOf(const CTransaction& tx) const; | |
319b1160 | 133 | |
2a72d459 | 134 | /** Affect CreateNewBlock prioritisation of transactions */ |
a372168e MF |
135 | void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount& nFeeDelta); |
136 | void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta); | |
2a72d459 LD |
137 | void ClearPrioritisation(const uint256 hash); |
138 | ||
319b1160 GA |
139 | unsigned long size() |
140 | { | |
141 | LOCK(cs); | |
142 | return mapTx.size(); | |
143 | } | |
6f2c26a4 JG |
144 | uint64_t GetTotalTxSize() |
145 | { | |
146 | LOCK(cs); | |
147 | return totalTxSize; | |
148 | } | |
319b1160 | 149 | |
b649e039 | 150 | bool exists(uint256 hash) const |
319b1160 GA |
151 | { |
152 | LOCK(cs); | |
153 | return (mapTx.count(hash) != 0); | |
154 | } | |
155 | ||
156 | bool lookup(uint256 hash, CTransaction& result) const; | |
171ca774 | 157 | |
7329fdd1 | 158 | /** Estimate fee rate needed to get into the next nBlocks */ |
171ca774 | 159 | CFeeRate estimateFee(int nBlocks) const; |
7329fdd1 MF |
160 | |
161 | /** Estimate priority needed to get into the next nBlocks */ | |
171ca774 | 162 | double estimatePriority(int nBlocks) const; |
7329fdd1 MF |
163 | |
164 | /** Write/Read estimates to disk */ | |
171ca774 GA |
165 | bool WriteFeeEstimates(CAutoFile& fileout) const; |
166 | bool ReadFeeEstimates(CAutoFile& filein); | |
319b1160 GA |
167 | }; |
168 | ||
7329fdd1 MF |
169 | /** |
170 | * CCoinsView that brings transactions from a memorypool into view. | |
171 | * It does not check for spendings by memory pool transactions. | |
172 | */ | |
a0fa20a1 PW |
173 | class CCoinsViewMemPool : public CCoinsViewBacked |
174 | { | |
175 | protected: | |
176 | CTxMemPool &mempool; | |
177 | ||
178 | public: | |
7c70438d | 179 | CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn); |
616f8d05 | 180 | bool GetNullifier(const uint256 &txid) const; |
a3dc587a DK |
181 | bool GetCoins(const uint256 &txid, CCoins &coins) const; |
182 | bool HaveCoins(const uint256 &txid) const; | |
a0fa20a1 PW |
183 | }; |
184 | ||
093303a8 | 185 | #endif // BITCOIN_TXMEMPOOL_H |