Skip to content

Commit f551841

Browse files
committed
[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.
1 parent 97dd1c7 commit f551841

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/txmempool.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,18 @@ void CTxMemPool::UpdateTransactionsFromBlock(const std::vector<uint256> &vHashes
150150
UpdateForDescendants(it, mapMemPoolDescendantsToUpdate, setAlreadyIncluded);
151151
}
152152
}
153-
bool CTxMemPool::CalculateAncestorsAndCheckLimits(const CTxMemPoolEntry& entry,
153+
154+
bool CTxMemPool::CalculateAncestorsAndCheckLimits(size_t entry_size,
155+
size_t entry_count,
154156
setEntries& setAncestors,
155-
CTxMemPoolEntry::Parents &staged_ancestors,
157+
CTxMemPoolEntry::Parents& staged_ancestors,
156158
uint64_t limitAncestorCount,
157159
uint64_t limitAncestorSize,
158160
uint64_t limitDescendantCount,
159161
uint64_t limitDescendantSize,
160162
std::string &errString) const
161163
{
162-
size_t totalSizeWithAncestors = entry.GetTxSize();
164+
size_t totalSizeWithAncestors = entry_size;
163165

164166
while (!staged_ancestors.empty()) {
165167
const CTxMemPoolEntry& stage = staged_ancestors.begin()->get();
@@ -169,10 +171,10 @@ bool CTxMemPool::CalculateAncestorsAndCheckLimits(const CTxMemPoolEntry& entry,
169171
staged_ancestors.erase(stage);
170172
totalSizeWithAncestors += stageit->GetTxSize();
171173

172-
if (stageit->GetSizeWithDescendants() + entry.GetTxSize() > limitDescendantSize) {
174+
if (stageit->GetSizeWithDescendants() + entry_size > limitDescendantSize) {
173175
errString = strprintf("exceeds descendant size limit for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantSize);
174176
return false;
175-
} else if (stageit->GetCountWithDescendants() + 1 > limitDescendantCount) {
177+
} else if (stageit->GetCountWithDescendants() + entry_count > limitDescendantCount) {
176178
errString = strprintf("too many descendants for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantCount);
177179
return false;
178180
} else if (totalSizeWithAncestors > limitAncestorSize) {
@@ -188,7 +190,7 @@ bool CTxMemPool::CalculateAncestorsAndCheckLimits(const CTxMemPoolEntry& entry,
188190
if (setAncestors.count(parent_it) == 0) {
189191
staged_ancestors.insert(parent);
190192
}
191-
if (staged_ancestors.size() + setAncestors.size() + 1 > limitAncestorCount) {
193+
if (staged_ancestors.size() + setAncestors.size() + entry_count > limitAncestorCount) {
192194
errString = strprintf("too many unconfirmed ancestors [limit: %u]", limitAncestorCount);
193195
return false;
194196
}
@@ -231,7 +233,8 @@ bool CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry,
231233
staged_ancestors = it->GetMemPoolParentsConst();
232234
}
233235

234-
return CalculateAncestorsAndCheckLimits(entry, setAncestors, staged_ancestors,
236+
return CalculateAncestorsAndCheckLimits(entry.GetTxSize(), /* entry_count */ 1,
237+
setAncestors, staged_ancestors,
235238
limitAncestorCount, limitAncestorSize,
236239
limitDescendantCount, limitDescendantSize, errString);
237240
}

src/txmempool.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -587,12 +587,15 @@ class CTxMemPool
587587

588588

589589
/**
590-
* Helper function to populate setAncestors with all the ancestors of entry and apply ancestor
591-
* and descendant limits.
592-
* param@[out] setAncestors Will be populated with all mempool ancestors of entry.
593-
* param@[in] staged_ancestors Should contain mempool parents of entry.
590+
* Helper function to calculate all in-mempool ancestors of staged_ancestors and apply ancestor
591+
* and descendant limits (including staged_ancestors thsemselves, entry_size and entry_count).
592+
* param@[in] entry_size Virtual size to include in the limits.
593+
* param@[in] entry_count How many entries to include in the limits.
594+
* param@[in] staged_ancestors Should contain entries in the mempool.
595+
* param@[out] setAncestors Will be populated with all mempool ancestors.
594596
*/
595-
bool CalculateAncestorsAndCheckLimits(const CTxMemPoolEntry& entry,
597+
bool CalculateAncestorsAndCheckLimits(size_t entry_size,
598+
size_t entry_count,
596599
setEntries& setAncestors,
597600
CTxMemPoolEntry::Parents &staged_ancestors,
598601
uint64_t limitAncestorCount,

0 commit comments

Comments
 (0)