perf(bifrost/evm): extend the logsBloom filter to BASE and stop fetching for known contracts

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

🔎 Visual review of de2d783c: ⏳ out of date, newer commits were pushed. Updating it for 26f2dc6e.

Problem

The BSC filter (!5321 (merged)) avoids fetching receipts for blocks that can't produce an observation, with two gaps:

  • BASE has no filter. It fetches receipts for every block, about one call every 2 seconds per node, and most of those blocks need nothing from them.
  • The filter fetches more than it needs to. A tx to a router or whitelisted contract forces a fetch even when no router is in the block's logsBloom, and that fetch can't produce an observation from that tx. In the BSC blocks below, these made up 14 of 49 fetches.

Changes

  • BASE uses the filter. A deposit tx is priced 0, as its receipt reports. EffectiveGasTip returns 0 for one, so base fee + tip would give the base fee, and every BASE block has a deposit tx.
  • txMayBeObserved drops the isToValidContractAddress(to, true) check.
  • An all-zero bloom on a block with txs now fetches. It can mean no tx emitted a log, or that nothing filled the bloom in, and the block alone can't tell them apart. Dropping the check leaves the bloom as the only thing that finds a direct router deposit, so this covers the case where it was never filled in. Blocks with no txs still skip.
  • POL is unchanged because I haven't checked it against live blocks.
  • AVAX stays out, see below.

Why dropping the check is safe

For a tx to a router or whitelisted contract, with no router in a bloom that isn't all zero:

Case What the receipts path does Filter
Succeeded, from a non-vault getTxInFromSmartContract calls GetTxInItem, which skips every log unless addressValidator(&item.Address, false) accepts it. false leaves out the whitelist, so only router logs count. No coins are set, so it returns nothing. Skips
Value sent to the router with no log The native value is only added when isVaultTransfer is true, which GetTxInItem never returns. No coins, no observation. Skips
A whitelisted contract emits a Deposit-shaped log Ignored, because the emitter isn't a router. Skips
Failed, from a non-vault getTxInFromFailedTransaction returns nothing unless the sender is a vault. Skips
From a vault, failed or not Observed if failed. Fetches through the sender check, as before

The bloom check and the parser use the same router list, GetContracts(chain, false), so the filter can't rule out a router the parser would accept.

AVAX can't use this filter

The first version of this MR also filtered AVAX. That was wrong, and a later commit takes it out. An AVAX header's logsBloom doesn't describe that block's own logs:

AVAX BASE BSC
Blocks checked 100 25 40
Header bloom equals the bloom of its own receipts' logs 6 25 40
Header bloom contains its own receipts' logs 22 25 40
Blocks with logs but an all-zero header bloom 30 0 0
  • Cause: a header's bloom equalled the combined logs of the blocks it newly settles, per its settledHeight, on 32 of 32 headers I could check. Those logs usually show up 5 to 8 blocks later. This fits ACP-194, which lists logsBloom with the other results of settled blocks but doesn't define how it's computed.
  • Why the first version's results looked clean: its 120 recent AVAX txs all fetched because they went to a router or a whitelisted contract, or came from a vault, not because the bloom found them. With the check dropped and the all-zero fetch in place, 22 of the 43 inbounds among them are in blocks the filter would skip.
  • Other endpoints: a public C-Chain endpoint returned the same logsBloom and receiptsRoot on the 6 blocks I compared.

AVAX keeps its batched lookups, and a test now requires that.

Live comparison

I ran the filter and the receipts path side by side on blocks just behind the tip. For every skipped block, the receipts path had to produce no observations and the same gas prices. The runs used the default token whitelist and aggregators, all 47 current active and inactive vaults, and mimirs set so that the contract whitelist was off and direct ERC20 inbounds were on. On each block they scored the filter with the known-contract check and with both changes above, so "before" and "after" judge the same blocks.

BSC BASE
Blocks 600, sampled over about 27 minutes 500 consecutive
Txs 48,155 88,545
Skipped before 551 (91.8%) 433 (86.6%)
Skipped after 565 (94.2%) 433 (86.6%)
Fetched before 49 67
Fetched after 35 67
Fetched after, with the router in the bloom 35 67
All-zero blooms 0 0
Missed observations 0 0
Skipped blocks with a gas price difference 0 0
Filter cost per block 0.29 ms 0.51 ms
  • BSC: 29% fewer receipt calls than the filter on develop in this sample, which covers about 27 minutes at one time of day.
  • BASE: an earlier 500-block run, before the two later changes, skipped 332, with 168 fetches all hitting the bloom. Blocks averaged 177 to 228 txs and about 55% to 72% of bloom bits set across the two runs, so a large share hit the bloom by chance. Each run had one observation, so nearly all those fetches were false positives. Busier periods will skip less.
  • Deposit txs: 504 in the later run and 502 in the earlier one, all priced the same as their receipts.
  • Known txs: I also read recent THORChain txs from Midgard, 119 on BSC (48 inbound) and 120 on BASE (47 inbound). All produced an observation and none was in a block the filter skips.

This builds on a throwaway merge with the first version of this MR. The rules for BSC and BASE are the same in this branch, and I didn't re-run them on its final commit.

What this gives up

  • A wrong bloom: a direct deposit to a router used to fetch even if the block's logsBloom was wrong. Now the bloom is the only thing protecting direct router deposits, as it already was for deposits through other contracts. The all-zero fetch covers a bloom nothing filled in, but a bloom that's wrong and not empty still skips. Catching that would mean comparing the bloom with the receipts.
  • Blocks with no logs: a block with txs and an accurate all-zero bloom costs a receipts call it doesn't need. None appeared in 600 BSC or 500 BASE blocks.
  • A vault outside PubKeyManager: the receipts path clears the signer cache for any failed tx. A failed tx to a router, signed by a vault that has since been pruned from PubKeyManager, now keeps its cache entry if its block is skipped. The filter already had that gap for such a tx sent anywhere other than a known contract.

Tests

receipts_gate_test.go adds 10 tests and narrows the "other chains keep fetching" test to POL. On both BSC and BASE they cover:

  • Skips: a tx to a router or whitelisted contract with no logs, failed, with a Deposit-shaped log from a non-router, or with a token transfer, in both whitelist modes. The receipts path must also observe nothing.
  • Fetches: the same txs with a real router log, a vault's tx to a known contract, and a deposit behind an all-zero bloom.
  • Replay: 300 random blocks in each whitelist mode with truthful blooms, once through getTxIn and once forced down the receipts path. Observations and gas prices must match.

They also cover deposit tx pricing and senders on BASE, and AVAX always looking up receipts. I broke the code 33 different ways, one at a time, and the tests caught each one, including putting AVAX back in three ways. Four of them break the assumptions behind dropping the check, outside the filter: the parser reading whitelisted logs, the parser reading all logs, a failed tx observed for any sender, and value sent to a router being observed.

Not covered

  • Real TSS module: tss-lib-private isn't available to me, so I ran the evm package, with -race on the getTxIn tests, against a local stub of it that isn't in this branch. CI needs to confirm the rest.
  • make lint-ci: not run. gofumpt and go vet are clean on the evm package against the stub.
  • History: this is under an hour of recent blocks, not a long replay. I used only the current vault set, so the older known txs may predate some vault rotations.
  • Tx types on live blocks: failed vault txs and direct ERC20 inbounds are covered by unit tests only.
  • Scan speed: I haven't measured the effect on real scan rates, so this MR claims fewer calls, not a specific speedup.
Edited by Huginn Ai

Merge request reports

Loading
Loading