Skip to content

Commit 0c1fda8

Browse files
authoredJun 3, 2024··
[Fix] start sequences at 1 (#39)
* fix: sequence number starts at 1 * docs: more of them * fix: snapshot
1 parent 98ba4ba commit 0c1fda8

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed
 

‎.gas-snapshot

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
ZenithTest:test_badSequence() (gas: 77339)
2-
ZenithTest:test_badSignature() (gas: 66856)
3-
ZenithTest:test_blockExpired() (gas: 55471)
4-
ZenithTest:test_notSequencer() (gas: 58576)
5-
ZenithTest:test_onePerBlock() (gas: 123118)
6-
ZenithTest:test_submitBlock() (gas: 90078)
1+
ZenithTest:test_badSequence() (gas: 60049)
2+
ZenithTest:test_badSignature() (gas: 66665)
3+
ZenithTest:test_blockExpired() (gas: 55344)
4+
ZenithTest:test_notSequencer() (gas: 58385)
5+
ZenithTest:test_onePerBlock() (gas: 105869)
6+
ZenithTest:test_submitBlock() (gas: 90173)

‎src/Zenith.sol

+20-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ contract Zenith is Passage {
2222
}
2323

2424
/// @notice The sequence number of the next block that can be submitted for a given rollup chainId.
25-
/// rollupChainId => nextSequence number
26-
mapping(uint256 => uint256) public nextSequence;
25+
/// @dev Because sequences must start at 1, accessors must add 1 to this number.
26+
/// rollupChainId => (nextSequence - 1)
27+
mapping(uint256 => uint256) sequences;
2728

2829
/// @notice The host block number that a block was last submitted at for a given rollup chainId.
2930
/// rollupChainId => host blockNumber that block was last submitted at
@@ -82,6 +83,22 @@ contract Zenith is Passage {
8283
sequencerAdmin = _sequencerAdmin;
8384
}
8485

86+
/// @notice Returns the next sequence number.
87+
/// @dev Because sequences must start at 1, we add 1 to the mapping value.
88+
/// @param _rollupChainId - the chainId of the rollup chain. Any chainId is accepted by the contract.
89+
/// @return The next sequence number.
90+
function nextSequence(uint256 _rollupChainId) public view returns (uint256) {
91+
return sequences[_rollupChainId] + 1;
92+
}
93+
94+
/// @notice Increments the sequence number and returns it.
95+
/// @dev Because sequences must start at 1, we add 1 to the mapping value.
96+
/// @param _rollupChainId - the chainId of the rollup chain. Any chainId is accepted by the contract.
97+
/// @return The next sequence number.
98+
function incrementSequence(uint256 _rollupChainId) internal returns (uint256) {
99+
return ++sequences[_rollupChainId];
100+
}
101+
85102
/// @notice Add a sequencer to the permissioned sequencer list.
86103
/// @param sequencer - the address of the sequencer to add.
87104
/// @custom:emits SequencerSet if the sequencer is added.
@@ -134,7 +151,7 @@ contract Zenith is Passage {
134151

135152
function _submitBlock(BlockHeader memory header, bytes32 blockDataHash, uint8 v, bytes32 r, bytes32 s) internal {
136153
// assert that the sequence number is valid and increment it
137-
uint256 _nextSequence = nextSequence[header.rollupChainId]++;
154+
uint256 _nextSequence = incrementSequence(header.rollupChainId);
138155
if (_nextSequence != header.sequence) revert BadSequence(_nextSequence);
139156

140157
// assert that confirmBy time has not passed

‎test/Zenith.t.sol

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ contract ZenithTest is Test {
3232

3333
// set default block values
3434
header.rollupChainId = block.chainid + 1;
35-
header.sequence = 0; // first block has index
35+
header.sequence = 1; // first block has index 1
3636
header.confirmBy = block.timestamp + 10 minutes;
3737
header.gasLimit = 30_000_000;
3838
header.rewardAddress = address(this);
@@ -46,13 +46,13 @@ contract ZenithTest is Test {
4646
// cannot submit block with incorrect sequence number
4747
function test_badSequence() public {
4848
// change to incorrect sequence number
49-
header.sequence = 1;
49+
header.sequence = 100;
5050
commit = target.blockCommitment(header, blockDataHash);
5151

5252
// sign block commitmenet with sequencer key
5353
(uint8 v, bytes32 r, bytes32 s) = vm.sign(sequencerKey, commit);
5454

55-
vm.expectRevert(abi.encodeWithSelector(Zenith.BadSequence.selector, 0));
55+
vm.expectRevert(abi.encodeWithSelector(Zenith.BadSequence.selector, 1));
5656
target.submitBlock(header, blockDataHash, v, r, s, blockData);
5757
}
5858

0 commit comments

Comments
 (0)
Please sign in to comment.