Skip to content

Commit 97dd1c7

Browse files
committed
MOVEONLY: add helper function for calculating ancestors and checking limits
1 parent f95bbf5 commit 97dd1c7

File tree

2 files changed

+62
-26
lines changed

2 files changed

+62
-26
lines changed

src/txmempool.cpp

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -150,33 +150,15 @@ void CTxMemPool::UpdateTransactionsFromBlock(const std::vector<uint256> &vHashes
150150
UpdateForDescendants(it, mapMemPoolDescendantsToUpdate, setAlreadyIncluded);
151151
}
152152
}
153-
154-
bool CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, setEntries &setAncestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString, bool fSearchForParents /* = true */) const
153+
bool CTxMemPool::CalculateAncestorsAndCheckLimits(const CTxMemPoolEntry& entry,
154+
setEntries& setAncestors,
155+
CTxMemPoolEntry::Parents &staged_ancestors,
156+
uint64_t limitAncestorCount,
157+
uint64_t limitAncestorSize,
158+
uint64_t limitDescendantCount,
159+
uint64_t limitDescendantSize,
160+
std::string &errString) const
155161
{
156-
CTxMemPoolEntry::Parents staged_ancestors;
157-
const CTransaction &tx = entry.GetTx();
158-
159-
if (fSearchForParents) {
160-
// Get parents of this transaction that are in the mempool
161-
// GetMemPoolParents() is only valid for entries in the mempool, so we
162-
// iterate mapTx to find parents.
163-
for (unsigned int i = 0; i < tx.vin.size(); i++) {
164-
std::optional<txiter> piter = GetIter(tx.vin[i].prevout.hash);
165-
if (piter) {
166-
staged_ancestors.insert(**piter);
167-
if (staged_ancestors.size() + 1 > limitAncestorCount) {
168-
errString = strprintf("too many unconfirmed parents [limit: %u]", limitAncestorCount);
169-
return false;
170-
}
171-
}
172-
}
173-
} else {
174-
// If we're not searching for parents, we require this to be an
175-
// entry in the mempool already.
176-
txiter it = mapTx.iterator_to(entry);
177-
staged_ancestors = it->GetMemPoolParentsConst();
178-
}
179-
180162
size_t totalSizeWithAncestors = entry.GetTxSize();
181163

182164
while (!staged_ancestors.empty()) {
@@ -216,6 +198,44 @@ bool CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, setEntr
216198
return true;
217199
}
218200

201+
bool CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry,
202+
setEntries &setAncestors,
203+
uint64_t limitAncestorCount,
204+
uint64_t limitAncestorSize,
205+
uint64_t limitDescendantCount,
206+
uint64_t limitDescendantSize,
207+
std::string &errString,
208+
bool fSearchForParents /* = true */) const
209+
{
210+
CTxMemPoolEntry::Parents staged_ancestors;
211+
const CTransaction &tx = entry.GetTx();
212+
213+
if (fSearchForParents) {
214+
// Get parents of this transaction that are in the mempool
215+
// GetMemPoolParents() is only valid for entries in the mempool, so we
216+
// iterate mapTx to find parents.
217+
for (unsigned int i = 0; i < tx.vin.size(); i++) {
218+
std::optional<txiter> piter = GetIter(tx.vin[i].prevout.hash);
219+
if (piter) {
220+
staged_ancestors.insert(**piter);
221+
if (staged_ancestors.size() + 1 > limitAncestorCount) {
222+
errString = strprintf("too many unconfirmed parents [limit: %u]", limitAncestorCount);
223+
return false;
224+
}
225+
}
226+
}
227+
} else {
228+
// If we're not searching for parents, we require this to already be an
229+
// entry in the mempool and use the entry's cached parents.
230+
txiter it = mapTx.iterator_to(entry);
231+
staged_ancestors = it->GetMemPoolParentsConst();
232+
}
233+
234+
return CalculateAncestorsAndCheckLimits(entry, setAncestors, staged_ancestors,
235+
limitAncestorCount, limitAncestorSize,
236+
limitDescendantCount, limitDescendantSize, errString);
237+
}
238+
219239
void CTxMemPool::UpdateAncestorsOf(bool add, txiter it, setEntries &setAncestors)
220240
{
221241
CTxMemPoolEntry::Parents parents = it->GetMemPoolParents();

src/txmempool.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,22 @@ class CTxMemPool
585585
*/
586586
std::set<uint256> m_unbroadcast_txids GUARDED_BY(cs);
587587

588+
589+
/**
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.
594+
*/
595+
bool CalculateAncestorsAndCheckLimits(const CTxMemPoolEntry& entry,
596+
setEntries& setAncestors,
597+
CTxMemPoolEntry::Parents &staged_ancestors,
598+
uint64_t limitAncestorCount,
599+
uint64_t limitAncestorSize,
600+
uint64_t limitDescendantCount,
601+
uint64_t limitDescendantSize,
602+
std::string &errString) const EXCLUSIVE_LOCKS_REQUIRED(cs);
603+
588604
public:
589605
indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
590606
std::map<uint256, CAmount> mapDeltas GUARDED_BY(cs);

0 commit comments

Comments
 (0)