fix: prevent unsigned underflow in FluidV1 swap routing functions#558
Open
sunce86 wants to merge 1 commit intopropeller-heads:mainfrom
Open
fix: prevent unsigned underflow in FluidV1 swap routing functions#558sunce86 wants to merge 1 commit intopropeller-heads:mainfrom
sunce86 wants to merge 1 commit intopropeller-heads:mainfrom
Conversation
swap_routing_in() and swap_routing_out() compute a subtraction that can produce a negative result when the debt pool offers better pricing. The Go reference (KyberSwap big.Int) handles this natively since big.Int is signed, and the caller checks a.Sign() <= 0 to route through debt. The Rust port used unsigned U256 subtraction which wraps on underflow, producing a large positive value that bypasses the debt routing path and incorrectly sends the swap through the collateral pool. Fix: use saturating_sub so underflow produces 0 instead of wrapping. The existing caller already treats a == 0 as "route through debt pool", so no caller changes are needed. Reference: KyberNetwork/kyberswap-dex-lib pool_simulator.go:273-307
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.
Summary
swap_routing_in()andswap_routing_out()use a subtraction that can produce a negative result when the debt pool offers better pricing than the collateral pool. The Go reference implementation (KyberSwap pool_simulator.go:273-307) uses*big.Int(signed), and the caller checksa.Sign() <= 0to route the entire trade through the debt pool.The Rust port uses
U256(unsigned), so the subtraction wraps around to a large positive number instead of going negative. The existing caller checksa == U256::ZERO || a == U256::MAXbut a wrapped value is neither — it falls through to the wrong branch and routes the trade through the collateral pool.Fix
Replace
-withsaturating_sub()in both functions. When the result would be negative, this produces0instead of wrapping. The existing caller already handlesa == U256::ZEROas "route through debt pool", so no caller changes are needed.Test plan