Skip to content

Commit 5481f65

Browse files
committed
mempool: add AssumeCalculateMemPoolAncestors helper function
There are quite a few places that assume CalculateMemPoolAncestors will return a value without raising an error. This helper function adds logging (and Assume for debug builds) that ensures robustness but increases visibility in case of unexpected failures
1 parent f911bdf commit 5481f65

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/txmempool.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,20 @@ util::Result<CTxMemPool::setEntries> CTxMemPool::CalculateMemPoolAncestors(
254254
limits);
255255
}
256256

257+
CTxMemPool::setEntries CTxMemPool::AssumeCalculateMemPoolAncestors(
258+
std::string_view calling_fn_name,
259+
const CTxMemPoolEntry &entry,
260+
const Limits& limits,
261+
bool fSearchForParents /* = true */) const
262+
{
263+
auto result{Assume(CalculateMemPoolAncestors(entry, limits, fSearchForParents))};
264+
if (!result) {
265+
LogPrintLevel(BCLog::MEMPOOL, BCLog::Level::Error, "%s: CalculateMemPoolAncestors failed unexpectedly, continuing with empty ancestor set (%s)\n",
266+
calling_fn_name, util::ErrorString(result).original);
267+
}
268+
return std::move(result).value_or(CTxMemPool::setEntries{});
269+
}
270+
257271
void CTxMemPool::UpdateAncestorsOf(bool add, txiter it, setEntries &setAncestors)
258272
{
259273
const CTxMemPoolEntry::Parents& parents = it->GetMemPoolParentsConst();

src/txmempool.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <optional>
1212
#include <set>
1313
#include <string>
14+
#include <string_view>
1415
#include <utility>
1516
#include <vector>
1617

@@ -566,6 +567,26 @@ class CTxMemPool
566567
const Limits& limits,
567568
bool fSearchForParents = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
568569

570+
/**
571+
* Same as CalculateMemPoolAncestors, but always returns a (non-optional) setEntries.
572+
* Should only be used when it is assumed CalculateMemPoolAncestors would not fail. If
573+
* CalculateMemPoolAncestors does unexpectedly fail, an empty setEntries is returned and the
574+
* error is logged to BCLog::MEMPOOL with level BCLog::Level::Error. In debug builds, failure
575+
* of CalculateMemPoolAncestors will lead to shutdown due to assertion failure.
576+
*
577+
* @param[in] calling_fn_name Name of calling function so we can properly log the call site
578+
*
579+
* @return a setEntries corresponding to the result of CalculateMemPoolAncestors or an empty
580+
* setEntries if it failed
581+
*
582+
* @see CTXMemPool::CalculateMemPoolAncestors()
583+
*/
584+
setEntries AssumeCalculateMemPoolAncestors(
585+
std::string_view calling_fn_name,
586+
const CTxMemPoolEntry &entry,
587+
const Limits& limits,
588+
bool fSearchForParents = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
589+
569590
/** Calculate all in-mempool ancestors of a set of transactions not already in the mempool and
570591
* check ancestor and descendant limits. Heuristics are used to estimate the ancestor and
571592
* descendant count of all entries if the package were to be added to the mempool. The limits

0 commit comments

Comments
 (0)