Skip to content

Commit 3f7afe2

Browse files
committed
add Optimism Bedrock
1 parent 821b96d commit 3f7afe2

File tree

4 files changed

+354
-92
lines changed

4 files changed

+354
-92
lines changed
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
# Optimism Bedrock
2+
3+
> by @paco0x
4+
5+
## Addresses
6+
7+
### Goerli
8+
9+
| Name | Type | Address |
10+
| ---------------------------- | --------- | ----------------------------------------------------------------------------------- |
11+
| Batcher Submitter | EOA | https://goerli.etherscan.io/address/0x7431310e026b69bfc676c0013e12a1a11411eec9 |
12+
| BatchInbox | EOA | https://goerli.etherscan.io/address/0xff00000000000000000000000000000000000420 |
13+
| Proposer | EOA | https://goerli.etherscan.io/address/0x02b1786A85Ec3f71fBbBa46507780dB7cF9014f6 |
14+
| Challenger | Multi-sig | https://goerli.etherscan.io/address/0xBc1233d0C3e6B5d53Ab455cF65A6623F6dCd7e4f |
15+
| L2OutputOracle | Contract | https://goerli.etherscan.io/address/0xE6Dfba0953616Bacab0c9A8ecb3a9BBa77FC15c0 |
16+
| OptimismPortal | Contract | https://goerli.etherscan.io/address/0x5b47E1A08Ea6d985D6649300584e6722Ec4B1383 |
17+
| L1CrossDomainMessenger | Contract | https://goerli.etherscan.io/address/0x5086d1eef304eb5284a0f6720f79403b4e9be294 |
18+
| L1StandardBridge | Contract | https://goerli.etherscan.io/address/0x636af16bf2f682dd3109e60102b8e1a089fedaa8#code |
19+
| L1ERC721Bridge | Contract | https://goerli.etherscan.io/address/0x8DD330DdE8D9898d43b4dc840Da27A07dF91b3c9 |
20+
| OptimismMintableERC20Factory | Contract | https://goerli.etherscan.io/address/0x883dcF8B05364083D849D8bD226bC8Cb4c42F9C5 |
21+
| SystemConfig | Contract | https://goerli.etherscan.io/address/0xAe851f927Ee40dE99aaBb7461C00f9622ab91d60 |
22+
| SystemDictator | Contract | https://goerli.etherscan.io/address/0x1f0613A44c9a8ECE7B3A2e0CdBdF0F5B47A50971#code |
23+
| SystemDictator Owner | Multi-sig | https://goerli.etherscan.io/address/0xBc1233d0C3e6B5d53Ab455cF65A6623F6dCd7e4f |
24+
25+
### OP Bedrock on Goerli
26+
27+
| Name | Type | Address |
28+
| ----------------------------- | ---------- | --------------------------------------------------------------------------------------- |
29+
| L1 Attribute Depositor | EOA | https://goerli-optimism.etherscan.io/address/0xdeaddeaddeaddeaddeaddeaddeaddeaddead0001 |
30+
| L1Block | Pre-deploy | https://goerli-optimism.etherscan.io/address/0x4200000000000000000000000000000000000015 |
31+
| L2ToL1MessagePasser | Pre-deploy | https://goerli-optimism.etherscan.io/address/0x4200000000000000000000000000000000000016 |
32+
| L2CrossDomainMessenger | Pre-deploy | https://goerli-optimism.etherscan.io/address/0x4200000000000000000000000000000000000007 |
33+
| L2StandardBridge | Pre-deploy | https://goerli-optimism.etherscan.io/address/0x4200000000000000000000000000000000000010 |
34+
| L2ERC721Bridge | Pre-deploy | https://goerli-optimism.etherscan.io/address/0x4200000000000000000000000000000000000014 |
35+
| OptimismMintableERC20Factory | Pre-deploy | https://goerli-optimism.etherscan.io/address/0x4200000000000000000000000000000000000012 |
36+
| OptimismMintableERC721Factory | Pre-deploy | https://goerli-optimism.etherscan.io/address/0x4200000000000000000000000000000000000017 |
37+
38+
Pre-deployed contracts are deployed in genesis block by directly setting the state DB: [https://github.com/ethereum-optimism/optimism/blob/c21476c1874702da8c0817d5c7c9de5b8066eb4b/op-chain-ops/genesis/setters.go#L226](https://github.com/ethereum-optimism/optimism/blob/c21476c1874702da8c0817d5c7c9de5b8066eb4b/op-chain-ops/genesis/setters.go#L226)
39+
40+
## Component
41+
42+
- **op-geth** is a slightly modified version of **geth**, the Execution Engine in Optimism Bedrock, used to produce L2 blocks. You can find all the changes at [https://op-geth.optimism.io/](https://op-geth.optimism.io/).
43+
- **op-node** is the rollup node, which is a standalone stateless binary.
44+
- It receives L2 user transactions and converts the deposit data into payload attributes for the Engine API.
45+
- It submits the payload attributes to the Engine API, where they are converted into blocks and added to the canonical chain.
46+
- It can also derive the L2 chain from L1 deposits & batchInbox.
47+
- It generates L2 outputRoots for submission or verification.
48+
- **op-batcher** submits the L2 transactions to DA (currently, L1 calldata) through batcher transactions.
49+
- **op-proposer** periodically submits the L2 state root to the L1 contract for verification and withdrawal proving.
50+
51+
## L2 Blocks
52+
53+
Before Bedrock, the blockchain had a variable block time and one transaction per block.
54+
55+
With Bedrock, the block time is fixed at 2 seconds and can contain multiple transactions.
56+
57+
Advantages of this change include:
58+
59+
- Consistency with the Ethereum mainnet after POS.
60+
- Convenience for contracts that use block numbers rather than timestamps to keep track of time; for example, the `MasterChef` contract. Block numbers are harder to manipulate than block timestamps.
61+
- Reduced overhead for storing the blockchain.
62+
- No need to update the state trie root after each transaction.
63+
64+
#### Epochs and the Sequencing Window
65+
66+
The rollup chain is subdivided into epochs. There is a 1:1 correspondence between L1 block numbers and epoch numbers.
67+
68+
L2 block timestamp can be 10 minutes (**sequencer drift parameter**) ahead of L1. This parameter is meant to ensure liveness of the L2 chain during temporary loss of connection to L1.
69+
70+
## Transaction Types
71+
72+
On L2, Optimism adds a new type of transaction in op-geth called deposit transaction.
73+
74+
There are 2 types of deposit transactions:
75+
76+
- System transactions, the first transaction of every L2 block, setting the attributes from L1
77+
- Deposit transactions, transactions sent by users on L1 to L2
78+
79+
Other transactions are sent by users on L2 directly
80+
81+
## Deposit
82+
83+
- Call `OptimismPortal.depositTransaction(to, value, gasLimit, isCreation, data)` on L1.
84+
- Emit the `TransactionDeposited(from, to, version, opaqueData)` event in the contract.
85+
- If `from` is a contract, address aliasing will be performed. For more information, see the [address aliasing documentation](https://community.optimism.io/docs/developers/build/differences/#using-eth-in-contracts).
86+
- The sequencer uses these events to generate deposited transactions on L2 and build the block.
87+
- Deposit transactions are not retryable.
88+
- It's invalid to have an L2 block without including the deposits of its L1 origin. (anti censorship)
89+
90+
The L2 execution gas is paid by the user on L1.
91+
92+
## Withdrawal
93+
94+
- Call `L2ToL1MessagePasser.initiateWithdrawal(target, gasLimit, data)` on L2.
95+
- Set the contract storage for `sentMessages[withdrawalHash]` to `true`.
96+
- The proposer should set the L2 state root and storage root of `L2ToL1MessagePasser` on L1 (`L2OutputOracle`).
97+
- Prove the state on L1 by calling `OptimismPortal.proveWithdrawalTransaction()`.
98+
- Prove the `withdrawalHash -> true` key pair exists in the contract’s storage(**_Merkle Patricia Trie_**)
99+
- Wait for the finalization period.
100+
- Finalize the withdrawal transaction by calling `OptimismPortal.finalizeWithdrawalTransaction()`.
101+
- Withdrawal transactions are not retryable.
102+
103+
They use `Safecall.call()` to ignore the return data to avoid the potential return data bomb.
104+
105+
[https://www.notonlyowner.com/research/message-traps-in-the-arbitrum-bridge](https://www.notonlyowner.com/research/message-traps-in-the-arbitrum-bridge)
106+
107+
## Message Passing
108+
109+
Optimism constructed a pair of messaging contracts for message passing by utilizing the low-level deposit and withdraw transactions. The contracts used for message passing are `L1CrossDomainMessenger.sol` and `L2CrossDomainMessenger.sol`.
110+
111+
The messages are retryable, unlike the raw deposits/withdrawals. Developers should use messenger contracts instead of low-level deposits/withdrawals.
112+
113+
- To pass a message to the other chain, users need to call `CrossDomainMessenger.sendMessage(target, message, minGasLimit)`.
114+
- To obtain the caller on the other chain, users can use `CrossDomainMessenger.xDomainMsgSender()`.
115+
- If it is an L1 → L2 message, remember to undo the address alias.
116+
117+
### L1 → L2 message
118+
119+
#### on L1
120+
121+
- user call `L1CrossDomainMessenger.sendMessage()`
122+
- gas is charged and computed on L1
123+
- gas usage: `gasLimit = minGasLimit * 1.016 + _message.length * 16 + 200,000`
124+
- call `OptimismPortal.depositTransaction()`
125+
126+
```solidity
127+
portal.depositTransaction{value: msg.value}(
128+
L2CrossDomainMessenger, // The pre-deplyed CrossDomainMessenger on L2
129+
msg.value,
130+
gasLimit,
131+
false,
132+
abi.encodeWithSelector(
133+
this.relayMessage.selector,
134+
messageNonce(),
135+
msg.sender,
136+
_target,
137+
msg.value,
138+
_minGasLimit,
139+
_message
140+
)
141+
)
142+
143+
```
144+
145+
- emit the `TransactionDeposited()` event in the `OptimismPortal` contract on L1
146+
147+
#### on L2
148+
149+
- op-node generate a deposit transaction by reading the `TransactionDeposited` events
150+
- the deposited transaction calls the `L2CrossDomainMessager.relayMessage()` on L2
151+
- call the target address in `L2CrossDomainMessager.relayMessage()` and record the result
152+
- call the target address with the message
153+
- If the replay transaction failed, user can replay it manually
154+
155+
### L2 → L1 message
156+
157+
#### on L2
158+
159+
- user call `L2CrossDomainMessenger.sendMessage()`
160+
- call `L2ToL1MessagePasser.intializeWithdraw()`
161+
- set `sentMessages[withdrawalHash] = true;` in the `L2ToL1MessagePasser`
162+
163+
#### on L1
164+
165+
- wait for the proposer submit the state root on L1 `L2OutputOracle`
166+
- prove the tx hash is already written to `L2ToL1MessagePasser` on L2
167+
- wait for the finalization period
168+
- finalize the withdrawal transaction by calling `OptimismPortal.finalizeWithdrawalTransaction()`
169+
- call `L1CrossDomainMessenger.relayMessage()`
170+
- call the target address with the message
171+
172+
## Bridges
173+
174+
Optimism built the following bridges for ERC20 and ERC721
175+
176+
- `L1StandardBridge` & `L2StandardBridge`
177+
- `L1ERC721Bridge` & `L2ERC721Bridge`
178+
179+
Under the hood, Optimism uses the message-passing mechanism provided by `CrossDomainMessenger` contracts.
180+
181+
### Bridging from L1 to L2
182+
183+
Before bridging from L1 to L2, users need to create a remote ERC20 contract on L2 by using the `OptimismMintableERC20Factory` contract.
184+
185+
#### On L1
186+
187+
- Lock the token/ETH and record the amount on L1's `L1StandardBridge`.
188+
- Send a message to L2's `L2StandardBridge`.
189+
190+
#### On L2
191+
192+
- Receive the message call in `L2StandardBridge`.
193+
- Mint the remote token with the specified amount.
194+
195+
### Bridging from L2 to L1
196+
197+
The process is very similar to the above.
198+
199+
## L2OutputOracle
200+
201+
### State Verification
202+
203+
`L2OutputOracle` is a contract on L1 that saves the L2 state roots for verification. Verifiers can challenge the result through a disputing system.
204+
205+
Currently, only an EOA address operated by Optimism can write to this contract.
206+
207+
The challenger address is set to a multi-sig wallet controlled by Optimism.
208+
209+
### Withdrawal proving
210+
211+
`L2OutputOracle` also saves the storage root of the `L2ToL1MessagePasser` contract on L2 for proving the withdrawal transactions on L1.
212+
213+
## Batcher
214+
215+
**op-batcher** submits the L2 data as calldata to `BatchInbox` address.
216+
217+
- op-node can read these calldata to rebuild the entire L2 chain.
218+
- Verifiers can use these data to verify the state root in `L2OutputOracle` contract.
219+
220+
### Data format
221+
222+
**Batches**, \***\*a batch \*\***contains all the txs of an L2 block
223+
224+
**Channels**, Batches are aggregated into **channel**
225+
226+
**Channel Frames**, channels are compressed and split into **channel frames**
227+
228+
Batcher submits those frames in the [batcher transactions](https://github.com/ethereum-optimism/optimism/blob/f30376825c82f62b846590487fe46b7435213d37/specs/glossary.md#batcher-transaction).
229+
230+
## Proposer
231+
232+
**op-proposer** submits the L2 state root and the storage trie root hash of the `L2ToL1MessagePasser` contract on L2 to L1 `L2OutputOracle`.
233+
234+
Proposer queries these data through op-node’s `optimism_outputAtBlock` RPC API. Inside `optimism_outputAtBlock`:
235+
236+
- fetch the L2 block header through `eth_getBlockByNumber` RPC in op-geth
237+
- fetch the storage root of `L2ToL1MessagePasser` contract through `eth_getProof` RPC in op-geth
238+
- return the state root of the L2 block and the storage root of `L2ToL1MessagePasser` contract
239+
- before returning, verify the storage root is contained in the state trie
240+
241+
Test account:
242+
243+
- Address: `0x3bb843cf8e26FF1Fdbdb6B2eC1B0dD5B37082B64`
244+
- Private Key: `0x8567689e64d90470cf9e30ed5d59d495e7a27de4fbe964620e45ecbe050018d7`
245+
246+
## Reference
247+
248+
- <https://paco0x.notion.site/Optimism-Bedrock-a546537289864e2dad5eca77e7386b96>
249+
- <https://community.optimism.io/docs/developers/bedrock/how-is-bedrock-different/>

basic/28-optimism-layer2/README-CN.md

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,66 @@
1-
# Optimism
2-
## 简介
3-
Optimistic Rollups(OR)是一种第二层解决方案,也就是说不直接在以太坊基础层中构建,而是基于以太坊进行构建。好处在于可以规模化运行智能合约,同时还能受到共享以太坊的安全性。其构造类似于Plasma,虽然无法达到Plasma几近无限的扩容能力,OR选择使用与EVM兼容的OVM(Optimistic虚拟机),使其能够像以太坊一样运作。
1+
# Optimism
42

5-
其名称“Opmistic Rollup”源自于这个解决方案本身的特征。之所以采用“Optimistic”(乐观),是因为聚合者仅发布最少量的所需信息,而无需提供任何证明,前提是聚合者没有实施欺诈行为,且仅在发生欺诈时提供证明。之所以使用“Rollups”,是因为交易以捆绑形式提交到主链(也即,交易被rolled-up)
3+
## 简介
64

7-
## 测试步骤
8-
- URI ETH 跨链
9-
Optimism 测试网络链接的是 goerli 网, 在 Optimistic 测试网路上进行交易前, 需要把 Goerli 网络的 ETH 经过跨链桥跨到 optimism.
10-
访问 Optimism 的 gateway, 选择 "Deposite" , 在其中输入跨链的 ETH 数量
5+
Optimistic Rollups(OR)是一种第二层解决方案,也就是说不直接在以太坊基础层中构建,而是基于以太坊进行构建。好处在于可以规模化运行智能合约,同时还能受到共享以太坊的安全性。其构造类似于 Plasma,虽然无法达到 Plasma 几近无限的扩容能力,OR 选择使用与 EVM 兼容的 OVM(Optimistic 虚拟机),使其能够像以太坊一样运作。
116

7+
其名称“Opmistic Rollup”源自于这个解决方案本身的特征。之所以采用“Optimistic”(乐观),是因为聚合者仅发布最少量的所需信息,而无需提供任何证明,前提是聚合者没有实施欺诈行为,且仅在发生欺诈时提供证明。之所以使用“Rollups”,是因为交易以捆绑形式提交到主链(也即,交易被 rolled-up)
128

13-
- 等待资产跨链成功
14-
可能需要等待 20 分钟左右才能完成 ETH 的跨链转移.
9+
## 测试步骤
10+
11+
- URI ETH 跨链
12+
Optimism 测试网络链接的是 goerli 网, 在 Optimistic 测试网路上进行交易前, 需要把 Goerli 网络的 ETH 经过跨链桥跨到 optimism.
13+
访问 Optimism 的 gateway, 选择 "Deposite" , 在其中输入跨链的 ETH 数量
1514

15+
- 等待资产跨链成功
16+
可能需要等待 20 分钟左右才能完成 ETH 的跨链转移.
1617

1718
- 查看余额
18-
ETH 转移成功后, 可以从 metaMask 查看 Optimism 网络上的余额
19+
ETH 转移成功后, 可以从 metaMask 查看 Optimism 网络上的余额
20+
21+
- 安装依赖
1922

20-
- 安装依赖
2123
```bash
2224
yarn
2325
```
2426

25-
- 配置环境变量
26-
复制 .env.example 文件为 .env 文件, 然后配置其中的 PRIVATE_KEY,INFURA_ID
27+
- 配置环境变量
28+
复制 .env.example 文件为 .env 文件, 然后配置其中的 PRIVATE_KEY,INFURA_ID
29+
30+
- 创建合约
2731

28-
- 创建合约
2932
```bash
3033
❯ npx hardhat run scripts/deploy.js --network optimism
3134
Deploying contracts with the account: 0xa3F2Cf140F9446AC4a57E9B72986Ce081dB61E75
3235
Account balance: 1500000000000000000
3336
Token address: 0x0d29e73F0b1AE67e28495880636e2407e41480F2
3437
```
3538

36-
- 脚本 ETH 跨链到 Optimism 网络
37-
上面除来可以通过 Optimism 的界面进行跨梁外, 还可以通过调用合约的方式进行 ETH 跨链
38-
调用如下脚本, 通过调用合约的方式进行 ETH 跨链
39-
```
39+
- 脚本 ETH 跨链到 Optimism 网络
40+
上面除来可以通过 Optimism 的界面进行跨梁外, 还可以通过调用合约的方式进行 ETH 跨链
41+
调用如下脚本, 通过调用合约的方式进行 ETH 跨链
42+
43+
```sh
4044
npx hardhat run scripts/deposit-eth.js --network goerli
4145

4246
## 调用结束后, 等待大约 5 分钟, 就可以发现 metaMask 上, Optimism 网络上账户余额增加了 0.0001 ETH
4347
```
4448

45-
- 脚本 ETH 跨链到 Goerli 网络
46-
跨链到 Optimism 链上的 ETH 还可以跨回 Goerli 链.
47-
调用如下脚本, 通过调用合约的方式进行 ETH 跨链
48-
```
49+
- 脚本 ETH 跨链到 Goerli 网络
50+
跨链到 Optimism 链上的 ETH 还可以跨回 Goerli 链.
51+
调用如下脚本, 通过调用合约的方式进行 ETH 跨链
52+
53+
```sh
4954
npx hardhat run scripts/withdraw-eth.js --network optimism
5055

5156
## 调用结束后, 等待大约 5 分钟, 就可以发现 metaMask 上, goerli 网络上账户余额增加了 0.0001 ETH
5257
```
5358

59+
## 参考文档
5460

55-
## 参考文档
56-
optimism 官方 github: https://github.com/ethereum-optimism/optimism-tutorial
57-
Optimistic Rollup 合约介绍: https://medium.com/plasma-group/ethereum-smart-contracts-in-l2-optimistic-rollup-2c1cef2ec537
58-
Optimism Rollup原理解析: https://zhuanlan.zhihu.com/p/350541979
59-
Optimism 跨链桥: https://gateway.optimism.io/
60-
Optimism Goerli deposite proxy contract : https://goerli.etherscan.io/address/0x636af16bf2f682dd3109e60102b8e1a089fedaa8#code
61-
Optimism Goerli withdraw proxy contract : https://goerli-optimism.etherscan.io/address/0x4200000000000000000000000000000000000010
61+
- Optimistic 官方 github: <https://github.com/ethereum-optimism/optimism-tutorial>
62+
- Optimistic Rollup 合约介绍: <https://medium.com/plasma-group/ethereum-smart-contracts-in-l2-optimistic-rollup-2c1cef2ec537>
63+
- Optimism Rollup 原理解析: <https://zhuanlan.zhihu.com/p/350541979>
64+
- Optimism 跨链桥: <https://gateway.optimism.io/>
65+
- Optimism Goerli deposite proxy contract : <https://goerli.etherscan.io/address/0x636af16bf2f682dd3109e60102b8e1a089fedaa8#code>
66+
- Optimism Goerli withdraw proxy contract : <https://goerli-optimism.etherscan.io/address/0x4200000000000000000000000000000000000010>

0 commit comments

Comments
 (0)