Loading
fix(thorchain): stop LP unit overflow panics and guard adds into synth-overhang pools
🔎 Visual review ofa1dfc9a6: 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):
- No more LP unit overflow panics.
calculatePoolUnitsranLegacyDecmath that panics once an intermediate leaves the 2^256 range. In overhang, pool units are clamped toP = L*(1+S), so each deposit multiplies LP units by about1+(1+S)f. Repeated POL deposits fromPOLCyclein EndBlock would reach that panic and halt the chain. Every intermediate is now checked with exact integer arithmetic first, anderrPoolUnitsOverflowis returned instead of a panic. Inputs that pass the check run the unchangedLegacyDecmath, so results are bit-identical (TestCalculatePoolUnitsMatchesReferencechecks this against a copy of the old function). The empty-pool branch and the post-addCalcUnitsproduct get the same checks. - New
LUVIOverhangGuardmimir (default1).addLiquidityrejects adds into a pool that has non-zero synth supply and is in overhang. This covers user adds, staged (pending) halves and POL adds.POLCycleskips such pools with a log line, andPOLReserveCyclepicks another pool. Withdrawals are unaffected. Setting the mimir to0turns 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
LUVIOverhangGuardconstant.LUVIGuardand the other guards' docs/constants arrive with their own MRs, so expect trivial adjacent-line conflicts inconstants/anddocs/mimir.mdfor whichever lands second. - The fuzz split (
odin/luvi-guards-fuzz) pins this finding as found inTestLuviFindingOverhangAddOverflowPanics(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 cleanerrPoolUnitsOverflowwith 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 handlerRun, 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, andPOLReserveCycleskips the pool. -
make lint-ci -
make generate, then a cleangit status - CI regression + simulation
Edited by Huginn Ai