perf(bifrost/utxo): read solvency vault accounts concurrently

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

Problem

The UTXO client (BTC, BCH, LTC, DOGE, ZEC) reports solvency from inside FetchTxs, before it returns the block's transactions, so the observations in that block wait for the report. ReportSolvency reads each vault with GetAccount, one vault after another:

  • BTC, BCH, LTC and DOGE: one listunspent call per vault.
  • ZEC: getblockcount and getaddressutxos per vault.
  • Any unconfirmed UTXO not known to be a self transaction adds a getrawtransaction lookup of the tx and its first input (isFromAsgard).
Chain Reports Vaults with funds today Calls per report At 100 ms per round trip
BTC every other block 11 11 1.1 s
BCH every other block 11 11 1.1 s
LTC every 10 blocks 6 6 0.6 s
DOGE every 10 blocks 6 6 0.6 s
ZEC every 10 blocks 10 20 2.0 s

During a churn there are more vaults, and every report costs more. A node far from its daemon delivers the observations of those blocks that much later than nodes close to theirs.

!5387 (closed), !5394 (closed), !5395 (closed) and !5396 (closed) make the same change for the ETH, EVM, TRON and GAIA clients.

Changes

  • readVaultAccounts (new): reads each vault with the existing GetAccount call, at most solvencyReadConcurrency (8) vaults at a time (errgroup with SetLimit), and returns the accounts and errors aligned with the vaults.
  • ReportSolvency uses it in place of the serial loop. Everything after the reads is unchanged: the same coins per vault, the same report order and height, the same insolvent/all-clear logic, and lastSolvencyCheckHeight is stored as before. A vault whose read fails is still logged and skipped.

A report now costs about one round trip for up to 8 vaults and two for up to 16 (twice that on ZEC), instead of one or two per vault. The per-vault requests are the same requests as before.

solvencyLock already serialises ReportSolvency. The goroutines share the RPC client, which is safe for concurrent use, and the asgard address cache, which is an atomic pointer. They read block metas from storage and write nothing back.

On ZEC each vault still reads its own getblockcount, as before, so a block that arrives during a report can give vaults different confirmation counts, just as it could when they were read in series. Solvency only uses confirmations to tell mempool UTXOs (0 confirmations) from the rest.

Batching (one JSON-RPC request for all vaults) was the other option. It would also put the unconfirmed-UTXO lookups after the batch, which needs a larger change to GetAccount, so this MR uses concurrent reads like !5395 (closed) and !5396 (closed).

Tests

utxo_solvency_reads_test.go adds tests to CoverageBackfillSuite (BTC). They point the client at a test RPC server that answers listunspent with one confirmed UTXO per vault address. The server holds each 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 listunspent fails is skipped, and the vaults on either side are reported with their own balances.

The existing ReportSolvency tests (all-clear gate, halt state) still pass.

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 solvency tests.

Not covered

  • Real TSS module: tss-lib-private isn't available to me, so I ran the utxo package tests (with and without -tags=mocknet) against a local stub of it that isn't in this branch. CI needs to confirm.
  • Other chains in tests: the new tests run on BTC only. The change is in shared code above the per-chain GetAccount, which the existing per-chain tests cover.
  • 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), trunk on the changed files, the chain-client skill lint and the float analyzer are clean against the stub. lint-tokens.bash didn't run, and this change touches no token lists or generated inputs.
  • Daemon load: a report now makes up to 8 requests at once. bitcoind and its forks run listunspent under the wallet lock, so the daemon still answers them one at a time, but the network round trips overlap. A daemon whose RPC work queue (rpcworkqueue) is already near full could refuse some; those reads fail and the vault is skipped that round, as with any read error today.
Edited by Huginn Ai

Merge request reports

Loading
Loading