perf(bifrost/tron): read solvency vault accounts concurrently
🔎 Visual review: building it for3f4e9942; the link appears here.
Problem
The TRON client reports solvency every 10 blocks from the ordered scan loop (TronBlockScanner.ReportSolvency), so the next block isn't scanned until the report is done. ReportSolvency reads each vault with GetAccount, one vault after another, and each read is itself one call for the TRX balance (/wallet/getaccount) plus one balanceOf eth_call per whitelisted token (USDT by default).
That is two round trips per vault, made in series:
| Vaults with TRON funds | Calls per report | At 100 ms per round trip |
|---|---|---|
| 6 | 12 | 1.2 s |
| 12 (during a churn) | 24 | 2.4 s |
The report runs about every 30 s, so a node further from its TRON API spends a noticeable part of each interval not scanning.
!5387 (closed) (ETH) and !5394 (closed) (the shared EVM client) fix the same pattern with JSON-RPC batches. The TRX balance comes from the HTTP API, which has no batch form, so this change reads the vaults concurrently instead.
Changes
readVaultAccounts(new): reads each vault with the existingGetAccountcall, at mostsolvencyReadConcurrency(8) vaults at a time (errgroupwithSetLimit), and returns the accounts and errors aligned with the vaults.ReportSolvencyuses it in place of the serial loop. Everything after the reads is unchanged: the same coins per vault, the same report order, the sameheight - FinalityBlocks, the same insolvent/all-clear logic, and a vault whose read fails is still logged and skipped.
Each vault's own reads are still made in series, so a report costs about two round trips for up to 8 vaults and four for up to 16, instead of two per vault. The per-vault requests are the same requests as before, so the reported balances don't change.
solvencyLock already serialises ReportSolvency, so concurrent reports can't stack up. The goroutines only read client state (api, rpc, abi, whitelist).
Tests
tron_solvency_reads_test.go runs ReportSolvency against a test server that answers /wallet/getaccount and the token eth_call per address. It holds each TRX read until a given number of them are in flight together, or 1 s has passed, and records the most it saw at once.
- Concurrent reads: four vaults are all read at once, and each report carries its own vault's balance, in vault order.
- Cap: with 10 vaults the server holds reads until all 10 are in flight, which only an uncapped report reaches. At most 8 are seen together, and all 10 vaults are reported.
- Failed read: a vault whose TRX read fails is skipped, and the vaults on either side are reported with their own balances.
- Tokens: with USDT whitelisted, three vaults are read at once and each report has both its TRX and its USDT balance.
Each of these made a test fail:
- reading one vault at a time (
SetLimit(1)); - removing the cap;
- ignoring a vault's read error;
- storing results at the wrong index.
go test -race is clean on the new tests.
Not covered
- Real TSS module:
tss-lib-privateisn't available to me, so I ran thetronpackage tests (with and without-tags=mocknet) against a local stub of it that isn't in this branch. CI needs to confirm. make lint-ciandmake generate: neither runs in full without that module.golangci-lint1.63.4 with the repo config (default,mainnetandmocknettags), trunk on the changed files, the handler and chain-client skill lints, and the float analyzer are clean against the stub.lint-tokens.bashdidn't run, and this change touches no token lists or generated inputs.- Provider limits: a report now makes up to 8 requests at once to the TRON API and JSON-RPC hosts. A provider that rate-limits that burst fails those reads, which are logged and skipped as before, so the vault isn't reported that round.