perf(bifrost/reporter): read solvency vaults concurrently, and in JSON-RPC batches on ETH/EVM

🔎 Visual review of 7bd70566 (same diff as b33cf46c): explainer, review flags and prompts to ask an AI about the change

Problem

!4597 (merged) moved solvency into a per-chain reporter worker. The block scanner waits in Dispatcher.Process until a cadence-height report has been collected and attested, so the report is in gossip before the next block is scanned. Collect reads the chain's vaults one at a time, though, with one GetVaultAccount call per vault:

  • ETH and the EVM chains read the vault's pending nonce, then each vault asset with its own GetBalance call. During a migration that is a couple of hundred round trips per report on ETH.
  • GAIA makes two gRPC queries per vault in series: bank AllBalances, then auth Account, which solvency doesn't use.
  • Every other chain makes one or two round trips per vault.
Chain Reads per report today At 100 ms per round trip
ETH, 12 vaults × ~15 assets (migration) 12 nonces + ~180 balances ~19 s
BSC/AVAX/BASE/POL, 6 vaults × ~5 assets 6 nonces + ~30 balances ~3.6 s
GAIA, 6 vaults 12 1.2 s
BTC/BCH/LTC/DOGE/TRON/XRP/SOL, 6 vaults 6 0.6 s

Because the scanner waits for Collect at every cadence height, this time is spent on the scan path, as the per-chain ReportSolvency did before it:

  • The chain's next block isn't scanned until the report is attested. On ETH during a migration that holds observations back for about 19 s at each cadence height.
  • TRON and the UTXO chains read balances at the latest block, not at the report height, so the longer a report takes, the more two nodes' reads of the same vault can disagree. !4597 (merged) named this balance fragmentation as an open TRON problem.
  • The ETH/EVM reports make several times more calls than they need to.

!5387 (closed), !5394 (closed), !5395 (closed), !5396 (closed), !5397 (closed) and !5398 (closed) make the same changes against the per-chain ReportSolvency that !4597 (merged) removed. This MR carries them over to the reporter, in less code: one generic change replaces the four per-chain concurrent-read MRs.

Changes

  1. reporter: read vaults concurrently. Collect reads up to solvencyReadConcurrency (8) vaults at once (errgroup with SetLimit), with the accounts and errors aligned with the vaults. Everything after the reads is unchanged: the same order, deferral handling, cadence and all-clear logic. A client can lower its own limit through the optional SolvencyReadLimiter interface. Monero returns 1, because its balances come from the node-local wallet sidecar, so there is no round trip to overlap. GetVaultAccount is now documented as needing to be safe for concurrent calls.
  2. reporter: optional VaultAccountsReader. A client that implements GetVaultAccounts(vaults, height) ([]common.Account, []error) has all of a report's vaults read in one call. A result that doesn't line up with the vaults fails every vault, so the round is retried as if every read had failed.
  3. ETH and EVM (BSC/AVAX/BASE/POL): GetVaultAccounts in JSON-RPC batches, without nonces. Both clients use one shared reader in shared/evm (VaultBalanceSource, VaultAssets, BalanceBatcher).
    • The reads of every vault go out together in batches of at most block_scanner.solvency_batch_size. That setting is new; 0 (the default) uses transaction_batch_size (500 by default), and below 2 means no batching. It lets balance batches be sized apart from the scanner's own batches when a provider limits them differently.
    • Each element is the request GetBalance makes at the same height: eth_getBalance for the gas asset, and the router's vaultAllowance eth_call for tokens. ETH's GetBalance refuses a token read without a router, so ETH leaves those out of batches too.
    • Any read a batch doesn't answer is made alone with GetBalance, up to 8 at a time. That covers a refused batch, an element error, and a null or undecodable result.
    • Solvency compares coins only, so the nonce isn't read. A vault with any balance left unread gets an error, so an RPC failure can't report a vault insolvent: a missing asset would otherwise read as a zero balance.
    • GetVaultAccount reads one vault through GetVaultAccounts, so both fail the same way. GetVaultBalances, whose only caller was GetVaultAccount, is removed from both clients.
  4. Docs: the chain-client skill's interface reference and docs/chain-clients/README.md describe the concurrency requirement and both optional interfaces.
  5. GAIA: GetVaultAccount makes the bank AllBalances query alone, at the solvency height. GetAccountByAddress still returns the full account. A vault address with no auth account used to fail the read, and the vault was skipped. It now reads as its bank balance, which is empty, so a vault THORChain thinks is funded is reported.
  6. UTXO: a vault read fails when an unconfirmed output's sender can't be checked. GetAccount counts an unconfirmed output only when asgard sent it. isFromAsgard returned false when a transaction lookup or the asgard address lookup failed, so the output was dropped and the vault could be reported insolvent instead of skipped. That predates this MR, but concurrent reads make a rate-limited lookup more likely. GetAccount now uses checkFromAsgard, which returns those failures, and fails the read on one. The signer keeps isFromAsgard.

A report then costs:

  • about one round trip on ETH and EVM (one per batch of up to 500 reads);
  • one round trip for up to 8 vaults, and two for up to 16, on the other chains;
  • on GAIA, half the queries it made before.

Concurrency

Collect runs on the chain's own reporter worker. Each concurrent read goes through the chain client's existing GetVaultAccount. I checked each client's read path:

  • UTXO: the RPC client, and the asgard address cache, which is an atomic pointer.
  • TRON: the HTTP API client.
  • XRP: xrpl-go builds a new request per call, and bifrost's retrying HTTP client clones a request before retrying it.
  • SOL: SolRPC builds a new request and HTTP client per call.
  • GAIA: gRPC clients.

None of these reads writes shared state. The reporter's deferral bookkeeping stays in the serial loop after the reads.

Tests

  • Reporter. Tests (reporter_solvency_reads_test.go) use a client that holds each read until a given number are in flight together, or 1 s has passed, and records the most it saw at once. They check that:
    • four vaults are read at once, each at the report height, and reported in vault order with their own balances;
    • with 10 vaults the client holds reads until all 10 are in flight, which only an uncapped Collect reaches; at most 8 are seen together, and all 10 are reported;
    • a vault whose read fails is skipped, and the vaults on either side are reported with their own balances;
    • a client's own read limit is honoured (1, and 0 treated as 1), and a limit above 8 is still held to 8;
    • a VaultAccountsReader gets one call with all of the chain's vaults (and only those) at the report height, and no vault is read alone;
    • a misaligned GetVaultAccounts result reports nothing and leaves the cadence cursor unmoved.
  • Shared EVM reader (shared/evm/vault_balances_test.go): reads a batch doesn't answer are made together, and a vault with one balance unread gets an error while the others are reported.
  • Existing mocks. The existing mock clients record heights and call counts. They now lock those, since Collect reads concurrently.
  • ETH and EVM.
    • The batch tests from !5387 (closed) and !5394 (closed) carry over: batch splitting, refused batches, element errors, null and empty results, params matching the single calls, and block-number encoding. Those that called GetVaultBalances now read one vault through the shared reader.
    • New GetVaultAccounts tests check that every vault is read in one request with no eth_getTransactionCount, each read at the report height, with each vault's own balance.
    • Further tests check that a vault with no balance read answered gets an error, that a vault with one token unread gets an error while its gas asset still reads, and that solvency_batch_size sizes the batches.
  • GAIA. GetVaultAccount makes no auth read and asks for the solvency height.
  • UTXO (utxo/vault_account_test.go): an unconfirmed output from asgard is counted and one from elsewhere isn't; a failed lookup of the transaction or of its vin 0 transaction fails the read, and so does having no asgard addresses. The three failure cases failed before the fix.

Each of these made a test fail (21 mutants on the first revision and 7 on the shared-reader revision; none survived):

  • reporter:
    • reading one vault at a time;
    • removing the cap;
    • reading XMR concurrently;
    • storing results at the wrong index;
    • dropping a read error;
    • reading at the wrong height;
    • ignoring VaultAccountsReader;
    • accepting a misaligned batch;
    • passing the reader the wrong height.
  • ETH and EVM, each:
    • not failing a vault with no read;
    • reading at the latest block;
    • one batch per vault;
    • misaligning coins;
    • reading the nonce again.
  • GAIA:
    • reading the auth account again;
    • reading at the latest height.
  • After the shared-reader revision:
    • accepting a partly read vault (shared, ETH and EVM tests);
    • making unanswered reads one at a time;
    • ignoring a client's read limit, or letting it exceed the default cap;
    • batching ETH token reads that have no router;
    • ignoring solvency_batch_size.

go test -race is clean on bifrost/reporter, ethereum, evm, shared/evm, gaia and utxo, with and without -tags=mocknet. The concurrency tests assert that reads overlap rather than an exact count, so a slow runner doesn't fail them.

Not covered

  • Real TSS module: tss-lib-private isn't available to me, so I ran these packages against a local stub of it that isn't in this branch. bifrost/observer, bifrost/pkg/chainclients/monero and the rest of bifrost don't load against that stub, so CI needs to confirm them. On the revision before this rebase, CI's unit tests passed on every package this MR changes, including monero and config. Monero's only change is SolvencyReadConcurrency and its compile-time assertion.
  • config tests: they stop on a missing seed file in my environment, before and after this change; the package builds and lints clean.
  • make lint-ci and make generate: neither runs in full without that module. golangci-lint 1.63.4 with the repo config (default, mainnet and mocknet tags) is clean on the changed packages against the stub, the chain-client skill lint passes, and prettier passes on the changed docs and default.yaml.
  • Provider limits:
    • On the chains read concurrently, a report now makes up to 8 requests at once. A provider that rate-limits that burst fails those reads, which are logged and skipped as before.
    • On ETH and EVM, a provider that refuses batches gets every read made alone, up to 8 at a time, with a warning at each cadence height. One with a lower batch-size limit can be met with solvency_batch_size. ETH didn't use transaction_batch_size before, so its batches are 500 reads by default.
    • A batch that times out is also retried as single reads. Those cost time only while the provider stays unresponsive, when the scanner can't fetch blocks either. Once it answers, the remaining reads finish at normal speed. Skipping them would leave a node whose provider answers single reads but times out a large batch with no ETH report at all.
Edited by Huginn Ai

Merge request reports

Loading
Loading