This repository was archived by the owner on Apr 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 336
How do estimate the cost of interop messages (spoiler: nearly nothing) #1629
Merged
krofax
merged 9 commits into
ethereum-optimism:main
from
qbzzt:250522-interop-cost-estimate
Jun 2, 2025
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a22e2ca
WIP
qbzzt be93292
WIP
qbzzt afde08c
Ready for review
qbzzt b3e80af
Auto-fix: Update breadcrumbs, spelling dictionary and other automated…
qbzzt e063f29
Code rabbit
qbzzt a9b77ce
Fix math
qbzzt 1317a9c
Update pages/interop/estimate-costs.mdx
krofax 3e29d4c
@krofax suggestions
qbzzt 139b283
last @krofax comments
qbzzt 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| --- | ||
| title: Estimating the cost of interop messages | ||
| description: Estimate the gas cost of Superchain interop messages. | ||
| lang: en-US | ||
| content_type: guide | ||
| topic: interop-gas-estimate | ||
| personas: | ||
| - protocol-developer | ||
| - app-developer | ||
| categories: | ||
| - interoperability | ||
| - cross-chain-messaging | ||
| - superchain | ||
| - message-passing | ||
| - gas-cost | ||
| is_imported_content: 'false' | ||
| --- | ||
|
|
||
| import { Callout } from 'nextra/components' | ||
| import { InteropCallout } from '@/components/WipCallout' | ||
|
|
||
| <InteropCallout /> | ||
|
|
||
| # Estimating the cost of interop messages | ||
|
|
||
| <Callout type="info"> | ||
| At the time of writing (May 2025) the cost of a hundred interop messages is just a few cents. | ||
| Unless the cost of OP Stack transactions rises significantly, the cost of interop is not an important consideration. | ||
|
|
||
| To see the current cost of gas, go to a [block explorer](https://optimism.blockscout.com/) and look at a recent transaction. | ||
| </Callout> | ||
|
|
||
| There are several factors that determine the cost of an [interop transaction](/interop/message-passing): | ||
|
|
||
| * How you pass the message. | ||
| You can either use [`CrossL2Inbox`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/CrossL2Inbox.sol) directly, or use the cross domain messenger, [`L2ToL2CrossDomainMessenger`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/L2ToL2CrossDomainMessenger.sol), which uses `CrossL2Inbox` internally. | ||
| * The transaction type. | ||
| Every interop message has two transactions, an [initiating message](/interop/message-passing#initiating-message) in a transaction to the source chain, and an [executing message](/interop/message-passing#executing-message) in the destination chain. | ||
| * Whether autorelay is turned on. | ||
| If it is turned on for a particular chain, there is still an executing message when using `L2ToL2CrossDomainMessenger`, but the chain operator pays for it so you may not care. | ||
|
qbzzt marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## CrossL2Inbox | ||
|
|
||
| This is the low level protocol used by all interop protocols, including `L2ToL2CrossDomainMessenger`. | ||
|
|
||
| ### Initiating message | ||
|
|
||
| The initiating message here is just any log entry. | ||
| A log entry emitted by Solidity code has 1–4 topics (t) and an unlimited number of unstructured data bytes (n). | ||
| The gas cost is [*375(t+1)+8n*](https://www.evm.codes/?fork=cancun#a1). | ||
|
qbzzt marked this conversation as resolved.
Outdated
|
||
|
|
||
| ### Executing message | ||
|
|
||
| The executing message cost has several components: | ||
|
|
||
| 1. The cost of posting the transaction. | ||
| 2. The cost of hashing the message. | ||
| 3. The cost of `CrossL2Inbox.validateMessage`. | ||
| 4. The cost of using the message. | ||
|
|
||
| The first and second components depend on the log entry. | ||
| `CrossL2Inbox.validateMessage` only requires a 32 byte hash of the log entry, but actually using it typically requires the information that has been hashed. | ||
|
|
||
| Additionally, you need to provide the `CrossL2Inbox` with how to find the log entry. | ||
| This is encoded in a [five member structure](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/CrossL2Inbox.sol#L7-L19), so *32×5=160* bytes. | ||
| And, of course, you need to call a function which requires a four byte selector. | ||
|
|
||
| So the number of bytes required is *164+32t+n*. | ||
|
qbzzt marked this conversation as resolved.
Outdated
|
||
|
|
||
| Every transaction posted costs at least *21,000* gas. | ||
| Compared with that, the hashing which costs approximately [*30+0.2×\<number of bytes>*](https://www.evm.codes/?fork=cancun#20), is negligible. | ||
|
qbzzt marked this conversation as resolved.
Outdated
|
||
| We can usually ignore the [memory expansion cost](https://www.evm.codes/about#memoryexpansion), unless the validating contract uses a really large amount of memory. | ||
|
|
||
| The cost of using the message is beyond the scope here, because it depends on your application. | ||
|
|
||
| So the main cost drivers are the 21,000 transaction gas cost has plus the cost of posting a *164+32t+n* byte transaction. | ||
|
krofax marked this conversation as resolved.
Outdated
qbzzt marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Cross domain messenger | ||
|
|
||
| This higher level protocol adds some expenses, mostly because replay protection requires storage, and [writing to storage](https://www.evm.codes/?fork=cancun#55) is a relatively expensive operation. | ||
|
|
||
| ### Initiating message | ||
|
|
||
| The initiating message is sent by [`L2ToL2CrossDomainMessenger.sendMessage`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/L2ToL2CrossDomainMessenger.sol#L128-L161). This function writes to storage twice. | ||
|
|
||
| It writes to [specify that the hash has a sent message](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/L2ToL2CrossDomainMessenger.sol#L157). | ||
| This would typically be written to previously empty storage, so the cost is *22,100* gas. | ||
|
|
||
| Then it [increments the nonce value](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/L2ToL2CrossDomainMessenger.sol#L158). | ||
| Overwriting previously used storage (which means storage where the present value is *not* zero) only costs *5,000* gas. | ||
|
|
||
| So, the initiating message costs a little over *27,100* gas, plus the negligible cost of emitting the log message, a few if statements, etc. | ||
| Of course, it is created as part of a transaction, so we might want to include the *21,000* transaction cost too. | ||
|
qbzzt marked this conversation as resolved.
Outdated
|
||
|
|
||
| ### Executing message | ||
|
|
||
| If autorelay is turned on in a blockchain, then you don't care about the cost of the executing message. | ||
| The chain operator will bear the cost. | ||
|
|
||
| If autorelay is not turned on, the executing message is a call to [`L2ToL2CrossDomainMessenger.relayMessage`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/L2ToL2CrossDomainMessenger.sol#L197-L256). | ||
|
qbzzt marked this conversation as resolved.
|
||
| The only storage operation here is [noting the hash has been used for a message already](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/L2ToL2CrossDomainMessenger.sol#L241). | ||
| This is previously unwritten storage, so we can expect to pay the tull *22,100* in gas. | ||
|
qbzzt marked this conversation as resolved.
Outdated
|
||
| Plus, of course, the *21,000* that any transaction costs. | ||
| All the other gas costs are negligible. | ||
|
|
||
| ## Conclusion | ||
|
|
||
| Unless the message is *extremely* long, the cost of an interop message, taking both sides together, is unlikely to exceed *100,000* gas. | ||
| At the time of writing, each gas unit costs approximately `$3×10^-9`, so it would take about thirty messages to add up to a full cent. | ||
|
|
||
| ## Next steps | ||
|
|
||
| * Build a [revolutionary app](/app-developers/get-started) that uses multiple blockchains within the Superchain | ||
| * Deploy a [SuperchainERC20](/interop/tutorials/deploy-superchain-erc20) to the Superchain | ||
| * Learn [how messages get from one chain to another chain](/interop/message-passing) | ||
| * Watch [this video](https://www.youtube.com/watch?v=FKc5RgjtGes), which gives an overview of Superchain interoperability. | ||
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
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.