-
Notifications
You must be signed in to change notification settings - Fork 264
Reading logs in superchain interop #1501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+227
−0
Merged
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
3c1deae
intial commit
krofax d22d0c8
add breadcrumbs
krofax c271a6e
Auto-fix: Update breadcrumbs, spelling dictionary and other automated…
krofax c58f69f
Described mermaid diagrams
krofax 7ac6de1
update links
krofax 8af0190
Update pages/stack/interop/reading-logs.mdx
krofax 11a09a9
Update pages/stack/interop/reading-logs.mdx
krofax 5a73a89
updated contract address
krofax 2394212
updated contract
krofax 8b23aef
Updated address
krofax b7d5e0c
updated the eas address
krofax 482efa1
Add steps to handle validation
krofax 4c882de
Auto-fix: Update breadcrumbs, spelling dictionary and other automated…
krofax 09634f5
Auto-fix: Update breadcrumbs, spelling dictionary and other automated…
krofax 54cf3ed
updated the table text
krofax c54eb31
Update bullet points
krofax a65245c
removed codes and used explanations
krofax 25e3adc
update links
krofax e3a876d
Update pages/stack/interop/reading-logs.mdx
krofax bdc3450
updated bullet points
krofax 33fb25a
updated the bullet point
krofax d2832ee
updated content based on zains comment
krofax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
--- | ||
title: Reading Logs in Superchain Interop | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lang: en-US | ||
description: Learn how to reference logs from one chain on another within the Superchain. | ||
--- | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import { Callout } from 'nextra/components' | ||
import { InteropCallout } from '@/components/WipCallout' | ||
|
||
<InteropCallout /> | ||
|
||
# Reading logs in superchain interop | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Superchain interop enables developers to leverage current and historical logs from other blockchains within the Superchain interop cluster directly on their local chain. | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This allows for cross-chain log consumption with low latency in a trust-minimized way. | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Overview | ||
|
||
Instead of relying solely on [L2ToL2CrossDomainMessenger](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/L2ToL2CrossDomainMessenger.sol), developers can use [`CrossL2Inbox#validateMessage`](https://github.com/ethereum-optimism/optimism/blob/af091753917c1d7101314cbfe8ac5cbc2efe0e5e/packages/contracts-bedrock/src/L2/CrossL2Inbox.sol#L49) and treat `CrossL2Inbox` as an oracle for logs that occurred on different chains or even their local chain. | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This enables developers to: | ||
|
||
* Reference attestations or events from other chains. | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Build cross-chain applications that react to events happening across the Superchain. | ||
* Create novel dApps that leverage data from multiple chains. | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Why use `CrossL2Inbox`? | ||
|
||
* **Lower latency**: Directly reference logs without waiting for cross-chain message confirmation. | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* **Trust-minimized**: Uses cryptographic validation rather than relying on off-chain relayers. | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* **Flexibility**: Can be used to validate events from another chain or even the same chain. | ||
|
||
## How it works | ||
|
||
### Architecture | ||
|
||
The process works through the [`CrossL2Inbox`](https://github.com/ethereum-optimism/optimism/blob/af091753917c1d7101314cbfe8ac5cbc2efe0e5e/packages/contracts-bedrock/src/L2/CrossL2Inbox.sol#L33) contract, which serves as an oracle for logs from other chains in the Superchain: | ||
|
||
1. A smart contract on `Chain A` emits a log (event) | ||
2. Your contract on `Chain B` calls `CrossL2Inbox#validateMessage` with the log's identifier | ||
3. The `CrossL2Inbox` contract verifies the log's authenticity | ||
4. Your contract can then use the validated log data | ||
|
||
### Key components | ||
|
||
* **Identifier**: A struct containing information about the log, including `chainId`, `origin` (contract address), and other log metadata | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* **validateMessage**: Function that verifies a log's authenticity before allowing its use | ||
|
||
## Example: cross-chain attestation verification | ||
|
||
Let's walk through a practical example of verifying an Ethereum Attestation Service (EAS) attestation across chains. | ||
|
||
### Source chain: creating an attestation | ||
|
||
On the source chain (e.g., OP Mainnet), a user creates an attestation using EAS: | ||
|
||
```mermaid | ||
sequenceDiagram | ||
participant User | ||
participant App as Application | ||
participant EAS as EAS Contract | ||
participant Log as Event Log | ||
|
||
User->>App: Request attestation | ||
App->>EAS: createAttestation() | ||
EAS->>Log: Emit AttestationCreated event | ||
Note over Log: Event contains attestation data | ||
``` | ||
|
||
1. The user initiates a request for an attestation through an application. | ||
|
||
2. The application calls the `createAttestation()` function on the EAS (Ethereum Attestation Service) contract on the source chain. | ||
|
||
3. The EAS contract processes the attestation request and emits an `AttestationCreated` event. | ||
|
||
4. The event is recorded in the chain's log, containing all necessary attestation data. | ||
|
||
### Destination chain: verifying the attestation | ||
|
||
On the destination chain (e.g., Base), a DeFi application wants to verify this attestation: | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```mermaid | ||
sequenceDiagram | ||
participant User | ||
participant DeFi as DeFi Application | ||
participant Verifier as AttestationVerifier | ||
participant CrossL2 as CrossL2Inbox | ||
participant OP as OP-Supervisor Node | ||
|
||
User->>DeFi: Request access using attestation | ||
DeFi->>Verifier: verifyAttestation(id, attestationEvent) | ||
Verifier->>CrossL2: validateMessage(id, keccak256(attestationEvent)) | ||
CrossL2->>OP: Check if log exists | ||
OP-->>CrossL2: Confirm log validity | ||
CrossL2-->>Verifier: Return validation result | ||
Verifier-->>DeFi: Return verification status | ||
DeFi-->>User: Grant access based on attestation | ||
``` | ||
|
||
1. The user requests access to a DeFi application on the destination chain, referencing an attestation created on the source chain. | ||
|
||
2. The DeFi application calls `verifyAttestation()` on the `AttestationVerifier` contract, passing the attestation's identifier and event data. | ||
|
||
3. The AttestationVerifier calls `validateMessage()` on the `CrossL2Inbox` contract, passing the attestation identifier and a hash of the event data. | ||
|
||
4. The `CrossL2Inbox` contract interacts with the [`OP-Supervisor`](/stack/interop/op-supervisor) node to check if the specified log exists on the source chain. | ||
|
||
5. The `OP-Supervisor` confirms the validity of the log to the `CrossL2Inbox` contract. | ||
|
||
6. The `CrossL2Inbox` returns the validation result to the `AttestationVerifier`. | ||
|
||
7. The `AttestationVerifier` returns the verification status to the DeFi application. | ||
|
||
8. If validation is successful, the DeFi application grants the user access based on the verified attestation. | ||
|
||
### Sample code for attestation verification | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```solidity | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.25; | ||
|
||
import { ICrossL2Inbox, Identifier } from "interfaces/L2/ICrossL2Inbox.sol"; | ||
import { Predeploys } from "src/libraries/Predeploys.sol"; | ||
|
||
contract AttestationVerifier { | ||
address constant CROSS_L2_INBOX = 0x4200000000000000000000000000000000000014; | ||
address constant EAS_CONTRACT = 0x4200000000000000000000000000000000000EAS; // Example address | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Event selector for AttestationCreated event | ||
bytes32 constant ATTESTATION_CREATED_EVENT_SELECTOR = | ||
0x831531dabaca5375ba77364edc9627be5638660e8b613d921b86766021d3c165; // Example selector | ||
|
||
function verifyAttestation( | ||
Identifier calldata _id, | ||
bytes calldata _attestationEvent | ||
) external returns (bool) { | ||
// Ensure the log came from the EAS contract | ||
require(_id.origin == EAS_CONTRACT, "Not from EAS contract"); | ||
|
||
// Validate the message through CrossL2Inbox | ||
ICrossL2Inbox(CROSS_L2_INBOX).validateMessage(_id, keccak256(_attestationEvent)); | ||
|
||
// Decode the attestation event data | ||
// (implementation depends on EAS event structure) | ||
|
||
// Process the attestation | ||
// For example, grant special permissions or better rates | ||
|
||
return true; | ||
} | ||
} | ||
``` | ||
|
||
## Implementation guide | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
To implement cross-chain log reading: | ||
|
||
```mermaid | ||
flowchart TD | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
A[1. Identify log to consume] --> B[2. Create Identifier struct] | ||
B --> C[3. Call validateMessage] | ||
C --> D[4. Process validated log data] | ||
|
||
subgraph "Code Examples" | ||
E["Identifier memory id = Identifier({ | ||
chainId: sourceChainId, | ||
origin: sourceContractAddress, | ||
// Other identifier parameters | ||
});"] | ||
|
||
F["ICrossL2Inbox(Predeploys.CROSS_L2_INBOX).validateMessage(id, keccak256(logData));"] | ||
end | ||
|
||
B -.-> E | ||
C -.-> F | ||
``` | ||
|
||
1. First, identify which log from another chain you want to consume in your application. | ||
|
||
2. Create an Identifier struct that contains all necessary information about the log, including the chain ID and the contract address that emitted the log. | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
3. Call the `validateMessage()` function on the `CrossL2Inbox` contract, passing the identifier and a hash of the log data. | ||
|
||
4. After validation, process the log data according to your application's requirements. | ||
|
||
## Important considerations | ||
|
||
* This feature works between chains within the same Superchain interop cluster | ||
* The same functionality can be used on a single chain (for example, to maintain a consistent architecture) | ||
* The `CrossL2Inbox` contract needs to be able to verify logs from the source chain | ||
* Ensure your contract handles validation failures gracefully | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Comparison with `L2ToL2CrossDomainMessenger` | ||
|
||
| Feature | L2ToL2CrossDomainMessenger | CrossL2Inbox#validateMessage | | ||
| ---------- | -------------------------------- | ------------------------------------- | | ||
| Purpose | Send messages between chains | Verify logs from other chains | | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Initiation | Active sending from source | Passive reading from destination | | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Use Case | Transfer tokens, trigger actions | Verify attestations, reference events | | ||
| Flow | Push model | Pull model | | ||
|
||
## End-to-End flow comparison | ||
|
||
```mermaid | ||
flowchart LR | ||
subgraph "L2ToL2CrossDomainMessenger (Push Model)" | ||
A[Source Contract] -->|sendMessage| B[Source L2ToL2CrossDomainMessenger] | ||
B -->|emit event| C[Event Log] | ||
C -.->|relayed by| D[Autorelayer] | ||
D -->|relayMessage| E[Destination L2ToL2CrossDomainMessenger] | ||
E -->|execute| F[Destination Contract] | ||
end | ||
|
||
subgraph "CrossL2Inbox (Pull Model)" | ||
G[Source Contract] -->|emit event| H[Event Log] | ||
H -.->|monitored by| I[OP-Supervisor] | ||
J[Destination Contract] -->|validateMessage| K[CrossL2Inbox] | ||
K <--->|verify log| I | ||
end | ||
``` | ||
|
||
This diagram compares the two approaches for cross-chain communication: | ||
|
||
### L2ToL2CrossDomainMessenger (Push Model): | ||
|
||
1. A source contract calls `sendMessage()` on the `L2ToL2CrossDomainMessenger`. | ||
|
||
2. The messenger emits an event to the event log. | ||
|
||
3. An autorelayer detects the event and relays it to the destination chain. | ||
|
||
4. The destination `L2ToL2CrossDomainMessenger` receives the relayed message. | ||
|
||
5. The destination messenger executes the message on the target contract. | ||
|
||
### CrossL2Inbox (Pull Model): | ||
|
||
1. A source contract emits an event to the event log. | ||
|
||
2. The `OP-Supervisor` node monitors events across chains. | ||
krofax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
3. A destination contract calls `validateMessage()` on the `CrossL2Inbox`. | ||
|
||
4. The `CrossL2Inbox` verifies the log's existence by communicating with the `OP-Supervisor`. | ||
|
||
5. The destination contract receives verification and proceeds with its logic. | ||
|
||
## Next steps | ||
|
||
* [Build a revolutionary app](/app-developers/get-started) that uses multiple blockchains within the Superchain | ||
* Learn how to [pass messages between blockchains](/stack/interop/tutorials/message-passing) | ||
* Deploy a [SuperchainERC20](/stack/interop/tutorials/deploy-superchain-erc20) to the Superchain |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.