mempool: keep prioritisetransaction deltas within MAX_MONEY #36367

Open
Bruce039 wants to merge 1 commit from fix-prioritisetransaction-delta-range into master AGit
Bruce039 commented 2026-09-28 14:44:39 +00:00 (Migrated from github.com)

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` 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.
DrahtBot commented 2026-09-28 14:44:51 +00:00 (Migrated from github.com)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36367 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36367.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36367#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | Concept ACK | [ismaelsadeeq](https://github.com/bitcoin/bitcoin/pull/36367#issuecomment-6821480047) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts No conflicts as of last run. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

Earlier concerns remain unresolved; see the full report for their current assessments.

🔵 Suggestion · RPC help omits the new fee-delta limits

The prioritisetransaction help describes addition and subtraction without explaining the new accepted range or that successful calls can apply less than the requested adjustment.

Location: src/rpc/mining.cpp:564 (head).

🔵 Suggestion · The fee-delta test never reaches the cumulative lower bound

The negative calls only cancel positive deltas, so removing the -MAX_MONEY floor would leave the new assertions unaffected.

Location: test/functional/mining_prioritisetransaction.py:114 (head).

🔵 Suggestion · Persisted int64-extreme deltas lack boundary assertions

The new regression test rejects oversized RPC inputs before mempool arithmetic, leaving the clamp's handling of raw int64 deltas from mempool.dat without deterministic boundary coverage.

Location: test/functional/mining_prioritisetransaction.py:98 (head).

Review coverage was incomplete.


Reviewed: 0d168345063e · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:9db9878230d996f42eeed0cfe09a960bfd438b544914312c40b17bb9fba26900:1e657457416e20a18d8d2d136919c48e28e1bfed7a76c26939ab1ca10a232c6a --> <!-- ralph-base:6c01bd0b2dc201c21e80ac3635828abf854ee5a0 --> <!-- ralph-head:0d168345063e6ba56155bfd97f647bbad4687546 --> <!-- ralph-case:f8068d9ec84e84abdd31dba8a9d20325a0f9127aca74616107c0ec1c4d471d37 --> Earlier concerns remain unresolved; see the full report for their current assessments. **🔵 Suggestion · RPC help omits the new fee-delta limits** The prioritisetransaction help describes addition and subtraction without explaining the new accepted range or that successful calls can apply less than the requested adjustment. Location: [`src/rpc/mining.cpp:564`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/0d168345063e6ba56155bfd97f647bbad4687546/src/rpc/mining.cpp#L564>) (head). **🔵 Suggestion · The fee-delta test never reaches the cumulative lower bound** The negative calls only cancel positive deltas, so removing the -MAX_MONEY floor would leave the new assertions unaffected. Location: [`test/functional/mining_prioritisetransaction.py:114`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/0d168345063e6ba56155bfd97f647bbad4687546/test/functional/mining_prioritisetransaction.py#L114>) (head). **🔵 Suggestion · Persisted int64-extreme deltas lack boundary assertions** The new regression test rejects oversized RPC inputs before mempool arithmetic, leaving the clamp's handling of raw int64 deltas from mempool.dat without deterministic boundary coverage. Location: [`test/functional/mining_prioritisetransaction.py:98`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/0d168345063e6ba56155bfd97f647bbad4687546/test/functional/mining_prioritisetransaction.py#L98>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`0d168345063e`](<https://git.fish.foo/bitcoin/bitcoin/commit/0d168345063e6ba56155bfd97f647bbad4687546>) · [Full review report](<https://review.fish.foo/traces/36367-0d168345063e6ba56155bfd97f647bbad4687546-1928-2.html>) · [Previous reports](<https://review.fish.foo/traces/36367/>) [permalink](<https://review.fish.foo/traces/36367-0d168345063e6ba56155bfd97f647bbad4687546-1928-2.html>)
sedited commented 2026-11-12 10:35:34 +00:00 (Migrated from github.com)

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

There's been recent work in this area, is this still an issue? Can you give this a quick look @ismaelsadeeq?
ismaelsadeeq commented 2026-11-12 10:46:11 +00:00 (Migrated from github.com)

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.

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 commented 2026-11-12 10:57:25 +00:00 (Migrated from github.com)

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.

> 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 commented 2026-11-16 09:12:21 +00:00 (Migrated from github.com)

@sedited I looked at current master (a527a0077a) 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.

@sedited I looked at current master (a527a0077a) 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 in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36367
No description provided.