diff options
author | stickies-v <stickies-v@protonmail.com> | 2023-11-09 17:51:20 +0100 |
---|---|---|
committer | glozow <gloriajzhao@gmail.com> | 2024-01-19 16:12:23 +0000 |
commit | fc62271015e9585bd3a3889adac894c9ef2e2ab2 (patch) | |
tree | 60b4d2a52636ba6ca4801261c8fc7f25fefe195c | |
parent | 04edf9f5862c3ca2d1aed6b7a0f24b8d2914f96c (diff) |
[refactor] Add helper for iterating through mempool entries
Instead of reaching into the mapTx data structure, use a helper method
that provides the required vector of CTxMemPoolEntry pointers.
Github-Pull: #28391
Rebased-From: 453b4813ebc74859864803e9972b58e4be76a4d6
-rw-r--r-- | src/kernel/mempool_entry.h | 2 | ||||
-rw-r--r-- | src/node/interfaces.cpp | 2 | ||||
-rw-r--r-- | src/rpc/mempool.cpp | 5 | ||||
-rw-r--r-- | src/txmempool.cpp | 12 | ||||
-rw-r--r-- | src/txmempool.h | 1 |
5 files changed, 18 insertions, 4 deletions
diff --git a/src/kernel/mempool_entry.h b/src/kernel/mempool_entry.h index 1f175a5ccf..edd95e8d53 100644 --- a/src/kernel/mempool_entry.h +++ b/src/kernel/mempool_entry.h @@ -176,4 +176,6 @@ public: mutable Epoch::Marker m_epoch_marker; //!< epoch when last touched, useful for graph algorithms }; +using CTxMemPoolEntryRef = CTxMemPoolEntry::CTxMemPoolEntryRef; + #endif // BITCOIN_KERNEL_MEMPOOL_ENTRY_H diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index f6dbe4f008..3930280797 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -806,7 +806,7 @@ public: { if (!m_node.mempool) return; LOCK2(::cs_main, m_node.mempool->cs); - for (const CTxMemPoolEntry& entry : m_node.mempool->mapTx) { + for (const CTxMemPoolEntry& entry : m_node.mempool->entryAll()) { notifications.transactionAddedToMempool(entry.GetSharedTx()); } } diff --git a/src/rpc/mempool.cpp b/src/rpc/mempool.cpp index 136969eb87..e113441993 100644 --- a/src/rpc/mempool.cpp +++ b/src/rpc/mempool.cpp @@ -344,14 +344,13 @@ UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose, bool include_mempoo } LOCK(pool.cs); UniValue o(UniValue::VOBJ); - for (const CTxMemPoolEntry& e : pool.mapTx) { - const uint256& hash = e.GetTx().GetHash(); + for (const CTxMemPoolEntry& e : pool.entryAll()) { UniValue info(UniValue::VOBJ); entryToJSON(pool, info, e); // Mempool has unique entries so there is no advantage in using // UniValue::pushKV, which checks if the key already exists in O(N). // UniValue::pushKVEnd is used instead which currently is O(1). - o.pushKVEnd(hash.ToString(), info); + o.pushKVEnd(e.GetTx().GetHash().ToString(), info); } return o; } else { diff --git a/src/txmempool.cpp b/src/txmempool.cpp index 461662ad93..8b744698ba 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -836,6 +836,18 @@ static TxMempoolInfo GetInfo(CTxMemPool::indexed_transaction_set::const_iterator return TxMempoolInfo{it->GetSharedTx(), it->GetTime(), it->GetFee(), it->GetTxSize(), it->GetModifiedFee() - it->GetFee()}; } +std::vector<CTxMemPoolEntryRef> CTxMemPool::entryAll() const +{ + AssertLockHeld(cs); + + std::vector<CTxMemPoolEntryRef> ret; + ret.reserve(mapTx.size()); + for (const auto& it : GetSortedDepthAndScore()) { + ret.emplace_back(*it); + } + return ret; +} + std::vector<TxMempoolInfo> CTxMemPool::infoAll() const { LOCK(cs); diff --git a/src/txmempool.h b/src/txmempool.h index cbeabb31fa..fd7006ab44 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -695,6 +695,7 @@ public: /** Returns info for a transaction if its entry_sequence < last_sequence */ TxMempoolInfo info_for_relay(const GenTxid& gtxid, uint64_t last_sequence) const; + std::vector<CTxMemPoolEntryRef> entryAll() const EXCLUSIVE_LOCKS_REQUIRED(cs); std::vector<TxMempoolInfo> infoAll() const; size_t DynamicMemoryUsage() const; |