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
This commit is contained in:
stickies-v 2022-10-25 12:33:37 +01:00
parent f911bdfff9
commit 5481f65849
No known key found for this signature in database
GPG key ID: 5CB1CE6E5E66A757
2 changed files with 35 additions and 0 deletions

View file

@ -254,6 +254,20 @@ util::Result<CTxMemPool::setEntries> CTxMemPool::CalculateMemPoolAncestors(
limits);
}
CTxMemPool::setEntries CTxMemPool::AssumeCalculateMemPoolAncestors(
std::string_view calling_fn_name,
const CTxMemPoolEntry &entry,
const Limits& limits,
bool fSearchForParents /* = true */) const
{
auto result{Assume(CalculateMemPoolAncestors(entry, limits, fSearchForParents))};
if (!result) {
LogPrintLevel(BCLog::MEMPOOL, BCLog::Level::Error, "%s: CalculateMemPoolAncestors failed unexpectedly, continuing with empty ancestor set (%s)\n",
calling_fn_name, util::ErrorString(result).original);
}
return std::move(result).value_or(CTxMemPool::setEntries{});
}
void CTxMemPool::UpdateAncestorsOf(bool add, txiter it, setEntries &setAncestors)
{
const CTxMemPoolEntry::Parents& parents = it->GetMemPoolParentsConst();

View file

@ -11,6 +11,7 @@
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
@ -566,6 +567,26 @@ public:
const Limits& limits,
bool fSearchForParents = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
/**
* Same as CalculateMemPoolAncestors, but always returns a (non-optional) setEntries.
* Should only be used when it is assumed CalculateMemPoolAncestors would not fail. If
* CalculateMemPoolAncestors does unexpectedly fail, an empty setEntries is returned and the
* error is logged to BCLog::MEMPOOL with level BCLog::Level::Error. In debug builds, failure
* of CalculateMemPoolAncestors will lead to shutdown due to assertion failure.
*
* @param[in] calling_fn_name Name of calling function so we can properly log the call site
*
* @return a setEntries corresponding to the result of CalculateMemPoolAncestors or an empty
* setEntries if it failed
*
* @see CTXMemPool::CalculateMemPoolAncestors()
*/
setEntries AssumeCalculateMemPoolAncestors(
std::string_view calling_fn_name,
const CTxMemPoolEntry &entry,
const Limits& limits,
bool fSearchForParents = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
/** Calculate all in-mempool ancestors of a set of transactions not already in the mempool and
* check ancestor and descendant limits. Heuristics are used to estimate the ancestor and
* descendant count of all entries if the package were to be added to the mempool. The limits