Skip to content

Commit 3a86f24

Browse files
committed
refactor: mempool: use CTxMempool::Limits
Simplifies function signatures by removing repetition of all the ancestor/descendant limits, and increases readability by being more verbose by naming the limits, while still reducing the LoC.
1 parent b85af25 commit 3a86f24

File tree

7 files changed

+59
-85
lines changed

7 files changed

+59
-85
lines changed

src/node/interfaces.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,7 @@ class ChainImpl : public Chain
660660
std::string unused_error_string;
661661
LOCK(m_node.mempool->cs);
662662
return m_node.mempool->CalculateMemPoolAncestors(
663-
entry, ancestors, limits.ancestor_count, limits.ancestor_size_vbytes,
664-
limits.descendant_count, limits.descendant_size_vbytes, unused_error_string);
663+
entry, ancestors, limits, unused_error_string);
665664
}
666665
CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc) override
667666
{

src/node/miner.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,8 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
396396
}
397397

398398
CTxMemPool::setEntries ancestors;
399-
uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
400399
std::string dummy;
401-
mempool.CalculateMemPoolAncestors(*iter, ancestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
400+
mempool.CalculateMemPoolAncestors(*iter, ancestors, CTxMemPool::Limits::NoLimits(), dummy, false);
402401

403402
onlyUnconfirmed(ancestors);
404403
ancestors.insert(iter);

src/policy/rbf.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ RBFTransactionState IsRBFOptIn(const CTransaction& tx, const CTxMemPool& pool)
3636

3737
// If all the inputs have nSequence >= maxint-1, it still might be
3838
// signaled for RBF if any unconfirmed parents have signaled.
39-
uint64_t noLimit = std::numeric_limits<uint64_t>::max();
4039
std::string dummy;
4140
CTxMemPoolEntry entry = *pool.mapTx.find(tx.GetHash());
42-
pool.CalculateMemPoolAncestors(entry, ancestors, noLimit, noLimit, noLimit, noLimit, dummy, false);
41+
pool.CalculateMemPoolAncestors(entry, ancestors, CTxMemPool::Limits::NoLimits(), dummy, false);
4342

4443
for (CTxMemPool::txiter it : ancestors) {
4544
if (SignalsOptInRBF(it->GetTx())) {

src/rpc/mempool.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,8 @@ static RPCHelpMan getmempoolancestors()
449449
}
450450

451451
CTxMemPool::setEntries setAncestors;
452-
uint64_t noLimit = std::numeric_limits<uint64_t>::max();
453452
std::string dummy;
454-
mempool.CalculateMemPoolAncestors(*it, setAncestors, noLimit, noLimit, noLimit, noLimit, dummy, false);
453+
mempool.CalculateMemPoolAncestors(*it, setAncestors, CTxMemPool::Limits::NoLimits(), dummy, false);
455454

456455
if (!fVerbose) {
457456
UniValue o(UniValue::VARR);

src/txmempool.cpp

+21-36
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,7 @@ bool CTxMemPool::CalculateAncestorsAndCheckLimits(size_t entry_size,
187187
size_t entry_count,
188188
setEntries& setAncestors,
189189
CTxMemPoolEntry::Parents& staged_ancestors,
190-
uint64_t limitAncestorCount,
191-
uint64_t limitAncestorSize,
192-
uint64_t limitDescendantCount,
193-
uint64_t limitDescendantSize,
190+
const Limits& limits,
194191
std::string &errString) const
195192
{
196193
size_t totalSizeWithAncestors = entry_size;
@@ -203,14 +200,14 @@ bool CTxMemPool::CalculateAncestorsAndCheckLimits(size_t entry_size,
203200
staged_ancestors.erase(stage);
204201
totalSizeWithAncestors += stageit->GetTxSize();
205202

206-
if (stageit->GetSizeWithDescendants() + entry_size > limitDescendantSize) {
207-
errString = strprintf("exceeds descendant size limit for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantSize);
203+
if (stageit->GetSizeWithDescendants() + entry_size > static_cast<uint64_t>(limits.descendant_size_vbytes)) {
204+
errString = strprintf("exceeds descendant size limit for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limits.descendant_size_vbytes);
208205
return false;
209-
} else if (stageit->GetCountWithDescendants() + entry_count > limitDescendantCount) {
210-
errString = strprintf("too many descendants for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantCount);
206+
} else if (stageit->GetCountWithDescendants() + entry_count > static_cast<uint64_t>(limits.descendant_count)) {
207+
errString = strprintf("too many descendants for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limits.descendant_count);
211208
return false;
212-
} else if (totalSizeWithAncestors > limitAncestorSize) {
213-
errString = strprintf("exceeds ancestor size limit [limit: %u]", limitAncestorSize);
209+
} else if (totalSizeWithAncestors > static_cast<uint64_t>(limits.ancestor_size_vbytes)) {
210+
errString = strprintf("exceeds ancestor size limit [limit: %u]", limits.ancestor_size_vbytes);
214211
return false;
215212
}
216213

@@ -222,8 +219,8 @@ bool CTxMemPool::CalculateAncestorsAndCheckLimits(size_t entry_size,
222219
if (setAncestors.count(parent_it) == 0) {
223220
staged_ancestors.insert(parent);
224221
}
225-
if (staged_ancestors.size() + setAncestors.size() + entry_count > limitAncestorCount) {
226-
errString = strprintf("too many unconfirmed ancestors [limit: %u]", limitAncestorCount);
222+
if (staged_ancestors.size() + setAncestors.size() + entry_count > static_cast<uint64_t>(limits.ancestor_count)) {
223+
errString = strprintf("too many unconfirmed ancestors [limit: %u]", limits.ancestor_count);
227224
return false;
228225
}
229226
}
@@ -233,10 +230,7 @@ bool CTxMemPool::CalculateAncestorsAndCheckLimits(size_t entry_size,
233230
}
234231

235232
bool CTxMemPool::CheckPackageLimits(const Package& package,
236-
uint64_t limitAncestorCount,
237-
uint64_t limitAncestorSize,
238-
uint64_t limitDescendantCount,
239-
uint64_t limitDescendantSize,
233+
const Limits& limits,
240234
std::string &errString) const
241235
{
242236
CTxMemPoolEntry::Parents staged_ancestors;
@@ -247,8 +241,8 @@ bool CTxMemPool::CheckPackageLimits(const Package& package,
247241
std::optional<txiter> piter = GetIter(input.prevout.hash);
248242
if (piter) {
249243
staged_ancestors.insert(**piter);
250-
if (staged_ancestors.size() + package.size() > limitAncestorCount) {
251-
errString = strprintf("too many unconfirmed parents [limit: %u]", limitAncestorCount);
244+
if (staged_ancestors.size() + package.size() > static_cast<uint64_t>(limits.ancestor_count)) {
245+
errString = strprintf("too many unconfirmed parents [limit: %u]", limits.ancestor_count);
252246
return false;
253247
}
254248
}
@@ -260,19 +254,15 @@ bool CTxMemPool::CheckPackageLimits(const Package& package,
260254
setEntries setAncestors;
261255
const auto ret = CalculateAncestorsAndCheckLimits(total_size, package.size(),
262256
setAncestors, staged_ancestors,
263-
limitAncestorCount, limitAncestorSize,
264-
limitDescendantCount, limitDescendantSize, errString);
257+
limits, errString);
265258
// It's possible to overestimate the ancestor/descendant totals.
266259
if (!ret) errString.insert(0, "possibly ");
267260
return ret;
268261
}
269262

270263
bool CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry,
271264
setEntries &setAncestors,
272-
uint64_t limitAncestorCount,
273-
uint64_t limitAncestorSize,
274-
uint64_t limitDescendantCount,
275-
uint64_t limitDescendantSize,
265+
const Limits& limits,
276266
std::string &errString,
277267
bool fSearchForParents /* = true */) const
278268
{
@@ -287,8 +277,8 @@ bool CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry,
287277
std::optional<txiter> piter = GetIter(tx.vin[i].prevout.hash);
288278
if (piter) {
289279
staged_ancestors.insert(**piter);
290-
if (staged_ancestors.size() + 1 > limitAncestorCount) {
291-
errString = strprintf("too many unconfirmed parents [limit: %u]", limitAncestorCount);
280+
if (staged_ancestors.size() + 1 > static_cast<uint64_t>(limits.ancestor_count)) {
281+
errString = strprintf("too many unconfirmed parents [limit: %u]", limits.ancestor_count);
292282
return false;
293283
}
294284
}
@@ -302,8 +292,7 @@ bool CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry,
302292

303293
return CalculateAncestorsAndCheckLimits(entry.GetTxSize(), /*entry_count=*/1,
304294
setAncestors, staged_ancestors,
305-
limitAncestorCount, limitAncestorSize,
306-
limitDescendantCount, limitDescendantSize, errString);
295+
limits, errString);
307296
}
308297

309298
void CTxMemPool::UpdateAncestorsOf(bool add, txiter it, setEntries &setAncestors)
@@ -347,7 +336,6 @@ void CTxMemPool::UpdateForRemoveFromMempool(const setEntries &entriesToRemove, b
347336
{
348337
// For each entry, walk back all ancestors and decrement size associated with this
349338
// transaction
350-
const uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
351339
if (updateDescendants) {
352340
// updateDescendants should be true whenever we're not recursively
353341
// removing a tx and all its descendants, eg when a transaction is
@@ -390,7 +378,7 @@ void CTxMemPool::UpdateForRemoveFromMempool(const setEntries &entriesToRemove, b
390378
// mempool parents we'd calculate by searching, and it's important that
391379
// we use the cached notion of ancestor transactions as the set of
392380
// things to update for removal.
393-
CalculateMemPoolAncestors(entry, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
381+
CalculateMemPoolAncestors(entry, setAncestors, Limits::NoLimits(), dummy, false);
394382
// Note that UpdateAncestorsOf severs the child links that point to
395383
// removeIt in the entries for the parents of removeIt.
396384
UpdateAncestorsOf(false, removeIt, setAncestors);
@@ -744,9 +732,8 @@ void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendhei
744732
assert(std::equal(setParentCheck.begin(), setParentCheck.end(), it->GetMemPoolParentsConst().begin(), comp));
745733
// Verify ancestor state is correct.
746734
setEntries setAncestors;
747-
uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
748735
std::string dummy;
749-
CalculateMemPoolAncestors(*it, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy);
736+
CalculateMemPoolAncestors(*it, setAncestors, Limits::NoLimits(), dummy);
750737
uint64_t nCountCheck = setAncestors.size() + 1;
751738
uint64_t nSizeCheck = it->GetTxSize();
752739
CAmount nFeesCheck = it->GetModifiedFee();
@@ -908,9 +895,8 @@ void CTxMemPool::PrioritiseTransaction(const uint256& hash, const CAmount& nFeeD
908895
mapTx.modify(it, [&nFeeDelta](CTxMemPoolEntry& e) { e.UpdateModifiedFee(nFeeDelta); });
909896
// Now update all ancestors' modified fees with descendants
910897
setEntries setAncestors;
911-
uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
912898
std::string dummy;
913-
CalculateMemPoolAncestors(*it, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
899+
CalculateMemPoolAncestors(*it, setAncestors, Limits::NoLimits(), dummy, false);
914900
for (txiter ancestorIt : setAncestors) {
915901
mapTx.modify(ancestorIt, [=](CTxMemPoolEntry& e){ e.UpdateDescendantState(0, nFeeDelta, 0);});
916902
}
@@ -1049,9 +1035,8 @@ int CTxMemPool::Expire(std::chrono::seconds time)
10491035
void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry, bool validFeeEstimate)
10501036
{
10511037
setEntries setAncestors;
1052-
uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
10531038
std::string dummy;
1054-
CalculateMemPoolAncestors(entry, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy);
1039+
CalculateMemPoolAncestors(entry, setAncestors, Limits::NoLimits(), dummy);
10551040
return addUnchecked(entry, setAncestors, validFeeEstimate);
10561041
}
10571042

src/txmempool.h

+12-20
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,8 @@ class CTxMemPool
526526

527527
typedef std::set<txiter, CompareIteratorByHash> setEntries;
528528

529+
using Limits = kernel::MemPoolLimits;
530+
529531
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
530532
private:
531533
typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
@@ -554,10 +556,7 @@ class CTxMemPool
554556
size_t entry_count,
555557
setEntries& setAncestors,
556558
CTxMemPoolEntry::Parents &staged_ancestors,
557-
uint64_t limitAncestorCount,
558-
uint64_t limitAncestorSize,
559-
uint64_t limitDescendantCount,
560-
uint64_t limitDescendantSize,
559+
const Limits& limits,
561560
std::string &errString) const EXCLUSIVE_LOCKS_REQUIRED(cs);
562561

563562
public:
@@ -576,8 +575,6 @@ class CTxMemPool
576575
const bool m_require_standard;
577576
const bool m_full_rbf;
578577

579-
using Limits = kernel::MemPoolLimits;
580-
581578
const Limits m_limits;
582579

583580
/** Create a new CTxMemPool.
@@ -670,36 +667,31 @@ class CTxMemPool
670667

671668
/** Try to calculate all in-mempool ancestors of entry.
672669
* (these are all calculated including the tx itself)
673-
* limitAncestorCount = max number of ancestors
674-
* limitAncestorSize = max size of ancestors
675-
* limitDescendantCount = max number of descendants any ancestor can have
676-
* limitDescendantSize = max size of descendants any ancestor can have
670+
* limits = maximum number and size of ancestors and descendants
677671
* errString = populated with error reason if any limits are hit
678672
* fSearchForParents = whether to search a tx's vin for in-mempool parents, or
679673
* look up parents from mapLinks. Must be true for entries not in the mempool
680674
*/
681-
bool 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 EXCLUSIVE_LOCKS_REQUIRED(cs);
675+
bool CalculateMemPoolAncestors(const CTxMemPoolEntry& entry,
676+
setEntries& setAncestors,
677+
const Limits& limits,
678+
std::string& errString,
679+
bool fSearchForParents = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
682680

683681
/** Calculate all in-mempool ancestors of a set of transactions not already in the mempool and
684682
* check ancestor and descendant limits. Heuristics are used to estimate the ancestor and
685683
* descendant count of all entries if the package were to be added to the mempool. The limits
686684
* are applied to the union of all package transactions. For example, if the package has 3
687-
* transactions and limitAncestorCount = 25, the union of all 3 sets of ancestors (including the
685+
* transactions and limits.ancestor_count = 25, the union of all 3 sets of ancestors (including the
688686
* transactions themselves) must be <= 22.
689687
* @param[in] package Transaction package being evaluated for acceptance
690688
* to mempool. The transactions need not be direct
691689
* ancestors/descendants of each other.
692-
* @param[in] limitAncestorCount Max number of txns including ancestors.
693-
* @param[in] limitAncestorSize Max virtual size including ancestors.
694-
* @param[in] limitDescendantCount Max number of txns including descendants.
695-
* @param[in] limitDescendantSize Max virtual size including descendants.
690+
* @param[in] limits Maximum number and size of ancestors and descendants
696691
* @param[out] errString Populated with error reason if a limit is hit.
697692
*/
698693
bool CheckPackageLimits(const Package& package,
699-
uint64_t limitAncestorCount,
700-
uint64_t limitAncestorSize,
701-
uint64_t limitDescendantCount,
702-
uint64_t limitDescendantSize,
694+
const Limits& limits,
703695
std::string &errString) const EXCLUSIVE_LOCKS_REQUIRED(cs);
704696

705697
/** Populate setDescendants with all in-mempool descendants of hash.

0 commit comments

Comments
 (0)