2525
2626#include < cmath>
2727#include < optional>
28+ #include < string_view>
29+ #include < utility>
2830
2931bool TestLockPointValidity (CChain& active_chain, const LockPoints& lp)
3032{
@@ -220,11 +222,10 @@ bool CTxMemPool::CheckPackageLimits(const Package& package,
220222 return ancestors.has_value ();
221223}
222224
223- bool CTxMemPool::CalculateMemPoolAncestors (const CTxMemPoolEntry &entry,
224- setEntries &setAncestors,
225- const Limits& limits,
226- std::string &errString,
227- bool fSearchForParents /* = true */ ) const
225+ util::Result<CTxMemPool::setEntries> CTxMemPool::CalculateMemPoolAncestors (
226+ const CTxMemPoolEntry &entry,
227+ const Limits& limits,
228+ bool fSearchForParents /* = true */ ) const
228229{
229230 CTxMemPoolEntry::Parents staged_ancestors;
230231 const CTransaction &tx = entry.GetTx ();
@@ -238,8 +239,7 @@ bool CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry,
238239 if (piter) {
239240 staged_ancestors.insert (**piter);
240241 if (staged_ancestors.size () + 1 > static_cast <uint64_t >(limits.ancestor_count )) {
241- errString = strprintf (" too many unconfirmed parents [limit: %u]" , limits.ancestor_count );
242- return false ;
242+ return util::Error{Untranslated (strprintf (" too many unconfirmed parents [limit: %u]" , limits.ancestor_count ))};
243243 }
244244 }
245245 }
@@ -250,14 +250,8 @@ bool CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry,
250250 staged_ancestors = it->GetMemPoolParentsConst ();
251251 }
252252
253- const auto calculated_ancestors{CalculateAncestorsAndCheckLimits (entry.GetTxSize (), /* entry_count=*/ 1 ,
254- staged_ancestors, limits)};
255- if (!calculated_ancestors.has_value ()) {
256- errString = util::ErrorString (calculated_ancestors).original ;
257- return false ;
258- }
259- setAncestors = *calculated_ancestors;
260- return true ;
253+ return CalculateAncestorsAndCheckLimits (entry.GetTxSize (), /* entry_count=*/ 1 , staged_ancestors,
254+ limits);
261255}
262256
263257void CTxMemPool::UpdateAncestorsOf (bool add, txiter it, setEntries &setAncestors)
@@ -321,9 +315,7 @@ void CTxMemPool::UpdateForRemoveFromMempool(const setEntries &entriesToRemove, b
321315 }
322316 }
323317 for (txiter removeIt : entriesToRemove) {
324- setEntries setAncestors;
325318 const CTxMemPoolEntry &entry = *removeIt;
326- std::string dummy;
327319 // Since this is a tx that is already in the mempool, we can call CMPA
328320 // with fSearchForParents = false. If the mempool is in a consistent
329321 // state, then using true or false should both be correct, though false
@@ -343,10 +335,11 @@ void CTxMemPool::UpdateForRemoveFromMempool(const setEntries &entriesToRemove, b
343335 // mempool parents we'd calculate by searching, and it's important that
344336 // we use the cached notion of ancestor transactions as the set of
345337 // things to update for removal.
346- CalculateMemPoolAncestors (entry, setAncestors, Limits::NoLimits (), dummy, false );
338+ auto ancestors_result{CalculateMemPoolAncestors (entry, Limits::NoLimits (), /* fSearchForParents=*/ false )};
339+ auto ancestors{std::move (ancestors_result).value_or (setEntries{})};
347340 // Note that UpdateAncestorsOf severs the child links that point to
348341 // removeIt in the entries for the parents of removeIt.
349- UpdateAncestorsOf (false , removeIt, setAncestors );
342+ UpdateAncestorsOf (false , removeIt, ancestors );
350343 }
351344 // After updating all the ancestor sizes, we can now sever the link between each
352345 // transaction being removed and any mempool children (ie, update CTxMemPoolEntry::m_parents
@@ -696,15 +689,14 @@ void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendhei
696689 assert (setParentCheck.size () == it->GetMemPoolParentsConst ().size ());
697690 assert (std::equal (setParentCheck.begin (), setParentCheck.end (), it->GetMemPoolParentsConst ().begin (), comp));
698691 // Verify ancestor state is correct.
699- setEntries setAncestors;
700- std::string dummy;
701- CalculateMemPoolAncestors (*it, setAncestors, Limits::NoLimits (), dummy);
702- uint64_t nCountCheck = setAncestors.size () + 1 ;
692+ auto ancestors_result{CalculateMemPoolAncestors (*it, Limits::NoLimits ())};
693+ auto ancestors{std::move (ancestors_result).value_or (setEntries{})};
694+ uint64_t nCountCheck = ancestors.size () + 1 ;
703695 uint64_t nSizeCheck = it->GetTxSize ();
704696 CAmount nFeesCheck = it->GetModifiedFee ();
705697 int64_t nSigOpCheck = it->GetSigOpCost ();
706698
707- for (txiter ancestorIt : setAncestors ) {
699+ for (txiter ancestorIt : ancestors ) {
708700 nSizeCheck += ancestorIt->GetTxSize ();
709701 nFeesCheck += ancestorIt->GetModifiedFee ();
710702 nSigOpCheck += ancestorIt->GetSigOpCost ();
@@ -859,10 +851,9 @@ void CTxMemPool::PrioritiseTransaction(const uint256& hash, const CAmount& nFeeD
859851 if (it != mapTx.end ()) {
860852 mapTx.modify (it, [&nFeeDelta](CTxMemPoolEntry& e) { e.UpdateModifiedFee (nFeeDelta); });
861853 // Now update all ancestors' modified fees with descendants
862- setEntries setAncestors;
863- std::string dummy;
864- CalculateMemPoolAncestors (*it, setAncestors, Limits::NoLimits (), dummy, false );
865- for (txiter ancestorIt : setAncestors) {
854+ auto ancestors_result{CalculateMemPoolAncestors (*it, Limits::NoLimits (), /* fSearchForParents=*/ false )};
855+ auto ancestors{std::move (ancestors_result).value_or (setEntries{})};
856+ for (txiter ancestorIt : ancestors) {
866857 mapTx.modify (ancestorIt, [=](CTxMemPoolEntry& e){ e.UpdateDescendantState (0 , nFeeDelta, 0 );});
867858 }
868859 // Now update all descendants' modified fees with ancestors
@@ -999,10 +990,9 @@ int CTxMemPool::Expire(std::chrono::seconds time)
999990
1000991void CTxMemPool::addUnchecked (const CTxMemPoolEntry &entry, bool validFeeEstimate)
1001992{
1002- setEntries setAncestors;
1003- std::string dummy;
1004- CalculateMemPoolAncestors (entry, setAncestors, Limits::NoLimits (), dummy);
1005- return addUnchecked (entry, setAncestors, validFeeEstimate);
993+ auto ancestors_result{CalculateMemPoolAncestors (entry, Limits::NoLimits ())};
994+ auto ancestors{std::move (ancestors_result).value_or (CTxMemPool::setEntries{})};
995+ return addUnchecked (entry, ancestors, validFeeEstimate);
1006996}
1007997
1008998void CTxMemPool::UpdateChild (txiter entry, txiter child, bool add)
0 commit comments