fix(thorchain): stop LP unit overflow panics and guard adds into synth-overhang pools

🔎 Visual review of a1dfc9a6: explainer, review flags and prompts to ask an AI about the change

What

Two fixes for LP adds into pools in synth overhang (synth supply S at least twice the asset depth A):

  1. No more LP unit overflow panics. calculatePoolUnits ran LegacyDec math that panics once an intermediate leaves the 2^256 range. In overhang, pool units are clamped to P = L*(1+S), so each deposit multiplies LP units by about 1+(1+S)f. Repeated POL deposits from POLCycle in EndBlock would reach that panic and halt the chain. Every intermediate is now checked with exact integer arithmetic first, and errPoolUnitsOverflow is returned instead of a panic. Inputs that pass the check run the unchanged LegacyDec math, so results are bit-identical (TestCalculatePoolUnitsMatchesReference checks this against a copy of the old function). The empty-pool branch and the post-add CalcUnits product get the same checks.
  2. New LUVIOverhangGuard mimir (default 1). addLiquidity rejects adds into a pool that has non-zero synth supply and is in overhang. This covers user adds, staged (pending) halves and POL adds. POLCycle skips such pools with a log line, and POLReserveCycle picks another pool. Withdrawals are unaffected. Setting the mimir to 0 turns the guard off; the overflow fix stays on either way.

Relation to the other luvi-guards splits

This is one of several standalone MRs split out of odin/luvi-guards. The others are the per-handler LUVI guard plus G accumulator, fair price, reward-allocation check, migration gate, and fuzz targets. This MR builds and passes its tests without any of them.

  • It brings only the LUVIOverhangGuard constant. LUVIGuard and the other guards' docs/constants arrive with their own MRs, so expect trivial adjacent-line conflicts in constants/ and docs/mimir.md for whichever lands second.
  • The fuzz split (odin/luvi-guards-fuzz) pins this finding as found in TestLuviFindingOverhangAddOverflowPanics (it expects a panic). Once this MR lands, that test has to be inverted. The parent branch did that in 5eba3e5ffb: it now expects an overhang rejection, and a clean errPoolUnitsOverflow with the guard off. Whichever of the two MRs merges second carries that change.

Risk

Consensus-visible: adds into overhang pools are now refunded, and POL no longer deposits into them. Pools that are not in overhang see identical results. Gated by mimir, but it defaults on, because the alternative is a potential EndBlock halt.

Test plan

  • go test -tags=mocknet ./x/thorchain/... ./constants/...
  • New LuviOverhangGuardSuite (x/thorchain/luvi_overhang_guard_test.go): overflow returns an error, matches the reference implementation in range, rejects adds by default (direct, via handler Run, and pending halves), allows them with the guard off, allows withdrawals, leaves non-overhang adds unchanged, POL skips overhang pools, POL with the guard off never panics, and POLReserveCycle skips the pool.
  • make lint-ci
  • make generate, then a clean git status
  • CI regression + simulation

🤖 Generated with Claude Code

Edited by Huginn Ai

Merge request reports

Loading
Loading