Skip to content

Commit 3efa340

Browse files
authored
refactor(l2): contracts (#2551)
**Motivation** - Some variable names are misleading and can confuse the reader. - There was no getter method for withdrawal logs merkle roots in `CommonBridge`. **Description** - Renamed deposit logs related variables in `OnChainProposer` and `CommonBridge` and their interfaces with clearer names. - Improved some documentation on the above. - Renamed some misleading naming in variables such as `commitment` in `OnChainProposer` and its interface.
1 parent f93e67a commit 3efa340

File tree

8 files changed

+121
-68
lines changed

8 files changed

+121
-68
lines changed

cmd/ethrex_l2/src/utils/config/default_values.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ pub const DEFAULT_ADDRESS: H160 = H160([
1717
0x3d, 0x1e, 0x15, 0xa1, 0xa5, 0x55, 0x78, 0xf7, 0xc9, 0x20, 0x88, 0x4a, 0x99, 0x43, 0xb3, 0xb3,
1818
0x5d, 0x0d, 0x88, 0x5b,
1919
]);
20-
// 0xc19743cc824301ef62bd5a962efc1bfbbab004d6
20+
// 0xaf6b492a2e60f9a366f3da02b78b6305b130edc7
2121
pub const DEFAULT_CONTRACTS_COMMON_BRIDGE_ADDRESS: H160 = H160([
22-
0xc1, 0x97, 0x43, 0xcc, 0x82, 0x43, 0x01, 0xef, 0x62, 0xbd, 0x5a, 0x96, 0x2e, 0xfc, 0x1b, 0xfb,
23-
0xba, 0xb0, 0x04, 0xd6,
22+
0xaf, 0x6b, 0x49, 0x2a, 0x2e, 0x60, 0xf9, 0xa3, 0x66, 0xf3, 0xda, 0x02, 0xb7, 0x8b, 0x63, 0x05,
23+
0xb1, 0x30, 0xed, 0xc7,
2424
]);
25-
// 0x52178cfc3db571f60016d43adf47d61c2009fa72
25+
// 0xea6d04861106c1fb69176d49eeb8de6dd14a9cfe
2626
pub const DEFAULT_CONTRACTS_ON_CHAIN_PROPOSER_ADDRESS: H160 = H160([
27-
0x52, 0x17, 0x8c, 0xfc, 0x3d, 0xb5, 0x71, 0xf6, 0x00, 0x16, 0xd4, 0x3a, 0xdf, 0x47, 0xd6, 0x1c,
28-
0x20, 0x09, 0xfa, 0x72,
27+
0xea, 0x6d, 0x04, 0x86, 0x11, 0x06, 0xc1, 0xfb, 0x69, 0x17, 0x6d, 0x49, 0xee, 0xb8, 0xde, 0x6d,
28+
0xd1, 0x4a, 0x9c, 0xfe,
2929
]);

crates/l2/configs/sequencer_config_example.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ coinbase_address = "0x0007a881CD95B1484fca47615B64803dad620C8d"
3434
l1_address = "0x3d1e15a1a55578f7c920884a9943b3b35d0d885b"
3535
# Private key corresponding to the above address.
3636
l1_private_key = "0x385c546456b6a603a1cfcaa9ec9494ba4832da08dd6bcf4de9a71e4a01b74924"
37-
on_chain_proposer_address = "0x52178cfc3db571f60016d43adf47d61c2009fa72"
37+
on_chain_proposer_address = "0xea6d04861106c1fb69176d49eeb8de6dd14a9cfe"
3838
# How often does the sequencer commit new blocks to the L1.
3939
commit_time_ms = 5000
4040
# 1 Gwei

crates/l2/contracts/src/l1/CommonBridge.sol

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ contract CommonBridge is ICommonBridge, Ownable, ReentrancyGuard {
2222
/// @dev The value is the merkle root of the logs.
2323
/// @dev If there exist a merkle root for a given block number it means
2424
/// that the logs were published on L1, and that that block was committed.
25-
mapping(uint256 => bytes32) public blockWithdrawalsLogs;
25+
mapping(uint256 => bytes32) public blockWithdrawalLogsMerkleRoots;
2626

27-
bytes32[] public depositLogs;
27+
/// @notice Array of hashed pending deposit logs.
28+
bytes32[] public pendingDepositLogs;
2829

2930
address public ON_CHAIN_PROPOSER;
3031

@@ -67,8 +68,8 @@ contract CommonBridge is ICommonBridge, Ownable, ReentrancyGuard {
6768
}
6869

6970
/// @inheritdoc ICommonBridge
70-
function getDepositLogs() public view returns (bytes32[] memory) {
71-
return depositLogs;
71+
function getPendingDepositLogs() public view returns (bytes32[] memory) {
72+
return pendingDepositLogs;
7273
}
7374

7475
function _deposit(DepositValues memory depositValues) private {
@@ -86,7 +87,7 @@ contract CommonBridge is ICommonBridge, Ownable, ReentrancyGuard {
8687
)
8788
);
8889

89-
depositLogs.push(
90+
pendingDepositLogs.push(
9091
keccak256(
9192
bytes.concat(
9293
bytes20(depositValues.to),
@@ -128,18 +129,18 @@ contract CommonBridge is ICommonBridge, Ownable, ReentrancyGuard {
128129
}
129130

130131
/// @inheritdoc ICommonBridge
131-
function getDepositLogsVersionedHash(
132+
function getPendingDepositLogsVersionedHash(
132133
uint16 number
133134
) public view returns (bytes32) {
134135
require(number > 0, "CommonBridge: number is zero (get)");
135136
require(
136-
uint256(number) <= depositLogs.length,
137+
uint256(number) <= pendingDepositLogs.length,
137138
"CommonBridge: number is greater than the length of depositLogs (get)"
138139
);
139140

140141
bytes memory logs;
141142
for (uint i = 0; i < number; i++) {
142-
logs = bytes.concat(logs, depositLogs[i]);
143+
logs = bytes.concat(logs, pendingDepositLogs[i]);
143144
}
144145

145146
return
@@ -148,31 +149,41 @@ contract CommonBridge is ICommonBridge, Ownable, ReentrancyGuard {
148149
}
149150

150151
/// @inheritdoc ICommonBridge
151-
function removeDepositLogs(uint16 number) public onlyOnChainProposer {
152+
function removePendingDepositLogs(
153+
uint16 number
154+
) public onlyOnChainProposer {
152155
require(
153-
number <= depositLogs.length,
156+
number <= pendingDepositLogs.length,
154157
"CommonBridge: number is greater than the length of depositLogs (remove)"
155158
);
156159

157-
for (uint i = 0; i < depositLogs.length - number; i++) {
158-
depositLogs[i] = depositLogs[i + number];
160+
for (uint i = 0; i < pendingDepositLogs.length - number; i++) {
161+
pendingDepositLogs[i] = pendingDepositLogs[i + number];
159162
}
160163

161164
for (uint _i = 0; _i < number; _i++) {
162-
depositLogs.pop();
165+
pendingDepositLogs.pop();
163166
}
164167
}
165168

169+
/// @inheritdoc ICommonBridge
170+
function getWithdrawalLogsMerkleRoot(
171+
uint256 blockNumber
172+
) public view returns (bytes32) {
173+
return blockWithdrawalLogsMerkleRoots[blockNumber];
174+
}
175+
166176
/// @inheritdoc ICommonBridge
167177
function publishWithdrawals(
168178
uint256 withdrawalLogsBlockNumber,
169179
bytes32 withdrawalsLogsMerkleRoot
170180
) public onlyOnChainProposer {
171181
require(
172-
blockWithdrawalsLogs[withdrawalLogsBlockNumber] == bytes32(0),
182+
blockWithdrawalLogsMerkleRoots[withdrawalLogsBlockNumber] ==
183+
bytes32(0),
173184
"CommonBridge: withdrawal logs already published"
174185
);
175-
blockWithdrawalsLogs[
186+
blockWithdrawalLogsMerkleRoots[
176187
withdrawalLogsBlockNumber
177188
] = withdrawalsLogsMerkleRoot;
178189
emit WithdrawalsPublished(
@@ -190,7 +201,7 @@ contract CommonBridge is ICommonBridge, Ownable, ReentrancyGuard {
190201
bytes32[] calldata withdrawalProof
191202
) public nonReentrant {
192203
require(
193-
blockWithdrawalsLogs[withdrawalBlockNumber] != bytes32(0),
204+
blockWithdrawalLogsMerkleRoots[withdrawalBlockNumber] != bytes32(0),
194205
"CommonBridge: the block that emitted the withdrawal logs was not committed"
195206
);
196207
require(
@@ -244,6 +255,8 @@ contract CommonBridge is ICommonBridge, Ownable, ReentrancyGuard {
244255
}
245256
withdrawalLogIndex /= 2;
246257
}
247-
return withdrawalLeaf == blockWithdrawalsLogs[withdrawalBlockNumber];
258+
return
259+
withdrawalLeaf ==
260+
blockWithdrawalLogsMerkleRoots[withdrawalBlockNumber];
248261
}
249262
}

crates/l2/contracts/src/l1/OnChainProposer.sol

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ import {IPicoVerifier} from "./interfaces/IPicoVerifier.sol";
1313
/// @title OnChainProposer contract.
1414
/// @author LambdaClass
1515
contract OnChainProposer is IOnChainProposer, ReentrancyGuard {
16+
/// @notice Committed blocks data.
17+
/// @dev This struct holds the information about the committed blocks.
18+
/// @dev processedDepositLogsRollingHash is the Merkle root of the logs of the
19+
/// deposits that were processed in the block being committed. The amount of
20+
/// logs that is encoded in this root are to be removed from the
21+
/// pendingDepositLogs queue of the CommonBridge contract.
1622
struct BlockCommitmentInfo {
17-
bytes32 commitmentHash;
18-
bytes32 depositLogs;
23+
bytes32 newStateRoot;
24+
bytes32 stateDiffKZGVersionedHash;
25+
bytes32 processedDepositLogsRollingHash;
1926
}
2027

2128
/// @notice The commitments of the committed blocks.
@@ -137,25 +144,29 @@ contract OnChainProposer is IOnChainProposer, ReentrancyGuard {
137144
/// @inheritdoc IOnChainProposer
138145
function commit(
139146
uint256 blockNumber,
140-
bytes32 commitment,
147+
bytes32 newStateRoot,
148+
bytes32 stateDiffKZGVersionedHash,
141149
bytes32 withdrawalsLogsMerkleRoot,
142-
bytes32 depositLogs
150+
bytes32 processedDepositLogsRollingHash
143151
) external override onlySequencer {
144152
require(
145153
blockNumber == lastCommittedBlock + 1,
146154
"OnChainProposer: blockNumber is not the immediate successor of lastCommittedBlock"
147155
);
148156
require(
149-
blockCommitments[blockNumber].commitmentHash == bytes32(0),
157+
blockCommitments[blockNumber].newStateRoot == bytes32(0),
150158
"OnChainProposer: block already committed"
151159
);
160+
152161
// Check if commitment is equivalent to blob's KZG commitment.
153162

154-
if (depositLogs != bytes32(0)) {
155-
bytes32 savedDepositLogs = ICommonBridge(BRIDGE)
156-
.getDepositLogsVersionedHash(uint16(bytes2(depositLogs)));
163+
if (processedDepositLogsRollingHash != bytes32(0)) {
164+
bytes32 claimedProcessedDepositLogs = ICommonBridge(BRIDGE)
165+
.getPendingDepositLogsVersionedHash(
166+
uint16(bytes2(processedDepositLogsRollingHash))
167+
);
157168
require(
158-
savedDepositLogs == depositLogs,
169+
claimedProcessedDepositLogs == processedDepositLogsRollingHash,
159170
"OnChainProposer: invalid deposit logs"
160171
);
161172
}
@@ -166,11 +177,12 @@ contract OnChainProposer is IOnChainProposer, ReentrancyGuard {
166177
);
167178
}
168179
blockCommitments[blockNumber] = BlockCommitmentInfo(
169-
commitment,
170-
depositLogs
180+
newStateRoot,
181+
stateDiffKZGVersionedHash,
182+
processedDepositLogsRollingHash
171183
);
172184
lastCommittedBlock = blockNumber;
173-
emit BlockCommitted(commitment);
185+
emit BlockCommitted(newStateRoot);
174186
}
175187

176188
/// @inheritdoc IOnChainProposer
@@ -202,7 +214,8 @@ contract OnChainProposer is IOnChainProposer, ReentrancyGuard {
202214
);
203215

204216
require(
205-
blockCommitments[blockNumber].commitmentHash != bytes32(0),
217+
blockCommitments[blockNumber].stateDiffKZGVersionedHash !=
218+
bytes32(0),
206219
"OnChainProposer: block not committed"
207220
);
208221

@@ -236,10 +249,12 @@ contract OnChainProposer is IOnChainProposer, ReentrancyGuard {
236249
lastVerifiedBlock = blockNumber;
237250
// The first 2 bytes are the number of deposits.
238251
uint16 deposits_amount = uint16(
239-
bytes2(blockCommitments[blockNumber].depositLogs)
252+
bytes2(
253+
blockCommitments[blockNumber].processedDepositLogsRollingHash
254+
)
240255
);
241256
if (deposits_amount > 0) {
242-
ICommonBridge(BRIDGE).removeDepositLogs(deposits_amount);
257+
ICommonBridge(BRIDGE).removePendingDepositLogs(deposits_amount);
243258
}
244259

245260
// Remove previous block commitment as it is no longer needed.

crates/l2/contracts/src/l1/interfaces/ICommonBridge.sol

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ interface ICommonBridge {
5858
bytes data;
5959
}
6060

61-
/// @notice Method to retrieve all the deposit logs hashes.
62-
/// @dev This method is used by the L2 L1_Watcher to get the remaining
63-
/// deposit logs to be processed.
64-
function getDepositLogs() external view returns (bytes32[] memory);
61+
/// @notice Method to retrieve all the pending deposit logs hashes.
62+
/// @dev This method is used by the L2 L1_Watcher to get the pending deposit
63+
/// logs to be processed.
64+
function getPendingDepositLogs() external view returns (bytes32[] memory);
6565

6666
/// @notice Initializes the contract.
6767
/// @dev This method is called only once after the contract is deployed.
@@ -76,19 +76,30 @@ interface ICommonBridge {
7676
/// @param depositValues the values needed to create the deposit.
7777
function deposit(DepositValues calldata depositValues) external payable;
7878

79-
/// @notice Method to retrieve the versioned hash of the first `number` deposit logs.
80-
/// @param number of deposit logs to retrieve the versioned hash.
81-
function getDepositLogsVersionedHash(
79+
/// @notice Method to retrieve the versioned hash of the first `number`
80+
/// pending deposit logs.
81+
/// @param number of pending deposit logs to retrieve the versioned hash.
82+
function getPendingDepositLogsVersionedHash(
8283
uint16 number
8384
) external view returns (bytes32);
8485

85-
/// @notice Remove deposit from depositLogs queue.
86-
/// @dev This method is used by the L2 OnChainOperator to remove the deposit
87-
/// logs from the queue after the deposit is verified.
88-
/// @param number of deposit logs to remove.
86+
/// @notice Remove pending deposit from the pendingDepositLogs queue.
87+
/// @dev This method is used by the L2 OnChainOperator to remove the pending
88+
/// deposit logs from the queue after the deposit is verified.
89+
/// @param number of pending deposit logs to remove.
8990
/// As deposits are processed in order, we don't need to specify
90-
/// the deposit logs to remove, only the number of them.
91-
function removeDepositLogs(uint16 number) external;
91+
/// the pending deposit logs to remove, only the number of them.
92+
function removePendingDepositLogs(uint16 number) external;
93+
94+
/// @notice Method to retrieve the merkle root of the withdrawal logs of a
95+
/// given block.
96+
/// @dev This method is used by the L2 OnChainOperator at the verify stage.
97+
/// @param blockNumber the block number in L2 where the withdrawal logs were
98+
/// emitted.
99+
/// @return the merkle root of the withdrawal logs of the given block.
100+
function getWithdrawalLogsMerkleRoot(
101+
uint256 blockNumber
102+
) external view returns (bytes32);
92103

93104
/// @notice Publishes the L2 withdrawals on L1.
94105
/// @dev This method is used by the L2 OnChainOperator to publish the L2

crates/l2/contracts/src/l1/interfaces/IOnChainProposer.sol

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ interface IOnChainProposer {
1616

1717
/// @notice A block has been committed.
1818
/// @dev Event emitted when a block is committed.
19-
event BlockCommitted(bytes32 indexed currentBlockCommitment);
19+
/// @param newStateRoot The new state root of the block that was committed.
20+
event BlockCommitted(bytes32 indexed newStateRoot);
2021

2122
/// @notice A block has been verified.
2223
/// @dev Event emitted when a block is verified.
@@ -40,15 +41,18 @@ interface IOnChainProposer {
4041
/// @dev Committing to an L2 block means to store the block's commitment
4142
/// and to publish withdrawals if any.
4243
/// @param blockNumber the number of the block to be committed.
43-
/// @param commitment of the block to be committed.
44+
/// @param newStateRoot the new state root of the block to be committed.
45+
/// @param stateDiffKZGVersionedHash of the block to be committed.
4446
/// @param withdrawalsLogsMerkleRoot the merkle root of the withdrawal logs
4547
/// of the block to be committed.
46-
/// @param depositLogs the deposit logs of the block to be committed.
48+
/// @param processedDepositLogsRollingHash the rolling hash of the processed
49+
/// deposits logs of the block to be committed.
4750
function commit(
4851
uint256 blockNumber,
49-
bytes32 commitment,
52+
bytes32 newStateRoot,
53+
bytes32 stateDiffKZGVersionedHash,
5054
bytes32 withdrawalsLogsMerkleRoot,
51-
bytes32 depositLogs
55+
bytes32 processedDepositLogsRollingHash
5256
) external;
5357

5458
/// @notice Method used to verify an L2 block proof.

0 commit comments

Comments
 (0)