|
21 | 21 | #include <primitives/transaction.h>
|
22 | 22 | #include <random.h>
|
23 | 23 | #include <sync.h>
|
| 24 | +#include <util/epochguard.h> |
24 | 25 | #include <util/hasher.h>
|
25 | 26 |
|
26 | 27 | #include <boost/multi_index_container.hpp>
|
@@ -64,6 +65,7 @@ struct CompareIteratorByHash {
|
64 | 65 | return a->GetTx().GetHash() < b->GetTx().GetHash();
|
65 | 66 | }
|
66 | 67 | };
|
| 68 | + |
67 | 69 | /** \class CTxMemPoolEntry
|
68 | 70 | *
|
69 | 71 | * CTxMemPoolEntry stores data about the corresponding transaction, as well
|
@@ -156,7 +158,7 @@ class CTxMemPoolEntry
|
156 | 158 | Children& GetMemPoolChildren() const { return m_children; }
|
157 | 159 |
|
158 | 160 | mutable size_t vTxHashesIdx; //!< Index in mempool's vTxHashes
|
159 |
| - mutable uint64_t m_epoch; //!< epoch when last touched, useful for graph algorithms |
| 161 | + mutable Epoch::Marker m_epoch_marker; //!< epoch when last touched, useful for graph algorithms |
160 | 162 | };
|
161 | 163 |
|
162 | 164 | // Helpers for modifying CTxMemPool::mapTx, which is a boost multi_index.
|
@@ -486,8 +488,7 @@ class CTxMemPool
|
486 | 488 | mutable int64_t lastRollingFeeUpdate;
|
487 | 489 | mutable bool blockSinceLastRollingFeeBump;
|
488 | 490 | mutable double rollingMinimumFeeRate; //!< minimum fee to get into the pool, decreases exponentially
|
489 |
| - mutable uint64_t m_epoch{0}; |
490 |
| - mutable bool m_has_epoch_guard{false}; |
| 491 | + mutable Epoch m_epoch GUARDED_BY(cs); |
491 | 492 |
|
492 | 493 | // In-memory counter for external mempool tracking purposes.
|
493 | 494 | // This number is incremented once every time a transaction
|
@@ -666,7 +667,7 @@ class CTxMemPool
|
666 | 667 | * for). Note: vHashesToUpdate should be the set of transactions from the
|
667 | 668 | * disconnected block that have been accepted back into the mempool.
|
668 | 669 | */
|
669 |
| - void UpdateTransactionsFromBlock(const std::vector<uint256>& vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main); |
| 670 | + void UpdateTransactionsFromBlock(const std::vector<uint256>& vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main) LOCKS_EXCLUDED(m_epoch); |
670 | 671 |
|
671 | 672 | /** Try to calculate all in-mempool ancestors of entry.
|
672 | 673 | * (these are all calculated including the tx itself)
|
@@ -827,52 +828,22 @@ class CTxMemPool
|
827 | 828 | */
|
828 | 829 | void removeUnchecked(txiter entry, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
|
829 | 830 | public:
|
830 |
| - /** EpochGuard: RAII-style guard for using epoch-based graph traversal algorithms. |
831 |
| - * When walking ancestors or descendants, we generally want to avoid |
832 |
| - * visiting the same transactions twice. Some traversal algorithms use |
833 |
| - * std::set (or setEntries) to deduplicate the transaction we visit. |
834 |
| - * However, use of std::set is algorithmically undesirable because it both |
835 |
| - * adds an asymptotic factor of O(log n) to traverals cost and triggers O(n) |
836 |
| - * more dynamic memory allocations. |
837 |
| - * In many algorithms we can replace std::set with an internal mempool |
838 |
| - * counter to track the time (or, "epoch") that we began a traversal, and |
839 |
| - * check + update a per-transaction epoch for each transaction we look at to |
840 |
| - * determine if that transaction has not yet been visited during the current |
841 |
| - * traversal's epoch. |
842 |
| - * Algorithms using std::set can be replaced on a one by one basis. |
843 |
| - * Both techniques are not fundamentally incompatible across the codebase. |
844 |
| - * Generally speaking, however, the remaining use of std::set for mempool |
845 |
| - * traversal should be viewed as a TODO for replacement with an epoch based |
846 |
| - * traversal, rather than a preference for std::set over epochs in that |
847 |
| - * algorithm. |
848 |
| - */ |
849 |
| - class EpochGuard { |
850 |
| - const CTxMemPool& pool; |
851 |
| - public: |
852 |
| - explicit EpochGuard(const CTxMemPool& in); |
853 |
| - ~EpochGuard(); |
854 |
| - }; |
855 |
| - // N.B. GetFreshEpoch modifies mutable state via the EpochGuard construction |
856 |
| - // (and later destruction) |
857 |
| - EpochGuard GetFreshEpoch() const EXCLUSIVE_LOCKS_REQUIRED(cs); |
858 |
| - |
859 | 831 | /** visited marks a CTxMemPoolEntry as having been traversed
|
860 |
| - * during the lifetime of the most recently created EpochGuard |
| 832 | + * during the lifetime of the most recently created Epoch::Guard |
861 | 833 | * and returns false if we are the first visitor, true otherwise.
|
862 | 834 | *
|
863 |
| - * An EpochGuard must be held when visited is called or an assert will be |
| 835 | + * An Epoch::Guard must be held when visited is called or an assert will be |
864 | 836 | * triggered.
|
865 | 837 | *
|
866 | 838 | */
|
867 |
| - bool visited(txiter it) const EXCLUSIVE_LOCKS_REQUIRED(cs) { |
868 |
| - assert(m_has_epoch_guard); |
869 |
| - bool ret = it->m_epoch >= m_epoch; |
870 |
| - it->m_epoch = std::max(it->m_epoch, m_epoch); |
871 |
| - return ret; |
| 839 | + bool visited(const txiter it) const EXCLUSIVE_LOCKS_REQUIRED(cs, m_epoch) |
| 840 | + { |
| 841 | + return m_epoch.visited(it->m_epoch_marker); |
872 | 842 | }
|
873 | 843 |
|
874 |
| - bool visited(Optional<txiter> it) const EXCLUSIVE_LOCKS_REQUIRED(cs) { |
875 |
| - assert(m_has_epoch_guard); |
| 844 | + bool visited(Optional<txiter> it) const EXCLUSIVE_LOCKS_REQUIRED(cs, m_epoch) |
| 845 | + { |
| 846 | + assert(m_epoch.guarded()); // verify guard even when it==nullopt |
876 | 847 | return !it || visited(*it);
|
877 | 848 | }
|
878 | 849 | };
|
|
0 commit comments