forked from matter-labs/zksync-era
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validator): modified validator timelock to allow more than 1 val…
…idator (#211)
- Loading branch information
Showing
5 changed files
with
196 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
l1-contracts/test/foundry/unit/concrete/ValidatorTimelock.t.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.20; | ||
|
||
import {Test} from "forge-std/Test.sol"; | ||
import {Utils} from "./Utils/Utils.sol"; | ||
import {ValidatorTimelock, IExecutor} from "solpp/zksync/ValidatorTimelock.sol"; | ||
|
||
contract ValidatorTimelockTest is Test { | ||
/// @notice Event emitted from ValidatorTimelock when new validator is added | ||
event ValidatorAdded(address _addedValidator); | ||
|
||
/// @notice Event emitted from ValidatorTimelock when new validator is removed | ||
event ValidatorRemoved(address _removedValidator); | ||
|
||
/// @notice Error for when an address is already a validator. | ||
error AddressAlreadyValidator(); | ||
|
||
/// @notice Error for when an address is not a validator. | ||
error ValidatorDoesNotExist(); | ||
|
||
ValidatorTimelock validator; | ||
|
||
address owner; | ||
address zkSync; | ||
address alice; | ||
address bob; | ||
|
||
function setUp() public { | ||
owner = makeAddr("owner"); | ||
zkSync = makeAddr("zkSync"); | ||
alice = makeAddr("alice"); | ||
bob = makeAddr("bob"); | ||
|
||
address[] memory initValidators = new address[](1); | ||
initValidators[0] = alice; | ||
|
||
validator = new ValidatorTimelock(owner, zkSync, 10, initValidators); | ||
} | ||
|
||
function test_addValidator() public { | ||
assert(validator.validators(bob) == false); | ||
|
||
vm.prank(owner); | ||
vm.expectEmit(true, true, true, true, address(validator)); | ||
emit ValidatorAdded(bob); | ||
validator.addValidator(bob); | ||
|
||
assert(validator.validators(bob) == true); | ||
} | ||
|
||
function test_removeValidator() public { | ||
vm.prank(owner); | ||
validator.addValidator(bob); | ||
assert(validator.validators(bob) == true); | ||
|
||
vm.prank(owner); | ||
vm.expectEmit(true, true, true, true, address(validator)); | ||
emit ValidatorRemoved(bob); | ||
validator.removeValidator(bob); | ||
|
||
assert(validator.validators(bob) == false); | ||
} | ||
|
||
function test_validatorCanMakeCall() public { | ||
// Setup Mock call to executor | ||
vm.mockCall(zkSync, abi.encodeWithSelector(IExecutor.commitBatches.selector), ""); | ||
|
||
IExecutor.StoredBatchInfo memory storedBatch = Utils.createStoredBatchInfo(); | ||
IExecutor.CommitBatchInfo memory batchToCommit = Utils.createCommitBatchInfo(); | ||
|
||
IExecutor.CommitBatchInfo[] memory batchesToCommit = new IExecutor.CommitBatchInfo[](1); | ||
batchesToCommit[0] = batchToCommit; | ||
|
||
vm.prank(alice); | ||
validator.commitBatches(storedBatch, batchesToCommit); | ||
} | ||
|
||
function test_addValidator_revertWhenNotOwner() public { | ||
assert(validator.validators(bob) == false); | ||
|
||
vm.expectRevert("Ownable: caller is not the owner"); | ||
validator.addValidator(bob); | ||
|
||
assert(validator.validators(bob) == false); | ||
} | ||
|
||
function test_removeValidator_revertWhenNotOwner() public { | ||
assert(validator.validators(alice) == true); | ||
|
||
vm.expectRevert("Ownable: caller is not the owner"); | ||
validator.removeValidator(alice); | ||
|
||
assert(validator.validators(alice) == true); | ||
} | ||
|
||
function test_addValidator_revertWhenAddressAlreadyValidator() public { | ||
assert(validator.validators(alice) == true); | ||
|
||
vm.prank(owner); | ||
vm.expectRevert(AddressAlreadyValidator.selector); | ||
validator.addValidator(alice); | ||
} | ||
|
||
function test_removeValidator_revertWhenAddressNotValidator() public { | ||
assert(validator.validators(bob) == false); | ||
|
||
vm.prank(owner); | ||
vm.expectRevert(ValidatorDoesNotExist.selector); | ||
validator.removeValidator(bob); | ||
} | ||
|
||
function test_validatorCanMakeCall_revertWhenNotValidator() public { | ||
IExecutor.StoredBatchInfo memory storedBatch = Utils.createStoredBatchInfo(); | ||
IExecutor.CommitBatchInfo memory batchToCommit = Utils.createCommitBatchInfo(); | ||
|
||
IExecutor.CommitBatchInfo[] memory batchesToCommit = new IExecutor.CommitBatchInfo[](1); | ||
batchesToCommit[0] = batchToCommit; | ||
|
||
vm.prank(bob); | ||
vm.expectRevert(bytes("8h")); | ||
validator.commitBatches(storedBatch, batchesToCommit); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters