perf(bifrost): fetch only the header for BASE's safe block height

Needs an explicit OK on a small behavior change. See "What changes" below.

Why

EthRPC.GetBlockHeightSafe (bifrost/pkg/chainclients/shared/evm/eth_rpc.go) is BASE's mainnet GetHeight (evm_block_scanner_mainnet.go), called every scanner iteration. It used BlockByNumber("safe"), which fetches and JSON-decodes every transaction in the block only to read .Number(). In a 120 s production bifrost profile this was 1.65 s / 3.4 % of CPU. HeaderByNumber("safe") returns the same number. Other EVM chains already use eth_blockNumber, and ETH has its own path; neither is touched.

What changes

On success the result is identical. The difference is in which failures the height lookup can hit. Besides transport errors, NotFound and a bad header (all unchanged), BlockByNumber also fails when:

  • a transaction doesn't decode (e.g. an unsupported tx type),
  • the tx list or uncle list disagrees with the header,
  • the uncle fetch fails,
  • the larger full-tx response times out or exceeds a provider's size limit.

How the scanner handles one malformed safe-head block X (bifrost/blockscanner/blockscanner.go):

today (BlockByNumber) with this MR (HeaderByNumber)
while X is the safe head GetHeight fails: log, backoff, loop. The scanner stalls even when it's far behind X, and keeps reporting healthy. It also skips solvency and network-fee reporting that iteration. height is returned, and the scanner keeps scanning good blocks up to X
when the cursor reaches X (still stalled) FetchTxs(X) → BlockByNumber(X) fails as before. The scanner marks itself unhealthy, retries, and is eligible for skip-ahead after MaxResumeBlockLag

Correctness is unaffected: every scanned block is still fully fetched and validated by FetchTxs. Transient body errors only ever delayed height discovery by one backoff. I'd argue the new behavior is the better one, but it is a change, so it is kept out of the strictly behavior-neutral !5222 (merged).

The height is also read by the halted-chain SolvencyCheckRunner (it skips the tick on error) and by signer instant observation (it falls back to the last scanned height on error). Neither depends on the body checks.

Benchmark

Fake node serving a 200-tx safe block, -count 10, AMD Ryzen Threadripper 3960X:

                      │     base      │               header                │
                      │    sec/op     │   sec/op     vs base                │
GetBlockHeightSafe-48   17727.5µ ± 1%   908.2µ ± 2%  -94.88% (p=0.000 n=10)

                      │      base      │                header                │
                      │      B/op      │     B/op      vs base                │
GetBlockHeightSafe-48   1471.76Ki ± 0%   69.78Ki ± 1%  -95.26% (p=0.000 n=10)

                      │    base     │               header               │
                      │  allocs/op  │ allocs/op   vs base                │
GetBlockHeightSafe-48   9582.0 ± 0%   232.0 ± 0%  -97.58% (p=0.000 n=10)

Testing

  • New eth_rpc_height_test.go. GetBlockHeightSafe previously had no tests.
    • TestGetBlockHeightSafe (committed first, passes before and after): the number for 0-tx and 3-tx blocks; null → NotFound; RPC error; undecodable header.
    • TestGetBlockHeightSafeIgnoresBody: a safe block with an unsupported tx type. Height succeeds, while BlockByNumber of the same block still fails with ErrTxTypeNotSupported. It fails on the old code with fail to get block: transaction type not supported.
  • go test -tags mocknet on shared/..., evm/..., blockscanner/...: pass. golangci-lint v1.63.4 (repo config) and gofumpt: clean.

Follow-up from the profiling in !5222 (merged).

🤖 Generated with Claude Code

Merge request reports

Loading
Loading