From 1a0877396b5f922b301ba19a88df45328708e458 Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Fri, 18 Aug 2023 20:52:56 +0200 Subject: [PATCH] [refactor] Make txmempool's GetSortedDepthAndScore static This removes the need for defining its complex boost multi index return type in the header. The context of this change is an effort towards removing the boost multi index includes from the header tree. The goal is that the experimental `bitcoin-chainstate` binary can be compiled without requiring boost includes. --- src/txmempool.cpp | 69 ++++++++++++++++++++++++----------------------- src/txmempool.h | 2 -- 2 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/txmempool.cpp b/src/txmempool.cpp index 06449781799e56..fbb4ebbf572486 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -675,6 +675,37 @@ void CTxMemPool::removeForBlock(const std::vector& vtx, unsigne blockSinceLastRollingFeeBump = true; } +namespace { +class DepthAndScoreComparator +{ +public: + bool operator()(const CTxMemPool::indexed_transaction_set::const_iterator& a, const CTxMemPool::indexed_transaction_set::const_iterator& b) + { + uint64_t counta = a->GetCountWithAncestors(); + uint64_t countb = b->GetCountWithAncestors(); + if (counta == countb) { + return CompareTxMemPoolEntryByScore()(*a, *b); + } + return counta < countb; + } +}; +} // namespace + +static std::vector GetSortedDepthAndScore( + const std::unique_ptr& mapTx, RecursiveMutex& cs) EXCLUSIVE_LOCKS_REQUIRED(cs) +{ + std::vector iters; + AssertLockHeld(cs); + + iters.reserve(mapTx->impl.size()); + + for (MemPoolMultiIndex::indexed_transaction_set::iterator mi = mapTx->impl.begin(); mi != mapTx->impl.end(); ++mi) { + iters.push_back(mi); + } + std::sort(iters.begin(), iters.end(), DepthAndScoreComparator()); + return iters; +} + void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendheight) const { if (m_check_ratio == 0) return; @@ -692,7 +723,7 @@ void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendhei CCoinsViewCache mempoolDuplicate(const_cast(&active_coins_tip)); - for (const auto& it : GetSortedDepthAndScore()) { + for (const auto& it : GetSortedDepthAndScore(mapTx, cs)) { checkTotal += it->GetTxSize(); check_total_fee += it->GetFee(); innerUsage += it->DynamicMemoryUsage(); @@ -800,40 +831,10 @@ bool CTxMemPool::CompareDepthAndScore(const uint256& hasha, const uint256& hashb return counta < countb; } -namespace { -class DepthAndScoreComparator -{ -public: - bool operator()(const CTxMemPool::indexed_transaction_set::const_iterator& a, const CTxMemPool::indexed_transaction_set::const_iterator& b) - { - uint64_t counta = a->GetCountWithAncestors(); - uint64_t countb = b->GetCountWithAncestors(); - if (counta == countb) { - return CompareTxMemPoolEntryByScore()(*a, *b); - } - return counta < countb; - } -}; -} // namespace - -std::vector CTxMemPool::GetSortedDepthAndScore() const -{ - std::vector iters; - AssertLockHeld(cs); - - iters.reserve(mapTx->impl.size()); - - for (indexed_transaction_set::iterator mi = mapTx->impl.begin(); mi != mapTx->impl.end(); ++mi) { - iters.push_back(mi); - } - std::sort(iters.begin(), iters.end(), DepthAndScoreComparator()); - return iters; -} - void CTxMemPool::queryHashes(std::vector& vtxid) const { LOCK(cs); - auto iters = GetSortedDepthAndScore(); + auto iters = GetSortedDepthAndScore(mapTx, cs); vtxid.clear(); vtxid.reserve(mapTx->impl.size()); @@ -853,7 +854,7 @@ std::vector CTxMemPool::entryAll() const std::vector ret; ret.reserve(mapTx->impl.size()); - for (const auto& it : GetSortedDepthAndScore()) { + for (const auto& it : GetSortedDepthAndScore(mapTx, cs)) { ret.emplace_back(*it); } return ret; @@ -862,7 +863,7 @@ std::vector CTxMemPool::entryAll() const std::vector CTxMemPool::infoAll() const { LOCK(cs); - auto iters = GetSortedDepthAndScore(); + auto iters = GetSortedDepthAndScore(mapTx, cs); std::vector ret; ret.reserve(mapTx->impl.size()); diff --git a/src/txmempool.h b/src/txmempool.h index 4108ec0a41681a..3e51c0dd409114 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -220,8 +220,6 @@ class CTxMemPool void UpdateParent(const CTxMemPoolEntry& entry, const CTxMemPoolEntry& parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs); void UpdateChild(const CTxMemPoolEntry& entry, const CTxMemPoolEntry& child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs); - std::vector GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs); - /** * Track locally submitted transactions to periodically retry initial broadcast. */