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 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 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 <memory>
  8. #include <set>
  9. #include <map>
  10. #include <vector>
  11. #include <utility>
  12. #include <string>
  13. #include "amount.h"
  14. #include "coins.h"
  15. #include "indirectmap.h"
  16. #include "primitives/transaction.h"
  17. #include "sync.h"
  18. #include "random.h"
  19. #include "boost/multi_index_container.hpp"
  20. #include "boost/multi_index/ordered_index.hpp"
  21. #include "boost/multi_index/hashed_index.hpp"
  22. #include <boost/multi_index/sequenced_index.hpp>
  23. #include <boost/signals2/signal.hpp>
  24. class CAutoFile;
  25. class CBlockIndex;
  26. /** Fake height value used in CCoins to signify they are only in the memory pool (since 0.8) */
  27. static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
  28. struct LockPoints
  29. {
  30. // Will be set to the blockchain height and median time past
  31. // values that would be necessary to satisfy all relative locktime
  32. // constraints (BIP68) of this tx given our view of block chain history
  33. int height;
  34. int64_t time;
  35. // As long as the current chain descends from the highest height block
  36. // containing one of the inputs used in the calculation, then the cached
  37. // values are still valid even after a reorg.
  38. CBlockIndex* maxInputBlock;
  39. LockPoints() : height(0), time(0), maxInputBlock(NULL) { }
  40. };
  41. class CTxMemPool;
  42. /** \class CTxMemPoolEntry
  43. *
  44. * CTxMemPoolEntry stores data about the corresponding transaction, as well
  45. * as data about all in-mempool transactions that depend on the transaction
  46. * ("descendant" transactions).
  47. *
  48. * When a new entry is added to the mempool, we update the descendant state
  49. * (nCountWithDescendants, nSizeWithDescendants, and nModFeesWithDescendants) for
  50. * all ancestors of the newly added transaction.
  51. *
  52. * If updating the descendant state is skipped, we can mark the entry as
  53. * "dirty", and set nSizeWithDescendants/nModFeesWithDescendants to equal nTxSize/
  54. * nFee+feeDelta. (This can potentially happen during a reorg, where we limit the
  55. * amount of work we're willing to do to avoid consuming too much CPU.)
  56. *
  57. */
  58. class CTxMemPoolEntry
  59. {
  60. private:
  61. CTransactionRef tx;
  62. CAmount nFee; //!< Cached to avoid expensive parent-transaction lookups
  63. size_t nTxWeight; //!< ... and avoid recomputing tx weight (also used for GetTxSize())
  64. size_t nUsageSize; //!< ... and total memory usage
  65. int64_t nTime; //!< Local time when entering the mempool
  66. unsigned int entryHeight; //!< Chain height when entering the mempool
  67. bool spendsCoinbase; //!< keep track of transactions that spend a coinbase
  68. int64_t sigOpCost; //!< Total sigop cost
  69. int64_t feeDelta; //!< Used for determining the priority of the transaction for mining in a block
  70. LockPoints lockPoints; //!< Track the height and time at which tx was final
  71. // Information about descendants of this transaction that are in the
  72. // mempool; if we remove this transaction we must remove all of these
  73. // descendants as well. if nCountWithDescendants is 0, treat this entry as
  74. // dirty, and nSizeWithDescendants and nModFeesWithDescendants will not be
  75. // correct.
  76. uint64_t nCountWithDescendants; //!< number of descendant transactions
  77. uint64_t nSizeWithDescendants; //!< ... and size
  78. CAmount nModFeesWithDescendants; //!< ... and total fees (all including us)
  79. // Analogous statistics for ancestor transactions
  80. uint64_t nCountWithAncestors;
  81. uint64_t nSizeWithAncestors;
  82. CAmount nModFeesWithAncestors;
  83. int64_t nSigOpCostWithAncestors;
  84. public:
  85. CTxMemPoolEntry(const CTransactionRef& _tx, const CAmount& _nFee,
  86. int64_t _nTime, unsigned int _entryHeight,
  87. bool spendsCoinbase,
  88. int64_t nSigOpsCost, LockPoints lp);
  89. CTxMemPoolEntry(const CTxMemPoolEntry& other);
  90. const CTransaction& GetTx() const { return *this->tx; }
  91. CTransactionRef GetSharedTx() const { return this->tx; }
  92. const CAmount& GetFee() const { return nFee; }
  93. size_t GetTxSize() const;
  94. size_t GetTxWeight() const { return nTxWeight; }
  95. int64_t GetTime() const { return nTime; }
  96. unsigned int GetHeight() const { return entryHeight; }
  97. int64_t GetSigOpCost() const { return sigOpCost; }
  98. int64_t GetModifiedFee() const { return nFee + feeDelta; }
  99. size_t DynamicMemoryUsage() const { return nUsageSize; }
  100. const LockPoints& GetLockPoints() const { return lockPoints; }
  101. // Adjusts the descendant state, if this entry is not dirty.
  102. void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount);
  103. // Adjusts the ancestor state
  104. void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int modifySigOps);
  105. // Updates the fee delta used for mining priority score, and the
  106. // modified fees with descendants.
  107. void UpdateFeeDelta(int64_t feeDelta);
  108. // Update the LockPoints after a reorg
  109. void UpdateLockPoints(const LockPoints& lp);
  110. uint64_t GetCountWithDescendants() const { return nCountWithDescendants; }
  111. uint64_t GetSizeWithDescendants() const { return nSizeWithDescendants; }
  112. CAmount GetModFeesWithDescendants() const { return nModFeesWithDescendants; }
  113. bool GetSpendsCoinbase() const { return spendsCoinbase; }
  114. uint64_t GetCountWithAncestors() const { return nCountWithAncestors; }
  115. uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
  116. CAmount GetModFeesWithAncestors() const { return nModFeesWithAncestors; }
  117. int64_t GetSigOpCostWithAncestors() const { return nSigOpCostWithAncestors; }
  118. mutable size_t vTxHashesIdx; //!< Index in mempool's vTxHashes
  119. };
  120. // Helpers for modifying CTxMemPool::mapTx, which is a boost multi_index.
  121. struct update_descendant_state
  122. {
  123. update_descendant_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount) :
  124. modifySize(_modifySize), modifyFee(_modifyFee), modifyCount(_modifyCount)
  125. {}
  126. void operator() (CTxMemPoolEntry &e)
  127. { e.UpdateDescendantState(modifySize, modifyFee, modifyCount); }
  128. private:
  129. int64_t modifySize;
  130. CAmount modifyFee;
  131. int64_t modifyCount;
  132. };
  133. struct update_ancestor_state
  134. {
  135. update_ancestor_state(int64_t _modifySize, CAmount _modifyFee, int64_t _modifyCount, int64_t _modifySigOpsCost) :
  136. modifySize(_modifySize), modifyFee(_modifyFee), modifyCount(_modifyCount), modifySigOpsCost(_modifySigOpsCost)
  137. {}
  138. void operator() (CTxMemPoolEntry &e)
  139. { e.UpdateAncestorState(modifySize, modifyFee, modifyCount, modifySigOpsCost); }
  140. private:
  141. int64_t modifySize;
  142. CAmount modifyFee;
  143. int64_t modifyCount;
  144. int64_t modifySigOpsCost;
  145. };
  146. struct update_fee_delta
  147. {
  148. update_fee_delta(int64_t _feeDelta) : feeDelta(_feeDelta) { }
  149. void operator() (CTxMemPoolEntry &e) { e.UpdateFeeDelta(feeDelta); }
  150. private:
  151. int64_t feeDelta;
  152. };
  153. struct update_lock_points
  154. {
  155. update_lock_points(const LockPoints& _lp) : lp(_lp) { }
  156. void operator() (CTxMemPoolEntry &e) { e.UpdateLockPoints(lp); }
  157. private:
  158. const LockPoints& lp;
  159. };
  160. // extracts a transaction hash from CTxMempoolEntry or CTransactionRef
  161. struct mempoolentry_txid
  162. {
  163. typedef uint256 result_type;
  164. result_type operator() (const CTxMemPoolEntry &entry) const
  165. {
  166. return entry.GetTx().GetHash();
  167. }
  168. result_type operator() (const CTransactionRef& tx) const
  169. {
  170. return tx->GetHash();
  171. }
  172. };
  173. /** \class CompareTxMemPoolEntryByDescendantScore
  174. *
  175. * Sort an entry by max(score/size of entry's tx, score/size with all descendants).
  176. */
  177. class CompareTxMemPoolEntryByDescendantScore
  178. {
  179. public:
  180. bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b)
  181. {
  182. bool fUseADescendants = UseDescendantScore(a);
  183. bool fUseBDescendants = UseDescendantScore(b);
  184. double aModFee = fUseADescendants ? a.GetModFeesWithDescendants() : a.GetModifiedFee();
  185. double aSize = fUseADescendants ? a.GetSizeWithDescendants() : a.GetTxSize();
  186. double bModFee = fUseBDescendants ? b.GetModFeesWithDescendants() : b.GetModifiedFee();
  187. double bSize = fUseBDescendants ? b.GetSizeWithDescendants() : b.GetTxSize();
  188. // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
  189. double f1 = aModFee * bSize;
  190. double f2 = aSize * bModFee;
  191. if (f1 == f2) {
  192. return a.GetTime() >= b.GetTime();
  193. }
  194. return f1 < f2;
  195. }
  196. // Calculate which score to use for an entry (avoiding division).
  197. bool UseDescendantScore(const CTxMemPoolEntry &a)
  198. {
  199. double f1 = (double)a.GetModifiedFee() * a.GetSizeWithDescendants();
  200. double f2 = (double)a.GetModFeesWithDescendants() * a.GetTxSize();
  201. return f2 > f1;
  202. }
  203. };
  204. /** \class CompareTxMemPoolEntryByScore
  205. *
  206. * Sort by score of entry ((fee+delta)/size) in descending order
  207. */
  208. class CompareTxMemPoolEntryByScore
  209. {
  210. public:
  211. bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b)
  212. {
  213. double f1 = (double)a.GetModifiedFee() * b.GetTxSize();
  214. double f2 = (double)b.GetModifiedFee() * a.GetTxSize();
  215. if (f1 == f2) {
  216. return b.GetTx().GetHash() < a.GetTx().GetHash();
  217. }
  218. return f1 > f2;
  219. }
  220. };
  221. class CompareTxMemPoolEntryByEntryTime
  222. {
  223. public:
  224. bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b)
  225. {
  226. return a.GetTime() < b.GetTime();
  227. }
  228. };
  229. class CompareTxMemPoolEntryByAncestorFee
  230. {
  231. public:
  232. bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b)
  233. {
  234. double aFees = a.GetModFeesWithAncestors();
  235. double aSize = a.GetSizeWithAncestors();
  236. double bFees = b.GetModFeesWithAncestors();
  237. double bSize = b.GetSizeWithAncestors();
  238. // Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
  239. double f1 = aFees * bSize;
  240. double f2 = aSize * bFees;
  241. if (f1 == f2) {
  242. return a.GetTx().GetHash() < b.GetTx().GetHash();
  243. }
  244. return f1 > f2;
  245. }
  246. };
  247. // Multi_index tag names
  248. struct descendant_score {};
  249. struct entry_time {};
  250. struct mining_score {};
  251. struct ancestor_score {};
  252. class CBlockPolicyEstimator;
  253. /**
  254. * Information about a mempool transaction.
  255. */
  256. struct TxMempoolInfo
  257. {
  258. /** The transaction itself */
  259. CTransactionRef tx;
  260. /** Time the transaction entered the mempool. */
  261. int64_t nTime;
  262. /** Feerate of the transaction. */
  263. CFeeRate feeRate;
  264. /** The fee delta. */
  265. int64_t nFeeDelta;
  266. };
  267. /** Reason why a transaction was removed from the mempool,
  268. * this is passed to the notification signal.
  269. */
  270. enum class MemPoolRemovalReason {
  271. UNKNOWN = 0, //! Manually removed or unknown reason
  272. EXPIRY, //! Expired from mempool
  273. SIZELIMIT, //! Removed in size limiting
  274. REORG, //! Removed for reorganization
  275. BLOCK, //! Removed for block
  276. CONFLICT, //! Removed for conflict with in-block transaction
  277. REPLACED //! Removed for replacement
  278. };
  279. /**
  280. * CTxMemPool stores valid-according-to-the-current-best-chain transactions
  281. * that may be included in the next block.
  282. *
  283. * Transactions are added when they are seen on the network (or created by the
  284. * local node), but not all transactions seen are added to the pool. For
  285. * example, the following new transactions will not be added to the mempool:
  286. * - a transaction which doesn't meet the minimum fee requirements.
  287. * - a new transaction that double-spends an input of a transaction already in
  288. * the pool where the new transaction does not meet the Replace-By-Fee
  289. * requirements as defined in BIP 125.
  290. * - a non-standard transaction.
  291. *
  292. * CTxMemPool::mapTx, and CTxMemPoolEntry bookkeeping:
  293. *
  294. * mapTx is a boost::multi_index that sorts the mempool on 4 criteria:
  295. * - transaction hash
  296. * - feerate [we use max(feerate of tx, feerate of tx with all descendants)]
  297. * - time in mempool
  298. * - mining score (feerate modified by any fee deltas from PrioritiseTransaction)
  299. *
  300. * Note: the term "descendant" refers to in-mempool transactions that depend on
  301. * this one, while "ancestor" refers to in-mempool transactions that a given
  302. * transaction depends on.
  303. *
  304. * In order for the feerate sort to remain correct, we must update transactions
  305. * in the mempool when new descendants arrive. To facilitate this, we track
  306. * the set of in-mempool direct parents and direct children in mapLinks. Within
  307. * each CTxMemPoolEntry, we track the size and fees of all descendants.
  308. *
  309. * Usually when a new transaction is added to the mempool, it has no in-mempool
  310. * children (because any such children would be an orphan). So in
  311. * addUnchecked(), we:
  312. * - update a new entry's setMemPoolParents to include all in-mempool parents
  313. * - update the new entry's direct parents to include the new tx as a child
  314. * - update all ancestors of the transaction to include the new tx's size/fee
  315. *
  316. * When a transaction is removed from the mempool, we must:
  317. * - update all in-mempool parents to not track the tx in setMemPoolChildren
  318. * - update all ancestors to not include the tx's size/fees in descendant state
  319. * - update all in-mempool children to not include it as a parent
  320. *
  321. * These happen in UpdateForRemoveFromMempool(). (Note that when removing a
  322. * transaction along with its descendants, we must calculate that set of
  323. * transactions to be removed before doing the removal, or else the mempool can
  324. * be in an inconsistent state where it's impossible to walk the ancestors of
  325. * a transaction.)
  326. *
  327. * In the event of a reorg, the assumption that a newly added tx has no
  328. * in-mempool children is false. In particular, the mempool is in an
  329. * inconsistent state while new transactions are being added, because there may
  330. * be descendant transactions of a tx coming from a disconnected block that are
  331. * unreachable from just looking at transactions in the mempool (the linking
  332. * transactions may also be in the disconnected block, waiting to be added).
  333. * Because of this, there's not much benefit in trying to search for in-mempool
  334. * children in addUnchecked(). Instead, in the special case of transactions
  335. * being added from a disconnected block, we require the caller to clean up the
  336. * state, to account for in-mempool, out-of-block descendants for all the
  337. * in-block transactions by calling UpdateTransactionsFromBlock(). Note that
  338. * until this is called, the mempool state is not consistent, and in particular
  339. * mapLinks may not be correct (and therefore functions like
  340. * CalculateMemPoolAncestors() and CalculateDescendants() that rely
  341. * on them to walk the mempool are not generally safe to use).
  342. *
  343. * Computational limits:
  344. *
  345. * Updating all in-mempool ancestors of a newly added transaction can be slow,
  346. * if no bound exists on how many in-mempool ancestors there may be.
  347. * CalculateMemPoolAncestors() takes configurable limits that are designed to
  348. * prevent these calculations from being too CPU intensive.
  349. *
  350. * Adding transactions from a disconnected block can be very time consuming,
  351. * because we don't have a way to limit the number of in-mempool descendants.
  352. * To bound CPU processing, we limit the amount of work we're willing to do
  353. * to properly update the descendant information for a tx being added from
  354. * a disconnected block. If we would exceed the limit, then we instead mark
  355. * the entry as "dirty", and set the feerate for sorting purposes to be equal
  356. * the feerate of the transaction without any descendants.
  357. *
  358. */
  359. class CTxMemPool
  360. {
  361. private:
  362. uint32_t nCheckFrequency; //!< Value n means that n times in 2^32 we check.
  363. unsigned int nTransactionsUpdated;
  364. CBlockPolicyEstimator* minerPolicyEstimator;
  365. uint64_t totalTxSize; //!< sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discounted. Defined in BIP 141.
  366. uint64_t cachedInnerUsage; //!< sum of dynamic memory usage of all the map elements (NOT the maps themselves)
  367. mutable int64_t lastRollingFeeUpdate;
  368. mutable bool blockSinceLastRollingFeeBump;
  369. mutable double rollingMinimumFeeRate; //!< minimum fee to get into the pool, decreases exponentially
  370. void trackPackageRemoved(const CFeeRate& rate);
  371. public:
  372. static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
  373. typedef boost::multi_index_container<
  374. CTxMemPoolEntry,
  375. boost::multi_index::indexed_by<
  376. // sorted by txid
  377. boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
  378. // sorted by fee rate
  379. boost::multi_index::ordered_non_unique<
  380. boost::multi_index::tag<descendant_score>,
  381. boost::multi_index::identity<CTxMemPoolEntry>,
  382. CompareTxMemPoolEntryByDescendantScore
  383. >,
  384. // sorted by entry time
  385. boost::multi_index::ordered_non_unique<
  386. boost::multi_index::tag<entry_time>,
  387. boost::multi_index::identity<CTxMemPoolEntry>,
  388. CompareTxMemPoolEntryByEntryTime
  389. >,
  390. // sorted by score (for mining prioritization)
  391. boost::multi_index::ordered_unique<
  392. boost::multi_index::tag<mining_score>,
  393. boost::multi_index::identity<CTxMemPoolEntry>,
  394. CompareTxMemPoolEntryByScore
  395. >,
  396. // sorted by fee rate with ancestors
  397. boost::multi_index::ordered_non_unique<
  398. boost::multi_index::tag<ancestor_score>,
  399. boost::multi_index::identity<CTxMemPoolEntry>,
  400. CompareTxMemPoolEntryByAncestorFee
  401. >
  402. >
  403. > indexed_transaction_set;
  404. mutable CCriticalSection cs;
  405. indexed_transaction_set mapTx;
  406. typedef indexed_transaction_set::nth_index<0>::type::iterator txiter;
  407. std::vector<std::pair<uint256, txiter> > vTxHashes; //!< All tx witness hashes/entries in mapTx, in random order
  408. struct CompareIteratorByHash {
  409. bool operator()(const txiter &a, const txiter &b) const {
  410. return a->GetTx().GetHash() < b->GetTx().GetHash();
  411. }
  412. };
  413. typedef std::set<txiter, CompareIteratorByHash> setEntries;
  414. const setEntries & GetMemPoolParents(txiter entry) const;
  415. const setEntries & GetMemPoolChildren(txiter entry) const;
  416. private:
  417. typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
  418. struct TxLinks {
  419. setEntries parents;
  420. setEntries children;
  421. };
  422. typedef std::map<txiter, TxLinks, CompareIteratorByHash> txlinksMap;
  423. txlinksMap mapLinks;
  424. void UpdateParent(txiter entry, txiter parent, bool add);
  425. void UpdateChild(txiter entry, txiter child, bool add);
  426. std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const;
  427. public:
  428. indirectmap<COutPoint, const CTransaction*> mapNextTx;
  429. std::map<uint256, CAmount> mapDeltas;
  430. /** Create a new CTxMemPool.
  431. */
  432. CTxMemPool(CBlockPolicyEstimator* estimator = nullptr);
  433. /**
  434. * If sanity-checking is turned on, check makes sure the pool is
  435. * consistent (does not contain two transactions that spend the same inputs,
  436. * all inputs are in the mapNextTx array). If sanity-checking is turned off,
  437. * check does nothing.
  438. */
  439. void check(const CCoinsViewCache *pcoins) const;
  440. void setSanityCheck(double dFrequency = 1.0) { nCheckFrequency = dFrequency * 4294967295.0; }
  441. // addUnchecked must updated state for all ancestors of a given transaction,
  442. // to track size/count of descendant transactions. First version of
  443. // addUnchecked can be used to have it call CalculateMemPoolAncestors(), and
  444. // then invoke the second version.
  445. bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool validFeeEstimate = true);
  446. bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, setEntries &setAncestors, bool validFeeEstimate = true);
  447. void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason = MemPoolRemovalReason::UNKNOWN);
  448. void removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags);
  449. void removeConflicts(const CTransaction &tx);
  450. void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight);
  451. void clear();
  452. void _clear(); //lock free
  453. bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb);
  454. void queryHashes(std::vector<uint256>& vtxid);
  455. void pruneSpent(const uint256& hash, CCoins &coins);
  456. unsigned int GetTransactionsUpdated() const;
  457. void AddTransactionsUpdated(unsigned int n);
  458. /**
  459. * Check that none of this transactions inputs are in the mempool, and thus
  460. * the tx is not dependent on other mempool transactions to be included in a block.
  461. */
  462. bool HasNoInputsOf(const CTransaction& tx) const;
  463. /** Affect CreateNewBlock prioritisation of transactions */
  464. void PrioritiseTransaction(const uint256& hash, const CAmount& nFeeDelta);
  465. void ApplyDelta(const uint256 hash, CAmount &nFeeDelta) const;
  466. void ClearPrioritisation(const uint256 hash);
  467. public:
  468. /** Remove a set of transactions from the mempool.
  469. * If a transaction is in this set, then all in-mempool descendants must
  470. * also be in the set, unless this transaction is being removed for being
  471. * in a block.
  472. * Set updateDescendants to true when removing a tx that was in a block, so
  473. * that any in-mempool descendants have their ancestor state updated.
  474. */
  475. void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason = MemPoolRemovalReason::UNKNOWN);
  476. /** When adding transactions from a disconnected block back to the mempool,
  477. * new mempool entries may have children in the mempool (which is generally
  478. * not the case when otherwise adding transactions).
  479. * UpdateTransactionsFromBlock() will find child transactions and update the
  480. * descendant state for each transaction in vHashesToUpdate (excluding any
  481. * child transactions present in vHashesToUpdate, which are already accounted
  482. * for). Note: vHashesToUpdate should be the set of transactions from the
  483. * disconnected block that have been accepted back into the mempool.
  484. */
  485. void UpdateTransactionsFromBlock(const std::vector<uint256> &vHashesToUpdate);
  486. /** Try to calculate all in-mempool ancestors of entry.
  487. * (these are all calculated including the tx itself)
  488. * limitAncestorCount = max number of ancestors
  489. * limitAncestorSize = max size of ancestors
  490. * limitDescendantCount = max number of descendants any ancestor can have
  491. * limitDescendantSize = max size of descendants any ancestor can have
  492. * errString = populated with error reason if any limits are hit
  493. * fSearchForParents = whether to search a tx's vin for in-mempool parents, or
  494. * look up parents from mapLinks. Must be true for entries not in the mempool
  495. */
  496. 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) const;
  497. /** Populate setDescendants with all in-mempool descendants of hash.
  498. * Assumes that setDescendants includes all in-mempool descendants of anything
  499. * already in it. */
  500. void CalculateDescendants(txiter it, setEntries &setDescendants);
  501. /** The minimum fee to get into the mempool, which may itself not be enough
  502. * for larger-sized transactions.
  503. * The incrementalRelayFee policy variable is used to bound the time it
  504. * takes the fee rate to go back down all the way to 0. When the feerate
  505. * would otherwise be half of this, it is set to 0 instead.
  506. */
  507. CFeeRate GetMinFee(size_t sizelimit) const;
  508. /** Remove transactions from the mempool until its dynamic size is <= sizelimit.
  509. * pvNoSpendsRemaining, if set, will be populated with the list of transactions
  510. * which are not in mempool which no longer have any spends in this mempool.
  511. */
  512. void TrimToSize(size_t sizelimit, std::vector<uint256>* pvNoSpendsRemaining=NULL);
  513. /** Expire all transaction (and their dependencies) in the mempool older than time. Return the number of removed transactions. */
  514. int Expire(int64_t time);
  515. /** Returns false if the transaction is in the mempool and not within the chain limit specified. */
  516. bool TransactionWithinChainLimit(const uint256& txid, size_t chainLimit) const;
  517. unsigned long size()
  518. {
  519. LOCK(cs);
  520. return mapTx.size();
  521. }
  522. uint64_t GetTotalTxSize()
  523. {
  524. LOCK(cs);
  525. return totalTxSize;
  526. }
  527. bool exists(uint256 hash) const
  528. {
  529. LOCK(cs);
  530. return (mapTx.count(hash) != 0);
  531. }
  532. CTransactionRef get(const uint256& hash) const;
  533. TxMempoolInfo info(const uint256& hash) const;
  534. std::vector<TxMempoolInfo> infoAll() const;
  535. size_t DynamicMemoryUsage() const;
  536. boost::signals2::signal<void (CTransactionRef)> NotifyEntryAdded;
  537. boost::signals2::signal<void (CTransactionRef, MemPoolRemovalReason)> NotifyEntryRemoved;
  538. private:
  539. /** UpdateForDescendants is used by UpdateTransactionsFromBlock to update
  540. * the descendants for a single transaction that has been added to the
  541. * mempool but may have child transactions in the mempool, eg during a
  542. * chain reorg. setExclude is the set of descendant transactions in the
  543. * mempool that must not be accounted for (because any descendants in
  544. * setExclude were added to the mempool after the transaction being
  545. * updated and hence their state is already reflected in the parent
  546. * state).
  547. *
  548. * cachedDescendants will be updated with the descendants of the transaction
  549. * being updated, so that future invocations don't need to walk the
  550. * same transaction again, if encountered in another transaction chain.
  551. */
  552. void UpdateForDescendants(txiter updateIt,
  553. cacheMap &cachedDescendants,
  554. const std::set<uint256> &setExclude);
  555. /** Update ancestors of hash to add/remove it as a descendant transaction. */
  556. void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors);
  557. /** Set ancestor state for an entry */
  558. void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors);
  559. /** For each transaction being removed, update ancestors and any direct children.
  560. * If updateDescendants is true, then also update in-mempool descendants'
  561. * ancestor state. */
  562. void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants);
  563. /** Sever link between specified transaction and direct children. */
  564. void UpdateChildrenForRemoval(txiter entry);
  565. /** Before calling removeUnchecked for a given transaction,
  566. * UpdateForRemoveFromMempool must be called on the entire (dependent) set
  567. * of transactions being removed at the same time. We use each
  568. * CTxMemPoolEntry's setMemPoolParents in order to walk ancestors of a
  569. * given transaction that is removed, so we can't remove intermediate
  570. * transactions in a chain before we've updated all the state for the
  571. * removal.
  572. */
  573. void removeUnchecked(txiter entry, MemPoolRemovalReason reason = MemPoolRemovalReason::UNKNOWN);
  574. };
  575. /**
  576. * CCoinsView that brings transactions from a memorypool into view.
  577. * It does not check for spendings by memory pool transactions.
  578. */
  579. class CCoinsViewMemPool : public CCoinsViewBacked
  580. {
  581. protected:
  582. const CTxMemPool& mempool;
  583. public:
  584. CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
  585. bool GetCoins(const uint256 &txid, CCoins &coins) const;
  586. bool HaveCoins(const uint256 &txid) const;
  587. };
  588. /**
  589. * DisconnectedBlockTransactions
  590. * During the reorg, it's desirable to re-add previously confirmed transactions
  591. * to the mempool, so that anything not re-confirmed in the new chain is
  592. * available to be mined. However, it's more efficient to wait until the reorg
  593. * is complete and process all still-unconfirmed transactions at that time,
  594. * since we expect most confirmed transactions to (typically) still be
  595. * confirmed in the new chain, and re-accepting to the memory pool is expensive
  596. * (and therefore better to not do in the middle of reorg-processing).
  597. * Instead, store the disconnected transactions (in order!) as we go, remove any
  598. * that are included in blocks in the new chain, and then process the remaining
  599. * still-unconfirmed transactions at the end.
  600. */
  601. // multi_index tag names
  602. struct txid_index {};
  603. struct insertion_order {};
  604. struct DisconnectedBlockTransactions {
  605. typedef boost::multi_index_container<
  606. CTransactionRef,
  607. boost::multi_index::indexed_by<
  608. // sorted by txid
  609. boost::multi_index::hashed_unique<
  610. boost::multi_index::tag<txid_index>,
  611. mempoolentry_txid,
  612. SaltedTxidHasher
  613. >,
  614. // sorted by order in the blockchain
  615. boost::multi_index::sequenced<
  616. boost::multi_index::tag<insertion_order>
  617. >
  618. >
  619. > indexed_disconnected_transactions;
  620. // It's almost certainly a logic bug if we don't clear out queuedTx before
  621. // destruction, as we add to it while disconnecting blocks, and then we
  622. // need to re-process remaining transactions to ensure mempool consistency.
  623. // For now, assert() that we've emptied out this object on destruction.
  624. // This assert() can always be removed if the reorg-processing code were
  625. // to be refactored such that this assumption is no longer true (for
  626. // instance if there was some other way we cleaned up the mempool after a
  627. // reorg, besides draining this object).
  628. ~DisconnectedBlockTransactions() { assert(queuedTx.empty()); }
  629. indexed_disconnected_transactions queuedTx;
  630. uint64_t cachedInnerUsage = 0;
  631. // Estimate the overhead of queuedTx to be 6 pointers + an allocation, as
  632. // no exact formula for boost::multi_index_contained is implemented.
  633. size_t DynamicMemoryUsage() const {
  634. return memusage::MallocUsage(sizeof(CTransactionRef) + 6 * sizeof(void*)) * queuedTx.size() + cachedInnerUsage;
  635. }
  636. void addTransaction(const CTransactionRef& tx)
  637. {
  638. queuedTx.insert(tx);
  639. cachedInnerUsage += RecursiveDynamicUsage(tx);
  640. }
  641. // Remove entries based on txid_index, and update memory usage.
  642. void removeForBlock(const std::vector<CTransactionRef>& vtx)
  643. {
  644. // Short-circuit in the common case of a block being added to the tip
  645. if (queuedTx.empty()) {
  646. return;
  647. }
  648. for (auto const &tx : vtx) {
  649. auto it = queuedTx.find(tx->GetHash());
  650. if (it != queuedTx.end()) {
  651. cachedInnerUsage -= RecursiveDynamicUsage(*it);
  652. queuedTx.erase(it);
  653. }
  654. }
  655. }
  656. // Remove an entry by insertion_order index, and update memory usage.
  657. void removeEntry(indexed_disconnected_transactions::index<insertion_order>::type::iterator entry)
  658. {
  659. cachedInnerUsage -= RecursiveDynamicUsage(*entry);
  660. queuedTx.get<insertion_order>().erase(entry);
  661. }
  662. void clear()
  663. {
  664. cachedInnerUsage = 0;
  665. queuedTx.clear();
  666. }
  667. };
  668. #endif // BITCOIN_TXMEMPOOL_H