fix(thorchain): canonicalize deposit and solvency asset identities

🔎 Visual review: building it for 0aad3c81; the link appears here.

Problem

  • MsgDeposit uses the coin's asset struct exactly as the client sent it. A client can send a symbol in mixed case: xchainjs keeps a contract address's case, for example USDT-0xdAC17F958D2ee523a2206206994597C13D831ec7. Nothing normalizes it.
  • String() and Equals hide the case, but struct == and map[common.Asset] keys do not. The asset reaches the txout and the swap queue as sent.
  • On mainnet, trade withdraw CD64487359835AEE6D3984817AEBD0A0EF828F00D47A0BC3FDDD77EEED44607B (height 28720381) did this. calculateNetworkSolvency failed with "not found in tracking map" until the payout was observed, about 1,800 blocks later. Over-solvency processing and /thorchain/vaults/solvency stopped for that time. The payout itself was paid.
  • Anyone can repeat this with one deposit. The 3.22.0 migration in !5348 (merged) reads solvency, so such a deposit just before the upgrade height makes it skip its pool legs.
  • Asset.IsWhitelisted() uses a struct-keyed map, so a mixed-case RUJI, NAMI, LQDY, XUSK or AUTO counts as a derived asset. It then gets the derived min-slip and the derived fee branch on refunds.

Fix

  • The deposit handler derives the working asset from the coin's string form with common.NewAsset, which uppercases the chain, symbol and ticker and takes the ticker from the symbol. Every check, debit, memo and queued tx uses that asset. The tx ID still comes from the original message bytes.
  • A positive deposit is refused when normalizing would change the bank denomination it debits. This covers RUNE, TCY, TOR, RUJI and the other special denominations sent with a wrong or empty ticker. The error names the ticker sent and the ticker expected.
  • IsWhitelisted() compares with Asset.Equals, like the other asset helpers.
  • Solvency keys its balances by asset.String(), the key the pool store uses, so commitments already stored in mixed case still resolve to their pool. The asset it returns for each pool is canonical. A missing pool balance now returns an error instead of dereferencing nil.

Limits

  • Records already stored keep their spelling. The solvency reader tolerates them, and a pending mixed-case txout clears once it is paid.
  • Other struct-keyed asset maps are unchanged. With deposits normalized, only node-built data and records written before the upgrade can still be in mixed case.
  • A client that sends a wrong or empty ticker for a special-denomination native asset is now refused. Mixed case in the symbol alone is accepted.

Scope

  • common/asset.go (IsWhitelisted), x/thorchain/handler_deposit.go, x/thorchain/manager_network_current.go (calculateNetworkSolvency), and their tests.
  • Consensus-breaking: deposit outcomes and the assets stored in txouts and the swap queue change. No version gate. It must ship in the coordinated 3.22.0 release, in the same sync to private as !5348 (merged).
  • No proto, store or migration changes.

Testing

  • New tests: TestDepositAssetCase, TestDepositTickerPreservesBankDenom, TestTradeWithdrawalSolvency (a raw mixed-case TRADE- withdrawal, the mainnet case), TestSolvencyMixedCaseCommitments and TestWhitelistAssetCase.
  • Mutations: with develop's deposit handler, 5 of the 6 focused tests fail at the txout-asset and debit checks. With develop's solvency function, TestSolvencyMixedCaseCommitments fails with the mainnet error. Without the per-pool normalization, its asset and migration assertions fail.
  • Passed at 0aad3c81d0: the mocknet suites for common, x/thorchain/types, x/thorchain/keeper/v1 and x/thorchain; 186 solvency and migration tests on the untagged (mainnet) build; go vet, gofumpt, git diff --check and the determinism analyzer. Trunk passed on the previous revision, which differed by 4 lines in calculateNetworkSolvency.
  • Not run: the regression and simulation suites (left to CI).

Related: !5348 (merged) (its 3.22.0 migration reads network solvency)

Edited by Huginn Ai

Merge request reports

Loading
Loading