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

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