Skip to content

Commit 453b481

Browse files
stickies-vTheCharlatan
authored andcommitted
[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.
1 parent d9007f5 commit 453b481

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

src/kernel/mempool_entry.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,6 @@ class CTxMemPoolEntry
176176
mutable Epoch::Marker m_epoch_marker; //!< epoch when last touched, useful for graph algorithms
177177
};
178178

179+
using CTxMemPoolEntryRef = CTxMemPoolEntry::CTxMemPoolEntryRef;
180+
179181
#endif // BITCOIN_KERNEL_MEMPOOL_ENTRY_H

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ class ChainImpl : public Chain
806806
{
807807
if (!m_node.mempool) return;
808808
LOCK2(::cs_main, m_node.mempool->cs);
809-
for (const CTxMemPoolEntry& entry : m_node.mempool->mapTx) {
809+
for (const CTxMemPoolEntry& entry : m_node.mempool->entryAll()) {
810810
notifications.transactionAddedToMempool(entry.GetSharedTx());
811811
}
812812
}

src/rpc/mempool.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,13 @@ UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose, bool include_mempoo
344344
}
345345
LOCK(pool.cs);
346346
UniValue o(UniValue::VOBJ);
347-
for (const CTxMemPoolEntry& e : pool.mapTx) {
348-
const uint256& hash = e.GetTx().GetHash();
347+
for (const CTxMemPoolEntry& e : pool.entryAll()) {
349348
UniValue info(UniValue::VOBJ);
350349
entryToJSON(pool, info, e);
351350
// Mempool has unique entries so there is no advantage in using
352351
// UniValue::pushKV, which checks if the key already exists in O(N).
353352
// UniValue::pushKVEnd is used instead which currently is O(1).
354-
o.pushKVEnd(hash.ToString(), info);
353+
o.pushKVEnd(e.GetTx().GetHash().ToString(), info);
355354
}
356355
return o;
357356
} else {

src/txmempool.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,18 @@ static TxMempoolInfo GetInfo(CTxMemPool::indexed_transaction_set::const_iterator
836836
return TxMempoolInfo{it->GetSharedTx(), it->GetTime(), it->GetFee(), it->GetTxSize(), it->GetModifiedFee() - it->GetFee()};
837837
}
838838

839+
std::vector<CTxMemPoolEntryRef> CTxMemPool::entryAll() const
840+
{
841+
AssertLockHeld(cs);
842+
843+
std::vector<CTxMemPoolEntryRef> ret;
844+
ret.reserve(mapTx.size());
845+
for (const auto& it : GetSortedDepthAndScore()) {
846+
ret.emplace_back(*it);
847+
}
848+
return ret;
849+
}
850+
839851
std::vector<TxMempoolInfo> CTxMemPool::infoAll() const
840852
{
841853
LOCK(cs);

src/txmempool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ class CTxMemPool
695695
/** Returns info for a transaction if its entry_sequence < last_sequence */
696696
TxMempoolInfo info_for_relay(const GenTxid& gtxid, uint64_t last_sequence) const;
697697

698+
std::vector<CTxMemPoolEntryRef> entryAll() const EXCLUSIVE_LOCKS_REQUIRED(cs);
698699
std::vector<TxMempoolInfo> infoAll() const;
699700

700701
size_t DynamicMemoryUsage() const;

0 commit comments

Comments
 (0)