[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.
This commit is contained in:
glozow committed 2021-08-05 12:37:28 +01:00
1 parent 97dd1c729d
commit f551841d3e
2 files changed
+18 -12

No files matched your search

+10 -7
View File
@@ -150,16 +150,18 @@ void CTxMemPool::UpdateTransactionsFromBlock(const std::vector<uint256> &vHashes
UpdateForDescendants(it, mapMemPoolDescendantsToUpdate, setAlreadyIncluded); UpdateForDescendants(it, mapMemPoolDescendantsToUpdate, setAlreadyIncluded);
} }
} }
bool CTxMemPool::CalculateAncestorsAndCheckLimits(const CTxMemPoolEntry& entry,
bool CTxMemPool::CalculateAncestorsAndCheckLimits(size_t entry_size,
size_t entry_count,
setEntries& setAncestors, setEntries& setAncestors,
CTxMemPoolEntry::Parents &staged_ancestors, CTxMemPoolEntry::Parents& staged_ancestors,
uint64_t limitAncestorCount, uint64_t limitAncestorCount,
uint64_t limitAncestorSize, uint64_t limitAncestorSize,
uint64_t limitDescendantCount, uint64_t limitDescendantCount,
uint64_t limitDescendantSize, uint64_t limitDescendantSize,
std::string &errString) const std::string &errString) const
{ {
size_t totalSizeWithAncestors = entry.GetTxSize(); size_t totalSizeWithAncestors = entry_size;
while (!staged_ancestors.empty()) { while (!staged_ancestors.empty()) {
const CTxMemPoolEntry& stage = staged_ancestors.begin()->get(); const CTxMemPoolEntry& stage = staged_ancestors.begin()->get();
@@ -169,10 +171,10 @@ bool CTxMemPool::CalculateAncestorsAndCheckLimits(const CTxMemPoolEntry& entry,
staged_ancestors.erase(stage); staged_ancestors.erase(stage);
totalSizeWithAncestors += stageit->GetTxSize(); 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); errString = strprintf("exceeds descendant size limit for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantSize);
return false; 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); errString = strprintf("too many descendants for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantCount);
return false; return false;
} else if (totalSizeWithAncestors > limitAncestorSize) { } else if (totalSizeWithAncestors > limitAncestorSize) {
@@ -188,7 +190,7 @@ bool CTxMemPool::CalculateAncestorsAndCheckLimits(const CTxMemPoolEntry& entry,
if (setAncestors.count(parent_it) == 0) { if (setAncestors.count(parent_it) == 0) {
staged_ancestors.insert(parent); 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); errString = strprintf("too many unconfirmed ancestors [limit: %u]", limitAncestorCount);
return false; return false;
} }
@@ -231,7 +233,8 @@ bool CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry,
staged_ancestors = it->GetMemPoolParentsConst(); staged_ancestors = it->GetMemPoolParentsConst();
} }
return CalculateAncestorsAndCheckLimits(entry, setAncestors, staged_ancestors, return CalculateAncestorsAndCheckLimits(entry.GetTxSize(), /* entry_count */ 1,
setAncestors, staged_ancestors,
limitAncestorCount, limitAncestorSize, limitAncestorCount, limitAncestorSize,
limitDescendantCount, limitDescendantSize, errString); limitDescendantCount, limitDescendantSize, errString);
} }
+8 -5
View File
@@ -587,12 +587,15 @@ private:
/** /**
* Helper function to populate setAncestors with all the ancestors of entry and apply ancestor * Helper function to calculate all in-mempool ancestors of staged_ancestors and apply ancestor
* and descendant limits. * and descendant limits (including staged_ancestors thsemselves, entry_size and entry_count).
* param@[out] setAncestors Will be populated with all mempool ancestors of entry. * param@[in] entry_size Virtual size to include in the limits.
* param@[in] staged_ancestors Should contain mempool parents of entry. * 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, setEntries& setAncestors,
CTxMemPoolEntry::Parents &staged_ancestors, CTxMemPoolEntry::Parents &staged_ancestors,
uint64_t limitAncestorCount, uint64_t limitAncestorCount,