thorchain: halve quorum handler time (verify attestation sigs ahead of the block, save voter once)

🔎 Visual review of 36c83eac: explainer, review flags and prompts to ask an AI about the change

Summary

Two independent speedups to the enshrined-bifrost quorum handlers. They are separate commits so they can be split if reviewers prefer:

  1. Save the observed-tx voter once per MsgObservedTxQuorum (1a41e81a9f, consensus-visible gas). The handler applied each attestation through processTx{In,Out}Attestation, which re-marshalled and rewrote the whole voter once per signer. The voter grows with every signer, so a 100-signer message wrote O(N²) bytes. Attestations are now applied in memory (applyTx{In,Out}Attestation) and the voter is saved once. The legacy single-signer MsgObservedTx{In,Out} keep the per-attestation save.
  2. Verify attestation signatures ahead of the block (c3bf6a6fe1, gas-neutral). secp256k1 verification is ~76% of quorum handler CPU (pprof of BenchmarkPerfObservedTxQuorumIn/batch-all). common.VerifyAttestationSignature memoizes successful verifications. ebifrost warms that cache in the background whenever bifrost submits tx, network-fee, solvency, errata and price-feed attestations, so by the time a block includes them the handlers usually find them already verified.

Consensus impact

  • Store contents and handler outcomes are unchanged. Nothing inside the attestation loop reads the voter back from the store, so the single final write leaves the same voter the per-attestation writes did. TestSingleWriteMatchesPerAttestationWrite runs the same message sequence through the new handler and through the old per-attestation-write flow, inbound and outbound: below quorum, crossing quorum with a duplicate, a late signer, then an all-duplicate message. After every message it compares the entire thorchain KV store byte for byte. Disabling the final write makes it fail.
  • gas_used changes (commit 1 only). Multi-attestation MsgObservedTxQuorum uses 45–70% less gas. gas_used feeds LastResultsHash, so commit 1 must ship at an upgrade height like any other consensus change. Single-attestation messages and every other message type are gas-identical. scripts/perf/ab.sh prints VERDICT: REVERT for this reason only: its guard rejects any gas change by design.
  • Signature cache (commit 2) cannot change results.
    • Only successful verifications are stored.
    • The key is a length-prefixed SHA-256 of (pubkey, signature, exact signed bytes), so a hit returns exactly what re-verifying would.
    • Misses verify inline as before. Signature checks consume no gas, so the cache's contents affect wall time only. The gas/op columns below are identical warm and cold.
    • Memory is bounded at two 32k-entry generations.
    • Warming runs in at most GOMAXPROCS/4 goroutines and is skipped, never queued, when they are busy. It never blocks bifrost's gRPC call, and it works from copies, so cached ebifrost items are not mutated. Solvency.GetSignablePayload fills in an empty Id; the existing ownership race test caught this during development.

Measurements

scripts/perf/ab.sh obsquorum 'BenchmarkPerf(ObservedTxQuorum|PriceFeedQuorumBatch)' HEAD~1 used ROUNDS=10 interleaved base/candidate rounds, -tags=mainnet, GOMAXPROCS=8 pinned to CPUs 32–39, and the mainnet-shaped fixture. Base is develop at ccfa609476.

Benchmark develop this MR, sigs warmed this MR, cold sigs¹ gas/op develop → MR
ObservedTxQuorumIn/batch-quorum 7.70 ms 4.23 ms (−45%) 7.15 ms (−7%) 9.43M → 4.86M (−48%)
ObservedTxQuorumIn/batch-all 9.51 ms 4.99 ms (−47%) 8.68 ms (−9%) 18.83M → 5.65M (−70%)
ObservedTxQuorumOut/batch-quorum 7.87 ms 4.74 ms (−40%) 7.42 ms (−6%) 10.10M → 5.51M (−45%)
ObservedTxQuorumIn/late-1-after-quorum 573 µs 333 µs (−42%) — unchanged
ObservedTxQuorumIn/fresh-1 543 µs 334 µs (−39%) — unchanged
ObservedTxQuorumOut/fresh-1 537 µs 350 µs (−35%) — unchanged
PriceFeedQuorumBatch 6.85 ms 3.49 ms (−49%) 6.73 ms (−2%, within ±10% noise) unchanged

All time deltas are p=0.000, n=10. Allocations drop 8–21% on the multi-attestation cases.

¹ Cold means the cache is emptied before every op, as for attestations this node never received before the block. Those rows isolate commit 1's effect on time.

What this means on mainnet

Measured over the last 60 blocks at the time of writing (111 active nodes, ~6.2 s blocks):

  • MsgObservedTxQuorum is 62% of all block gas. The largest messages use ~44–52M gas against 18.8M in the fixture, because a final-round voter also carries the pre-confirm entry. So the mainnet gas saving should exceed the fixture's 70%.
  • About 4.3 observed-tx quorum messages and one price-feed batch go into each block. That puts quorum handling at roughly 20–30 ms per block today, and about half that with this MR (estimated from the per-message numbers, not measured on a node).
  • This is capacity and gas headroom, not a user-visible latency change. Observation already finalises within one or two blocks of L1 confirmation.

Tests

  • common: AttestationSigCacheSuite, covering valid, invalid, other key and other bytes, length-prefix boundaries, malformed keys not cached, and generation rotation.
  • x/thorchain/ebifrost: each Send* warms the payload its handler verifies; warming is skipped when slots are full; nil, empty and panicking attestations are tolerated; cached solvency is not mutated. Runs clean with -race -count=3.
  • x/thorchain: TestSingleWriteMatchesPerAttestationWrite (above). Existing quorum suites pass. Cold-signature variants are added to the perf benchmarks.
  • go test -tags=mocknet ./x/thorchain/... ./common/... passes.
  • Changed-line coverage, from a merged profile through scripts/test-quality-coverage.py, is 100% in each package: common 33/33, x/thorchain 47/47, x/thorchain/ebifrost 37/37 statements.
  • tools/analyze -rand -map_iteration is clean.
  • Regression: make test-regression with CI=1 EXPORT=1 DEBUG=1 passes all 192 suites (0 failed, 0 retried), and the export regeneration leaves test/regression/mnt/ unchanged. Fixtures zero gas_used unless UNFILTER_EXPORT=1, so none move.

No regression suites are added because there is no behavior change; the export fixtures are the check. No docs change: no Mimir key, API, memo or protocol mechanic changes.

🤖 Generated with Claude Code

Edited by Huginn Ai

Merge request reports

Loading
Loading