Skip to content

Commit 4e62dbb

Browse files
committed
finished
1 parent 085f745 commit 4e62dbb

File tree

1 file changed

+115
-9
lines changed

1 file changed

+115
-9
lines changed

ecosystem/l2tol2cdm-gasreceipt.md

+115-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# [Project Name]: Design Doc
1+
# [L2ToL2CrossDomainMessenger Gas Receipt]: Design Doc
22

3-
| | |
4-
| ------------------ | -------------------------------------------------- |
5-
| Author | _Author Name_ |
6-
| Created at | _YYYY-MM-DD_ |
7-
| Initial Reviewers | _Reviewer Name 1, Reviewer Name 2_ |
8-
| Need Approval From | _Reviewer Name_ |
9-
| Status | _Draft / In Review / Implementing Actions / Final_ |
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_ |
1010

1111
## Purpose
1212

@@ -15,19 +15,36 @@
1515
<!-- It is fine to remove this section from the final document,
1616
but understanding the purpose of the doc when writing is very helpful. -->
1717

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+
1820
## Summary
1921

2022
<!-- Most (if not all) documents should have a summary.
2123
While the length will likely be proportional to the length of the full document,
2224
the summary should be as succinct as possible. -->
2325

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 providing a mechanism for which the accumulated gas used for all cross domain calls with the initial `tx.origin` can be verifiably tracked, it provides the foundation for a single-tx experience.
27+
2428
## Problem Statement + Context
2529

2630
<!-- Describe the specific problem that the document is seeking to address as well
2731
as information needed to understand the problem and design space.
2832
If more information is needed on the costs of the problem,
2933
this is a good place to that information. -->
3034

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 dust on the remote chain for the user.
38+
- **Relayer 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 makes it nearly impossible to switch without introducing complexity (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**. A single transaction can fan out calls to multiple chains which then calls back to the originating contract, (A -> B -> A) & (A->C). For example, a Swap -> Bridge & Swap -> Bridge. Or two cross-chain DeFi protocols integrating each other. This type of cross-chain communication does not work in the world of relayers today.
45+
46+
- For applications composing each other (A & B), we can not gaurantee the same relayer vendor is used. 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.
47+
3148
## Proposed Solution
3249

3350
<!-- A high level overview of the proposed solution.
@@ -36,29 +53,118 @@ of why one solution was picked over other solutions.
3653
As a rule of thumb, including code snippets (except for defining an external API)
3754
is likely too low level. -->
3855

56+
Providing a mechanism for which the accumulated gas used for all cross domain calls can be correlated with the original `tx.origin`.
57+
58+
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.
59+
60+
Support can be enumerated in 2 parts.
61+
62+
### 1. Propogated SentMessage "Headers" or "Context".
63+
64+
With nested cross domain calls, there's no way to correlate the subsequent messages with the original `tx.origin`, as each message has a unique message hash associated with it. By introducing a new field, "headers" or "context" that are appropriately propogated as nested cross domain calls are made, we can introduce contextual information unrelated to the cross domain message itself.
65+
66+
event SentMessage(uint256 indexed destination, address indexed target, uint256 indexed messageNonce, address sender, bytes message, bytes context);
67+
68+
This added context should be versioned such that the propogated information can evolve over time to fit various needs. In this first iteration, we propose encoding the root-most `messageHash`, the `tx.origin` of that root call, and `call depth` of the cross domain message. This information is made availble in transient storage when relaying a message so that any further outbound messages simply forwards the appropriate context, rather than repopoulate an entirely new one.
69+
70+
Some pseudocode describing the above, the exact api will be fleshed out in a specs PR.
71+
72+
```solidity
73+
function sendMessage(...) {
74+
(,,bytes ctx memory) = crossDomainMessageContext();
75+
if (ctx.length == 0) {
76+
// new "top-level" cross domain call (messageHash_ == outbound message)
77+
ctx = abi.encodePacked(version, abi.encode(messageHash_, tx.origin, 0));
78+
} else {
79+
// propogate, incrementing call dpeth
80+
(bytes32 rootMessageHash_, address txorigin, uint256 depth) = _decodeContext(ctx);
81+
ctx = abi.encodePacked(version, abi.encode(rootMessageHash_, txorigin, depth+1))
82+
}
83+
84+
...
85+
86+
emit SentMessage(..., ctx)
87+
}
88+
89+
function relayMessage(...) {
90+
(uint256 source, address sender, bytes memory ctx) = decodeSentMessage(...)
91+
_storeMessageMetaData(source, sender, ctx);
92+
}
93+
```
94+
95+
Although first use of this propogated context is for gas receipts, this versioned abstraction can prove useful for much more information in the future without breaking the signature of `SentMessage`, critical for not breaking the call flow between `sendMessage` and `relayMessage`.
96+
97+
### 2. Gas Receipt
98+
99+
All superchain interop cross domain messages are relayed via the `L2ToL2CrossDomainMessenger#relayMessage` function. We can leverage this single-entrypoint to track the gas consumption and emit relevant execution information.
100+
101+
We include the `block.basefee` and not the `tx.gasprice`, so that the priority fee (included in gasPrice) set by the relaying entity is not included and only the minimum execution costs are computed.
102+
103+
- **Question**: _Should we simply emit the computed cost here rather than the individual parts? Would emitting the gasPrice be useful for someone else building on top?_
104+
105+
New Event:
106+
107+
event RelayedMessageGasReceipt(bytes32 indexed msgHash, bytes32 indexed rootMsgHash, uint256 indexed depth, address indexed txOrigin, uint256 baseFee, uint256 gasUsed)
108+
109+
```solidity
110+
function relayMessage(...) {
111+
uint256 _gasLeft = gasLeft()
112+
_ = target.call{ value: msg.value }(message);
113+
uint256 gasUsed = _gasLeft - gasLeft();
114+
115+
// there will always be populated context when relaying.
116+
(,,bytes ctx memory) = crossDomainMessageContext();
117+
(bytes32 rootMsgHash, address txorigin, uint256 depth) = _decodeContext(ctx);
118+
emit RelayedMessageGasReceipt(msgHash, rootMsgHash, depth, txorigin, block.basefee, gasUsed + RELAY_MESSAGE_OVERHEAD);
119+
}
120+
```
121+
122+
Thus this receipt can be used to reliably track `tx.origin` reponsible for all cross domain calls that occur from the first transaction. And with `rootMessageHash`, obtaining a unique identifier to tie all nested cross domain calls with their root-most outbound message.
123+
124+
- **Question**: _Might the parent message hash be useful here too? It could easily be fetched via the a gas receipt where the rootMsgHash matches and the depth is 1 behind. These are indexed topics making it an efficient getLogs query._
125+
39126
### Resource Usage
40127

41128
<!-- What is the resource usage of the proposed solution?
42129
Does it consume a large amount of computational resources or time? -->
43130

131+
There is an increase in gas cost to the `relayMessage` function:
132+
133+
1. Additional encoded bytes for the `SentMessage` event, used to relay the message.
134+
2. Emission of a new event, `RelayedMessageGasReceipt`.
135+
136+
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.
137+
44138
### Single Point of Failure and Multi Client Considerations
45139

46140
<!-- Details on how this change will impact multiple clients. Do we need to plan for changes to both op-geth and op-reth? -->
47141

142+
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.
143+
48144
## Failure Mode Analysis
49145

50146
<!-- Link to the failure mode analysis document, created from the fma-template.md file. -->
51147

148+
-
149+
52150
## Impact on Developer Experience
151+
53152
<!-- Does this proposed design change the way application developers interact with the protocol?
54-
Will any Superchain developer tools (like Supersim, templates, etc.) break as a result of this change? ---!>
153+
Will any Superchain developer tools (like Supersim, templates, etc.) break as a result of this change? --->
154+
155+
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.
55156

56157
## Alternatives Considered
57158

58159
<!-- List out a short summary of each possible solution that was considered.
59160
Comparing the effort of each solution -->
60161

162+
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 to any complex use case.
163+
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.
164+
61165
## Risks & Uncertainties
62166

63167
<!-- An overview of what could go wrong.
64168
Also any open questions that need more work to resolve. -->
169+
170+
`RelayedMessageGasReceipt` does not end up used in any meaningful gas payment system. In this world, the event can be ignored and even removed in a future hardfork. The serialized context propogated with every SentMessage can also be versioned to empty bytes to reduced the overhead that was added. It is important to note here that serialized context is an added feature irrespective of the gas receipt that can be re-purposed to propogate _any_ useful context.

0 commit comments

Comments
 (0)