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

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

Problem

At every ETH height divisible by 20, ReportSolvency reads the balance of every asset of every ETH vault, and it runs inside the block scanner's ordered loop. For each vault, GetVaultAccount reads the pending nonce, then GetVaultBalances reads each asset with its own GetBalance call: eth_getBalance for ETH and the router's vaultAllowance eth_call for each token. All of these run one after another, and the next block's observations wait until they are done.

The number of calls is one per vault plus the sum of each vault's ETH-chain assets. The vaults hold 5 to 22 such assets each. During the migration after a churn there are 12 vaults, about 200 balance reads and 12 nonce reads in all. At 100 ms per round trip that is about 20 s of scanning stopped every 20 ETH blocks (4 minutes). A node close to its ETH RPC barely notices. A node further away falls several blocks behind for that time.

Falling behind costs slash points. A tx in one of the next blocks can reach consensus in both its unfinalized and final forms before the delayed node sends its observation. The late observation then refunds only one of the two, so the node keeps net points for every such tx.

Changes

  • One list of reads for all vaults: ReportSolvency builds the balance reads of every vault and readVaultCoins makes them together, so a solvency report costs one round trip per batch instead of one per asset. At the default batch size that is one request.
  • Batches capped by transaction_batch_size: batchBalances sends at most block_scanner.transaction_batch_size reads per batch (500 by default for ETH). That is the existing setting the EVM scanners use to size their batches, so an operator can match a provider's batch limit without a new key, and a refused batch costs only its own reads. A size below 2 makes no batches.
  • Same requests as before: each batch element is the request GetBalance makes for that read at the same height, so batching and non-batching nodes read the same values.
    • eth_getBalance for ETH and the vault router's vaultAllowance eth_call for tokens.
    • The block number is encoded as ethclient does (toBlockNumArg: nil is latest, -1 is pending).
    • The call object has the same fields as ethclient's toCallArg (from, to, input).
  • Failures fall back to the old path: any read a batch doesn't answer is made alone with GetBalance, and an error there is logged and the asset left out, as before. That covers a refused batch, an element error, and a null or undecodable result. A null or bad result is never read as a zero balance.
  • 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 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

ethereum_vault_balances_test.go uses a JSON-RPC test server that answers single calls and batches, keeps balances per owner address, and records every HTTP request.

  • ReportSolvency:
    • With the fixture's vaults, it makes one request, reads no nonce, and reports every vault.
    • With every read failing, it reports none.
  • All vaults in one batch: two vaults at different addresses and routers are read in one request of 5 elements. Each balance lands on the right vault and asset, and each vault's token reads go to its own router.
  • Batch size: a size of 2 splits the 5 reads into requests of 2, 2 and 1. Sizes 0 and 1 make 5 single requests.
  • Refused batch among several: the refused batch's reads are made alone, and the other batch's answers are kept.
  • Single-vault path (GetVaultBalances):
    • one batch per vault;
    • a failed element read alone, and one that always fails left out;
    • a server that refuses batches;
    • null and empty results read again alone;
    • a single-asset vault and a vault without a router address.
  • Same wire requests: at latest, at a fixed height and at pending, the batch sends params byte-identical to those GetBalance sends through ethclient for ETH and two tokens, 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.

The suite's existing mock RPC server answers single calls only. It now refuses a batch with HTTP 400, as a provider without batch support does, so a batch sent to it can't crash its handler silently.

The tests cover 93 of the 95 statements in ethereum_vault_balances.go. The two left are guards for an ABI pack or type conversion failing on the fixed router ABI. Each of these made a test fail:

  • forcing the batch to read latest;
  • sending the call data as data instead of input;
  • removing the null guard on eth_call results;
  • ignoring the batch size;
  • reading a nonce per vault again in ReportSolvency.

The outage test failed before the skip was added: 4 vaults were reported insolvent with no coins.

Not covered

  • Real TSS module: tss-lib-private isn't available to me, so I ran the ethereum package tests (with and without -tags=mocknet), and -race on the balance and solvency tests, against a local stub of it that isn't in this branch. Nothing TSS-related ran, so CI needs to confirm that.
  • 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), trunk on the changed files, gofumpt, and the lint.sh steps that can load these packages 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 against a live ETH node.
    • 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.
  • Other EVM chains: the shared evm client has the same serial loop in its own GetVaultBalances and ReportSolvency. It's left for a follow-up so this stays small.
Edited by Huginn Ai

Merge request reports

Loading
Loading