Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Zenith sequencer fns #52

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
PassageTest:test_configureEnter() (gas: 82311)
PassageTest:test_disallowedEnter() (gas: 17916)
PassageTest:test_enter() (gas: 25563)
PassageTest:test_enterToken() (gas: 64332)
PassageTest:test_enterToken_defaultChain() (gas: 62915)
PassageTest:test_enterTransact() (gas: 60890)
PassageTest:test_enter_defaultChain() (gas: 24033)
PassageTest:test_fallback() (gas: 21534)
PassageTest:test_onlyTokenAdmin() (gas: 16926)
PassageTest:test_receive() (gas: 21384)
PassageTest:test_setUp() (gas: 16968)
PassageTest:test_transact() (gas: 58562)
PassageTest:test_transact_defaultChain() (gas: 57475)
PassageTest:test_withdraw() (gas: 59033)
ZenithTest:test_addSequencer() (gas: 88191)
ZenithTest:test_badSignature() (gas: 37477)
ZenithTest:test_configureEnter() (gas: 82311)
ZenithTest:test_disallowedEnter() (gas: 17916)
ZenithTest:test_enter() (gas: 25563)
ZenithTest:test_enterToken() (gas: 64332)
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_onlyTokenAdmin() (gas: 16926)
ZenithTest:test_receive() (gas: 21384)
ZenithTest:test_setUp() (gas: 16968)
ZenithTest:test_submitBlock() (gas: 61187)
ZenithTest:test_transact() (gas: 58562)
ZenithTest:test_transact_defaultChain() (gas: 57475)
ZenithTest:test_withdraw() (gas: 59033)
ZenithTest:test_incorrectHostBlock() (gas: 35310)
ZenithTest:test_notSequencer() (gas: 34223)
ZenithTest:test_notSequencerAdmin() (gas: 10193)
ZenithTest:test_onePerBlock() (gas: 68463)
ZenithTest:test_removeSequencer() (gas: 39857)
ZenithTest:test_setUp() (gas: 8434)
ZenithTest:test_submitBlock() (gas: 63468)
2 changes: 1 addition & 1 deletion test/Passage.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract TestERC20 is ERC20 {
}
}

contract ZenithTest is Test {
contract PassageTest is Test {
Passage public target;
address token;
address newToken;
Expand Down
65 changes: 60 additions & 5 deletions test/Zenith.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
Expand All @@ -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)));
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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));
}
}