From f551841d3ec080a2d7a7988c7b35088dff6c5830 Mon Sep 17 00:00:00 2001 From: glozow Date: Thu, 15 Jul 2021 07:09:22 +0100 Subject: [PATCH] [refactor] pass size/count instead of entry to CalculateAncestorsAndCheckLimits This does not change existing behavior. The ancestor/descendant limits are inclusive of the entries themselves, but CalculateAncestorsAndCheckLimits() does not need access to them. --- src/txmempool.cpp | 17 ++++++++++------- src/txmempool.h | 13 ++++++++----- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/txmempool.cpp b/src/txmempool.cpp index 53de2d26184..4a992bf2a44 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -150,16 +150,18 @@ void CTxMemPool::UpdateTransactionsFromBlock(const std::vector &vHashes UpdateForDescendants(it, mapMemPoolDescendantsToUpdate, setAlreadyIncluded); } } -bool CTxMemPool::CalculateAncestorsAndCheckLimits(const CTxMemPoolEntry& entry, + +bool CTxMemPool::CalculateAncestorsAndCheckLimits(size_t entry_size, + size_t entry_count, setEntries& setAncestors, - CTxMemPoolEntry::Parents &staged_ancestors, + CTxMemPoolEntry::Parents& staged_ancestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString) const { - size_t totalSizeWithAncestors = entry.GetTxSize(); + size_t totalSizeWithAncestors = entry_size; while (!staged_ancestors.empty()) { const CTxMemPoolEntry& stage = staged_ancestors.begin()->get(); @@ -169,10 +171,10 @@ bool CTxMemPool::CalculateAncestorsAndCheckLimits(const CTxMemPoolEntry& entry, staged_ancestors.erase(stage); totalSizeWithAncestors += stageit->GetTxSize(); - if (stageit->GetSizeWithDescendants() + entry.GetTxSize() > limitDescendantSize) { + if (stageit->GetSizeWithDescendants() + entry_size > limitDescendantSize) { errString = strprintf("exceeds descendant size limit for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantSize); return false; - } else if (stageit->GetCountWithDescendants() + 1 > limitDescendantCount) { + } else if (stageit->GetCountWithDescendants() + entry_count > limitDescendantCount) { errString = strprintf("too many descendants for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantCount); return false; } else if (totalSizeWithAncestors > limitAncestorSize) { @@ -188,7 +190,7 @@ bool CTxMemPool::CalculateAncestorsAndCheckLimits(const CTxMemPoolEntry& entry, if (setAncestors.count(parent_it) == 0) { staged_ancestors.insert(parent); } - if (staged_ancestors.size() + setAncestors.size() + 1 > limitAncestorCount) { + if (staged_ancestors.size() + setAncestors.size() + entry_count > limitAncestorCount) { errString = strprintf("too many unconfirmed ancestors [limit: %u]", limitAncestorCount); return false; } @@ -231,7 +233,8 @@ bool CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, staged_ancestors = it->GetMemPoolParentsConst(); } - return CalculateAncestorsAndCheckLimits(entry, setAncestors, staged_ancestors, + return CalculateAncestorsAndCheckLimits(entry.GetTxSize(), /* entry_count */ 1, + setAncestors, staged_ancestors, limitAncestorCount, limitAncestorSize, limitDescendantCount, limitDescendantSize, errString); } diff --git a/src/txmempool.h b/src/txmempool.h index 76ca83c25c7..71345ffb5d6 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -587,12 +587,15 @@ private: /** - * Helper function to populate setAncestors with all the ancestors of entry and apply ancestor - * and descendant limits. - * param@[out] setAncestors Will be populated with all mempool ancestors of entry. - * param@[in] staged_ancestors Should contain mempool parents of entry. + * Helper function to calculate all in-mempool ancestors of staged_ancestors and apply ancestor + * and descendant limits (including staged_ancestors thsemselves, entry_size and entry_count). + * param@[in] entry_size Virtual size to include in the limits. + * param@[in] entry_count How many entries to include in the limits. + * param@[in] staged_ancestors Should contain entries in the mempool. + * param@[out] setAncestors Will be populated with all mempool ancestors. */ - bool CalculateAncestorsAndCheckLimits(const CTxMemPoolEntry& entry, + bool CalculateAncestorsAndCheckLimits(size_t entry_size, + size_t entry_count, setEntries& setAncestors, CTxMemPoolEntry::Parents &staged_ancestors, uint64_t limitAncestorCount,