Batch: resolve issues #3006, #2972, #2971, #2970, #3007, #3008

Resolve #3006 Resolve #2972 Resolve #2971 Resolve #2970 Resolve #3007 Resolve #3008 This MR implements the changes described in issues #3006, #2972, #2971, #2970, #3007, #3008.

I'll look at the actual diff first.

Summary

These changes harden the pool-ragnarok wind-down path, mainly in x/thorchain/manager_network_current.go, so that liquidity-provider / saver claims can no longer be deleted without a payout, so that queued saver redemption swaps are not stranded, and so that reward/pool accounting is committed atomically.

Saver redemption swaps during ragnarok

  • WithdrawLiquidityHandler.validate now bypasses the BurnSynths gate when the pool's Layer-1 asset is in ragnarok, so ragnarok can actually pay savers out on chainnet/stagenet (where BurnSynths=1) instead of deleting every saver with no payout.
  • SwapperImpl.Swap applies the same exception: a synth-source swap is no longer rejected by BurnSynths when its Layer-1 asset is in ragnarok, so the queued synth→L1 saver payout is not thrown away.
  • WithdrawLiquidityHandler.swap now queues the redemption where it can actually be settled. When the advanced swap queue is enabled the swap is added via AdvSwapQueueMgr().AddSwapQueueItem instead of the legacy SetSwapQueueItem; SwapperImpl.Swap deliberately skips TryAddTxOutItem while the advanced queue is enabled (deferring to settleSwap), so writing to the legacy namespace burned the synth and dropped the item with no outbound ever scheduled. The TxID-collision check was extended to HasAdvSwapQueueItem, and when streaming was not requested (by config, or because ragnarok disabled it) the swap quantity is pinned to 1 — AddSwapQueueItem otherwise expands a zero quantity to maxSwapQuantity, which IsStreaming() reads as a streaming swap.
  • The streaming-disable check in that handler now uses the canonical IsRagnarok predicate rather than a direct RAGNAROK-<asset> mimir lookup. A chain gas asset ragnarok covers every token pool on that chain (exactly how checkPoolRagnarok selects pools), and there is no RAGNAROK-ETH-USDC key to find, so a token pool's redemption was otherwise queued as a streaming swap.
  • SwapQueueAdv.isSwapReady now only blocks streaming swaps on ragnarok source/target assets (mirroring handler_swap.go), and likewise evaluates IsRagnarok against the Layer-1 assets so token pools of a ragnarok chain are recognised. Non-streaming market swaps are still processed during ragnarok so the synth→L1 saver redemption settles; non-synth market swaps against a ragnarok pool remain blocked by the pool-availability check in swapOne.

Saver withdrawals

  • withdrawSavers immediately removes records that have nothing to claim (zero units and no pending balances), cleaning up any zero-unit records left by a prior successful withdraw.
  • It otherwise removes a saver record only when the payout is unrecoverable dust (below the outbound fee, ErrNotEnoughToPayFee). Any other (potentially transient) error retains the record for retry in a later interval; because such a record keeps totalCount non-zero, the pool is not finalized until it clears.

LP withdrawals

  • withdrawLPs similarly removes an LP record only for the unrecoverable-dust case. Transient errors (chain halt, LP pause, etc.) retain the record, set a retryRemaining flag, and report not-done so the pool is not finalized until every LP can be withdrawn.
  • A dual-address LP is withdrawn symmetrically and the handler schedules the asset outbound before the RUNE one, so an asset leg that cannot cover its own outbound fee rolls back the whole withdraw — including a RUNE leg that was never attempted. withdrawLPs now retries such an LP RUNE-only, which converts the asset side in the pool and emits a single native outbound; nothing is forfeited, and hitting ErrNotEnoughToPayFee again then genuinely means every remaining leg is individually unpayable. When asymmetric withdrawals are paused for the chain (PauseAsymWithdrawal-<chain>) the retry is skipped — assetToWithdraw() would turn it back into a symmetric withdraw and prove nothing about the RUNE leg — and the failure is reported as a plain (unwrapped) error so the LP is retained for retry rather than removed as dust.
  • Staged pending liquidity is no longer stranded. A unit withdraw pays out only the unit-derived share and deliberately leaves PendingRune / PendingAsset untouched on a retained record, which withdraw() refunds on a later pass once the record is zero-unit. Two paths cover this:
    • After a successful withdraw, lpClaimRemains re-reads the record and reports not-done if any units or pending balances survive, so the pool cannot be finalized — and its pending accounting swept to the Reserve — before the provider has been paid. A record that no longer exists reads back all-zero and correctly reports done; a failed re-read is treated as "still owed" and retried.
    • When the withdraw fails with ErrNotEnoughToPayFee on a record that has both units and a pending balance, the dust only proves the unit-derived payout is unpayable. That case now forfeits just the dust units — zeroing lp.Units, stamping LastWithdrawHeight and re-storing the record, leaving the pool's LPUnits alone exactly as a full dust removal does — and retains the record so the next interval takes the pending branch and pays it out. Merely retaining the record intact would re-attempt the same unpayable unit withdraw every interval and stall the ragnarok forever. If the pending balance is itself unpayable, that pass reaches the same error with zero units and the record is removed then.
  • withdraw relaxes the "cannot withdraw 100% of only one side of the pool" guard during ragnarok. That guard exists to stop a live pool being left with a zeroed depth, but a one-sided (asymmetric) last LP necessarily drains a single side, so during ragnarok it rejected the withdraw on every interval forever — the provider was never paid and the pool never finalized. Both flavours of ragnarok count: a per-asset RAGNAROK-* mimir (IsRagnarok) and a network-wide ragnarok (RagnarokInProgress), which winds every pool down through ragnarokPools without setting any of those mimirs. The bypass is restricted to the protocol's own ragnarok withdraw (a blank tx ID), so a user-submitted asymmetric withdraw is still rejected rather than silently forfeiting the opposite side.

Pool finalization

  • withdrawLiquidity now treats a failed transfer of remaining pool RUNE (BalanceRune + PendingInboundRune) to the Reserve as fatal: it returns the error before suspending/removing the pool, so the interval's ragnarok cache is rolled back and retried rather than deleting pool accounting while the RUNE was never credited.
  • For pool assets backed by the Pool Module (TCY and whitelisted base-layer assets), any remaining BalanceAsset + PendingInboundAsset is likewise swept to the Reserve before finalization, with the same fatal-on-failure handling — otherwise a RUNE-address-only last withdraw could leave module-backed balances behind a removed pool. External L1 remainders have no module balance to reconcile and are left orphaned in the vault for the nodes to dispose of.

Reward accounting atomicity

  • UpdateNetwork wraps its Reserve deductions, reward transfers (dev/marketing/burn/tcy/stable-reserve/pol/bond and pool/saver yield), the MaxRuneSupply mimir decrement and the final SetNetwork in a CacheContext, committing only after every step succeeds so partial deductions cannot persist while network bookkeeping is dropped. The Stable Reserve POL-share deduction is inside the same cache, so it rolls back with the rest.

Tests

Added regression tests covering: saver/LP claim retention on transient errors, retention of an LP's pending claim both after a successful unit withdraw and after a dust ErrNotEnoughToPayFee on a record that still holds pending liquidity (plus removal once that pending claim is itself proven unpayable), the ragnarok BurnSynths bypass in the withdraw handler and swapper, the savers redemption actually scheduling an outbound when the advanced swap queue is enabled, a token-pool ragnarok (gas-asset mimir only) queueing a non-streaming redemption, the non-streaming saver-redemption swap passing isSwapReady during ragnarok for both gas-asset and token pools (while a streaming swap on a ragnarok asset is still skipped), the fatal remaining-RUNE-transfer guard, RUNE-only and asset-only last-LP ragnarok finalization plus the protocol-only restriction and network-wide-ragnarok coverage of the one-sided withdraw bypass, a dual-sided LP with a dust asset leg still being paid its RUNE leg (and being retained instead when asymmetric withdrawals are paused), the sweep of a remaining native pool asset to the Reserve, and UpdateNetwork committing the Stable Reserve deduction atomically / rolling it back when a later step (SetNetwork) fails.

Edited by Huginn Ai

Merge request reports

Loading
Loading