fix(stake): #169 — compute pool value in i128 to stop mode-1 fee-withdraw brick - #187
Conversation
…draw brick In a mode-1 (trading) pool a withdrawer's payout includes their share of accrued fees, so total_withdrawn legitimately exceeds total_deposited once fees have been paid out. total_pool_value()'s left-to-right `total_deposited.checked_sub( total_withdrawn)` then underflowed to None and PERMANENTLY bricked the pool — every op (LP pricing, HWM, AccrueFees, vault check) fails closed, trapping the LP funds still in the pool. principal_tvl() had the same shape and bricked DEPOSITS. Fix: sum both in a wide SIGNED i128 intermediate so evaluation order is irrelevant. - total_pool_value(): fail closed (None) only when the FINAL value is out of range — negative (genuine insolvency: claims > assets) or > u64::MAX. The false-underflow brick is gone; the insolvency guard is preserved. #161 realized_junior_loss term and PERC-272 mode-1 fee inclusion are kept. - principal_tvl(): clamp a net-negative basis to 0 (no live principal → the deposit cap admits new principal) instead of bricking deposits. #161 RL still excluded. This is the idea from #170 (0x-SquidSol), reimplemented on current main so it KEEPS the post-#161 `- realized_junior_loss()` term (the PR's stale base dropped it, which would have reverted the #161 fair-recovery fix) and also fixes the twin principal_tvl(). Verified: - Kani: proof_169_mode1_no_false_underflow_brick — for any ledger the i128 value equals the true signed sum when in range (incl. withdrawn>deposited, the case that bricked) and is None iff truly insolvent. 0/104 checks failed; cover (withdrawn>deposited) satisfied (non-vacuous). - Unit: 5 #169 regression tests (fee-withdrawal not bricked, principal_tvl clamps, insolvency still None, mode-0 unaffected + RL preserved, fees in value not principal). Full suite green (46 integration + 67 unit). build-sbf clean. Closes #169. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughFixes issue ChangesIssue
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Bug (#169)
In a mode-1 trading pool, a withdrawer's payout includes their share of accrued fees, so
total_withdrawnlegitimately exceedstotal_depositedonce fees are paid out.total_pool_value()'s left-to-righttotal_deposited.checked_sub(total_withdrawn)then underflowed toNoneand permanently bricked the pool — LP funds trapped.principal_tvl()had the same shape and bricked deposits.Fix
Sum both in a wide signed i128 intermediate (order no longer matters):
total_pool_value()→Noneonly when the final value is out of range (negative = genuine insolvency, or > u64::MAX). False-underflow brick gone; insolvency guard preserved. Keeps the Design: last junior exiting during an outstanding loss windfalls the recovery to senior (junior-withdraw side of #145) #161realized_junior_lossterm + PERC-272 mode-1 fee inclusion.principal_tvl()→ clamp a net-negative basis to0(deposit cap admits new principal) instead of bricking. Design: last junior exiting during an outstanding loss windfalls the recovery to senior (junior-withdraw side of #145) #161 RL still excluded.This is the i128 idea from #170 (@0x-SquidSol), reimplemented on current
mainso it keeps the post-#161- realized_junior_loss()term (the PR's stale base dropped it, which would have reverted #161) and also fixes the twinprincipal_tvl().Verified
proof_169_mode1_no_false_underflow_brick: i128 value == true signed sum when in range (incl.withdrawn > deposited, the bricking case) andNoneiff truly insolvent.0/104failed; cover satisfied (non-vacuous).build-sbfclean.Closes #169.
🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
#169: pool value calculations no longer fail when total withdrawals exceed total deposits.Tests