You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

txmempool.h 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #ifndef BITCOIN_TXMEMPOOL_H
  6. #define BITCOIN_TXMEMPOOL_H
  7. #include <list>
  8. #include "amount.h"
  9. #include "coins.h"
  10. #include "primitives/transaction.h"
  11. #include "sync.h"
  12. class CAutoFile;
  13. inline double AllowFreeThreshold()
  14. {
  15. return COIN * 144 / 250;
  16. }
  17. inline bool AllowFree(double dPriority)
  18. {
  19. // Large (in bytes) low-priority (new, small-coin) transactions
  20. // need a fee.
  21. return dPriority > AllowFreeThreshold();
  22. }
  23. /** Fake height value used in CCoins to signify they are only in the memory pool (since 0.8) */
  24. static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
  25. /**
  26. * CTxMemPool stores these:
  27. */
  28. class CTxMemPoolEntry
  29. {
  30. private:
  31. CTransaction tx;
  32. CAmount nFee; //! Cached to avoid expensive parent-transaction lookups
  33. size_t nTxSize; //! ... and avoid recomputing tx size
  34. size_t nModSize; //! ... and modified size for priority
  35. size_t nUsageSize; //! ... and total memory usage
  36. int64_t nTime; //! Local time when entering the mempool
  37. double dPriority; //! Priority when entering the mempool
  38. unsigned int nHeight; //! Chain height when entering the mempool
  39. bool hadNoDependencies; //! Not dependent on any other txs when it entered the mempool
  40. public:
  41. CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
  42. int64_t _nTime, double _dPriority, unsigned int _nHeight, bool poolHasNoInputsOf = false);
  43. CTxMemPoolEntry();
  44. CTxMemPoolEntry(const CTxMemPoolEntry& other);
  45. const CTransaction& GetTx() const { return this->tx; }
  46. double GetPriority(unsigned int currentHeight) const;
  47. CAmount GetFee() const { return nFee; }
  48. size_t GetTxSize() const { return nTxSize; }
  49. int64_t GetTime() const { return nTime; }
  50. unsigned int GetHeight() const { return nHeight; }
  51. bool WasClearAtEntry() const { return hadNoDependencies; }
  52. size_t DynamicMemoryUsage() const { return nUsageSize; }
  53. };
  54. class CBlockPolicyEstimator;
  55. /** An inpoint - a combination of a transaction and an index n into its vin */
  56. class CInPoint
  57. {
  58. public:
  59. const CTransaction* ptx;
  60. uint32_t n;
  61. CInPoint() { SetNull(); }
  62. CInPoint(const CTransaction* ptxIn, uint32_t nIn) { ptx = ptxIn; n = nIn; }
  63. void SetNull() { ptx = NULL; n = (uint32_t) -1; }
  64. bool IsNull() const { return (ptx == NULL && n == (uint32_t) -1); }
  65. size_t DynamicMemoryUsage() const { return 0; }
  66. };
  67. /**
  68. * CTxMemPool stores valid-according-to-the-current-best-chain
  69. * transactions that may be included in the next block.
  70. *
  71. * Transactions are added when they are seen on the network
  72. * (or created by the local node), but not all transactions seen
  73. * are added to the pool: if a new transaction double-spends
  74. * an input of a transaction in the pool, it is dropped,
  75. * as are non-standard transactions.
  76. */
  77. class CTxMemPool
  78. {
  79. private:
  80. bool fSanityCheck; //! Normally false, true if -checkmempool or -regtest
  81. unsigned int nTransactionsUpdated;
  82. CBlockPolicyEstimator* minerPolicyEstimator;
  83. uint64_t totalTxSize; //! sum of all mempool tx' byte sizes
  84. uint64_t cachedInnerUsage; //! sum of dynamic memory usage of all the map elements (NOT the maps themselves)
  85. public:
  86. mutable CCriticalSection cs;
  87. std::map<uint256, CTxMemPoolEntry> mapTx;
  88. std::map<COutPoint, CInPoint> mapNextTx;
  89. std::map<uint256, std::pair<double, CAmount> > mapDeltas;
  90. CTxMemPool(const CFeeRate& _minRelayFee);
  91. ~CTxMemPool();
  92. /**
  93. * If sanity-checking is turned on, check makes sure the pool is
  94. * consistent (does not contain two transactions that spend the same inputs,
  95. * all inputs are in the mapNextTx array). If sanity-checking is turned off,
  96. * check does nothing.
  97. */
  98. void check(const CCoinsViewCache *pcoins) const;
  99. void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }
  100. bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate = true);
  101. void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
  102. void removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight);
  103. void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
  104. void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
  105. std::list<CTransaction>& conflicts, bool fCurrentEstimate = true);
  106. void clear();
  107. void queryHashes(std::vector<uint256>& vtxid);
  108. void pruneSpent(const uint256& hash, CCoins &coins);
  109. unsigned int GetTransactionsUpdated() const;
  110. void AddTransactionsUpdated(unsigned int n);
  111. /**
  112. * Check that none of this transactions inputs are in the mempool, and thus
  113. * the tx is not dependent on other mempool transactions to be included in a block.
  114. */
  115. bool HasNoInputsOf(const CTransaction& tx) const;
  116. /** Affect CreateNewBlock prioritisation of transactions */
  117. void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount& nFeeDelta);
  118. void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta);
  119. void ClearPrioritisation(const uint256 hash);
  120. unsigned long size()
  121. {
  122. LOCK(cs);
  123. return mapTx.size();
  124. }
  125. uint64_t GetTotalTxSize()
  126. {
  127. LOCK(cs);
  128. return totalTxSize;
  129. }
  130. bool exists(uint256 hash) const
  131. {
  132. LOCK(cs);
  133. return (mapTx.count(hash) != 0);
  134. }
  135. bool lookup(uint256 hash, CTransaction& result) const;
  136. /** Estimate fee rate needed to get into the next nBlocks */
  137. CFeeRate estimateFee(int nBlocks) const;
  138. /** Estimate priority needed to get into the next nBlocks */
  139. double estimatePriority(int nBlocks) const;
  140. /** Write/Read estimates to disk */
  141. bool WriteFeeEstimates(CAutoFile& fileout) const;
  142. bool ReadFeeEstimates(CAutoFile& filein);
  143. size_t DynamicMemoryUsage() const;
  144. };
  145. /**
  146. * CCoinsView that brings transactions from a memorypool into view.
  147. * It does not check for spendings by memory pool transactions.
  148. */
  149. class CCoinsViewMemPool : public CCoinsViewBacked
  150. {
  151. protected:
  152. CTxMemPool &mempool;
  153. public:
  154. CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn);
  155. bool GetCoins(const uint256 &txid, CCoins &coins) const;
  156. bool HaveCoins(const uint256 &txid) const;
  157. };
  158. #endif // BITCOIN_TXMEMPOOL_H