Rust fix 1 - #166
Merged
Merged
Conversation
rust: Align remaining Result<_, String> APIs to PoolError
rust: No panic when current time < last for quant and reclamm
Fix `stable_math::compute_invariant` panics on a zero balance
rust: Renamed → to make clear it returns the fee in the token's raw…
fix(rust): propagate math errors instead of masking with unwrap_or(ZERO)
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.
Report issue:
1. reCLAMM: unchecked arithmetic panics on reachable pool states
This is the one we would most like fixed. It panics rather than returning
PoolError, so asingle pathological pool takes down the calling thread, and it is on the ordinary quoting path
(
ReClammV2Pool::on_swap), not only on an edge API.Deterministic reproduction
Calling the public
compute_current_virtual_balanceswith these arguments panics withattempt to divide by zeroatruint-1.20.0/src/div.rs:93:Two conditions have to coincide, and both are ordinary:
centeredness < centeredness_margin, socompute_virtual_balances_updating_price_rangeruns, and(balance_a + virtual_a) * (balance_b + virtual_b) < 1e18, socompute_invarianttruncates theproduct to zero.
Condition 2 needs no overflow —
mul_down_fixedisa * b / WAD, so any product underWADtruncates. A pool whose scaled-18 balances and virtual balances are small enough qualifies. The path
is then:
Observed in production
57–69 panics per run of our mainnet integration test,
RUST_BACKTRACE=1:The division above is not the only unguarded spot
Auditing
reclammv2_math.rsfor raw/and-onU256turns up six more, all reachable from thesame public entry point. Line numbers are from the published 0.4.4 source:
centeredness + (4 * sqrt_price_ratio) - TWO_WADsqrt_price_ratiotruncated to 0 ⇒ underflow/ (2 * (sqrt_price_ratio - WAD))sqrt_price_ratio < WAD⇒ underflow;== WAD⇒ divide by zero/ last_virtual_balance_undervaluedcurrent_timestamp - last_timestampsqrt_scaled_18(&sqrt_price_ratio) - WAD/ (mul_down_fixed(sqrt_price_ratio - WAD, v_over) - balances_over)/ current_invariantpools/reclamm/reclamm_math.rs(the v1 module) has the same shape.Suggested fix: make
compute_price_range,compute_price_ratioandcompute_virtual_balances_updating_price_rangereturnResult<_, PoolError>and use checkedoperations, so callers get an error they can skip the pool on rather than an unwinding panic.