Skip to content

Commit 8c3dae0

Browse files
committed
gas receipt
1 parent 45d65ad commit 8c3dae0

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed

ecosystem/l2tol2cdm-gasreceipt.md

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# [L2ToL2CrossDomainMessenger Gas Receipt]: Design Doc
2+
3+
| | |
4+
| ------------------ | -------------------------------------------------------- |
5+
| Author | _Hamdi Allam_ |
6+
| Created at | _2025-04-14_ |
7+
| Initial Reviewers | _Wonderland, Mark Tyneway, Harry Markley, Karl Floersch_ |
8+
| Need Approval From | _Wonderland, Ben Clabby, Mark Tyneway_ |
9+
| Status | _Draft / In Review / Implementing Actions / Final_ |
10+
11+
## Purpose
12+
13+
<!-- This section is also sometimes called “Motivations” or “Goals”. -->
14+
15+
<!-- It is fine to remove this section from the final document,
16+
but understanding the purpose of the doc when writing is very helpful. -->
17+
18+
As more contracts are built that natively integrate with superchain interop, several single-action experiences can span up to N+1 op-stack chains to achieve their desired outcome. It is important we include the needed features & data to preserve a single-tx experience in the Superchain to not lose developers or users due to either a poor developer or user experience.
19+
20+
## Summary
21+
22+
<!-- Most (if not all) documents should have a summary.
23+
While the length will likely be proportional to the length of the full document,
24+
the summary should be as succinct as possible. -->
25+
26+
The general mental model for a transaction is that the sending account pays for the gas used. This mental model is the backbone for 4337 or 7702 sponsored transactions where tx.origin is decoupled from the calling account. By emitted a receipt of execution that emits the cost of gas consumped for all relayed cross domain messages, as well as the initial `tx.origin` responsible for all nested cross domain calls, it provides the foundation where the sending account is charged for all computation associated with the first transaction.
27+
28+
## Problem Statement + Context
29+
30+
<!-- Describe the specific problem that the document is seeking to address as well
31+
as information needed to understand the problem and design space.
32+
If more information is needed on the costs of the problem,
33+
this is a good place to that information. -->
34+
35+
Today, a single transaction cross-chain experience is achieved by pushing gas from the source -> destination chain with the message. Relayers are incentivized as long as the pushed funds for gas is sufficiently enough to cover the cost. This works in primitive usecases, where there's a single message from A -> B such as bridging funds. However even this has several drawbacks:
36+
37+
- **Gas Estimation**. Doing on-chain estimation is very inaccurate, resulting in overestimation for high odds of delivery. Some protocols/relayers even pocket this difference instead of refunding the account. And if the account is refunded, it is likely dust on the remote chain for the user.
38+
- **Vendor lock-in**. Contract developers must lock into a crosschain provider (Hyperlane,LayerZero,Wormhole) in order to make use of these services. This tight coupling introduces complexity in order to retain flexibility -- upgradable smart contracts and provider-agonstic interfaces.
39+
40+
Stepping outside this simple usecase, we can enumerate more scenarios in which pushing gas from source -> destination quickly falls apart.
41+
42+
1. **Transitive cross-domain calls**. A -> B -> C. Gas estmation from the source chain (A), is already inefficient in the single hop. This scheme must also additionally overestimate and provide enough gas for each cross-domain call, B -> C. If a cross-domain call was to fail in an intermediate hop, it's unclear how to fund that transaction through from A.
43+
44+
2. **Cross-Domain Composability**. Transitive call domain calls may be between unrelated contracts such as different DeFi protocols. We can not assume to applications are using the same relaying vendor. Hop A->B might leverage Hyperlane to send the message, while B might transitively use Wormhole to send a message to C. **This is why it's important to solve this problem natively**, such that as long as the providers (LayerZero/Wormhole/Hyperlane) use native message passing, everything works.
45+
46+
## Proposed Solution
47+
48+
<!-- A high level overview of the proposed solution.
49+
When there are multiple alternatives there should be an explanation
50+
of why one solution was picked over other solutions.
51+
As a rule of thumb, including code snippets (except for defining an external API)
52+
is likely too low level. -->
53+
54+
Providing a mechanism for which the accumulated gas used for all cross domain calls can be correlated with the original `tx.origin`.
55+
56+
By natively providing this in the messenger, anyone can charge tx senders exactly what was used for all incurred cross domain messages. It is important to note here that this design doc does not prescribe a solution for charging this gas. This can happen within shared block building, enshrined in a app-level relay bounties framework, or used the 3rdparty providers in their own accounting systems.
57+
58+
Support is enumerated in 2 parts.
59+
60+
### 1. Populated SentMessage "Context".
61+
62+
[#266](https://github.com/ethereum-optimism/design-docs/pull/266) introduces the ability to embed context propogated with cross domain messages. We propose a first version format for this context
63+
64+
- `abi.encodePacked(version0, abi.encode(rootMsgHash, txOrigin))`
65+
66+
The root-most cross domain call is responsible to populating the responsible tx.origin and root message hash that will be propogated to all nested transactions.
67+
68+
```solidity
69+
function sendMessage(...) {
70+
(,,bytes ctx memory) = crossDomainMessageContext();
71+
if (ctx.length == 0) {
72+
// new "top-level" cross domain call (messageHash_ == outbound message)
73+
ctx = abi.encodePacked(version, abi.encode(messageHash_, tx.origin));
74+
}
75+
76+
// keep the ctx as-is for nested messages, this format does not require re-encoding
77+
78+
...
79+
80+
emit SentMessage(..., ctx)
81+
}
82+
```
83+
84+
### 2. Gas Receipt
85+
86+
All superchain interop cross domain messages are relayed with `L2ToL2CrossDomainMessenger#relayMessage` as the single entrypoint. We can leverage this to track gas consumption and emit a relevant execution trace.
87+
88+
New Event:
89+
90+
```solidity
91+
event RelayedMessageGasReceipt(bytes32 indexed msgHash, bytes32 indexed rootMsgHash, address relayer, address txOrigin, uint256 cost)
92+
```
93+
94+
When computing the total cost used for gas, the block's basefee is used and not the gasPrice of the transaction. This ensures the cost is a pure representation of the true cost of execution parameterized by the block and not skewed by any priority fee set to influence ordering.
95+
96+
```solidity
97+
function relayMessage(...) {
98+
uint256 _gasLeft = gasLeft()
99+
_ = target.call{ value: msg.value }(message);
100+
uint256 gasUsed = (_gasLeft - gasLeft()) + RELAY_MESSAGE_OVERHEAD;
101+
102+
uint256 cost = block.basefee * gasUsed;
103+
104+
// there will always be populated context when relaying.
105+
(,,bytes ctx memory) = crossDomainMessageContext();
106+
(bytes32 rootMsgHash, address txorigin) = _decodeContext(ctx);
107+
108+
emit RelayedMessageGasReceipt(msgHash, rootMsgHash, msg.sender, txorigin, cost);
109+
}
110+
```
111+
112+
Thus this receipt can be used to reliably track `tx.origin` reponsible for all cross domain calls that occur from the first transaction. With the `rootMsgHash` serving as a unique identifier to tie all nested cross domain calls with their root-most outbound message.
113+
114+
- **Question**: \_Might the parent message hash be useful here too?
115+
116+
### Resource Usage
117+
118+
<!-- What is the resource usage of the proposed solution?
119+
Does it consume a large amount of computational resources or time? -->
120+
121+
There is an increase in gas cost to the `relayMessage` function:
122+
123+
1. Additional encoded bytes for the `SentMessage` event, used to relay the message.
124+
2. Emission of a new event, `RelayedMessageGasReceipt`.
125+
126+
However the added event fields to `SentMessage` and fields of the new event, `RelayedMessageGasReceipt`, are encodings of fixed sized data fields bounding the added overhead.
127+
128+
### Single Point of Failure and Multi Client Considerations
129+
130+
<!-- Details on how this change will impact multiple clients. Do we need to plan for changes to both op-geth and op-reth? -->
131+
132+
The added gas cost, if unbounded, could be a single point of failure to relaying messages since we are attaching auxilliary data to each outbound message. However as noted in the [Resource Usage](#resource-usage) section, the encoding of this data is fixed is size and cannot be exploited to include more.
133+
134+
## Failure Mode Analysis
135+
136+
<!-- Link to the failure mode analysis document, created from the fma-template.md file. -->
137+
138+
-
139+
140+
## Impact on Developer Experience
141+
142+
<!-- Does this proposed design change the way application developers interact with the protocol?
143+
Will any Superchain developer tools (like Supersim, templates, etc.) break as a result of this change? --->
144+
145+
The contract API of the L2ToL2CrossDomainMessenger does not change with this introduction. Although any relayers operating on the devnet must update the their abi for the `SentMessage` event to relay.
146+
147+
## Alternatives Considered
148+
149+
<!-- List out a short summary of each possible solution that was considered.
150+
Comparing the effort of each solution -->
151+
152+
1. Rely on 3rdparty Bridge/Relay providers. As listed in the problem statement, this can work well for single cross-chain messages but quickly falls apart in minimally more complex use case.
153+
2. Offchain attribution. Schemes can be derived with web2-esque approaches such as API keys. With a special tx-submission endpoint, being able to tag cross-chain messages with off-chain accounts to thus charge for gas used. This might a good fallback mechanism to have in place.
154+
155+
## Risks & Uncertainties
156+
157+
<!-- An overview of what could go wrong.
158+
Also any open questions that need more work to resolve. -->
159+
160+
`RelayedMessageGasReceipt` does not end up used in any meaningful gas payment system. In this world, the event can be simple be ignored. In hardfork, the event can be removed and the contexted versioned such that the added fields are removed to save on the gas overhead added from this feature.

0 commit comments

Comments
 (0)