Skip to content

Commit fcf92fd

Browse files
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 <[email protected]>
1 parent 11d28f2 commit fcf92fd

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

src/rpc/mempool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,12 @@ static RPCHelpMan getmempooldescendants()
523523
if (!request.params[1].isNull())
524524
fVerbose = request.params[1].get_bool();
525525

526-
uint256 hash = ParseHashV(request.params[0], "parameter 1");
526+
Txid txid{Txid::FromUint256(ParseHashV(request.params[0], "parameter 1"))};
527527

528528
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
529529
LOCK(mempool.cs);
530530

531-
const auto it{mempool.GetIter(hash)};
531+
const auto it{mempool.GetIter(txid)};
532532
if (!it) {
533533
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
534534
}

src/test/txvalidation_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ BOOST_FIXTURE_TEST_CASE(version3_tests, RegTestingSetup)
304304

305305
Package package_v3_v2{mempool_tx_v3, tx_v2_from_v3};
306306
BOOST_CHECK_EQUAL(*PackageTRUCChecks(tx_v2_from_v3, GetVirtualTransactionSize(*tx_v2_from_v3), package_v3_v2, empty_ancestors), expected_error_str);
307-
CTxMemPool::setEntries entries_mempool_v3{pool.GetIter(mempool_tx_v3->GetHash().ToUint256()).value()};
307+
CTxMemPool::setEntries entries_mempool_v3{pool.GetIter(mempool_tx_v3->GetHash()).value()};
308308
BOOST_CHECK_EQUAL(*PackageTRUCChecks(tx_v2_from_v3, GetVirtualTransactionSize(*tx_v2_from_v3), {tx_v2_from_v3}, entries_mempool_v3), expected_error_str);
309309

310310
// mempool_tx_v3 mempool_tx_v2
@@ -339,7 +339,7 @@ BOOST_FIXTURE_TEST_CASE(version3_tests, RegTestingSetup)
339339

340340
Package package_v2_v3{mempool_tx_v2, tx_v3_from_v2};
341341
BOOST_CHECK_EQUAL(*PackageTRUCChecks(tx_v3_from_v2, GetVirtualTransactionSize(*tx_v3_from_v2), package_v2_v3, empty_ancestors), expected_error_str);
342-
CTxMemPool::setEntries entries_mempool_v2{pool.GetIter(mempool_tx_v2->GetHash().ToUint256()).value()};
342+
CTxMemPool::setEntries entries_mempool_v2{pool.GetIter(mempool_tx_v2->GetHash()).value()};
343343
BOOST_CHECK_EQUAL(*PackageTRUCChecks(tx_v3_from_v2, GetVirtualTransactionSize(*tx_v3_from_v2), {tx_v3_from_v2}, entries_mempool_v2), expected_error_str);
344344

345345
// mempool_tx_v3 mempool_tx_v2
@@ -536,7 +536,7 @@ BOOST_FIXTURE_TEST_CASE(version3_tests, RegTestingSetup)
536536
// Configuration where parent already has 2 other children in mempool (no sibling eviction allowed). This may happen as the result of a reorg.
537537
AddToMempool(pool, entry.FromTx(tx_v3_child2));
538538
auto tx_v3_child3 = make_tx({COutPoint{mempool_tx_v3->GetHash(), 24}}, /*version=*/3);
539-
auto entry_mempool_parent = pool.GetIter(mempool_tx_v3->GetHash().ToUint256()).value();
539+
auto entry_mempool_parent = pool.GetIter(mempool_tx_v3->GetHash()).value();
540540
BOOST_CHECK_EQUAL(entry_mempool_parent->GetCountWithDescendants(), 3);
541541
auto ancestors_2siblings{pool.CalculateMemPoolAncestors(entry.FromTx(tx_v3_child3), m_limits)};
542542

src/txmempool.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ void CTxMemPool::UpdateTransactionsFromBlock(const std::vector<uint256>& vHashes
154154
for (const auto& txid : descendants_to_remove) {
155155
// This txid may have been removed already in a prior call to removeRecursive.
156156
// Therefore we ensure it is not yet removed already.
157-
if (const std::optional<txiter> txiter = GetIter(txid)) {
157+
if (const std::optional<txiter> txiter = GetIter(Txid::FromUint256(txid))) {
158158
removeRecursive((*txiter)->GetTx(), MemPoolRemovalReason::SIZELIMIT);
159159
}
160160
}
@@ -984,9 +984,9 @@ const CTransaction* CTxMemPool::GetConflictTx(const COutPoint& prevout) const
984984
return it == mapNextTx.end() ? nullptr : it->second;
985985
}
986986

987-
std::optional<CTxMemPool::txiter> CTxMemPool::GetIter(const uint256& txid) const
987+
std::optional<CTxMemPool::txiter> CTxMemPool::GetIter(const Txid& txid) const
988988
{
989-
auto it = mapTx.find(txid);
989+
auto it = mapTx.find(txid.ToUint256());
990990
if (it != mapTx.end()) return it;
991991
return std::nullopt;
992992
}
@@ -1007,7 +1007,7 @@ std::vector<CTxMemPool::txiter> CTxMemPool::GetIterVec(const std::vector<uint256
10071007
std::vector<txiter> ret;
10081008
ret.reserve(txids.size());
10091009
for (const auto& txid : txids) {
1010-
const auto it{GetIter(txid)};
1010+
const auto it{GetIter(Txid::FromUint256(txid))};
10111011
if (!it) return {};
10121012
ret.push_back(*it);
10131013
}

src/txmempool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ class CTxMemPool
498498
const CTransaction* GetConflictTx(const COutPoint& prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs);
499499

500500
/** Returns an iterator to the given hash, if found */
501-
std::optional<txiter> GetIter(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
501+
std::optional<txiter> GetIter(const Txid& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
502502

503503
/** Translate a set of hashes into a set of pool iterators to avoid repeated lookups.
504504
* Does not require that all of the hashes correspond to actual transactions in the mempool,

0 commit comments

Comments
 (0)