-
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.
feat(contracts): add
ValidatorRegistered
, ValidatorResigned
event…
…s and improve resignation logic (#733) * Remove todo * Test validator registrations * Check validator data * Emit event * Remove performValidatorResignations * Extract base contract * Test validator resignation with emit * Test requires * Skip resigned validators from topValidatorsCalculation * Add resignedValidatorsCount * Update abi
- Loading branch information
1 parent
35cd52c
commit 990ac1a
Showing
7 changed files
with
287 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-License-Identifier: GNU GENERAL PUBLIC LICENSE | ||
pragma solidity ^0.8.13; | ||
|
||
import {Test, console} from "@forge-std/Test.sol"; | ||
|
||
contract Base is Test { | ||
function prepareBLSKey(address addr, uint8 lenght) public pure returns (bytes memory) { | ||
bytes32 h = keccak256(abi.encode(addr)); | ||
bytes memory validatorKey = new bytes(lenght); | ||
for (uint256 j = 0; j < 32; j++) { | ||
validatorKey[j] = h[j]; | ||
} | ||
return validatorKey; | ||
} | ||
|
||
function prepareBLSKey(address addr) public pure returns (bytes memory) { | ||
return prepareBLSKey(addr, 48); | ||
} | ||
} |
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
74 changes: 74 additions & 0 deletions
74
contracts/test/consensus/Consensus-ValidatorRegistration.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,74 @@ | ||
// SPDX-License-Identifier: GNU GENERAL PUBLIC LICENSE | ||
pragma solidity ^0.8.13; | ||
|
||
import {Consensus, ValidatorData, Validator, ValidatorRegistered} from "@contracts/consensus/Consensus.sol"; | ||
import {Base} from "./Base.sol"; | ||
|
||
contract ConsensusTest is Base { | ||
Consensus public consensus; | ||
|
||
function setUp() public { | ||
consensus = new Consensus(); | ||
} | ||
|
||
function test_validator_registration_pass() public { | ||
assertEq(consensus.registeredValidatorsCount(), 0); | ||
address addr = address(1); | ||
|
||
// Act | ||
vm.startPrank(addr); | ||
vm.expectEmit(address(consensus)); | ||
emit ValidatorRegistered(addr, prepareBLSKey(addr)); | ||
consensus.registerValidator(prepareBLSKey(addr)); | ||
vm.stopPrank(); | ||
|
||
// Assert | ||
assertEq(consensus.registeredValidatorsCount(), 1); | ||
Validator memory validator = consensus.getValidator(addr); | ||
assertEq(validator.addr, addr); | ||
assertEq(validator.data.bls12_381_public_key, prepareBLSKey(addr)); | ||
assertEq(validator.data.voteBalance, 0); | ||
assertEq(validator.data.votersCount, 0); | ||
assertEq(validator.data.isResigned, false); | ||
} | ||
|
||
function test_validator_registration_revert_if_caller_is_owner() public { | ||
vm.expectRevert("Invalid caller"); | ||
consensus.registerValidator(prepareBLSKey(address(1))); | ||
} | ||
|
||
function test_validator_registration_revert_if_validator_is_already_registered() public { | ||
address addr = address(1); | ||
|
||
vm.startPrank(addr); | ||
consensus.registerValidator(prepareBLSKey(addr)); | ||
|
||
vm.expectRevert("Validator is already registered"); | ||
consensus.registerValidator(prepareBLSKey(address(2))); | ||
} | ||
|
||
function test_validator_registration_revert_if_bls_key_is_already_registered() public { | ||
address addr = address(1); | ||
vm.startPrank(addr); | ||
|
||
consensus.registerValidator(prepareBLSKey(addr)); | ||
|
||
vm.startPrank(address(2)); | ||
vm.expectRevert("BLS12-381 key is already registered"); | ||
consensus.registerValidator(prepareBLSKey(addr)); | ||
} | ||
|
||
function test_validator_registration_revert_if_bls_key_length_is_invalid() public { | ||
address addr = address(1); | ||
vm.startPrank(addr); | ||
|
||
vm.expectRevert("BLS12-381 publicKey length is invalid"); | ||
consensus.registerValidator(prepareBLSKey(addr, 46)); | ||
vm.expectRevert("BLS12-381 publicKey length is invalid"); | ||
consensus.registerValidator(prepareBLSKey(addr, 47)); | ||
vm.expectRevert("BLS12-381 publicKey length is invalid"); | ||
consensus.registerValidator(prepareBLSKey(addr, 49)); | ||
vm.expectRevert("BLS12-381 publicKey length is invalid"); | ||
consensus.registerValidator(prepareBLSKey(addr, 50)); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
contracts/test/consensus/Consensus-ValidatorResignation.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,75 @@ | ||
// SPDX-License-Identifier: GNU GENERAL PUBLIC LICENSE | ||
pragma solidity ^0.8.13; | ||
|
||
import {Consensus, ValidatorData, Validator, ValidatorResigned} from "@contracts/consensus/Consensus.sol"; | ||
import {Base} from "./Base.sol"; | ||
|
||
contract ConsensusTest is Base { | ||
Consensus public consensus; | ||
|
||
function setUp() public { | ||
consensus = new Consensus(); | ||
} | ||
|
||
function test_validator_resignation_pass() public { | ||
assertEq(consensus.registeredValidatorsCount(), 0); | ||
address addr = address(1); | ||
|
||
// Act | ||
vm.startPrank(addr); | ||
consensus.registerValidator(prepareBLSKey(addr)); | ||
vm.stopPrank(); | ||
|
||
// Assert | ||
assertEq(consensus.registeredValidatorsCount(), 1); | ||
Validator memory validator = consensus.getValidator(addr); | ||
assertEq(validator.addr, addr); | ||
assertEq(validator.data.bls12_381_public_key, prepareBLSKey(addr)); | ||
assertEq(validator.data.voteBalance, 0); | ||
assertEq(validator.data.votersCount, 0); | ||
assertEq(validator.data.isResigned, false); | ||
|
||
// Act | ||
vm.startPrank(addr); | ||
vm.expectEmit(address(consensus)); | ||
emit ValidatorResigned(addr); | ||
consensus.resignValidator(); | ||
vm.stopPrank(); | ||
|
||
|
||
assertEq(consensus.registeredValidatorsCount(), 1); | ||
validator = consensus.getValidator(addr); | ||
assertEq(validator.addr, addr); | ||
assertEq(validator.data.bls12_381_public_key, prepareBLSKey(addr)); | ||
assertEq(validator.data.voteBalance, 0); | ||
assertEq(validator.data.votersCount, 0); | ||
assertEq(validator.data.isResigned, true); | ||
} | ||
|
||
function test_validator_resignation_revert_if_caller_is_not_validator() public { | ||
vm.expectRevert("Caller is not a validator"); | ||
consensus.resignValidator(); | ||
} | ||
|
||
function test_validator_resignation_revert_if_resigned() public { | ||
assertEq(consensus.registeredValidatorsCount(), 0); | ||
address addr = address(1); | ||
|
||
// Act | ||
vm.startPrank(addr); | ||
consensus.registerValidator(prepareBLSKey(addr)); | ||
vm.stopPrank(); | ||
|
||
// Assert | ||
assertEq(consensus.registeredValidatorsCount(), 1); | ||
|
||
// Act | ||
vm.startPrank(addr); | ||
vm.expectEmit(address(consensus)); | ||
emit ValidatorResigned(addr); | ||
consensus.resignValidator(); | ||
|
||
vm.expectRevert("Validator is already resigned"); | ||
consensus.resignValidator(); | ||
} | ||
} |
Oops, something went wrong.