Skip to content

mempool: keep prioritisetransaction deltas within MAX_MONEY - #36367

Open
Bruce039 wants to merge 1 commit into
bitcoin:masterfrom
Bruce039:fix-prioritisetransaction-delta-range
Open

Bruce039 wants to merge 1 commit into
bitcoin:masterfrom
Bruce039:fix-prioritisetransaction-delta-range

Conversation

@Bruce039

Copy link
Copy Markdown
Contributor

prioritisetransaction takes fee_delta as any int64. CTxMemPool::PrioritiseTransaction stacks deltas with SaturatingAdd, so the accumulated delta and the modified fee can go up to INT64_MAX. The cluster mempool then adds modified fees of several transactions together in FeeFrac (util/feefrac.h:108) without overflow checks.

For example, with a parent and child in the mempool:

prioritisetransaction <parent> 0 4611686018427387904
prioritisetransaction <child> 0 9223372036854775807
  • getmempoolcluster reports chunkfee: -46116860184.27356705
  • getblocktemplate contains neither transaction, and the next block mined has only the coinbase
  • UBSan: feefrac.h:108:13: runtime error: signed integer overflow, reached from linearization via getrawmempool

There's no use for a delta larger than the total money supply, so this:

  • rejects fee_delta outside -MAX_MONEY..MAX_MONEY in the RPC with "fee_delta out of range"
  • clamps the accumulated delta to -MAX_MONEY..MAX_MONEY in PrioritiseTransaction, which also covers deltas loaded from mempool.dat, and updates the modified fee by the change in the clamped delta

With each modified fee bounded by 2 * MAX_MONEY, cluster sums stay far away from int64 limits.

The new test_fee_delta_limits in mining_prioritisetransaction.py checks the RPC range error, that stacked deltas stop at MAX_MONEY, that the chunk fees add up, and that both transactions are in the block template. It fails on master ("No exception raised"). mempool_persist.py, mempool_packages.py, rpc_packages.py and the miner/rbf/txpackage unit tests pass.

AI tools were used to help find this issue and to prepare the patch and test.

prioritisetransaction accepts any int64 fee_delta, and
CTxMemPool::PrioritiseTransaction only saturates the accumulated delta
at the int64 limits. With large deltas the modified fees of a cluster
add up past int64 in the txgraph chunk calculations (UBSan reports a
signed overflow in FeeFrac::operator+=). For example, prioritising a
parent by 2^62 and its child by INT64_MAX gives a negative chunk fee
in getmempoolcluster, and both transactions are left out of block
templates.

Reject fee_delta values outside +-MAX_MONEY in the RPC, and clamp the
accumulated delta to +-MAX_MONEY in PrioritiseTransaction (which also
covers deltas loaded from mempool.dat). The modified fee is updated by
the change in the clamped delta.
@DrahtBot

DrahtBot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK ismaelsadeeq

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

No conflicts as of last run.

@sedited

sedited commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

There's been recent work in this area, is this still an issue? Can you give this a quick look @ismaelsadeeq?

@ismaelsadeeq

Copy link
Copy Markdown
Member

Concept ACK, why max money? I think preventing it from overflowing should be the goal here; having the fee above max money won't hurt; it's just a way to prioritise txs during inclusion in the block template.

@maflcko

maflcko commented Nov 12, 2026

Copy link
Copy Markdown
Member

why max money?

I think it is just a best-effort attempt to prevent SaturatingAdd(delta, nFeeDelta) to saturate too often. Though, I haven't yet looked if this is possible/testable.

@Bruce039

Copy link
Copy Markdown
Contributor Author

@sedited I looked at current master (a527a00) and both parts are unchanged: CTxMemPool::PrioritiseTransaction still stacks deltas with SaturatingAdd, and FeeFrac::operator+= still adds the fees without an overflow check. So the parent/child example from the description should still lead to the negative chunk fee and the empty block template. The only related open PR I found is #35511 (CAmount as a class), which doesn't change this arithmetic.

@ismaelsadeeq agreed, preventing the overflow is the actual goal, a large fee itself is harmless. Some bound on the delta is needed for that, though, and MAX_MONEY was the obvious one: no meaningful delta is larger than the total supply, and with every modified fee at most 2 * MAX_MONEY the sum over a whole cluster stays far below the int64 limit. As maflcko said, it also keeps SaturatingAdd from saturating in practice. If you'd rather cap it somewhere else, e.g. only at the point where fees are summed, I'm happy to change it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants