perf(bifrost/evm): read all solvency balances in JSON-RPC batches, without nonces
🔎 Visual review: building it forbd042f21; 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 toGetBalance, which builds the same requests.- Each element is the request
GetBalancemakes for that read at the same height, so a batching node reads the same values as one that reads singly. eth_getBalancefor the native token, and the router'svaultAllowanceeth_callfor tokens. The call object has the fields ofethclient'stoCallArg(from,to,input), withtoasGetBalancesets it, including the zero address for an empty router.- The block number is encoded as
ethclientdoes (toBlockNumArg: nil islatest, -1 ispending). - 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.
- Each element is the request
- All vaults together:
ReportSolvencybuilds the reads of every vault into one list, andreadVaultCoinsmakes them throughBatchBalancesin batches of at mostblock_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), soReportSolvencyno 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,
ReportSolvencyskips 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.
- Behaviour changes in edge cases:
- Same asset set and order:
vaultAssetsis the existing selection fromGetVaultBalances(this chain's assets, without duplicates, sorted), moved out so both callers share it.GetVaultBalanceskeeps its signature and results, and now reads through the same batches. unpackVaultAllowance(new): thevaultAllowancedecoding, moved out ofTokenManager.GetBalanceso 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_callresult are each left nil, never zero. - Same wire requests: at
latest, at a fixed height and atpending, the batch sends params byte-identical to thoseGetBalancesends throughethclient, 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,safeand 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
datainstead ofinput; - removing the null guard on
eth_callresults; - 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-privateisn't available to me, so I ran theevmandshared/evmpackage tests (with and without-tags=mocknet, and-raceon the new tests) against a local stub of it that isn't in this branch. Nothing TSS-related ran, andshared/evm/key_sign_wrapper_test.go(3 tests) needs the real module, so CI needs to confirm both. make lint-ciandmake generate: neither runs in full without that module.golangci-lint1.63.4 with the repo config (default andmainnettags) 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.bashdidn't run, and this change touches no token lists.- The change touches no proto, OpenAPI or
go:generateinput.
- Live provider behaviour: not run as part of this branch.
- A provider that caps batches below
transaction_batch_sizerefuses 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.
- A provider that caps batches below
- POL: no vault holds POL-chain funds today, so it has nothing to report until one does.