From fcf92fd640eae60d1f601136a4e1c9de8ccb68b5 Mon Sep 17 00:00:00 2001 From: marcofleon Date: Mon, 23 Jun 2025 14:52:13 +0100 Subject: [PATCH] refactor: make CTxMemPool::GetIter strongly typed This allows adding a GetIter(const Wtxid&) overload in a next commit, making it easier to visit this function from a variant. Co-authored-by: stickies-v --- src/rpc/mempool.cpp | 4 ++-- src/test/txvalidation_tests.cpp | 6 +++--- src/txmempool.cpp | 8 ++++---- src/txmempool.h | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/rpc/mempool.cpp b/src/rpc/mempool.cpp index e9bae93642c..8538c764794 100644 --- a/src/rpc/mempool.cpp +++ b/src/rpc/mempool.cpp @@ -523,12 +523,12 @@ static RPCHelpMan getmempooldescendants() if (!request.params[1].isNull()) fVerbose = request.params[1].get_bool(); - uint256 hash = ParseHashV(request.params[0], "parameter 1"); + Txid txid{Txid::FromUint256(ParseHashV(request.params[0], "parameter 1"))}; const CTxMemPool& mempool = EnsureAnyMemPool(request.context); LOCK(mempool.cs); - const auto it{mempool.GetIter(hash)}; + const auto it{mempool.GetIter(txid)}; if (!it) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool"); } diff --git a/src/test/txvalidation_tests.cpp b/src/test/txvalidation_tests.cpp index 83d6c246849..d7f78443a88 100644 --- a/src/test/txvalidation_tests.cpp +++ b/src/test/txvalidation_tests.cpp @@ -304,7 +304,7 @@ BOOST_FIXTURE_TEST_CASE(version3_tests, RegTestingSetup) Package package_v3_v2{mempool_tx_v3, tx_v2_from_v3}; BOOST_CHECK_EQUAL(*PackageTRUCChecks(tx_v2_from_v3, GetVirtualTransactionSize(*tx_v2_from_v3), package_v3_v2, empty_ancestors), expected_error_str); - CTxMemPool::setEntries entries_mempool_v3{pool.GetIter(mempool_tx_v3->GetHash().ToUint256()).value()}; + CTxMemPool::setEntries entries_mempool_v3{pool.GetIter(mempool_tx_v3->GetHash()).value()}; BOOST_CHECK_EQUAL(*PackageTRUCChecks(tx_v2_from_v3, GetVirtualTransactionSize(*tx_v2_from_v3), {tx_v2_from_v3}, entries_mempool_v3), expected_error_str); // mempool_tx_v3 mempool_tx_v2 @@ -339,7 +339,7 @@ BOOST_FIXTURE_TEST_CASE(version3_tests, RegTestingSetup) Package package_v2_v3{mempool_tx_v2, tx_v3_from_v2}; BOOST_CHECK_EQUAL(*PackageTRUCChecks(tx_v3_from_v2, GetVirtualTransactionSize(*tx_v3_from_v2), package_v2_v3, empty_ancestors), expected_error_str); - CTxMemPool::setEntries entries_mempool_v2{pool.GetIter(mempool_tx_v2->GetHash().ToUint256()).value()}; + CTxMemPool::setEntries entries_mempool_v2{pool.GetIter(mempool_tx_v2->GetHash()).value()}; BOOST_CHECK_EQUAL(*PackageTRUCChecks(tx_v3_from_v2, GetVirtualTransactionSize(*tx_v3_from_v2), {tx_v3_from_v2}, entries_mempool_v2), expected_error_str); // mempool_tx_v3 mempool_tx_v2 @@ -536,7 +536,7 @@ BOOST_FIXTURE_TEST_CASE(version3_tests, RegTestingSetup) // Configuration where parent already has 2 other children in mempool (no sibling eviction allowed). This may happen as the result of a reorg. AddToMempool(pool, entry.FromTx(tx_v3_child2)); auto tx_v3_child3 = make_tx({COutPoint{mempool_tx_v3->GetHash(), 24}}, /*version=*/3); - auto entry_mempool_parent = pool.GetIter(mempool_tx_v3->GetHash().ToUint256()).value(); + auto entry_mempool_parent = pool.GetIter(mempool_tx_v3->GetHash()).value(); BOOST_CHECK_EQUAL(entry_mempool_parent->GetCountWithDescendants(), 3); auto ancestors_2siblings{pool.CalculateMemPoolAncestors(entry.FromTx(tx_v3_child3), m_limits)}; diff --git a/src/txmempool.cpp b/src/txmempool.cpp index 3a5a3fb306d..36bd58bfbff 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -154,7 +154,7 @@ void CTxMemPool::UpdateTransactionsFromBlock(const std::vector& vHashes for (const auto& txid : descendants_to_remove) { // This txid may have been removed already in a prior call to removeRecursive. // Therefore we ensure it is not yet removed already. - if (const std::optional txiter = GetIter(txid)) { + if (const std::optional txiter = GetIter(Txid::FromUint256(txid))) { removeRecursive((*txiter)->GetTx(), MemPoolRemovalReason::SIZELIMIT); } } @@ -984,9 +984,9 @@ const CTransaction* CTxMemPool::GetConflictTx(const COutPoint& prevout) const return it == mapNextTx.end() ? nullptr : it->second; } -std::optional CTxMemPool::GetIter(const uint256& txid) const +std::optional CTxMemPool::GetIter(const Txid& txid) const { - auto it = mapTx.find(txid); + auto it = mapTx.find(txid.ToUint256()); if (it != mapTx.end()) return it; return std::nullopt; } @@ -1007,7 +1007,7 @@ std::vector CTxMemPool::GetIterVec(const std::vector ret; ret.reserve(txids.size()); for (const auto& txid : txids) { - const auto it{GetIter(txid)}; + const auto it{GetIter(Txid::FromUint256(txid))}; if (!it) return {}; ret.push_back(*it); } diff --git a/src/txmempool.h b/src/txmempool.h index 10acb2aa22f..4bce0d43ab8 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -498,7 +498,7 @@ public: const CTransaction* GetConflictTx(const COutPoint& prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs); /** Returns an iterator to the given hash, if found */ - std::optional GetIter(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs); + std::optional GetIter(const Txid& txid) 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,