diff --git a/.gas-snapshot b/.gas-snapshot index a978abc..a9cf629 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,3 +1,4 @@ +ZenithTest:test_addSequencer() (gas: 88191) ZenithTest:test_badSignature() (gas: 37477) ZenithTest:test_configureEnter() (gas: 82311) ZenithTest:test_disallowedEnter() (gas: 17916) @@ -7,13 +8,16 @@ ZenithTest:test_enterToken_defaultChain() (gas: 62915) ZenithTest:test_enterTransact() (gas: 60890) ZenithTest:test_enter_defaultChain() (gas: 24033) ZenithTest:test_fallback() (gas: 21534) -ZenithTest:test_incorrectHostBlock() (gas: 35288) -ZenithTest:test_notSequencer() (gas: 34201) -ZenithTest:test_onePerBlock() (gas: 68453) +ZenithTest:test_incorrectHostBlock() (gas: 35310) +ZenithTest:test_notSequencer() (gas: 34223) +ZenithTest:test_notSequencerAdmin() (gas: 10193) +ZenithTest:test_onePerBlock() (gas: 68463) ZenithTest:test_onlyTokenAdmin() (gas: 16926) ZenithTest:test_receive() (gas: 21384) +ZenithTest:test_removeSequencer() (gas: 39857) ZenithTest:test_setUp() (gas: 16968) -ZenithTest:test_submitBlock() (gas: 61187) +ZenithTest:test_setUp() (gas: 8434) +ZenithTest:test_submitBlock() (gas: 63468) ZenithTest:test_transact() (gas: 58562) ZenithTest:test_transact_defaultChain() (gas: 57475) ZenithTest:test_withdraw() (gas: 59033) \ No newline at end of file diff --git a/test/Zenith.t.sol b/test/Zenith.t.sol index b7eddeb..9020f84 100644 --- a/test/Zenith.t.sol +++ b/test/Zenith.t.sol @@ -23,6 +23,8 @@ contract ZenithTest is Test { bytes32 blockDataHash ); + event SequencerSet(address indexed sequencer, bool indexed permissioned); + function setUp() public { address[] memory initialEnterTokens; target = new Zenith(block.chainid + 1, address(this), initialEnterTokens, address(this)); @@ -39,13 +41,18 @@ contract ZenithTest is Test { commit = target.blockCommitment(header); } + function test_setUp() public { + assertEq(target.sequencerAdmin(), address(this)); + assertEq(target.deployBlockNumber(), block.number); + } + // cannot submit block with incorrect host block number function test_incorrectHostBlock() public { // change to incorrect host block number header.hostBlockNumber = 100; commit = target.blockCommitment(header); - // sign block commitmenet with sequencer key + // sign block commitment with sequencer key (uint8 v, bytes32 r, bytes32 s) = vm.sign(sequencerKey, commit); vm.expectRevert(Zenith.IncorrectHostBlock.selector); @@ -54,20 +61,24 @@ contract ZenithTest is Test { // can submit block successfully with acceptable header & correct signature provided function test_submitBlock() public { - // sign block commitmenet with correct sequencer key + // sign block commitment with correct sequencer key (uint8 v, bytes32 r, bytes32 s) = vm.sign(sequencerKey, commit); + assertEq(target.lastSubmittedAtBlock(header.rollupChainId), 0); + // should emit BlockSubmitted event vm.expectEmit(); emit BlockSubmitted( vm.addr(sequencerKey), header.rollupChainId, header.gasLimit, header.rewardAddress, header.blockDataHash ); target.submitBlock(header, v, r, s, blockData); + + assertEq(target.lastSubmittedAtBlock(header.rollupChainId), block.number); } // cannot submit block with invalid sequencer signer from non-permissioned key function test_notSequencer() public { - // sign block commitmenet with NOT sequencer key + // sign block commitment with NOT sequencer key (uint8 v, bytes32 r, bytes32 s) = vm.sign(notSequencerKey, commit); vm.expectRevert(abi.encodeWithSelector(Zenith.BadSignature.selector, vm.addr(notSequencerKey))); @@ -76,7 +87,7 @@ contract ZenithTest is Test { // cannot submit block with sequencer signature over different block header data function test_badSignature() public { - // sign original block commitmenet with sequencer key + // sign original block commitment with sequencer key (uint8 v, bytes32 r, bytes32 s) = vm.sign(sequencerKey, commit); // change header data from what was signed by sequencer @@ -90,7 +101,7 @@ contract ZenithTest is Test { // cannot submit two rollup blocks within one host block function test_onePerBlock() public { - // sign block commitmenet with correct sequencer key + // sign block commitment with correct sequencer key (uint8 v, bytes32 r, bytes32 s) = vm.sign(sequencerKey, commit); // should emit BlockSubmitted event @@ -105,4 +116,48 @@ contract ZenithTest is Test { vm.expectRevert(Zenith.OneRollupBlockPerHostBlock.selector); target.submitBlock(header, v, r, s, blockData); } + + function test_addSequencer() public { + address newSequencer = vm.addr(notSequencerKey); + assertFalse(target.isSequencer(newSequencer)); + + vm.expectEmit(); + emit SequencerSet(newSequencer, true); + target.addSequencer(newSequencer); + + assertTrue(target.isSequencer(newSequencer)); + + // can sign block now with new sequencer key + (uint8 v, bytes32 r, bytes32 s) = vm.sign(notSequencerKey, commit); + vm.expectEmit(); + emit BlockSubmitted( + newSequencer, header.rollupChainId, header.gasLimit, header.rewardAddress, header.blockDataHash + ); + target.submitBlock(header, v, r, s, blockData); + } + + function test_removeSequencer() public { + address sequencer = vm.addr(sequencerKey); + assertTrue(target.isSequencer(sequencer)); + + vm.expectEmit(); + emit SequencerSet(sequencer, false); + target.removeSequencer(sequencer); + + assertFalse(target.isSequencer(sequencer)); + + // cannot sign block with the sequencer key + (uint8 v, bytes32 r, bytes32 s) = vm.sign(sequencerKey, commit); + vm.expectRevert(abi.encodeWithSelector(Zenith.BadSignature.selector, vm.addr(sequencerKey))); + target.submitBlock(header, v, r, s, blockData); + } + + function test_notSequencerAdmin() public { + vm.startPrank(address(0x01)); + vm.expectRevert(Zenith.OnlySequencerAdmin.selector); + target.addSequencer(address(0x02)); + + vm.expectRevert(Zenith.OnlySequencerAdmin.selector); + target.removeSequencer(address(0x02)); + } }