Skip to content

Commit a08d76b

Browse files
committed
mempool: Calculate descendant maximum thoroughly
1 parent 6d35683 commit a08d76b

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/txmempool.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,14 +1056,25 @@ void CTxMemPool::TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpends
10561056
}
10571057

10581058
uint64_t CTxMemPool::CalculateDescendantMaximum(txiter entry) const {
1059-
// find top parent
1060-
txiter top = entry;
1061-
for (;;) {
1062-
const setEntries& parents = GetMemPoolParents(top);
1063-
if (parents.size() == 0) break;
1064-
top = *parents.begin();
1059+
// find parent with highest descendant count
1060+
std::vector<txiter> candidates;
1061+
setEntries counted;
1062+
candidates.push_back(entry);
1063+
uint64_t maximum = 0;
1064+
while (candidates.size()) {
1065+
txiter candidate = candidates.back();
1066+
candidates.pop_back();
1067+
if (!counted.insert(candidate).second) continue;
1068+
const setEntries& parents = GetMemPoolParents(candidate);
1069+
if (parents.size() == 0) {
1070+
maximum = std::max(maximum, candidate->GetCountWithDescendants());
1071+
} else {
1072+
for (txiter i : parents) {
1073+
candidates.push_back(i);
1074+
}
1075+
}
10651076
}
1066-
return top->GetCountWithDescendants();
1077+
return maximum;
10671078
}
10681079

10691080
void CTxMemPool::GetTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) const {

0 commit comments

Comments
 (0)