1818#include < policy/policy.h>
1919#include < policy/settings.h>
2020#include < reverse_iterator.h>
21+ #include < util/check.h>
2122#include < util/moneystr.h>
23+ #include < util/overflow.h>
2224#include < util/system.h>
2325#include < util/time.h>
2426#include < validationinterface.h>
@@ -89,6 +91,7 @@ CTxMemPoolEntry::CTxMemPoolEntry(const CTransactionRef& tx, CAmount fee,
8991 entryHeight{entry_height},
9092 spendsCoinbase{spends_coinbase},
9193 sigOpCost{sigops_cost},
94+ m_modified_fee{nFee},
9295 lockPoints{lp},
9396 nSizeWithDescendants{GetTxSize ()},
9497 nModFeesWithDescendants{nFee},
@@ -98,11 +101,11 @@ CTxMemPoolEntry::CTxMemPoolEntry(const CTransactionRef& tx, CAmount fee,
98101 discountSizeWithAncestors{GetDiscountTxSize ()},
99102 setPeginsSpent (_setPeginsSpent) {}
100103
101- void CTxMemPoolEntry::UpdateFeeDelta (CAmount newFeeDelta )
104+ void CTxMemPoolEntry::UpdateModifiedFee (CAmount fee_diff )
102105{
103- nModFeesWithDescendants += newFeeDelta - feeDelta ;
104- nModFeesWithAncestors += newFeeDelta - feeDelta ;
105- feeDelta = newFeeDelta ;
106+ nModFeesWithDescendants = SaturatingAdd (nModFeesWithDescendants, fee_diff) ;
107+ nModFeesWithAncestors = SaturatingAdd (nModFeesWithAncestors, fee_diff) ;
108+ m_modified_fee = SaturatingAdd (m_modified_fee, fee_diff) ;
106109}
107110
108111void CTxMemPoolEntry::UpdateLockPoints (const LockPoints& lp)
@@ -457,7 +460,7 @@ void CTxMemPoolEntry::UpdateDescendantState(int64_t modifySize, CAmount modifyFe
457460{
458461 nSizeWithDescendants += modifySize;
459462 assert (int64_t (nSizeWithDescendants) > 0 );
460- nModFeesWithDescendants += modifyFee;
463+ nModFeesWithDescendants = SaturatingAdd (nModFeesWithDescendants, modifyFee) ;
461464 nCountWithDescendants += modifyCount;
462465 assert (int64_t (nCountWithDescendants) > 0 );
463466}
@@ -466,7 +469,7 @@ void CTxMemPoolEntry::UpdateAncestorState(int64_t modifySize, CAmount modifyFee,
466469{
467470 nSizeWithAncestors += modifySize;
468471 assert (int64_t (nSizeWithAncestors) > 0 );
469- nModFeesWithAncestors += modifyFee;
472+ nModFeesWithAncestors = SaturatingAdd (nModFeesWithAncestors, modifyFee) ;
470473 nCountWithAncestors += modifyCount;
471474 assert (int64_t (nCountWithAncestors) > 0 );
472475 nSigOpCostWithAncestors += modifySigOps;
@@ -509,8 +512,10 @@ void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry, setEntries &setAnces
509512 // into mapTx.
510513 CAmount delta{0 };
511514 ApplyDelta (entry.GetTx ().GetHash (), delta);
515+ // The following call to UpdateModifiedFee assumes no previous fee modifications
516+ Assume (entry.GetFee () == entry.GetModifiedFee ());
512517 if (delta) {
513- mapTx.modify (newit, [&delta](CTxMemPoolEntry& e) { e.UpdateFeeDelta (delta); });
518+ mapTx.modify (newit, [&delta](CTxMemPoolEntry& e) { e.UpdateModifiedFee (delta); });
514519 }
515520
516521 // Update cachedInnerUsage to include contained transaction's usage.
@@ -1014,10 +1019,10 @@ void CTxMemPool::PrioritiseTransaction(const uint256& hash, const CAmount& nFeeD
10141019 {
10151020 LOCK (cs);
10161021 CAmount &delta = mapDeltas[hash];
1017- delta += nFeeDelta;
1022+ delta = SaturatingAdd (delta, nFeeDelta) ;
10181023 txiter it = mapTx.find (hash);
10191024 if (it != mapTx.end ()) {
1020- mapTx.modify (it, [&delta ](CTxMemPoolEntry& e) { e.UpdateFeeDelta (delta ); });
1025+ mapTx.modify (it, [&nFeeDelta ](CTxMemPoolEntry& e) { e.UpdateModifiedFee (nFeeDelta ); });
10211026 // Now update all ancestors' modified fees with descendants
10221027 setEntries setAncestors;
10231028 uint64_t nNoLimit = std::numeric_limits<uint64_t >::max ();
0 commit comments