Skip to content

Commit

Permalink
[refactor] Make txmempool's GetSortedDepthAndScore static
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TheCharlatan committed Nov 14, 2023
1 parent 1fbd9d7 commit 1a08773
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
69 changes: 35 additions & 34 deletions src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,37 @@ void CTxMemPool::removeForBlock(const std::vector<CTransactionRef>& 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<MemPoolMultiIndex::indexed_transaction_set::const_iterator> GetSortedDepthAndScore(
const std::unique_ptr<MemPoolMultiIndex::MapTxImpl>& mapTx, RecursiveMutex& cs) EXCLUSIVE_LOCKS_REQUIRED(cs)
{
std::vector<MemPoolMultiIndex::indexed_transaction_set::const_iterator> 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;
Expand All @@ -692,7 +723,7 @@ void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendhei

CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(&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();
Expand Down Expand Up @@ -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::indexed_transaction_set::const_iterator> CTxMemPool::GetSortedDepthAndScore() const
{
std::vector<indexed_transaction_set::const_iterator> 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<uint256>& vtxid) const
{
LOCK(cs);
auto iters = GetSortedDepthAndScore();
auto iters = GetSortedDepthAndScore(mapTx, cs);

vtxid.clear();
vtxid.reserve(mapTx->impl.size());
Expand All @@ -853,7 +854,7 @@ std::vector<CTxMemPoolEntryRef> CTxMemPool::entryAll() const

std::vector<CTxMemPoolEntryRef> ret;
ret.reserve(mapTx->impl.size());
for (const auto& it : GetSortedDepthAndScore()) {
for (const auto& it : GetSortedDepthAndScore(mapTx, cs)) {
ret.emplace_back(*it);
}
return ret;
Expand All @@ -862,7 +863,7 @@ std::vector<CTxMemPoolEntryRef> CTxMemPool::entryAll() const
std::vector<TxMempoolInfo> CTxMemPool::infoAll() const
{
LOCK(cs);
auto iters = GetSortedDepthAndScore();
auto iters = GetSortedDepthAndScore(mapTx, cs);

std::vector<TxMempoolInfo> ret;
ret.reserve(mapTx->impl.size());
Expand Down
2 changes: 0 additions & 2 deletions src/txmempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<MemPoolMultiIndex::const_txiter> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);

/**
* Track locally submitted transactions to periodically retry initial broadcast.
*/
Expand Down

0 comments on commit 1a08773

Please sign in to comment.