perf(bifrost/evm): read all solvency balances in JSON-RPC batches, without nonces

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

Problem

The EVM client (BSC, BASE, AVAX, POL) reports solvency at every solvency_blocks height from inside FetchTxs, so the block at that height can't be handed on until the report is done. For each vault, GetVaultAccount reads the pending nonce, then GetVaultBalances reads each asset with its own TokenManager.GetBalance call: eth_getBalance for the native token and the router's vaultAllowance eth_call for each token. All of these run one after another.

With today's 12 vaults (6 of them retiring after the churn), that is one call per vault plus one per vault asset:

Chain Calls per report At 100 ms per round trip Report every
BSC 12 + 72 = 84 8.4 s 100 blocks
AVAX 12 + 37 = 49 4.9 s 100 blocks
BASE 12 + 30 = 42 4.2 s 100 blocks, about every 20 s at 200 ms blocks

The observations in that block, and the blocks after it, wait that long. A node close to its RPC barely notices; a node further away falls behind and loses the race to observe txs in those blocks.

!5387 (closed) makes the same change for the ETH client.

Changes

  • TokenManager.BatchBalances (new, shared/evm/token_manager_batch.go): makes a list of balance reads as JSON-RPC batches (rpc.Client.BatchCallContext) of at most a given size, next to GetBalance, which builds the same requests.
    • Each element is the request GetBalance makes for that read at the same height, so a batching node reads the same values as one that reads singly.
    • eth_getBalance for the native token, and the router's vaultAllowance eth_call for tokens. The call object has the fields of ethclient's toCallArg (from, to, input), with to as GetBalance sets it, including the zero address for an empty router.
    • The block number is encoded as ethclient does (toBlockNumArg: nil is latest, -1 is pending).
    • A read is left unanswered (nil) when its batch is refused, or its element fails or comes back null or undecodable. A null or bad answer is never taken as a zero balance.
  • All vaults together: ReportSolvency builds the reads of every vault into one list, and readVaultCoins makes them through BatchBalances in batches of at most block_scanner.transaction_batch_size (500 by default for every EVM chain, the setting the scanner already uses for its receipt batches). At the default that is one request per report. A size below 2 makes no batches.
  • Failures fall back to the old path: any read no batch answered is made alone with GetBalance, and an error there is logged and the asset left out, as before.
  • No nonce reads: solvency compares coins only (IsVaultSolvent, Solvency.Coins), so ReportSolvency no longer reads each vault's pending nonce.
  • A vault with no balance read is skipped: when none of a vault's reads is answered, for example with the RPC down, ReportSolvency skips the vault, which also keeps the all-clear from being sent. Before, the failed nonce read skipped it the same way. Without this, an empty coin set would report every funded vault insolvent during an outage.
    • Behaviour changes in edge cases:
      • A vault whose nonce read failed but whose balances were read is now reported. Before, it was skipped.
      • A vault whose nonce was read but none of whose balances were is now skipped. Before, it was reported insolvent with no coins.
    • A vault with some reads answered is reported without the failed assets, as before.
  • Same asset set and order: vaultAssets is the existing selection from GetVaultBalances (this chain's assets, without duplicates, sorted), moved out so both callers share it. GetVaultBalances keeps its signature and results, and now reads through the same batches.
  • unpackVaultAllowance (new): the vaultAllowance decoding, moved out of TokenManager.GetBalance so both paths share it. The unreachable conversion-failure branch now returns an error instead of dereferencing a nil pointer.

GetVaultAccount is unchanged and no longer called by ReportSolvency.

Tests

shared/evm/token_manager_batch_test.go uses a JSON-RPC test server that answers single calls and batches and records every HTTP request.

  • One request: five reads (native token and two tokens for one owner, native token and one token for another) go out in one request, pinned to the requested height, and each balance lands on its read.
  • Batch size: a size of 2 makes two batches of 2 and leaves the fifth read, alone, for the caller. Sizes 0 and 1 send nothing.
  • Unanswered reads: a refused batch, element errors, null results for the native token and for a token, and an empty eth_call result are each left nil, never zero.
  • Same wire requests: at latest, at a fixed height and at pending, the batch sends params byte-identical to those GetBalance sends through ethclient, including a token read without a router, and reads the same balances.
  • toBlockNumArg: gives the expected tag or hex for nil, positive heights, pending, latest, finalized, safe and an out-of-range height.

evm/client_vault_balances_test.go runs an AVAX client against an RPC test server:

  • Across vaults: two vaults at different addresses and routers are read in one request of 5 elements, and each balance lands on the right vault and asset.
  • Fallback: reads that fail in the batch are made alone, and one that always fails is left out.
  • Batch size: a size of 2 sends batches of 2 and 2, then the last read alone.
  • GetVaultBalances: reads this chain's assets once each, in order, in one batch.
  • ReportSolvency:
    • Using the suite's asgard vaults, each holding AVAX and a token, it makes one request, reads no nonce, and reports every vault.
    • With every read failing, it reports none.

Each of these made a test fail:

  • reading a nonce per vault again;
  • removing the skip for a vault with no balance read;
  • ignoring the batch size;
  • sending the call data as data instead of input;
  • removing the null guard on eth_call results;
  • reading every batch at latest;
  • sending a router-less read anywhere but the zero address.

The new code is covered except three guards for an ABI pack or type conversion failing on the fixed router ABI.

Not covered

  • Real TSS module: tss-lib-private isn't available to me, so I ran the evm and shared/evm package tests (with and without -tags=mocknet, and -race on the new tests) against a local stub of it that isn't in this branch. Nothing TSS-related ran, and shared/evm/key_sign_wrapper_test.go (3 tests) needs the real module, so CI needs to confirm both.
  • make lint-ci and make generate: neither runs in full without that module.
    • golangci-lint 1.63.4 with the repo config (default and mainnet tags) on both packages, trunk on the changed files, gofumpt, the chain-client skill check, and the float analyzer are all clean against the stub.
    • lint-tokens.bash didn't run, and this change touches no token lists.
    • The change touches no proto, OpenAPI or go:generate input.
  • Live provider behaviour: not run as part of this branch.
    • A provider that caps batches below transaction_batch_size refuses them, and their reads fall back to single calls until the operator lowers the setting.
    • Each batch has its own HTTPRequestTimeout, so a batch that times out costs at most one timeout before its fallback.
  • POL: no vault holds POL-chain funds today, so it has nothing to report until one does.
Edited by Huginn Ai

Merge request reports

Loading
Loading