-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(contracts): improve Consenus.sol naming and code structure (#…
…762) * Remove vote swapped * Update variables structure * Move functions * Remove updateValidator * Order functions * Use _prefixes * Rename * Renames * Rename * Rename * Use modifier errors * Validator errors * Vote errors * Range error * Layout * Remove todos * Remove block.prevrandao * Rename methods * UpdateValidator * Shuffle validators * Parameter naming * Rename blsPublicKey * Rename blsPublicKeyHash * Implement _verifyAndRegisterBlsPublicKey * Version contract * Update abi * Update crypto * Rename calculateActiveValidators * Rename getTopValidators * Update e2e * Update function sigs * Update function sigs
- Loading branch information
1 parent
c4f07e4
commit 348a089
Showing
37 changed files
with
2,694 additions
and
2,457 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// SPDX-License-Identifier: GNU GENERAL PUBLIC LICENSE | ||
pragma solidity ^0.8.13; | ||
|
||
import { | ||
ConsensusV1, | ||
ValidatorData, | ||
Validator, | ||
ValidatorUpdated, | ||
CallerIsOwner, | ||
ValidatorAlreadyRegistered, | ||
BlsKeyAlreadyRegistered, | ||
BlsKeyIsInvalid, | ||
CallerIsNotValidator | ||
} from "@contracts/consensus/ConsensusV1.sol"; | ||
import {Base} from "./Base.sol"; | ||
|
||
contract ConsensusTest is Base { | ||
ConsensusV1 public consensus; | ||
|
||
function setUp() public { | ||
consensus = new ConsensusV1(); | ||
} | ||
|
||
function test_updateBlsPublicKey_revert_if_caller_is_not_validator() public { | ||
vm.expectRevert(CallerIsNotValidator.selector); | ||
consensus.resignValidator(); | ||
} | ||
|
||
function test_updateBlsPublicKey_revert_if_bls_key_is_already_registered() public { | ||
address addr = address(1); | ||
vm.startPrank(addr); | ||
|
||
consensus.registerValidator(prepareBLSKey(addr)); | ||
|
||
vm.expectRevert(BlsKeyAlreadyRegistered.selector); | ||
consensus.updateValidator(prepareBLSKey(addr)); | ||
} | ||
|
||
function test_updateBlsPublicKey_revert_if_bls_key_is_already_registered_by_different_vlidator() public { | ||
address addr = address(1); | ||
vm.startPrank(addr); | ||
consensus.registerValidator(prepareBLSKey(addr)); | ||
|
||
address addr2 = address(2); | ||
vm.startPrank(addr2); | ||
consensus.registerValidator(prepareBLSKey(addr2)); | ||
|
||
vm.expectRevert(BlsKeyAlreadyRegistered.selector); | ||
consensus.updateValidator(prepareBLSKey(addr)); | ||
} | ||
|
||
function test_updateBlsPublicKey_revert_if_bls_key_is_invalid() public { | ||
address addr = address(1); | ||
vm.startPrank(addr); | ||
consensus.registerValidator(prepareBLSKey(addr)); | ||
|
||
vm.expectRevert(BlsKeyIsInvalid.selector); | ||
consensus.updateValidator(prepareBLSKey(addr, 46)); | ||
vm.expectRevert(BlsKeyIsInvalid.selector); | ||
consensus.updateValidator(prepareBLSKey(addr, 47)); | ||
vm.expectRevert(BlsKeyIsInvalid.selector); | ||
consensus.updateValidator(prepareBLSKey(addr, 49)); | ||
vm.expectRevert(BlsKeyIsInvalid.selector); | ||
consensus.updateValidator(prepareBLSKey(addr, 50)); | ||
} | ||
|
||
function test_updateBlsPublicKey_should_pass() public { | ||
address addr = address(1); | ||
vm.startPrank(addr); | ||
consensus.registerValidator(prepareBLSKey(addr)); | ||
|
||
vm.expectEmit(address(consensus)); | ||
emit ValidatorUpdated(addr, prepareBLSKey(address(2))); | ||
consensus.updateValidator(prepareBLSKey(address(2))); | ||
|
||
Validator memory validator = consensus.getValidator(addr); | ||
assertEq(validator.addr, addr); | ||
assertEq(validator.data.blsPublicKey, prepareBLSKey(address(2))); | ||
} | ||
|
||
function test_updateBlsPublicKey_revert_on_second_update() public { | ||
address addr = address(1); | ||
vm.startPrank(addr); | ||
consensus.registerValidator(prepareBLSKey(addr)); | ||
|
||
vm.expectEmit(address(consensus)); | ||
emit ValidatorUpdated(addr, prepareBLSKey(address(2))); | ||
consensus.updateValidator(prepareBLSKey(address(2))); | ||
|
||
Validator memory validator = consensus.getValidator(addr); | ||
assertEq(validator.addr, addr); | ||
assertEq(validator.data.blsPublicKey, prepareBLSKey(address(2))); | ||
|
||
vm.expectRevert(BlsKeyAlreadyRegistered.selector); | ||
consensus.updateValidator(prepareBLSKey(address(2))); | ||
} | ||
} |
Oops, something went wrong.