refactor: replace get_iter_from_wtxid with GetIter(const Wtxid&)

Overloading GetIter makes it easier to use std::visit patterns
from a GenTxid.
This commit is contained in:
stickies-v
2025-06-23 15:14:10 +01:00
committed by marcofleon
parent fcf92fd640
commit 243553d590
2 changed files with 20 additions and 19 deletions

View File

@@ -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 * both are in the mempool and a has a higher score than b
*/ */
LOCK(cs); LOCK(cs);
indexed_transaction_set::const_iterator j = wtxid ? get_iter_from_wtxid(hashb) : mapTx.find(hashb); auto j = wtxid ? GetIter(Wtxid::FromUint256(hashb)) : GetIter(Txid::FromUint256(hashb));
if (j == mapTx.end()) return false; if (!j.has_value()) return false;
indexed_transaction_set::const_iterator i = wtxid ? get_iter_from_wtxid(hasha) : mapTx.find(hasha); auto i = wtxid ? GetIter(Wtxid::FromUint256(hasha)) : GetIter(Txid::FromUint256(hasha));
if (i == mapTx.end()) return true; if (!i.has_value()) return true;
uint64_t counta = i->GetCountWithAncestors(); uint64_t counta = i.value()->GetCountWithAncestors();
uint64_t countb = j->GetCountWithAncestors(); uint64_t countb = j.value()->GetCountWithAncestors();
if (counta == countb) { if (counta == countb) {
return CompareTxMemPoolEntryByScore()(*i, *j); return CompareTxMemPoolEntryByScore()(*i.value(), *j.value());
} }
return counta < countb; return counta < countb;
} }
@@ -893,18 +893,16 @@ CTransactionRef CTxMemPool::get(const uint256& hash) const
TxMempoolInfo CTxMemPool::info(const GenTxid& gtxid) const TxMempoolInfo CTxMemPool::info(const GenTxid& gtxid) const
{ {
LOCK(cs); LOCK(cs);
indexed_transaction_set::const_iterator i = (gtxid.IsWtxid() ? get_iter_from_wtxid(gtxid.GetHash()) : mapTx.find(gtxid.GetHash())); auto i = (gtxid.IsWtxid() ? GetIter(Wtxid::FromUint256(gtxid.GetHash())) : GetIter(Txid::FromUint256(gtxid.GetHash())));
if (i == mapTx.end()) return i.has_value() ? GetInfo(*i) : TxMempoolInfo{};
return TxMempoolInfo();
return GetInfo(i);
} }
TxMempoolInfo CTxMemPool::info_for_relay(const GenTxid& gtxid, uint64_t last_sequence) const TxMempoolInfo CTxMemPool::info_for_relay(const GenTxid& gtxid, uint64_t last_sequence) const
{ {
LOCK(cs); LOCK(cs);
indexed_transaction_set::const_iterator i = (gtxid.IsWtxid() ? get_iter_from_wtxid(gtxid.GetHash()) : mapTx.find(gtxid.GetHash())); auto i = (gtxid.IsWtxid() ? GetIter(Wtxid::FromUint256(gtxid.GetHash())) : GetIter(Txid::FromUint256(gtxid.GetHash())));
if (i != mapTx.end() && i->GetSequence() < last_sequence) { if (i.has_value() && (*i)->GetSequence() < last_sequence) {
return GetInfo(i); return GetInfo(*i);
} else { } else {
return TxMempoolInfo(); return TxMempoolInfo();
} }
@@ -991,6 +989,13 @@ std::optional<CTxMemPool::txiter> CTxMemPool::GetIter(const Txid& txid) const
return std::nullopt; return std::nullopt;
} }
std::optional<CTxMemPool::txiter> CTxMemPool::GetIter(const Wtxid& wtxid) const
{
AssertLockHeld(cs);
auto it{mapTx.project<0>(mapTx.get<index_by_wtxid>().find(wtxid))};
return it != mapTx.end() ? std::make_optional(it) : std::nullopt;
}
CTxMemPool::setEntries CTxMemPool::GetIterSet(const std::set<Txid>& hashes) const CTxMemPool::setEntries CTxMemPool::GetIterSet(const std::set<Txid>& hashes) const
{ {
CTxMemPool::setEntries ret; CTxMemPool::setEntries ret;

View File

@@ -499,6 +499,7 @@ public:
/** Returns an iterator to the given hash, if found */ /** Returns an iterator to the given hash, if found */
std::optional<txiter> GetIter(const Txid& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs); std::optional<txiter> GetIter(const Txid& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
std::optional<txiter> GetIter(const Wtxid& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
/** Translate a set of hashes into a set of pool iterators to avoid repeated lookups. /** 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, * 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); const CTxMemPoolEntry* GetEntry(const Txid& txid) const LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(cs);
CTransactionRef get(const uint256& hash) const; 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<index_by_wtxid>().find(wtxid));
}
TxMempoolInfo info(const GenTxid& gtxid) const; TxMempoolInfo info(const GenTxid& gtxid) const;
/** Returns info for a transaction if its entry_sequence < last_sequence */ /** Returns info for a transaction if its entry_sequence < last_sequence */