fix: deterministic fee-only swap queue sort with min-slip floor

Summary

The swap queue's Sort() used to rank items twice (by liquidity fee and by slip) and sum the ranks, with a tx-hash tiebreaker on the final score. That scheme was input-order dependent: stable sort preserved the caller's ordering within ties, and position in the ranked list was the score, so equal-ranked items picked up different scores based on who came first in the input.

On top of that, the keeper's string-backed queue index uses swap-and-truncate removal (removeString in keeper_adv_swap_queue.go), so the insertion-order-preserving iteration that Sort() implicitly relied on was in fact perturbed any time an item was removed mid-queue — giving [fish, pig, cat] for the three affiliate preferred-asset swaps of the same TxID, instead of the expected [pig, cat, fish].

Changes

  1. Sort() is fee-desc with (TxID asc, sub-swap index asc) tiebreakers — fully deterministic regardless of input order. Slip scoring was removed entirely: effective liquidity fee in RUNE already reflects slip (fee = x × slip × Y), and summing rank positions produced a score that depended on how many other items shared a bucket, which was the bug.
  2. scoreMsgs / addLiquidityFee now use GetSwapCalc so the score honors the min-slip floor that swapOne actually charges. Without this, small swaps that hit the floor were scored below their raw CalcLiquidityFee value and got deprioritized despite paying the floor rate at execution.
  3. Both legacy SwapQueueImpl and adv SwapQueueAdv are updated for consistency; the slip field on swapItem is dropped.
  4. TestRapidSwapDirectionSkipDoesNotConsumeBudget now sorts its tracked TxIDs so index 0 is the lowest — the test's expectedCounts array was implicitly assuming random-insertion order happened to match the first-processed swap.

Fixture churn

All regression fixture changes are deterministic consequences of the new sort order; no handler logic changed.

  • suites/swaps/multiple-affiliates.{yaml,json} — affiliate preferred-asset swaps now run in memo order (pig→cat→fish) instead of the removeString-perturbed order (fish→pig→cat). Payouts rotate; cascading pool-state effects update absolute balances in follow-on scenarios.
  • suites/swaps/streaming-postponed-synths-mint.{yaml,json} (~1.2k lines) — two symmetric streaming swaps (RUNE→BTC and BTC→RUNE) now interleave differently under fee-only ordering, which legitimately shifts cumulative pool effects over 10 iterations. The test's structural invariant (wolf and goat share a pool and see the same luvi_growth_pct) is preserved; only numeric values shift.
  • mnt/blocks/adv-swap-queue/rapid-swaps-*.json (basic-clean, overlap-extended, overlap-matrix, matrix-asset-classes) — EndBlock event order only, no balance changes.

⚠️ Consensus-safety note

This changes swap ordering in EndBlock, which is consensus-critical. THORChain's coordinated-upgrade model means all validators adopt a release simultaneously, so targeting develop should be safe — but please confirm during review whether a version gate is needed (the existing code already calls GetSwapper(vm.k.GetVersion()), so there's precedent for version-specific behavior in this exact path).

Test plan

  • `go build ./x/thorchain/...` passes
  • Unit tests pass (`make test`)
  • Regression pass (locally, 0 failures)
  • `TestRapidSwapDirectionSkipDoesNotConsumeBudget` passes after the TxID-sort adjustment
  • Regression test pass (CI)
  • Reviewer confirms whether a version gate is needed given consensus impact

🤖 Generated with Claude Code

Edited by Son of Odin

Merge request reports

Loading
Loading