Skip to content

Commit 243553d

Browse files
stickies-vmarcofleon
authored andcommitted
refactor: replace get_iter_from_wtxid with GetIter(const Wtxid&)
Overloading GetIter makes it easier to use std::visit patterns from a GenTxid.
1 parent fcf92fd commit 243553d

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/txmempool.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -802,14 +802,14 @@ bool CTxMemPool::CompareDepthAndScore(const uint256& hasha, const uint256& hashb
802802
* both are in the mempool and a has a higher score than b
803803
*/
804804
LOCK(cs);
805-
indexed_transaction_set::const_iterator j = wtxid ? get_iter_from_wtxid(hashb) : mapTx.find(hashb);
806-
if (j == mapTx.end()) return false;
807-
indexed_transaction_set::const_iterator i = wtxid ? get_iter_from_wtxid(hasha) : mapTx.find(hasha);
808-
if (i == mapTx.end()) return true;
809-
uint64_t counta = i->GetCountWithAncestors();
810-
uint64_t countb = j->GetCountWithAncestors();
805+
auto j = wtxid ? GetIter(Wtxid::FromUint256(hashb)) : GetIter(Txid::FromUint256(hashb));
806+
if (!j.has_value()) return false;
807+
auto i = wtxid ? GetIter(Wtxid::FromUint256(hasha)) : GetIter(Txid::FromUint256(hasha));
808+
if (!i.has_value()) return true;
809+
uint64_t counta = i.value()->GetCountWithAncestors();
810+
uint64_t countb = j.value()->GetCountWithAncestors();
811811
if (counta == countb) {
812-
return CompareTxMemPoolEntryByScore()(*i, *j);
812+
return CompareTxMemPoolEntryByScore()(*i.value(), *j.value());
813813
}
814814
return counta < countb;
815815
}
@@ -893,18 +893,16 @@ CTransactionRef CTxMemPool::get(const uint256& hash) const
893893
TxMempoolInfo CTxMemPool::info(const GenTxid& gtxid) const
894894
{
895895
LOCK(cs);
896-
indexed_transaction_set::const_iterator i = (gtxid.IsWtxid() ? get_iter_from_wtxid(gtxid.GetHash()) : mapTx.find(gtxid.GetHash()));
897-
if (i == mapTx.end())
898-
return TxMempoolInfo();
899-
return GetInfo(i);
896+
auto i = (gtxid.IsWtxid() ? GetIter(Wtxid::FromUint256(gtxid.GetHash())) : GetIter(Txid::FromUint256(gtxid.GetHash())));
897+
return i.has_value() ? GetInfo(*i) : TxMempoolInfo{};
900898
}
901899

902900
TxMempoolInfo CTxMemPool::info_for_relay(const GenTxid& gtxid, uint64_t last_sequence) const
903901
{
904902
LOCK(cs);
905-
indexed_transaction_set::const_iterator i = (gtxid.IsWtxid() ? get_iter_from_wtxid(gtxid.GetHash()) : mapTx.find(gtxid.GetHash()));
906-
if (i != mapTx.end() && i->GetSequence() < last_sequence) {
907-
return GetInfo(i);
903+
auto i = (gtxid.IsWtxid() ? GetIter(Wtxid::FromUint256(gtxid.GetHash())) : GetIter(Txid::FromUint256(gtxid.GetHash())));
904+
if (i.has_value() && (*i)->GetSequence() < last_sequence) {
905+
return GetInfo(*i);
908906
} else {
909907
return TxMempoolInfo();
910908
}
@@ -991,6 +989,13 @@ std::optional<CTxMemPool::txiter> CTxMemPool::GetIter(const Txid& txid) const
991989
return std::nullopt;
992990
}
993991

992+
std::optional<CTxMemPool::txiter> CTxMemPool::GetIter(const Wtxid& wtxid) const
993+
{
994+
AssertLockHeld(cs);
995+
auto it{mapTx.project<0>(mapTx.get<index_by_wtxid>().find(wtxid))};
996+
return it != mapTx.end() ? std::make_optional(it) : std::nullopt;
997+
}
998+
994999
CTxMemPool::setEntries CTxMemPool::GetIterSet(const std::set<Txid>& hashes) const
9951000
{
9961001
CTxMemPool::setEntries ret;

src/txmempool.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ class CTxMemPool
499499

500500
/** Returns an iterator to the given hash, if found */
501501
std::optional<txiter> GetIter(const Txid& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
502+
std::optional<txiter> GetIter(const Wtxid& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
502503

503504
/** Translate a set of hashes into a set of pool iterators to avoid repeated lookups.
504505
* Does not require that all of the hashes correspond to actual transactions in the mempool,
@@ -656,11 +657,6 @@ class CTxMemPool
656657
const CTxMemPoolEntry* GetEntry(const Txid& txid) const LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(cs);
657658

658659
CTransactionRef get(const uint256& hash) const;
659-
txiter get_iter_from_wtxid(const uint256& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
660-
{
661-
AssertLockHeld(cs);
662-
return mapTx.project<0>(mapTx.get<index_by_wtxid>().find(wtxid));
663-
}
664660
TxMempoolInfo info(const GenTxid& gtxid) const;
665661

666662
/** Returns info for a transaction if its entry_sequence < last_sequence */

0 commit comments

Comments
 (0)