From 243553d59071f3e43a42f3809706790495b17ffc Mon Sep 17 00:00:00 2001 From: stickies-v Date: Mon, 23 Jun 2025 15:14:10 +0100 Subject: [PATCH] refactor: replace get_iter_from_wtxid with GetIter(const Wtxid&) Overloading GetIter makes it easier to use std::visit patterns from a GenTxid. --- src/txmempool.cpp | 33 +++++++++++++++++++-------------- src/txmempool.h | 6 +----- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/txmempool.cpp b/src/txmempool.cpp index 36bd58bfbff..930fd4de18e 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -802,14 +802,14 @@ bool CTxMemPool::CompareDepthAndScore(const uint256& hasha, const uint256& hashb * both are in the mempool and a has a higher score than b */ LOCK(cs); - indexed_transaction_set::const_iterator j = wtxid ? get_iter_from_wtxid(hashb) : mapTx.find(hashb); - if (j == mapTx.end()) return false; - indexed_transaction_set::const_iterator i = wtxid ? get_iter_from_wtxid(hasha) : mapTx.find(hasha); - if (i == mapTx.end()) return true; - uint64_t counta = i->GetCountWithAncestors(); - uint64_t countb = j->GetCountWithAncestors(); + auto j = wtxid ? GetIter(Wtxid::FromUint256(hashb)) : GetIter(Txid::FromUint256(hashb)); + if (!j.has_value()) return false; + auto i = wtxid ? GetIter(Wtxid::FromUint256(hasha)) : GetIter(Txid::FromUint256(hasha)); + if (!i.has_value()) return true; + uint64_t counta = i.value()->GetCountWithAncestors(); + uint64_t countb = j.value()->GetCountWithAncestors(); if (counta == countb) { - return CompareTxMemPoolEntryByScore()(*i, *j); + return CompareTxMemPoolEntryByScore()(*i.value(), *j.value()); } return counta < countb; } @@ -893,18 +893,16 @@ CTransactionRef CTxMemPool::get(const uint256& hash) const TxMempoolInfo CTxMemPool::info(const GenTxid& gtxid) const { LOCK(cs); - indexed_transaction_set::const_iterator i = (gtxid.IsWtxid() ? get_iter_from_wtxid(gtxid.GetHash()) : mapTx.find(gtxid.GetHash())); - if (i == mapTx.end()) - return TxMempoolInfo(); - return GetInfo(i); + auto i = (gtxid.IsWtxid() ? GetIter(Wtxid::FromUint256(gtxid.GetHash())) : GetIter(Txid::FromUint256(gtxid.GetHash()))); + return i.has_value() ? GetInfo(*i) : TxMempoolInfo{}; } TxMempoolInfo CTxMemPool::info_for_relay(const GenTxid& gtxid, uint64_t last_sequence) const { LOCK(cs); - indexed_transaction_set::const_iterator i = (gtxid.IsWtxid() ? get_iter_from_wtxid(gtxid.GetHash()) : mapTx.find(gtxid.GetHash())); - if (i != mapTx.end() && i->GetSequence() < last_sequence) { - return GetInfo(i); + auto i = (gtxid.IsWtxid() ? GetIter(Wtxid::FromUint256(gtxid.GetHash())) : GetIter(Txid::FromUint256(gtxid.GetHash()))); + if (i.has_value() && (*i)->GetSequence() < last_sequence) { + return GetInfo(*i); } else { return TxMempoolInfo(); } @@ -991,6 +989,13 @@ std::optional CTxMemPool::GetIter(const Txid& txid) const return std::nullopt; } +std::optional CTxMemPool::GetIter(const Wtxid& wtxid) const +{ + AssertLockHeld(cs); + auto it{mapTx.project<0>(mapTx.get().find(wtxid))}; + return it != mapTx.end() ? std::make_optional(it) : std::nullopt; +} + CTxMemPool::setEntries CTxMemPool::GetIterSet(const std::set& hashes) const { CTxMemPool::setEntries ret; diff --git a/src/txmempool.h b/src/txmempool.h index 4bce0d43ab8..c30bc96942d 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -499,6 +499,7 @@ public: /** Returns an iterator to the given hash, if found */ std::optional GetIter(const Txid& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs); + std::optional GetIter(const Wtxid& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs); /** Translate a set of hashes into a set of pool iterators to avoid repeated lookups. * Does not require that all of the hashes correspond to actual transactions in the mempool, @@ -656,11 +657,6 @@ public: const CTxMemPoolEntry* GetEntry(const Txid& txid) const LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(cs); CTransactionRef get(const uint256& hash) const; - txiter get_iter_from_wtxid(const uint256& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs) - { - AssertLockHeld(cs); - return mapTx.project<0>(mapTx.get().find(wtxid)); - } TxMempoolInfo info(const GenTxid& gtxid) const; /** Returns info for a transaction if its entry_sequence < last_sequence */