Architecture
The Unruggable Gateways codebase (opens in a new tab) includes both a Typescript (opens in a new tab) and a Solidity (opens in a new tab) version of the virtual machine. The idea is simple: write your code in Typescript to read data from your chosen Layer 2 (L2) network, then easily convert it to Solidity and deploy it without changes.
How It Works (High-Level Overview)
- Contract Definition: A contract defines an
GatewayRequest
. This request is passed to the fetch method defined inGatewayFetchTarget.sol
. - Offchain Data Handling: The fetch method uses the ERC-3668 (opens in a new tab) standard and reverts with an
OffchainLookup
, instructing the client to query the Gateway. - Response Processing: The
fetchCallback
method processes the response, using a chain-specific "Verifier" to validate proofs and return the results to the caller.
Core Components
1. Request Builder
GatewayFetcher.sol
provides a public API for creating anGatewayRequest
. This request consists of a series of operations that the virtual machine interprets to read and process data from other rollups.- Solidity developers can write contracts that build these requests by inheriting from
GatewayFetchTarget.sol
. - A Typescript version is also available for easier development and testing. You can check out our Examples page for more details.
2. The Gateway
- The
gateway.ts
Typescript file sets up a simple HTTP server to handle requests for data from other rollups. It uses a “Rollup” as a parameter (explained below). - The gateway connects to the necessary rollup to gather data, create proofs, and send them back to the client, so users don’t have to interact directly with the rollups.
3. The Rollup
- Rollup classes in Typescript use “Prover” and “Commit” generics to instruct the Gateway on how to request data and create verifiable proofs.
- They have methods (
encodeWitness
andencodeWitnessV1
) that explain how to encode proof data from the gateway in a way that is understood by the "Verifier".
Commits
- Commits represent the data submitted to Layer 1 (L1) by the L2 chain. They are necessary to verify a proof or extract the right data to check against a proof.
- The
rollup.ts
base class defines methods to help the gateway fetch commit data (fetchLatestCommitIndex
andfetchCommit
).
4. Verifiers
- A Verifier is a Solidity contract deployed on L1. It is used by the ERC 3668 callback to verify the data sent from the gateway server.
- Different chains utilize different hashing functions and Trie structures. Verifiers use different verification steps based on the rollup. Helper contracts are used include
EthTrieHooks.sol
andLineaTrieHooks.sol
.
Gateway URL
- The Verifier specifies the URLs of the Gateways to query for handling a request via the
gatewayURLs()
method.
Context
- Each Verifier implements the
getLatestContext
method from theIVerifier
interface. This method determines what L2 chain state the request can access. The gateway considers this context when responding and includes it in its response.