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

refactor(contracts): move events, structs and errors inside contracts #776

Merged
merged 4 commits into from
Nov 26, 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
128 changes: 64 additions & 64 deletions contracts/src/consensus/ConsensusV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,6 @@ pragma solidity ^0.8.27;
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

struct ValidatorData {
uint256 votersCount;
uint256 voteBalance;
bool isResigned;
bytes blsPublicKey; // 96 bits
}

struct Validator {
address addr;
ValidatorData data;
}

struct RoundValidator {
address addr;
uint256 voteBalance;
}

struct Round {
uint256 round;
RoundValidator[] validators;
}

struct Vote {
address validator;
uint256 balance;
address prev;
address next;
}

struct VoteResult {
address voter;
address validator;
}

event ValidatorRegistered(address addr, bytes blsPublicKey);

event ValidatorUpdated(address addr, bytes blsPublicKey);

event ValidatorResigned(address addr);

event Voted(address voter, address validator);

event Unvoted(address voter, address validator);

error CallerIsNotValidator();
error ValidatorNotRegistered();
error ValidatorAlreadyRegistered();
error ValidatorAlreadyResigned();
error BellowMinValidators();
error NoActiveValidators();

error BlsKeyAlreadyRegistered();
error BlsKeyIsInvalid();

error VoteResignedValidator();
error VoteSameValidator();
error VoteValidatorWithoutBlsPublicKey();
error AlreadyVoted();
error MissingVote();

error InvalidRange(uint256 min, uint256 max);
error InvalidParameters();
error ImportIsNotAllowed();

// Validators:
// - Registered -> All validators that are registered including resigned validators
// - Active -> Top N validators with the highest vote balance, that participate in the consensus
Expand All @@ -87,6 +23,70 @@ error ImportIsNotAllowed();
// This process will only work fine if we pass the new wallet balance (88) and keep track of voteBalances in EVM contract.

contract ConsensusV1 is UUPSUpgradeable, OwnableUpgradeable {
struct ValidatorData {
uint256 votersCount;
uint256 voteBalance;
bool isResigned;
bytes blsPublicKey; // 96 bits
}

struct Validator {
address addr;
ValidatorData data;
}

struct RoundValidator {
address addr;
uint256 voteBalance;
}

struct Round {
uint256 round;
RoundValidator[] validators;
}

struct Vote {
address validator;
uint256 balance;
address prev;
address next;
}

struct VoteResult {
address voter;
address validator;
}

event ValidatorRegistered(address addr, bytes blsPublicKey);

event ValidatorUpdated(address addr, bytes blsPublicKey);

event ValidatorResigned(address addr);

event Voted(address voter, address validator);

event Unvoted(address voter, address validator);

error CallerIsNotValidator();
error ValidatorNotRegistered();
error ValidatorAlreadyRegistered();
error ValidatorAlreadyResigned();
error BellowMinValidators();
error NoActiveValidators();

error BlsKeyAlreadyRegistered();
error BlsKeyIsInvalid();

error VoteResignedValidator();
error VoteSameValidator();
error VoteValidatorWithoutBlsPublicKey();
error AlreadyVoted();
error MissingVote();

error InvalidRange(uint256 min, uint256 max);
error InvalidParameters();
error ImportIsNotAllowed();

mapping(address => ValidatorData) private _validatorsData;
mapping(address => bool) private _hasValidator;
mapping(bytes32 => bool) private _blsPublicKeys;
Expand Down
8 changes: 4 additions & 4 deletions contracts/src/multi-payment/MultiPayment.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: GNU GENERAL PUBLIC LICENSE
pragma solidity ^0.8.27;

error RecipientsAndAmountsMismatch();
error InvalidValue();
error FailedToSendEther();

contract MultiPayment {
error RecipientsAndAmountsMismatch();
error InvalidValue();
error FailedToSendEther();

function pay(address payable[] calldata recipients, uint256[] calldata amounts) public payable {
if (recipients.length != amounts.length) {
revert RecipientsAndAmountsMismatch();
Expand Down
20 changes: 10 additions & 10 deletions contracts/src/usernames/UsernamesV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ pragma solidity ^0.8.27;
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

error InvalidUsername();
error TakenUsername();
error UsernameNotRegistered();
contract UsernamesV1 is UUPSUpgradeable, OwnableUpgradeable {
error InvalidUsername();
error TakenUsername();
error UsernameNotRegistered();

event UsernameRegistered(address addr, string username, string previousUsername);
event UsernameRegistered(address addr, string username, string previousUsername);

event UsernameResigned(address addr, string username);
event UsernameResigned(address addr, string username);

struct User {
address addr;
string username;
}
struct User {
address addr;
string username;
}

contract UsernamesV1 is UUPSUpgradeable, OwnableUpgradeable {
mapping(address => string) private _usernames;
mapping(bytes32 => bool) private _usernameExists;

Expand Down
18 changes: 13 additions & 5 deletions contracts/test/consensus/Base.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity ^0.8.13;

import {Test, console} from "@forge-std/Test.sol";
import {ConsensusV1, Validator} from "@contracts/consensus/ConsensusV1.sol";
import {ConsensusV1} from "@contracts/consensus/ConsensusV1.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

contract Base is Test {
Expand All @@ -27,13 +27,17 @@ contract Base is Test {
return prepareBLSKey(addr, 48);
}

function sortValidators(Validator[] memory validators) public pure returns (Validator[] memory) {
function sortValidators(ConsensusV1.Validator[] memory validators)
public
pure
returns (ConsensusV1.Validator[] memory)
{
uint256 length = validators.length;
for (uint256 i = 0; i < length; i++) {
for (uint256 j = i + 1; j < length; j++) {
// Sort in descending order by votersCount
if (_isGreater(validators[i], validators[j])) {
Validator memory temp = validators[i];
ConsensusV1.Validator memory temp = validators[i];
validators[i] = validators[j];
validators[j] = temp;
}
Expand All @@ -42,7 +46,11 @@ contract Base is Test {
return validators;
}

function _isGreater(Validator memory validatorA, Validator memory validatorB) internal pure returns (bool) {
function _isGreater(ConsensusV1.Validator memory validatorA, ConsensusV1.Validator memory validatorB)
internal
pure
returns (bool)
{
if (validatorA.data.voteBalance == validatorB.data.voteBalance) {
return validatorA.addr > validatorB.addr;
}
Expand All @@ -61,7 +69,7 @@ contract Base is Test {
consensus.registerValidator(validatorKey);
vm.stopPrank();

Validator memory validator = consensus.getValidator(addr);
ConsensusV1.Validator memory validator = consensus.getValidator(addr);
assertEq(validator.addr, addr);
assertEq(validator.data.voteBalance, 0 ether);
assertEq(validator.data.votersCount, 0);
Expand Down
26 changes: 10 additions & 16 deletions contracts/test/consensus/Consensus-CalculateTop.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
// SPDX-License-Identifier: GNU GENERAL PUBLIC LICENSE
pragma solidity ^0.8.13;

import {
ConsensusV1,
ValidatorData,
Validator,
InvalidParameters,
NoActiveValidators
} from "@contracts/consensus/ConsensusV1.sol";
import {ConsensusV1} from "@contracts/consensus/ConsensusV1.sol";
import {Base} from "./Base.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
Expand All @@ -18,7 +12,7 @@ contract ConsensusTest is Base {
registerValidator(addr);

consensus.calculateActiveValidators(1);
Validator[] memory validators = consensus.getActiveValidators();
ConsensusV1.Validator[] memory validators = consensus.getActiveValidators();
assertEq(validators.length, 1);
assertEq(validators[0].addr, addr);
}
Expand All @@ -33,26 +27,26 @@ contract ConsensusTest is Base {
function test_should_revert_with_0_parameter() public {
registerValidator(address(1));

vm.expectRevert(InvalidParameters.selector);
vm.expectRevert(ConsensusV1.InvalidParameters.selector);
consensus.calculateActiveValidators(0);
}

function test_should_revert_without_validators() public {
vm.expectRevert(NoActiveValidators.selector);
vm.expectRevert(ConsensusV1.NoActiveValidators.selector);
consensus.calculateActiveValidators(1);
}

function test_should_revert_with_only_resigned_validators() public {
consensus.addValidator(address(2), prepareBLSKey(address(2)), true);

vm.expectRevert(NoActiveValidators.selector);
vm.expectRevert(ConsensusV1.NoActiveValidators.selector);
consensus.calculateActiveValidators(1);
}

function test_should_revert_with_only_validators_without_public_key() public {
consensus.addValidator(address(1), new bytes(0), false);

vm.expectRevert(NoActiveValidators.selector);
vm.expectRevert(ConsensusV1.NoActiveValidators.selector);
consensus.calculateActiveValidators(1);
}

Expand All @@ -64,7 +58,7 @@ contract ConsensusTest is Base {
resignValidator(addr);

consensus.calculateActiveValidators(2);
Validator[] memory validators = consensus.getActiveValidators();
ConsensusV1.Validator[] memory validators = consensus.getActiveValidators();
assertEq(validators.length, 2);
assertEq(validators[0].addr, address(2));
assertEq(validators[1].addr, address(2)); // Second validator is duplicated
Expand All @@ -79,7 +73,7 @@ contract ConsensusTest is Base {
resignValidator(address(2));

consensus.calculateActiveValidators(2);
Validator[] memory validators = consensus.getActiveValidators();
ConsensusV1.Validator[] memory validators = consensus.getActiveValidators();
assertEq(validators.length, 2);
assertEq(validators[0].addr, addr);
assertEq(validators[1].addr, addr); // Second validator is duplicated
Expand All @@ -92,7 +86,7 @@ contract ConsensusTest is Base {
consensus.addValidator(address(2), new bytes(0), false);

consensus.calculateActiveValidators(2);
Validator[] memory validators = consensus.getActiveValidators();
ConsensusV1.Validator[] memory validators = consensus.getActiveValidators();
assertEq(validators.length, 2);
assertEq(validators[0].addr, addr);
assertEq(validators[1].addr, addr); // Second validator is duplicated
Expand Down Expand Up @@ -134,7 +128,7 @@ contract ConsensusTest is Base {
uint160 activeValidators = 53;

consensus.calculateActiveValidators(uint8(activeValidators));
Validator[] memory validators = consensus.getActiveValidators();
ConsensusV1.Validator[] memory validators = consensus.getActiveValidators();
assertEq(validators.length, activeValidators);

assertEq(validators[activeValidators - 1].addr, address(0xAE)); // Shuffled address
Expand Down
4 changes: 2 additions & 2 deletions contracts/test/consensus/Consensus-GetAllValidators.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GNU GENERAL PUBLIC LICENSE
pragma solidity ^0.8.13;

import {ConsensusV1, ValidatorData, Validator} from "@contracts/consensus/ConsensusV1.sol";
import {ConsensusV1} from "@contracts/consensus/ConsensusV1.sol";
import {Base} from "./Base.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

Expand All @@ -22,7 +22,7 @@ contract ConsensusTest is Base {

vm.resumeGasMetering();

Validator[] memory validators = consensus.getAllValidators();
ConsensusV1.Validator[] memory validators = consensus.getAllValidators();
assertEq(validators.length, n);
}
}
6 changes: 3 additions & 3 deletions contracts/test/consensus/Consensus-Proxy.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GNU GENERAL PUBLIC LICENSE
pragma solidity ^0.8.13;

import {ConsensusV1, ValidatorData, Validator} from "@contracts/consensus/ConsensusV1.sol";
import {ConsensusV1} from "@contracts/consensus/ConsensusV1.sol";
import {Base} from "./Base.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import {Initializable} from "@openzeppelin/contracts/proxy/utils/Initializable.sol";
Expand Down Expand Up @@ -65,7 +65,7 @@ contract ConsensusTest is Base {
assertEq(consensus.activeValidatorsCount(), 2);
assertEq(consensus.getVotesCount(), 1);
assertEq(consensus.getActiveValidators().length, 2);
Validator[] memory validatorsBefore = consensus.getAllValidators();
ConsensusV1.Validator[] memory validatorsBefore = consensus.getAllValidators();
assertEq(validatorsBefore.length, 3);

// Upgrade
Expand All @@ -80,7 +80,7 @@ contract ConsensusTest is Base {
assertEq(consensusNew.activeValidatorsCount(), 2);
assertEq(consensusNew.getVotesCount(), 1);
assertEq(consensus.getActiveValidators().length, 2);
Validator[] memory validatorsAfter = consensusNew.getAllValidators();
ConsensusV1.Validator[] memory validatorsAfter = consensusNew.getAllValidators();
assertEq(validatorsAfter.length, 3);

// Compare valdiators
Expand Down
Loading
Loading