Line data Source code
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.
5 :
6 : #ifndef BITCOIN_TXMEMPOOL_H
7 : #define BITCOIN_TXMEMPOOL_H
8 :
9 : #include <list>
10 : #include <set>
11 :
12 : #include "amount.h"
13 : #include "coins.h"
14 : #include "primitives/transaction.h"
15 : #include "sync.h"
16 :
17 : #undef foreach
18 : #include "boost/multi_index_container.hpp"
19 : #include "boost/multi_index/ordered_index.hpp"
20 :
21 : class CAutoFile;
22 :
23 : inline double AllowFreeThreshold()
24 : {
25 : return COIN * 144 / 250;
26 : }
27 :
28 : inline bool AllowFree(double dPriority)
29 : {
30 : // Large (in bytes) low-priority (new, small-coin) transactions
31 : // need a fee.
32 : return dPriority > AllowFreeThreshold();
33 : }
34 :
35 : /** Fake height value used in CCoins to signify they are only in the memory pool (since 0.8) */
36 : static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
37 :
38 : class CTxMemPool;
39 :
40 : /** \class CTxMemPoolEntry
41 : *
42 : * CTxMemPoolEntry stores data about the correponding transaction, as well
43 : * as data about all in-mempool transactions that depend on the transaction
44 : * ("descendant" transactions).
45 : *
46 : * When a new entry is added to the mempool, we update the descendant state
47 : * (nCountWithDescendants, nSizeWithDescendants, and nFeesWithDescendants) for
48 : * all ancestors of the newly added transaction.
49 : *
50 : * If updating the descendant state is skipped, we can mark the entry as
51 : * "dirty", and set nSizeWithDescendants/nFeesWithDescendants to equal nTxSize/
52 : * nTxFee. (This can potentially happen during a reorg, where we limit the
53 : * amount of work we're willing to do to avoid consuming too much CPU.)
54 : *
55 : */
56 :
57 204405 : class CTxMemPoolEntry
58 : {
59 : private:
60 : CTransaction tx;
61 : CAmount nFee; //! Cached to avoid expensive parent-transaction lookups
62 : size_t nTxSize; //! ... and avoid recomputing tx size
63 : size_t nModSize; //! ... and modified size for priority
64 : size_t nUsageSize; //! ... and total memory usage
65 : int64_t nTime; //! Local time when entering the mempool
66 : double dPriority; //! Priority when entering the mempool
67 : unsigned int nHeight; //! Chain height when entering the mempool
68 : bool hadNoDependencies; //! Not dependent on any other txs when it entered the mempool
69 :
70 : // Information about descendants of this transaction that are in the
71 : // mempool; if we remove this transaction we must remove all of these
72 : // descendants as well. if nCountWithDescendants is 0, treat this entry as
73 : // dirty, and nSizeWithDescendants and nFeesWithDescendants will not be
74 : // correct.
75 : uint64_t nCountWithDescendants; //! number of descendant transactions
76 : uint64_t nSizeWithDescendants; //! ... and size
77 : CAmount nFeesWithDescendants; //! ... and total fees (all including us)
78 :
79 : public:
80 : CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
81 : int64_t _nTime, double _dPriority, unsigned int _nHeight, bool poolHasNoInputsOf = false);
82 : CTxMemPoolEntry(const CTxMemPoolEntry& other);
83 :
84 37197617 : const CTransaction& GetTx() const { return this->tx; }
85 : double GetPriority(unsigned int currentHeight) const;
86 0 : CAmount GetFee() const { return nFee; }
87 0 : size_t GetTxSize() const { return nTxSize; }
88 0 : int64_t GetTime() const { return nTime; }
89 0 : unsigned int GetHeight() const { return nHeight; }
90 0 : bool WasClearAtEntry() const { return hadNoDependencies; }
91 0 : size_t DynamicMemoryUsage() const { return nUsageSize; }
92 :
93 : // Adjusts the descendant state, if this entry is not dirty.
94 : void UpdateState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount);
95 :
96 : /** We can set the entry to be dirty if doing the full calculation of in-
97 : * mempool descendants will be too expensive, which can potentially happen
98 : * when re-adding transactions from a block back to the mempool.
99 : */
100 : void SetDirty();
101 0 : bool IsDirty() const { return nCountWithDescendants == 0; }
102 :
103 0 : uint64_t GetCountWithDescendants() const { return nCountWithDescendants; }
104 0 : uint64_t GetSizeWithDescendants() const { return nSizeWithDescendants; }
105 0 : CAmount GetFeesWithDescendants() const { return nFeesWithDescendants; }
106 : };
107 :
108 : // Helpers for modifying CTxMemPool::mapTx, which is a boost multi_index.
109 : struct update_descendant_state
110 : {
111 0 : update_descendant_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount) :
112 0 : modifySize(_modifySize), modifyFee(_modifyFee), modifyCount(_modifyCount)
113 0 : {}
114 :
115 : void operator() (CTxMemPoolEntry &e)
116 508773 : { e.UpdateState(modifySize, modifyFee, modifyCount); }
117 :
118 : private:
119 : int64_t modifySize;
120 : CAmount modifyFee;
121 : int64_t modifyCount;
122 : };
123 :
124 : struct set_dirty
125 : {
126 0 : void operator() (CTxMemPoolEntry &e)
127 0 : { e.SetDirty(); }
128 : };
129 :
130 : // extracts a TxMemPoolEntry's transaction hash
131 : struct mempoolentry_txid
132 : {
133 : typedef uint256 result_type;
134 0 : result_type operator() (const CTxMemPoolEntry &entry) const
135 : {
136 3049553 : return entry.GetTx().GetHash();
137 : }
138 : };
139 :
140 : /** \class CompareTxMemPoolEntryByFee
141 : *
142 : * Sort an entry by max(feerate of entry's tx, feerate with all descendants).
143 : */
144 : class CompareTxMemPoolEntryByFee
145 : {
146 : public:
147 1143243 : bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b)
148 : {
149 2286486 : bool fUseADescendants = UseDescendantFeeRate(a);
150 2286486 : bool fUseBDescendants = UseDescendantFeeRate(b);
151 :
152 1143243 : double aFees = fUseADescendants ? a.GetFeesWithDescendants() : a.GetFee();
153 1143243 : double aSize = fUseADescendants ? a.GetSizeWithDescendants() : a.GetTxSize();
154 :
155 1143243 : double bFees = fUseBDescendants ? b.GetFeesWithDescendants() : b.GetFee();
156 1143243 : double bSize = fUseBDescendants ? b.GetSizeWithDescendants() : b.GetTxSize();
157 :
158 : // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
159 1143243 : double f1 = aFees * bSize;
160 1143243 : double f2 = aSize * bFees;
161 :
162 1143243 : if (f1 == f2) {
163 1078006 : return a.GetTime() < b.GetTime();
164 : }
165 65237 : return f1 > f2;
166 : }
167 :
168 : // Calculate which feerate to use for an entry (avoiding division).
169 0 : bool UseDescendantFeeRate(const CTxMemPoolEntry &a)
170 : {
171 2286486 : double f1 = (double)a.GetFee() * a.GetSizeWithDescendants();
172 2286486 : double f2 = (double)a.GetFeesWithDescendants() * a.GetTxSize();
173 2286486 : return f2 > f1;
174 : }
175 : };
176 :
177 : class CompareTxMemPoolEntryByEntryTime
178 : {
179 : public:
180 : bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b)
181 : {
182 : return a.GetTime() < b.GetTime();
183 : }
184 : };
185 :
186 : class CBlockPolicyEstimator;
187 :
188 : /** An inpoint - a combination of a transaction and an index n into its vin */
189 : class CInPoint
190 : {
191 : public:
192 : const CTransaction* ptx;
193 : uint32_t n;
194 :
195 0 : CInPoint() { SetNull(); }
196 0 : CInPoint(const CTransaction* ptxIn, uint32_t nIn) { ptx = ptxIn; n = nIn; }
197 0 : void SetNull() { ptx = NULL; n = (uint32_t) -1; }
198 : bool IsNull() const { return (ptx == NULL && n == (uint32_t) -1); }
199 : size_t DynamicMemoryUsage() const { return 0; }
200 : };
201 :
202 : /**
203 : * CTxMemPool stores valid-according-to-the-current-best-chain
204 : * transactions that may be included in the next block.
205 : *
206 : * Transactions are added when they are seen on the network
207 : * (or created by the local node), but not all transactions seen
208 : * are added to the pool: if a new transaction double-spends
209 : * an input of a transaction in the pool, it is dropped,
210 : * as are non-standard transactions.
211 : *
212 : * CTxMemPool::mapTx, and CTxMemPoolEntry bookkeeping:
213 : *
214 : * mapTx is a boost::multi_index that sorts the mempool on 2 criteria:
215 : * - transaction hash
216 : * - feerate [we use max(feerate of tx, feerate of tx with all descendants)]
217 : *
218 : * Note: the term "descendant" refers to in-mempool transactions that depend on
219 : * this one, while "ancestor" refers to in-mempool transactions that a given
220 : * transaction depends on.
221 : *
222 : * In order for the feerate sort to remain correct, we must update transactions
223 : * in the mempool when new descendants arrive. To facilitate this, we track
224 : * the set of in-mempool direct parents and direct children in mapLinks. Within
225 : * each CTxMemPoolEntry, we track the size and fees of all descendants.
226 : *
227 : * Usually when a new transaction is added to the mempool, it has no in-mempool
228 : * children (because any such children would be an orphan). So in
229 : * addUnchecked(), we:
230 : * - update a new entry's setMemPoolParents to include all in-mempool parents
231 : * - update the new entry's direct parents to include the new tx as a child
232 : * - update all ancestors of the transaction to include the new tx's size/fee
233 : *
234 : * When a transaction is removed from the mempool, we must:
235 : * - update all in-mempool parents to not track the tx in setMemPoolChildren
236 : * - update all ancestors to not include the tx's size/fees in descendant state
237 : * - update all in-mempool children to not include it as a parent
238 : *
239 : * These happen in UpdateForRemoveFromMempool(). (Note that when removing a
240 : * transaction along with its descendants, we must calculate that set of
241 : * transactions to be removed before doing the removal, or else the mempool can
242 : * be in an inconsistent state where it's impossible to walk the ancestors of
243 : * a transaction.)
244 : *
245 : * In the event of a reorg, the assumption that a newly added tx has no
246 : * in-mempool children is false. In particular, the mempool is in an
247 : * inconsistent state while new transactions are being added, because there may
248 : * be descendant transactions of a tx coming from a disconnected block that are
249 : * unreachable from just looking at transactions in the mempool (the linking
250 : * transactions may also be in the disconnected block, waiting to be added).
251 : * Because of this, there's not much benefit in trying to search for in-mempool
252 : * children in addUnchecked(). Instead, in the special case of transactions
253 : * being added from a disconnected block, we require the caller to clean up the
254 : * state, to account for in-mempool, out-of-block descendants for all the
255 : * in-block transactions by calling UpdateTransactionsFromBlock(). Note that
256 : * until this is called, the mempool state is not consistent, and in particular
257 : * mapLinks may not be correct (and therefore functions like
258 : * CalculateMemPoolAncestors() and CalculateDescendants() that rely
259 : * on them to walk the mempool are not generally safe to use).
260 : *
261 : * Computational limits:
262 : *
263 : * Updating all in-mempool ancestors of a newly added transaction can be slow,
264 : * if no bound exists on how many in-mempool ancestors there may be.
265 : * CalculateMemPoolAncestors() takes configurable limits that are designed to
266 : * prevent these calculations from being too CPU intensive.
267 : *
268 : * Adding transactions from a disconnected block can be very time consuming,
269 : * because we don't have a way to limit the number of in-mempool descendants.
270 : * To bound CPU processing, we limit the amount of work we're willing to do
271 : * to properly update the descendant information for a tx being added from
272 : * a disconnected block. If we would exceed the limit, then we instead mark
273 : * the entry as "dirty", and set the feerate for sorting purposes to be equal
274 : * the feerate of the transaction without any descendants.
275 : *
276 : */
277 : class CTxMemPool
278 : {
279 : private:
280 : bool fSanityCheck; //! Normally false, true if -checkmempool or -regtest
281 : unsigned int nTransactionsUpdated;
282 : CBlockPolicyEstimator* minerPolicyEstimator;
283 :
284 : uint64_t totalTxSize; //! sum of all mempool tx' byte sizes
285 : uint64_t cachedInnerUsage; //! sum of dynamic memory usage of all the map elements (NOT the maps themselves)
286 :
287 : public:
288 : typedef boost::multi_index_container<
289 : CTxMemPoolEntry,
290 : boost::multi_index::indexed_by<
291 : // sorted by txid
292 : boost::multi_index::ordered_unique<mempoolentry_txid>,
293 : // sorted by fee rate
294 : boost::multi_index::ordered_non_unique<
295 : boost::multi_index::identity<CTxMemPoolEntry>,
296 : CompareTxMemPoolEntryByFee
297 : >
298 : >
299 : > indexed_transaction_set;
300 :
301 : mutable CCriticalSection cs;
302 : indexed_transaction_set mapTx;
303 : typedef indexed_transaction_set::nth_index<0>::type::iterator txiter;
304 : struct CompareIteratorByHash {
305 0 : bool operator()(const txiter &a, const txiter &b) const {
306 68103552 : return a->GetTx().GetHash() < b->GetTx().GetHash();
307 : }
308 : };
309 : typedef std::set<txiter, CompareIteratorByHash> setEntries;
310 :
311 : private:
312 : typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
313 :
314 401051 : struct TxLinks {
315 : setEntries parents;
316 : setEntries children;
317 : };
318 :
319 : typedef std::map<txiter, TxLinks, CompareIteratorByHash> txlinksMap;
320 : txlinksMap mapLinks;
321 :
322 : const setEntries & GetMemPoolParents(txiter entry) const;
323 : const setEntries & GetMemPoolChildren(txiter entry) const;
324 : void UpdateParent(txiter entry, txiter parent, bool add);
325 : void UpdateChild(txiter entry, txiter child, bool add);
326 :
327 : public:
328 : std::map<COutPoint, CInPoint> mapNextTx;
329 : std::map<uint256, std::pair<double, CAmount> > mapDeltas;
330 :
331 : CTxMemPool(const CFeeRate& _minRelayFee);
332 : ~CTxMemPool();
333 :
334 : /**
335 : * If sanity-checking is turned on, check makes sure the pool is
336 : * consistent (does not contain two transactions that spend the same inputs,
337 : * all inputs are in the mapNextTx array). If sanity-checking is turned off,
338 : * check does nothing.
339 : */
340 : void check(const CCoinsViewCache *pcoins) const;
341 94 : void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }
342 :
343 : // addUnchecked must updated state for all ancestors of a given transaction,
344 : // to track size/count of descendant transactions. First version of
345 : // addUnchecked can be used to have it call CalculateMemPoolAncestors(), and
346 : // then invoke the second version.
347 : bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate = true);
348 : bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, setEntries &setAncestors, bool fCurrentEstimate = true);
349 :
350 : void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
351 : void removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight);
352 : void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
353 : void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
354 : std::list<CTransaction>& conflicts, bool fCurrentEstimate = true);
355 : void clear();
356 : void queryHashes(std::vector<uint256>& vtxid);
357 : void pruneSpent(const uint256& hash, CCoins &coins);
358 : unsigned int GetTransactionsUpdated() const;
359 : void AddTransactionsUpdated(unsigned int n);
360 : /**
361 : * Check that none of this transactions inputs are in the mempool, and thus
362 : * the tx is not dependent on other mempool transactions to be included in a block.
363 : */
364 : bool HasNoInputsOf(const CTransaction& tx) const;
365 :
366 : /** Affect CreateNewBlock prioritisation of transactions */
367 : void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount& nFeeDelta);
368 : void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta);
369 : void ClearPrioritisation(const uint256 hash);
370 :
371 : public:
372 : /** Remove a set of transactions from the mempool.
373 : * If a transaction is in this set, then all in-mempool descendants must
374 : * also be in the set.*/
375 : void RemoveStaged(setEntries &stage);
376 :
377 : /** When adding transactions from a disconnected block back to the mempool,
378 : * new mempool entries may have children in the mempool (which is generally
379 : * not the case when otherwise adding transactions).
380 : * UpdateTransactionsFromBlock() will find child transactions and update the
381 : * descendant state for each transaction in hashesToUpdate (excluding any
382 : * child transactions present in hashesToUpdate, which are already accounted
383 : * for). Note: hashesToUpdate should be the set of transactions from the
384 : * disconnected block that have been accepted back into the mempool.
385 : */
386 : void UpdateTransactionsFromBlock(const std::vector<uint256> &hashesToUpdate);
387 :
388 : /** Try to calculate all in-mempool ancestors of entry.
389 : * (these are all calculated including the tx itself)
390 : * limitAncestorCount = max number of ancestors
391 : * limitAncestorSize = max size of ancestors
392 : * limitDescendantCount = max number of descendants any ancestor can have
393 : * limitDescendantSize = max size of descendants any ancestor can have
394 : * errString = populated with error reason if any limits are hit
395 : * fSearchForParents = whether to search a tx's vin for in-mempool parents, or
396 : * look up parents from mapLinks. Must be true for entries not in the mempool
397 : */
398 : bool CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, setEntries &setAncestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString, bool fSearchForParents = true);
399 :
400 350 : unsigned long size()
401 : {
402 350 : LOCK(cs);
403 700 : return mapTx.size();
404 : }
405 :
406 1 : uint64_t GetTotalTxSize()
407 : {
408 1 : LOCK(cs);
409 2 : return totalTxSize;
410 : }
411 :
412 23386 : bool exists(uint256 hash) const
413 : {
414 23386 : LOCK(cs);
415 70158 : return (mapTx.count(hash) != 0);
416 : }
417 :
418 : bool lookup(uint256 hash, CTransaction& result) const;
419 :
420 : /** Estimate fee rate needed to get into the next nBlocks */
421 : CFeeRate estimateFee(int nBlocks) const;
422 :
423 : /** Estimate priority needed to get into the next nBlocks */
424 : double estimatePriority(int nBlocks) const;
425 :
426 : /** Write/Read estimates to disk */
427 : bool WriteFeeEstimates(CAutoFile& fileout) const;
428 : bool ReadFeeEstimates(CAutoFile& filein);
429 :
430 : size_t DynamicMemoryUsage() const;
431 :
432 : private:
433 : /** UpdateForDescendants is used by UpdateTransactionsFromBlock to update
434 : * the descendants for a single transaction that has been added to the
435 : * mempool but may have child transactions in the mempool, eg during a
436 : * chain reorg. setExclude is the set of descendant transactions in the
437 : * mempool that must not be accounted for (because any descendants in
438 : * setExclude were added to the mempool after the transaction being
439 : * updated and hence their state is already reflected in the parent
440 : * state).
441 : *
442 : * If updating an entry requires looking at more than maxDescendantsToVisit
443 : * transactions, outside of the ones in setExclude, then give up.
444 : *
445 : * cachedDescendants will be updated with the descendants of the transaction
446 : * being updated, so that future invocations don't need to walk the
447 : * same transaction again, if encountered in another transaction chain.
448 : */
449 : bool UpdateForDescendants(txiter updateIt,
450 : int maxDescendantsToVisit,
451 : cacheMap &cachedDescendants,
452 : const std::set<uint256> &setExclude);
453 : /** Update ancestors of hash to add/remove it as a descendant transaction. */
454 : void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors);
455 : /** For each transaction being removed, update ancestors and any direct children. */
456 : void UpdateForRemoveFromMempool(const setEntries &entriesToRemove);
457 : /** Sever link between specified transaction and direct children. */
458 : void UpdateChildrenForRemoval(txiter entry);
459 : /** Populate setDescendants with all in-mempool descendants of hash.
460 : * Assumes that setDescendants includes all in-mempool descendants of anything
461 : * already in it. */
462 : void CalculateDescendants(txiter it, setEntries &setDescendants);
463 :
464 : /** Before calling removeUnchecked for a given transaction,
465 : * UpdateForRemoveFromMempool must be called on the entire (dependent) set
466 : * of transactions being removed at the same time. We use each
467 : * CTxMemPoolEntry's setMemPoolParents in order to walk ancestors of a
468 : * given transaction that is removed, so we can't remove intermediate
469 : * transactions in a chain before we've updated all the state for the
470 : * removal.
471 : */
472 : void removeUnchecked(txiter entry);
473 : };
474 :
475 : /**
476 : * CCoinsView that brings transactions from a memorypool into view.
477 : * It does not check for spendings by memory pool transactions.
478 : */
479 1160 : class CCoinsViewMemPool : public CCoinsViewBacked
480 : {
481 : protected:
482 : CTxMemPool &mempool;
483 :
484 : public:
485 : CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn);
486 : bool GetCoins(const uint256 &txid, CCoins &coins) const;
487 : bool HaveCoins(const uint256 &txid) const;
488 : };
489 :
490 : #endif // BITCOIN_TXMEMPOOL_H
|