Skip to content

feat(adapters)!: resolve chain ids through a governed ChainIdRegistry - #13

Merged
xavikh merged 4 commits into
mainfrom
registry-audit
Sep 3, 2026
Merged

xavikh merged 4 commits into
mainfrom
registry-audit

Conversation

@xavikh

@xavikh xavikh commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Targets main.

Every adapter carried its chain-id table in bytecode, so adding a chain to a live deployment meant redeploying the adapter and re-attaching it on the controller through governance on both sides — for a two-integer lookup.

The table is a contract now.

The shape

One registry per bridge protocol. CCIP addresses chains by its own selector, a future LayerZero adapter by something else, and the two are unrelated. The registry is deployed before the adapter and baked in as an immutable, so the send path resolves it correctly under delegatecall.

The table is governable; the binding is not. Adding a chain is one setChainIdPair. Moving an adapter to a different registry still means a new adapter.

BaseAdapter owns both mappers now, non-virtual, turning the registry's 0 into the revert IBaseAdapter requires. CCIPAdapter loses both constant tables — its call sites are unchanged, because they already went through those functions.

What falls out

TestnetCCIPAdapter deleted it existed only to replace the mainnet-only table for testnets. Test_Deploy.s.sol now deploys a registry and the shipping adapter, so a testnet rehearsal exercises the contract that ships
ChainIds.sol / CCIPChainIds.sol deleted no importer left in src/; values moved to test/fixtures/chains.json
OversizedSelectorCCIPAdapter deleted a selector wider than uint64 is reachable through config now

Tests seed their registries from chains.json rather than from a Solidity table only the tests can see.

The trade, stated plainly

Selector correctness moved out of audited bytecode and into config. A wrong-but-nonzero selector is caught by nothing — resolution proves a lane exists, never that it points at the right chain.

That was impossible before, when the table was audited bytecode, and it is the price of making the table configurable. The repair is the same trade in the other direction: one setChainIdPair through governance rather than replacement adapters on both sides.

MANAGE_CHAIN_ID_REGISTRY_PERMISSION warrants the same governance rigor as MANAGE_CONTROLLER_CONFIG_PERMISSION: its holder can repoint a live lane in one call, redirecting both outbound sends and inbound attribution. Monitoring that watches chainToAdapter will not see it — watch ChainIdPairSet too.

Review

Four adversarial agents went at this (deploy/ops, security, simplicity, docs). No fail-open path was found. Traced and confirmed clean:

  • The registry call compiles to STATICCALL — a malicious or buggy registry cannot reenter or corrupt controller state. Its entire capability is: return a number, revert, or burn gas.
  • receiveMessage re-anchors on the payload's originChainId, so no repoint can make a message authenticate as the wrong origin. A repoint can only deny.

Their findings are folded into this commit: the uint64 bound, the corrected claims about what resolution proves, and the docs the change obsoleted — the root README taught a compiled-in table, and SPEC's install recipe listed a four-argument adapter constructor.

Deliberately not done: no uint64 constraint inside ChainIdRegistry. Baking one bridge's width into a protocol-agnostic table would break LayerZero next, whose EIDs are uint32; SafeCast at the adapter is the backstop.

Verification

386/386 pass, 0 skipped under forge 1.4.0 (the CI pin — local 1.7.1 formats differently):

MAINNET_RPC_URL=… BASE_RPC_URL=… forge test

That includes test_fork_everyMappedSelectorIsALiveLane, which now validates all 15 mainnet entries of chains.json against the real Ethereum CCIP router. _chainSelectorPairs stays an independent transcription — that check is worthless if both sides are copied from one place.

xavikh added 4 commits August 26, 2026 13:27
Every adapter carried its chain-id table in bytecode, so adding a chain to a live
deployment meant redeploying the adapter and re-attaching it on the controller
through governance on both sides -- for a two-integer lookup.

The table is a contract now. One registry per bridge protocol -- CCIP addresses
chains by its own selector, a future LayerZero adapter by something else, and the
two are unrelated -- deployed before the adapter and baked in as an immutable, so
the send path resolves it correctly under `delegatecall`.

The trade is deliberate: the TABLE is governable, the BINDING is not. Adding a
chain is one `setChainIdPair`; moving an adapter to a different registry still
means a new adapter. What the permission holder gains is not small -- they can
repoint a live lane in one call, redirecting both outbound sends and inbound
attribution, and the adapter follows without notice. It warrants the same
governance rigor as MANAGE_CONTROLLER_CONFIG_PERMISSION.

`BaseAdapter` owns both mappers now, non-virtual, turning the registry's `0` into
the revert `IBaseAdapter` requires. `CCIPAdapter` loses both constant tables; its
call sites are unchanged because they already went through those functions.

Falls out of it:
- `TestnetCCIPAdapter` is gone. It existed only to replace the mainnet-only table
  for testnets; `Test_Deploy.s.sol` now deploys a registry and the shipping
  adapter, so a testnet rehearsal exercises the contract that ships.
- `ChainIds.sol` and `CCIPChainIds.sol` had no importer left in `src/`, so they
  move to `test/fixtures/chains.json`. Tests seed their registries from that file
  rather than from a Solidity table only the tests can see.
- `OversizedSelectorCCIPAdapter` is gone: a selector wider than `uint64` is now
  reachable through config, so the mock had no reason to exist.

What the registry does NOT buy: a wrong-but-nonzero selector is caught by nothing.
Resolution proves a lane exists, never that it points at the right chain. That was
impossible before, when the table was audited bytecode, and it is the price of
making the table configurable -- the repair is the same trade in the other
direction, one `setChainIdPair` instead of replacement adapters on both sides.

The mainnet fork test changed target rather than coverage: it used to validate the
hardcoded table against the real CCIP router, and now validates every mainnet
entry of chains.json the same way. `_chainSelectorPairs` stays an independent
transcription; that check is worthless if both sides are copied from one place.

386/386 pass, forge 1.4.0.
feat(adapters)!: resolve chain ids through a governed ChainIdRegistry
`setChainIdPair` wrote `_fromNative` unconditionally, so two standard chain ids
could claim one bridge selector. `_fromNative` then stopped being the inverse of
`_toNative`: the loser kept sending over the shared selector and paying real
CCIP fees, while inbound traffic over it resolved to the winner, so `ccipReceive`
attributed a genuine message to the wrong origin chain.

That also made the `delete` of the previous reverse entry unsafe. It never
checked the entry belonged to the chain being written, so repairing the
collision cleared a bystander's inbound lane -- a chain named in neither the
call nor any emitted event.

Rejecting the second claimant closes both. With exclusivity enforced,
`_fromNative[previous]` is by construction the caller's own entry, so the delete
needs no ownership check. Reassigning a selector now means clearing its owner
first; both calls fit in one proposal.

Also switch `ChainsFixture.ccipSelector` to `SafeCast.toUint64`. The selectors
used to be `uint64` constants, so an over-wide one was a compile error. Read
from JSON, a plain cast truncated silently and every assertion reading the same
file agreed with itself on the wrong value. Verified by mutation: an extra digit
now fails 16 tests where it previously failed none.
`id(Transaction)` spelled `abi.encode` independently of `encode()`, so the
invariant that `id(bytes)` only matches it for canonically encoded payloads
was held by convention across two lines rather than structurally. Routing
through `encode()` means a future change to the encoding cannot desync the
two overloads.

Byte-identical output; no behaviour change.
@xavikh
xavikh requested a review from giorgilagidze September 3, 2026 09:19
@xavikh
xavikh merged commit 3255628 into main Sep 3, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants