Skip to content

Commit 3f5ddb1

Browse files
committed
refactor: keep blockData as calldata
1 parent 3d14eb1 commit 3f5ddb1

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

src/Zenith.sol

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ contract Zenith is HostPassage, AccessControlDefaultAdminRules {
2020
/// @param confirmBy - the timestamp by which the block must be submitted. Enforced by the contract.
2121
/// @param gasLimit - the gas limit for the rollup block. Ignored by the contract; enforced by the Node.
2222
/// @param rewardAddress - the address to receive the rollup block reward. Ignored by the contract; enforced by the Node.
23+
2324
struct BlockHeader {
2425
uint256 rollupChainId;
2526
uint256 sequence;
@@ -75,8 +76,10 @@ contract Zenith is HostPassage, AccessControlDefaultAdminRules {
7576
/// @param s - see _submitBlock.
7677
/// @custom:reverts see _submitBlock.
7778
/// @custom:emits see _submitBlock.
78-
function submitBlock(BlockHeader memory header, bytes memory blockData, uint8 v, bytes32 r, bytes32 s) external {
79-
_submitBlock(DataLocation.Calldata, header, blockData, v, r, s);
79+
function submitBlock(BlockHeader memory header, bytes calldata blockData, uint8 v, bytes32 r, bytes32 s) external {
80+
bytes32 blockCommit = blockCommitment(header, blockData);
81+
82+
_submitBlock(DataLocation.Calldata, header, blockCommit, v, r, s);
8083

8184
emit BlockData(blockData);
8285
}
@@ -89,23 +92,20 @@ contract Zenith is HostPassage, AccessControlDefaultAdminRules {
8992
/// @param s - see _submitBlock.
9093
/// @custom:reverts see _submitBlock.
9194
/// @custom:emits see _submitBlock.
92-
function submitBlock(BlockHeader memory header, uint32[] memory blobIndices, uint8 v, bytes32 r, bytes32 s)
95+
function submitBlock(BlockHeader memory header, uint32[] calldata blobIndices, uint8 v, bytes32 r, bytes32 s)
9396
external
9497
{
95-
// query the blob hashes via the indices in order to
96-
// ensure that the committed blob hashes are available in the transaction.
97-
bytes memory encodedHashes = getHashes(blobIndices);
98+
bytes32 blockCommit = blockCommitment(header, blobIndices);
9899

99-
_submitBlock(DataLocation.Blobs, header, encodedHashes, v, r, s);
100+
_submitBlock(DataLocation.Blobs, header, blockCommit, v, r, s);
100101

101102
emit BlobIndices(blobIndices);
102103
}
103104

104105
/// @notice Submit a rollup block.
105106
/// @dev Blocks are submitted by Builders, with an attestation to the block data signed by a Sequencer.
106107
/// @param header - the header information for the rollup block.
107-
/// @param committedData - if DataLocation = Blobs, committedData = encoded blob hashes for the blobs where the block data resides.
108-
/// if DataLocation = Calldata, committedData = full block data.
108+
/// @param blockCommit - the hashes `blockCommitment` signed by the Sequencer.
109109
/// @param v - the v component of the Sequencer's ECSDA signature over the block commitment.
110110
/// @param r - the r component of the Sequencer's ECSDA signature over the block commitment.
111111
/// @param s - the s component of the Sequencer's ECSDA signature over the block commitment.
@@ -117,7 +117,7 @@ contract Zenith is HostPassage, AccessControlDefaultAdminRules {
117117
function _submitBlock(
118118
DataLocation location,
119119
BlockHeader memory header,
120-
bytes memory committedData,
120+
bytes32 blockCommit,
121121
uint8 v,
122122
bytes32 r,
123123
bytes32 s
@@ -129,9 +129,6 @@ contract Zenith is HostPassage, AccessControlDefaultAdminRules {
129129
// assert that confirmBy time has not passed
130130
if (block.timestamp > header.confirmBy) revert BlockExpired();
131131

132-
// derive block commitment from sequence number and blobhashes
133-
bytes32 blockCommit = blockCommitment(header, committedData);
134-
135132
// derive sequencer from signature
136133
address sequencer = ecrecover(blockCommit, v, r, s);
137134

@@ -145,19 +142,18 @@ contract Zenith is HostPassage, AccessControlDefaultAdminRules {
145142
/// @notice Encode an array of blob hashes, given their indices in the transaction.
146143
/// @param blobIndices - the indices of the 4844 blob hashes for the block data.
147144
/// @return encodedHashes - the encoded blob hashes.
148-
function getHashes(uint32[] memory blobIndices) internal view returns (bytes memory encodedHashes) {
145+
function getHashes(uint32[] calldata blobIndices) internal view returns (bytes memory encodedHashes) {
149146
for (uint32 i = 0; i < blobIndices.length; i++) {
150147
encodedHashes = abi.encodePacked(encodedHashes, blobhash(blobIndices[i]));
151148
}
152149
}
153150

154151
/// @notice Construct hash of block details that the sequencer signs.
152+
/// @dev NOTE: we have separate blockCommitment functions in order to keep `blockData` as `calldata`, which is not possible for `encodedHashes`
155153
/// @param header - the header information for the rollup block.
156-
/// @param committedData - the data for the rollup block that was signed by the sequencer.
157-
/// if DataLocation = Blobs, committedData = encoded blob hashes.
158-
/// if DataLocation = Blobs, committedData = full block data.
154+
/// @param blockData - full data for the block supplied directly.
159155
/// @return commit - the hash of the encoded block details.
160-
function blockCommitment(BlockHeader memory header, bytes memory committedData)
156+
function blockCommitment(BlockHeader memory header, bytes calldata blockData)
161157
public
162158
view
163159
returns (bytes32 commit)
@@ -170,8 +166,35 @@ contract Zenith is HostPassage, AccessControlDefaultAdminRules {
170166
header.gasLimit,
171167
header.confirmBy,
172168
header.rewardAddress,
173-
committedData.length,
174-
committedData
169+
blockData.length,
170+
blockData
171+
);
172+
commit = keccak256(encoded);
173+
}
174+
175+
/// @notice Construct hash of block details that the sequencer signs.
176+
/// @param header - the header information for the rollup block.
177+
/// @param blobIndices - the indices of the 4844 blob hashes for the block data.
178+
/// @return commit - the hash of the encoded block details.
179+
function blockCommitment(BlockHeader memory header, uint32[] calldata blobIndices)
180+
public
181+
view
182+
returns (bytes32 commit)
183+
{
184+
// query the blob hashes via the indices in order to
185+
// ensure that the committed blob hashes are available in the transaction.
186+
bytes memory encodedHashes = getHashes(blobIndices);
187+
188+
bytes memory encoded = abi.encodePacked(
189+
"init4.sequencer.v0",
190+
block.chainid,
191+
header.rollupChainId,
192+
header.sequence,
193+
header.gasLimit,
194+
header.confirmBy,
195+
header.rewardAddress,
196+
encodedHashes.length,
197+
encodedHashes
175198
);
176199
commit = keccak256(encoded);
177200
}

0 commit comments

Comments
 (0)