Optimism 백서

Optimism

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 4쪽

, chains, and assets to support. * Learn about [smart wallets & signers](/app-developers/reference/actions/integrating-wallets#smart-wallets--signers) if you want Actions to deploy smart contract wallets controlled by your users' embedded wallets. * See the [Wallet Documentation](/app-developers/reference/actions/wallet-definitions) for API details on wallet classes, functions, and parameters. # Build interoperable apps on OP Stack devnet Source: https://docs.optimism.io/app-developers/guides/interoperability/get-started Learn about deploying contracts, cross-chain messaging, and tutorials to help you build applications on OP Stack chains. Reimagine your app with OP Stack interop to deliver the unified UX your users expect. Hack on net-new, bold use cases on Interop devnet. <Info> Explore the [Superchain Dev Console](https://console.optimism.io/?utm_source=op-docs\&utm_medium=docs) to build, launch, and grow your app on OP Stack chains. </Info> ## Connect to OP Stack Interop Choose your development environment to build, test, and quickly iterate on your apps. | Environment | Purpose | Getting Started | | - | - | - | | **Local development** | Rapid iteration and testing with Supersim | [Setup Supersim guide](/app-developers/tutorials/development/supersim/installation) | | **Interop devnet** | Large-scale testing on testnets | [Network specs](/app-developers/guides/building-apps) | ## Tools & resources for building interoperable apps | Tool | Description | | - | - | | [Superchain Faucet](https://console.optimism.io/faucet?utm_source=op-docs\&utm_medium=docs) | One-stop shop to grab testnet ETH for any OP Stack network. | | [Supersim](/app-developers/tools-sdks/supersim) | Local multi-chain testing environment for smart contracts. | | [Super CLI](https://github.com/ethereum-optimism/super-cli) | Command-line tool for seamless multichain app deployment and testing. | | [Superchain Relayer](https://github.com/ethereum-optimism/superchain-relayer) | UI for monitoring and managing cross-chain transactions. | | [Interop Docs](/op-stack/interop/explainer) | Comprehensive Interop information in the Optimism Docs. | | [Developer Console](https://console.optimism.io/?utm_source=op-docs\&utm_medium=docs) | Comprehensive tool to build, launch, and grow your app on OP Stack chains. | | [Developer Support GitHub](https://github.com/ethereum-optimism/developers/discussions) | Quick and easy developer support. | ## Handy step-by-step guides <CardGroup> <Card title="Supersim tutorials" icon="flask" href="/app-developers/tutorials/supersim" /> <Card title="Interop tutorials" icon="network-wired" href="/app-developers/tutorials/interop" /> <Card title="Transaction tutorials" icon="arrow-right-arrow-left" href="/app-developers/tutorials/transactions/sdk-estimate-costs" /> <Card title="Bridging tutorials" icon="bridge-water" href="/app-developers/tutorials/bridging/standard-bridge-standard-token" /> </CardGroup> ## Discover and build net-new use cases with OP Stack Interop There is so much more than just bridge abstraction. Hack on the various cutting-edge applications that are uniquely enabled by OP Stack Interop. Here are some ideas to get you started: | Superloans | Superlend | SuperCDP | | :- | :- | :- | | Use collateral on ChainA and ChainB to execute an arbitrage opportunity on ChainA. | Deposit ETH into lending protocols across chains for optimal yield, with automatic rebalancing based on best rates. | Collateralized crosschain debt protocol that holds assets and issues cross-chain tokens on user preferred chains. | # Message expiration Source: https://docs.optimism.io/app-developers/guides/interoperability/message-expiration What message expiration is, why it exists, and how to reemit a previously sent message if it has expired and was never relayed. # Message expiration [Messages](/app-developers/guides/interoperability/message-passing) referenced between OP Stack chains have a limited validity period called the expiry window. Once this window elapses, the referenced message becomes invalid and can no longer be referenced. For messages using [`L2ToL2CrossDomainMessenger`](/app-developers/guides/interoperability/message-passing), if a message expires before being referenced, developers can reemit the message on the source chain. This triggers a fresh `SentMessage` event, enabling the message to be relayed. ## The expiry window The expiry window is an offchain constant that defines how long a cross-chain message or event emitted remains valid. For any chain in the [Superchain interop cluster](/interop/explainer#superchain-interop-cluster), messages must be referenced within 7 days (604,800 seconds) of the log being created. After this period, a message can no longer be referenced unless the event is remitted. ## Reemitting an expired message The `resendMessage` function on the [`L2ToL2CrossDomainMessenger`](/app-developers/guides/interoperability/message-passing) contract allows developers to reemit a message that was sent but not yet relayed. This emits a new `SentMessage` log with the same content as the original message, enabling offchain relayers to pick it up again. The process to reemit an expired message: 1. Call [`resendMessage`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/interfaces/L2/IL2ToL2CrossDomainMessenger.sol#L114-L122) on the origin chain to reemit the message event. The contract verifies the message hash was originally sent. The call requires [every parameter](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/interfaces/L2/IL2ToL2CrossDomainMessenger.sol#L114-L122) to rebuild the original message. 2. [Relay the new message](/interop/message-passing#executing-message) as normal. Note: Re-emitting an already relayed message will have no effect on the destination chain, since the destination chain only honors the original initiating message. ## Next steps * Learn how to [pass messages between blockchains](/app-developers/tutorials/interoperability/message-passing) # Interop message passing overview Source: https://docs.optimism.io/app-developers/guides/interoperability/message-passing Understand how interop message passing works, from the initiating message on the source chain to the executing message on the destination chain. <Info>OP Stack interop is in active development. Some features may be experimental.</Info> <Info> This is an explanation of how interop works. You can find a step by step tutorial [here](/app-developers/tutorials/interoperability/message-passing). </Info> The low-level [`CrossL2Inbox`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/CrossL2Inbox.sol) contract handles basic message execution. It verifies whether an initiating message exists but does not check the message's destination, processing status, or other attributes. The [`L2ToL2CrossDomainMessenger`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/L2ToL2CrossDomainMessenger.sol) contract extends `CrossL2Inbox` by providing complete cross-domain messaging functionality. For high-level interoperability, both messages use the `L2ToL2CrossDomainMessenger` contract on their respective chains. ## Initiating message ```mermaid theme={null} sequenceDiagram participant app as Application box rgba(0,0,0,0.1) Source Chain participant srcContract as Source Contract participant srcXdom as L2ToL2CrossDomainMessenger participant log as Event Log end app->>srcContract: 1. Send a message srcContract->>srcXdom: 2. Call contract A<br/>in chainId B<br/>with calldata C note over srcXdom: 3. Sanity checks srcXdom->>log: 4. Log event SentMessage ``` 1. The application sends a transaction to a contract on the source chain. 2. The contract calls [`L2ToL2CrossDomainMessenger.SendMessage`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/L2ToL2CrossDomainMessenger.sol#L125-L142). The call requires these parameters: * `_destination`: The chain ID of the destination blockchain. * `_target`: The address of the contract on that blockchain. * `_message`: The actual message. This message is provided to `_target` as calldata, which means it includes a function selector and the parameters for that function call. 3. `L2ToL2CrossDomainMessenger` on the source chain verifies the message is legitimate: * The destination chain is one to which this chain can send messages. * The destination chain is *not* the source chain. * The target is neither `CrossL2Inbox` nor `L2ToL2CrossDomainMessenger`. 4. `L2ToL2CrossDomainMessenger` emits a log entry. In addition to the parameters, the log entry also includes: * `_nonce`: A [nonce](https://en.wikipedia.org/wiki/Cryptographic_nonce) value to ensure the message is only executed once. * `_sender`: The contract that sent the cross domain message. ## Executing message ```mermaid theme={null} sequenceDiagram participant app as Autorelayer box rgba(0,0,0,0.1) Source Chain participant log as Event Log end box rgba(0,0,0,0.1) Destination Chain participant dstNode as Destination Chain Node participant dstXdom as L2ToL2CrossDomainMessenger participant Xinbox as CrossL2Inbox participant dstContract as Destination Contract end log->>dstNode: 1. Initiating message log event app->>dstXdom: 2. Send an executing message dstXdom->>Xinbox: 3. Verify the initiating message is real note over dstXdom: 4. Sanity checks dstXdom->>dstContract: 5. Call with provided calldata ``` 1. Before the executing message is processed, the log event of the initiating message has to reach the destination chain's node before the [expiry window](/app-developers/guides/interoperability/message-expiration) of 7 days. 2. The autorelayer, the application, or a contract calling on the application's behalf calls [`L2ToL2CrossDomainMessenger.relayMessage`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/L2ToL2CrossDomainMessenger.sol#L150-L203). This call includes the message that was sent (`_sentMessage`), as well as the [fields required to find that message (`_id`)](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/interfaces/L2/ICrossL2Inbox.sol#L4-L10). 3. The `L2ToL2CrossDomainMessenger` uses `CrossL2Inbox` to verify the message was sent from the source. 4. `L2ToL2CrossDomainMessenger` on the destination chain verifies the message is legitimate: * `_destination`: Chain ID of the destination chain. * `_nonce`: Nonce of the message sent * `_sender`: Address that sent the message * `_target`: Target contract or wallet address. * `message`: Message payload to call target with. 5. If everything checks out, `L2ToL2CrossDomainMessenger` calls the destination contract with the calldata provided in the message. ## Next steps * Learn how to [pass messages between blockchains](/app-developers/tutorials/interoperability/message-passing). # Reading Logs with OP Stack Interop Source: https://docs.optimism.io/app-developers/guides/interoperability/reading-logs Understand how contracts use CrossL2Inbox to validate logs from other interop chains, and how this pull model differs from sending messages. <Info>OP Stack interop is in active development. Some features may be experimental.</Info> OP Stack interop enables developers to leverage current and historical logs from other blockchains within the [OP Stack interop cluster](/interop/explainer#superchain-interop-cluster) directly on their local chain. This allows smart contracts to consume local and cross-chain logs with low latency in a trust-minimized way. ## 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/develop/packages/contracts-bedrock/src/L2/CrossL2Inbox.sol#L76) and treat `CrossL2Inbox` as an oracle for logs that occurred on different chains or even their local chain. This enables developers to: * Build cross-chain applications that react to events happening across OP Stack chains. * Create novel applications that leverage data from multiple chains. <Info> When reading logs, you must reference logs created within the [expiry window of 7 days](/app-developers/guides/interoperability/message-expiration). </Info> ## Why use `CrossL2Inbox`? * **Reference existing logs**: Allows contracts to verify and use logs that were already emitted, without requiring those logs to have been sent as cross-chain messages. * **Trust-minimized security**: Leverages the existing OP Stack security model with no additional trust assumptions. * **Flexibility**: Can be used to validate events from another chain or even the local chain. ## How it works ### Architecture The process works through the [`CrossL2Inbox`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/CrossL2Inbox.sol#L31) contract, which serves as an oracle for logs from other chains in the OP Stack interop cluster: 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 * **[validateMessage](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/CrossL2Inbox.sol#L79)**: Function that verifies a log's authenticity before allowing its use ## Example: cross-chain attestation verification Let's walk through a conceptual example of verifying an Ethereum Attestation Service (EAS) attestation across chains. EAS is a [predeploy](/app-developers/reference/contracts/interop/predeploy) in the OP Stack for making attestations on or off-chain about anything. ### Source chain: creating an attestation On the source chain (e.g., OP Mainnet), a user creates an attestation using EAS: ```mermaid theme={null} 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., Unichain), a DeFi application wants to verify this attestation: ```mermaid theme={null} sequenceDiagram participant User participant DeFi as DeFi Application participant Verifier as AttestationVerifier participant CrossL2 as CrossL2Inbox User->>DeFi: Request access using attestation DeFi->>Verifier: verifyAttestation(id, attestationEvent) Verifier->>CrossL2: validateMessage(id, keccak256(attestationEvent)) Note over CrossL2: Check the log exists on the source chain. 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 a verification function on an attestation verifier contract, passing the attestation's identifier and event data. 3. The attestation verifier calls `validateMessage()` on the `CrossL2Inbox` contract, passing the attestation identifier and a hash of the event data. 4. The [`CrossL2Inbox`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/CrossL2Inbox.sol) contract checks whether the specified log exists on the source chain. 5. The `CrossL2Inbox` returns the validation result to the attestation verifier. 6. The attestation verifier returns the verification status to the DeFi application. 7. If validation is successful, the DeFi application grants the user access based on the verified attestation. The primary benefit of this approach is that it allows your contract to verify attestations that already exist on another chain without requiring those attestations to have been explicitly sent as cross-chain messages. ## Overview of the process To implement cross-chain log reading: ```mermaid theme={null} flowchart TD A[1. Identify log to consume] --> B[2. Create Identifier struct] B --> C[3. Call validateMessage] C --> D[4. Process validated log data] subgraph "Conceptual Approach" E["Define an Identifier struct with: - chainId: The source chain ID - origin: The source contract address - Other required identifier parameters"] F["Call validateMessage on CrossL2Inbox Pass the identifier and hash of log data"] 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. 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 [OP Stack interop cluster](/interop/explainer#superchain-interop-cluster). * The same functionality can be used on a single chain (for example, to maintain a consistent architecture). ### Handling validation failures * The `validateMessage` call will revert the entire transaction if validation fails. * Consider implementing a try-catch pattern in your application's frontend to handle these failures. * Design your contract to allow for retry mechanisms where appropriate. ## Comparison with `L2ToL2CrossDomainMessenger` | Feature | L2ToL2CrossDomainMessenger | CrossL2Inbox#validateMessage | | - | - | - | | Purpose | Send messages between chains | Verify logs from other chains or local chain | | Initiation | Source explicitly sends message to destination | Destination queries for existing logs from source | | Use Case | Transfer tokens, trigger actions | Verify attestations, reference events | | Flow | Push model | Pull model | ## End-to-End flow comparison ```mermaid theme={null} 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] J[Destination Contract] -->|validateMessage| K[CrossL2Inbox] K -.->|verify log exists on source chain| H 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. ##
프로젝트로 돌아가기