fix: Assert the ammAssetOut denominator is non-zero - #7959
Draft
SoubeDev wants to merge 2 commits into
Draft
Conversation
ammAssetOut divides by (t1 * f - 1) in both the pre-fixAMMv1_3 and current paths without recording why the denominator is safe. Hoist it into a named local and assert it, so the contract lives with the code and a future change that widens the input ranges trips in Debug. The invariant holds because f = tfee / kAuctionSlotFeeScaleFactor is bounded by kTradingFeeThreshold (0.01) and t1 <= 1, putting denom in [-1, -0.99]. The tighter 0 < t1 <= 1 is deliberately not asserted: it is violable for a sole LP without fixAMMv1_1 and t1 == 0 is reachable pre-fixAMMv1_3, which would crash Debug builds on real ledger data. Fixes XRPLF#6661
SoubeDev
marked this pull request as draft
August 5, 2026 18:16
ammAssetOut divides by (t1 * f - 1) in both the pre-fixAMMv1_3 and current paths without recording why the denominator is safe. Hoist it into a named local and assert it, so the contract lives with the code and a future change that widens the input ranges trips in Debug. The invariant holds because f = tfee / kAuctionSlotFeeScaleFactor is bounded by kTradingFeeThreshold (0.01) and t1 <= 1, putting denom in [-1, -0.99]. The tighter 0 < t1 <= 1 is deliberately not asserted: it is violable for a sole LP without fixAMMv1_1 and t1 == 0 is reachable pre-fixAMMv1_3, which would crash Debug builds on real ledger data. Fixes XRPLF#6661
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #6661.
Adds the defensive
XRPL_ASSERTrequested in the issue.Two notes on the issue as filed:
src/libxrpl/ledger/helpers/AMMHelpers.cpp, notsrc/libxrpl/tx/transactors/dex/.fixAMMv1_3path but not the legacy branch. Hoisting intodenomcovers both with a single assert.Uses
beast::kZerorather than the issue'sNumber{0}— both compile, butbeast::kZerois the existing idiom in this file and in the sibling denominator guard inAMMWithdraw::singleWithdrawEPrice.The tighter
0 < t1 <= 1invariant is deliberately not asserted. It is genuinely violable in production —t1can exceed 1 marginally for a sole LP withoutfixAMMv1_1, andt1 == 0is rle pre-fixAMMv1_3insingleWithdrawTokens— so asserting it would crash Debug builds on real ledger data, contrary to CONTRIBUTING.md's rule on contracts violable by external conditions.Release behavior is unchanged;
XRPL_ASSERTcompiles out, and hoisting the subexpression is value-identical (no rounding-mode guard is active at that point).🤖 Generated with Claude Code