diff --git a/packages/protocol/test-sol/unit/governance/voting/Election.t.sol b/packages/protocol/test-sol/unit/governance/voting/Election.t.sol index 3a3b2a398f..c500628b1b 100644 --- a/packages/protocol/test-sol/unit/governance/voting/Election.t.sol +++ b/packages/protocol/test-sol/unit/governance/voting/Election.t.sol @@ -30,7 +30,7 @@ contract ElectionMock is Election(true) { } } -contract ElectionTest is Utils, TestConstants { +contract ElectionTest is Utils, TestConstants, IsL2Check { using FixidityLib for FixidityLib.Fraction; Accounts accounts; @@ -173,6 +173,14 @@ contract ElectionTest is Utils, TestConstants { deployCodeTo("Registry.sol", abi.encode(false), PROXY_ADMIN_ADDRESS); epochManager.initializeSystem(l1EpochNumber, block.number, _elected); } + + function travelNEpoch(uint256 n) public { + if (isL2()) { + epochManager.setCurrentEpochNumber(epochManager.getCurrentEpochNumber() + n); + } else { + blockTravel((n * ph.epochSize()) + 1); + } + } } contract TransitionToL2After is ElectionTest { @@ -226,20 +234,10 @@ contract ElectionTest_SetElectabilityThreshold is ElectionTest { } } -contract ElectionTest_SetElectabilityThreshold_L2 is ElectionTest { - function test_shouldSetElectabilityThreshold() public { - _whenL2(); - uint256 newElectabilityThreshold = FixidityLib.newFixedFraction(1, 200).unwrap(); - election.setElectabilityThreshold(newElectabilityThreshold); - assertEq(election.electabilityThreshold(), newElectabilityThreshold); - } - - function test_ShouldRevertWhenThresholdLargerThan100Percent() public { - _whenL2(); - vm.expectRevert("Electability threshold must be lower than 100%"); - election.setElectabilityThreshold(FixidityLib.fixed1().unwrap() + 1); - } -} +contract ElectionTest_SetElectabilityThreshold_L2 is + ElectionTest_SetElectabilityThreshold, + TransitionToL2After +{} contract ElectionTest_SetElectableValidators is ElectionTest { function test_shouldSetElectableValidators() public { @@ -281,49 +279,10 @@ contract ElectionTest_SetElectableValidators is ElectionTest { } } -contract ElectionTest_SetElectableValidators_L2 is ElectionTest { - function setUp() public { - super.setUp(); - _whenL2(); - } - function test_shouldSetElectableValidators() public { - uint256 newElectableValidatorsMin = 2; - uint256 newElectableValidatorsMax = 4; - election.setElectableValidators(newElectableValidatorsMin, newElectableValidatorsMax); - (uint256 min, uint256 max) = election.getElectableValidators(); - assertEq(min, newElectableValidatorsMin); - assertEq(max, newElectableValidatorsMax); - } - - function test_ShouldEmitTheElectableValidatorsSetEvent() public { - uint256 newElectableValidatorsMin = 2; - uint256 newElectableValidatorsMax = 4; - vm.expectEmit(true, false, false, false); - emit ElectableValidatorsSet(newElectableValidatorsMin, newElectableValidatorsMax); - election.setElectableValidators(newElectableValidatorsMin, newElectableValidatorsMax); - } - - function test_ShouldRevertWhenMinElectableValidatorsIsZero() public { - vm.expectRevert("Minimum electable validators cannot be zero"); - election.setElectableValidators(0, electableValidatorsMax); - } - - function test_ShouldRevertWhenTHeminIsGreaterThanMax() public { - vm.expectRevert("Maximum electable validators cannot be smaller than minimum"); - election.setElectableValidators(electableValidatorsMax, electableValidatorsMin); - } - - function test_ShouldRevertWhenValuesAreUnchanged() public { - vm.expectRevert("Electable validators not changed"); - election.setElectableValidators(electableValidatorsMin, electableValidatorsMax); - } - - function test_ShouldRevertWhenCalledByNonOwner() public { - vm.expectRevert("Ownable: caller is not the owner"); - vm.prank(nonOwner); - election.setElectableValidators(1, 2); - } -} +contract ElectionTest_SetElectableValidators_L2 is + ElectionTest_SetElectableValidators, + TransitionToL2After +{} contract ElectionTest_SetMaxNumGroupsVotedFor is ElectionTest { function test_shouldSetMaxNumGroupsVotedFor() public { @@ -351,36 +310,10 @@ contract ElectionTest_SetMaxNumGroupsVotedFor is ElectionTest { } } -contract ElectionTest_SetMaxNumGroupsVotedFor_L2 is ElectionTest { - function setUp() public { - super.setUp(); - _whenL2(); - } - - function test_shouldSetMaxNumGroupsVotedFor() public { - uint256 newMaxNumGroupsVotedFor = 4; - election.setMaxNumGroupsVotedFor(newMaxNumGroupsVotedFor); - assertEq(election.maxNumGroupsVotedFor(), newMaxNumGroupsVotedFor); - } - - function test_ShouldEmitMaxNumGroupsVotedForSetEvent() public { - uint256 newMaxNumGroupsVotedFor = 4; - vm.expectEmit(true, false, false, false); - emit MaxNumGroupsVotedForSet(newMaxNumGroupsVotedFor); - election.setMaxNumGroupsVotedFor(newMaxNumGroupsVotedFor); - } - - function test_ShouldRevertWhenCalledByNonOwner() public { - vm.expectRevert("Ownable: caller is not the owner"); - vm.prank(nonOwner); - election.setMaxNumGroupsVotedFor(1); - } - - function test_ShouldRevert_WhenMaxNumGroupsVotedForIsUnchanged() public { - vm.expectRevert("Max groups voted for not changed"); - election.setMaxNumGroupsVotedFor(maxNumGroupsVotedFor); - } -} +contract ElectionTest_SetMaxNumGroupsVotedFor_L2 is + ElectionTest_SetMaxNumGroupsVotedFor, + TransitionToL2After +{} contract ElectionTest_SetAllowedToVoteOverMaxNumberOfGroups is ElectionTest { function test_shouldSetAllowedToVoteOverMaxNumberOfGroups() public { @@ -421,49 +354,10 @@ contract ElectionTest_SetAllowedToVoteOverMaxNumberOfGroups is ElectionTest { } } -contract ElectionTest_SetAllowedToVoteOverMaxNumberOfGroups_L2 is ElectionTest { - function setUp() public { - super.setUp(); - _whenL2(); - } - - function test_shouldSetAllowedToVoteOverMaxNumberOfGroups() public { - election.setAllowedToVoteOverMaxNumberOfGroups(true); - assertEq(election.allowedToVoteOverMaxNumberOfGroups(address(this)), true); - } - - function test_ShouldRevertWhenCalledByValidator() public { - validators.setValidator(address(this)); - vm.expectRevert("Validators cannot vote for more than max number of groups"); - election.setAllowedToVoteOverMaxNumberOfGroups(true); - } - - function test_ShouldRevertWhenCalledByValidatorGroup() public { - validators.setValidatorGroup(address(this)); - vm.expectRevert("Validator groups cannot vote for more than max number of groups"); - election.setAllowedToVoteOverMaxNumberOfGroups(true); - } - - function test_ShouldEmitAllowedToVoteOverMaxNumberOfGroupsEvent() public { - vm.expectEmit(true, false, false, false); - emit AllowedToVoteOverMaxNumberOfGroups(address(this), true); - election.setAllowedToVoteOverMaxNumberOfGroups(true); - } - - function test_ShouldSwitchAllowedToVoteOverMaxNumberOfGroupsOff_WhenTurnedOn() public { - election.setAllowedToVoteOverMaxNumberOfGroups(true); - assertEq(election.allowedToVoteOverMaxNumberOfGroups(address(this)), true); - election.setAllowedToVoteOverMaxNumberOfGroups(false); - assertEq(election.allowedToVoteOverMaxNumberOfGroups(address(this)), false); - } - - function test_ShouldEmitAllowedToVoteOverMaxNumberOfGroupsEvent_WhenTurnedOn() public { - election.setAllowedToVoteOverMaxNumberOfGroups(true); - vm.expectEmit(true, false, false, false); - emit AllowedToVoteOverMaxNumberOfGroups(address(this), false); - election.setAllowedToVoteOverMaxNumberOfGroups(false); - } -} +contract ElectionTest_SetAllowedToVoteOverMaxNumberOfGroups_L2 is + ElectionTest_SetAllowedToVoteOverMaxNumberOfGroups, + TransitionToL2After +{} contract ElectionTest_MarkGroupEligible is ElectionTest { function setUp() public { @@ -501,41 +395,7 @@ contract ElectionTest_MarkGroupEligible is ElectionTest { } } -contract ElectionTest_MarkGroupEligible_L2 is ElectionTest { - function setUp() public { - super.setUp(); - _whenL2(); - registry.setAddressFor("Validators", address(address(this))); - } - - function test_shouldMarkGroupEligible() public { - address group = address(this); - election.markGroupEligible(group, address(0), address(0)); - address[] memory eligibleGroups = election.getEligibleValidatorGroups(); - assertEq(eligibleGroups.length, 1); - assertEq(eligibleGroups[0], group); - } - - function test_ShouldEmitValidatorGroupMarkedEligibleEvent() public { - address group = address(this); - vm.expectEmit(true, false, false, false); - emit ValidatorGroupMarkedEligible(group); - election.markGroupEligible(group, address(0), address(0)); - } - - function test_ShouldRevertWhenAlreadyMarkedEligible() public { - address group = address(this); - election.markGroupEligible(group, address(0), address(0)); - vm.expectRevert("invalid key"); - election.markGroupEligible(group, address(0), address(0)); - } - - function test_ShouldRevertWhenCalledByNonValidator() public { - vm.expectRevert("only registered contract"); - vm.prank(nonOwner); - election.markGroupEligible(address(this), address(0), address(0)); - } -} +contract ElectionTest_MarkGroupEligible_L2 is ElectionTest_MarkGroupEligible, TransitionToL2After {} contract ElectionTest_MarkGroupInEligible is ElectionTest { function setUp() public { @@ -573,6 +433,11 @@ contract ElectionTest_MarkGroupInEligible is ElectionTest { } } +contract ElectionTest_MarkGroupInEligible_L2 is + ElectionTest_MarkGroupInEligible, + TransitionToL2After +{} + contract ElectionTest_Vote_WhenGroupEligible is ElectionTest { address voter = address(this); address group = account1; @@ -694,7 +559,8 @@ contract ElectionTest_Vote_WhenGroupEligible is ElectionTest { function WhenVotesAreBeingActivated() public returns (address newGroup) { newGroup = WhenVotedForMoreThanMaxNumberOfGroups(); - blockTravel(ph.epochSize() + 1); + + travelNEpoch(1); election.activateForAccount(group, voter); } @@ -753,568 +619,41 @@ contract ElectionTest_Vote_WhenGroupEligible is ElectionTest { address newGroup = WhenAwardsAreDistributed(); manuallyUpdateTotalVotesForAllGroups(voter); - election.vote(newGroup, originallyNotVotedWithAmount, account4, group); - - assertEq(election.getTotalVotes(), value + rewardValue); - } - - function test_ShouldRevert_WhenTheGroupCannotReceiveVotes() public { - lockedGold.setTotalLockedGold(value / 2 - 1); - address[] memory members = new address[](1); - members[0] = account9; - validators.setMembers(group, members); - validators.setNumRegisteredValidators(1); - assertEq(election.getNumVotesReceivable(group), value - 2); - - vm.expectRevert("Group cannot receive votes"); - election.vote(group, value, address(0), address(0)); - } -} - -contract ElectionTest_Vote_WhenGroupEligible_L2 is ElectionTest { - address voter = address(this); - address group = account1; - uint256 value = 1000; - - uint256 originallyNotVotedWithAmount = 1; - uint256 voterFirstGroupVote = value - maxNumGroupsVotedFor - originallyNotVotedWithAmount; - uint256 rewardValue = 1000000; - - function setUp() public { - super.setUp(); - _whenL2(); - address[] memory members = new address[](1); - members[0] = account9; - validators.setMembers(group, members); - - vm.prank(address(validators)); - election.markGroupEligible(group, address(0), address(0)); - } - - function test_ShouldRevert_WhenTheVoterDoesNotHaveSufficientNonVotingBalance() public { - lockedGold.incrementNonvotingAccountBalance(voter, value - 1); - vm.expectRevert("SafeMath: subtraction overflow"); - election.vote(group, value, address(0), address(0)); - } - - function WhenVotedForMaxNumberOfGroups() public returns (address newGroup) { - lockedGold.incrementNonvotingAccountBalance(voter, value); - - for (uint256 i = 0; i < maxNumGroupsVotedFor; i++) { - address[] memory members = new address[](1); - members[0] = accountsArray[9]; - newGroup = accountsArray[i + 2]; - setupGroupAndVote(newGroup, group, members, true); - } - } - - function test_ShouldRevert_WhenTheVoterCannotVoteForAnAdditionalGroup() public { - address newGroup = WhenVotedForMaxNumberOfGroups(); - - vm.expectRevert("Voted for too many groups"); - election.vote(group, value - maxNumGroupsVotedFor, newGroup, address(0)); - } - - function test_ShouldAllowToVoteForAnotherGroup_WhenTheVoterIsOverMaxNumberGroupsVotedForButCanVoteForAdditionalGroup() - public - { - address newGroup = WhenVotedForMaxNumberOfGroups(); - election.setAllowedToVoteOverMaxNumberOfGroups(true); - - vm.expectEmit(true, true, true, true); - emit ValidatorGroupVoteCast(voter, group, value - maxNumGroupsVotedFor); - election.vote(group, value - maxNumGroupsVotedFor, newGroup, address(0)); - assertEq(election.getPendingVotesForGroupByAccount(group, voter), value - maxNumGroupsVotedFor); - } - - function test_ShouldSetTotalVotesByAccount_WhenMaxNumberOfGroupsWasNotReached() public { - WhenVotedForMaxNumberOfGroups(); - assertEq(election.getTotalVotesByAccount(voter), maxNumGroupsVotedFor); - } - - function WhenVotedForMoreThanMaxNumberOfGroups() public returns (address newGroup) { - newGroup = WhenVotedForMaxNumberOfGroups(); - election.setAllowedToVoteOverMaxNumberOfGroups(true); - election.vote(group, voterFirstGroupVote, newGroup, address(0)); - } - - function test_ShouldRevert_WhenTurningOffSetAllowedToVoteOverMaxNUmberOfGroups_WhenOverMaximumNumberOfGroupsVoted() - public - { - WhenVotedForMoreThanMaxNumberOfGroups(); - - vm.expectRevert("Too many groups voted for!"); - election.setAllowedToVoteOverMaxNumberOfGroups(false); - } - - function test_ShouldReturnOnlyLastVotedWith_WhenVotesWereNotManuallyCounted() public { - WhenVotedForMoreThanMaxNumberOfGroups(); - assertEq(election.getTotalVotesByAccount(voter), voterFirstGroupVote); - } - - function manuallyUpdateTotalVotesForAllGroups(address _voter) public { - for (uint256 i = 0; i < maxNumGroupsVotedFor; i++) { - election.updateTotalVotesByAccountForGroup(_voter, accountsArray[i + 2]); - } - election.updateTotalVotesByAccountForGroup(_voter, group); - } - - function WhenTotalVotesWereManuallyCounted() public { - WhenVotedForMoreThanMaxNumberOfGroups(); - manuallyUpdateTotalVotesForAllGroups(voter); - } - - function test_ShouldReturnTotalVotesByAccount_WhenTotalVotesAreManuallyCounted() public { - WhenTotalVotesWereManuallyCounted(); - assertEq(election.getTotalVotesByAccount(voter), value - originallyNotVotedWithAmount); - } - - function test_ShouldReturnLoweredTotalNumberOfVotes_WhenVotesRevoked_WhenTotalVotesWereManuallyCounted() - public - { - uint256 revokeDiff = 100; - uint256 revokeValue = voterFirstGroupVote - revokeDiff; - - WhenTotalVotesWereManuallyCounted(); - election.revokePending(group, revokeValue, accountsArray[4], address(0), 3); - assertEq(election.getTotalVotesByAccount(voter), maxNumGroupsVotedFor + revokeDiff); - } - - function WhenVotesAreBeingActivated() public returns (address newGroup) { - newGroup = WhenVotedForMoreThanMaxNumberOfGroups(); - epochManager.setCurrentEpochNumber(epochManager.getCurrentEpochNumber() + 1); - election.activateForAccount(group, voter); - } - - function test_ShouldIncrementTheAccountsActiveVotesForGroup_WhenVotesAreBeingActivated() public { - WhenVotesAreBeingActivated(); - assertEq(election.getActiveVotesForGroupByAccount(group, voter), voterFirstGroupVote); - } - - function test_ShouldReturnCorrectValueWhenManuallyCounted_WhenVotesAreBeingActivated() public { - WhenVotesAreBeingActivated(); - manuallyUpdateTotalVotesForAllGroups(voter); - - assertEq(election.getTotalVotesByAccount(voter), value - originallyNotVotedWithAmount); - } - - function WhenAwardsAreDistributed() public returns (address newGroup) { - newGroup = WhenVotesAreBeingActivated(); - election.distributeEpochRewards(group, rewardValue, newGroup, address(0)); - } - - // TODO: Implement when validator L2 rewards mechanism is implemented. - function skip_test_ShouldRevokeActiveVotes_WhenAwardsAreDistributed() public { - // (more then original votes without rewards) - address newGroup = WhenAwardsAreDistributed(); - election.revokeActive(group, value, newGroup, address(0), 3); - assertEq( - election.getActiveVotesForGroupByAccount(group, voter), - rewardValue - maxNumGroupsVotedFor - originallyNotVotedWithAmount - ); - } - - // TODO: Implement when validator L2 rewards mechanism is implemented. - function skip_test_ShouldReturnCorrectValueWhenManuallyCounted_WhenMoreVotesThanActiveIsRevoked_WhenAwardsAreDistributed() - public - { - address newGroup = WhenAwardsAreDistributed(); - election.revokeActive(group, value, newGroup, address(0), 3); - manuallyUpdateTotalVotesForAllGroups(voter); - - assertEq(election.getTotalVotesByAccount(voter), rewardValue - originallyNotVotedWithAmount); - } - - // TODO: Implement when validator L2 rewards mechanism is implemented. - function skip_test_ShouldReturnTotalVotesByAccount_WhenTotalVotesAreManuallyCountedOnReward_WhenAwardsAreDistributed() - public - { - WhenAwardsAreDistributed(); - manuallyUpdateTotalVotesForAllGroups(voter); - - assertEq( - election.getTotalVotesByAccount(voter), - value + rewardValue - originallyNotVotedWithAmount - ); - } - - // TODO: Implement when validator L2 rewards mechanism is implemented. - function skip_test_ShouldIncreaseTotalVotesCountOnceVoted_WhenTotalVotesAreManuallyCountedOnReward_WhenAwardsAreDistributed() - public - { - address newGroup = WhenAwardsAreDistributed(); - manuallyUpdateTotalVotesForAllGroups(voter); - - election.vote(newGroup, originallyNotVotedWithAmount, account4, group); - - assertEq(election.getTotalVotes(), value + rewardValue); - } - - function test_ShouldRevert_WhenTheGroupCannotReceiveVotes() public { - lockedGold.setTotalLockedGold(value / 2 - 1); - address[] memory members = new address[](1); - members[0] = account9; - validators.setMembers(group, members); - validators.setNumRegisteredValidators(1); - assertEq(election.getNumVotesReceivable(group), value - 2); - - vm.expectRevert("Group cannot receive votes"); - election.vote(group, value, address(0), address(0)); - } -} - -contract ElectionTest_Vote_WhenGroupEligible_WhenGroupCanReceiveVotes is ElectionTest { - address voter = address(this); - address group = account1; - uint256 value = 1000; - - uint256 originallyNotVotedWithAmount = 1; - uint256 voterFirstGroupVote = value - maxNumGroupsVotedFor - originallyNotVotedWithAmount; - uint256 rewardValue = 1000000; - - function setUp() public { - super.setUp(); - - address[] memory members = new address[](1); - members[0] = account9; - validators.setMembers(group, members); - - vm.prank(address(validators)); - election.markGroupEligible(group, address(0), address(0)); - - lockedGold.setTotalLockedGold(value); - validators.setNumRegisteredValidators(1); - } - - function WhenTheVoterCanVoteForAnAdditionalGroup() public { - lockedGold.incrementNonvotingAccountBalance(voter, value); - } - - function WhenTheVoterHasNotAlreadyVotedForThisGroup() public { - WhenTheVoterCanVoteForAnAdditionalGroup(); - election.vote(group, value, address(0), address(0)); - } - - function test_ShouldAddTheGroupToListOfGroupsTheAccountHasVotedFor_WhenTheVoterHasNotAlreadyVotedForThisGroup() - public - { - WhenTheVoterHasNotAlreadyVotedForThisGroup(); - address[] memory groupsVotedFor = election.getGroupsVotedForByAccount(voter); - assertEq(groupsVotedFor.length, 1); - assertEq(groupsVotedFor[0], group); - } - - function test_ShouldIncrementTheAccountsPendingVotesForTheGroup_WhenTheVoterHasNotAlreadyVotedForThisGroup() - public - { - WhenTheVoterHasNotAlreadyVotedForThisGroup(); - assertEq(election.getPendingVotesForGroupByAccount(group, voter), value); - } - - function test_ShouldIncrementTheAccountsTotalVotesForTheGroup_WhenTheVoterHasNotAlreadyVotedForThisGroup() - public - { - WhenTheVoterHasNotAlreadyVotedForThisGroup(); - assertEq(election.getTotalVotesForGroupByAccount(group, voter), value); - } - - function test_ShouldIncrementTheACcountsTotalVotes_WhenTheVoterHasNotAlreadyVotedForThisGroup() - public - { - WhenTheVoterHasNotAlreadyVotedForThisGroup(); - assertEq(election.getTotalVotesByAccount(voter), value); - } - - function test_ShouldIncrementTheTotalVotesForTheGroup_WhenTheVoterHasNotAlreadyVotedForThisGroup() - public - { - WhenTheVoterHasNotAlreadyVotedForThisGroup(); - assertEq(election.getTotalVotesForGroup(group), value); - } - - function test_ShouldIncrementTheTotalVotes_WhenTheVoterHasNotAlreadyVotedForThisGroup() public { - WhenTheVoterHasNotAlreadyVotedForThisGroup(); - assertEq(election.getTotalVotes(), value); - } - - function test_ShouldDecrementTheAccountsNonVotingLockedGoldBalance_WhenTheVoterHasNotAlreadyVotedForThisGroup() - public - { - WhenTheVoterHasNotAlreadyVotedForThisGroup(); - assertEq(lockedGold.nonvotingAccountBalance(voter), 0); - } - - function test_ShouldEmitTheValidatorGroupVoteCastEvent_WhenTheVoterHasNotAlreadyVotedForThisGroup() - public - { - WhenTheVoterCanVoteForAnAdditionalGroup(); - vm.expectEmit(true, false, false, false); - emit ValidatorGroupVoteCast(voter, group, value); - election.vote(group, value, address(0), address(0)); - } - - function WhenTheVoterHasAlreadyVotedForThisGroup() public { - WhenTheVoterHasNotAlreadyVotedForThisGroup(); - lockedGold.incrementNonvotingAccountBalance(voter, value); - election.vote(group, value, address(0), address(0)); - } - - function test_ShouldNotChangeTheListOfGroupsTheAccountVotedFor_WhenTheVoterHasAlreadyVotedForThisGroup() - public - { - WhenTheVoterHasAlreadyVotedForThisGroup(); - address[] memory groupsVotedFor = election.getGroupsVotedForByAccount(voter); - assertEq(groupsVotedFor.length, 1); - assertEq(groupsVotedFor[0], group); - } - - function test_ShouldIncreaseAccountsPendingVotesForTheGroup_WhenTheVoterHasAlreadyVotedForThisGroup() - public - { - WhenTheVoterHasAlreadyVotedForThisGroup(); - assertEq(election.getPendingVotesForGroupByAccount(group, voter), value * 2); - } - - function test_ShouldIncrementAccountTotalVotesForTheGroup_WhenTheVoterHasAlreadyVotedForThisGroup() - public - { - WhenTheVoterHasAlreadyVotedForThisGroup(); - assertEq(election.getTotalVotesForGroupByAccount(group, voter), value * 2); - } - - function test_ShouldIncrementTheAccountsTotalVotes_WhenTheVoterHasAlreadyVotedForThisGroup() - public - { - WhenTheVoterHasAlreadyVotedForThisGroup(); - assertEq(election.getTotalVotesByAccount(voter), value * 2); - } - - function test_ShouldIncrementTotalVotesForTheGroup_WhenTheVoterHasAlreadyVotedForThisGroup() - public - { - WhenTheVoterHasAlreadyVotedForThisGroup(); - assertEq(election.getTotalVotesForGroup(group), value * 2); - } - - function test_ShouldIncrementTotalVotes_WhenTheVoterHasAlreadyVotedForThisGroup() public { - WhenTheVoterHasAlreadyVotedForThisGroup(); - assertEq(election.getTotalVotes(), value * 2); - } - - function test_ShouldDecrementAccountNonVotingBalance_WhenTheVoterHasAlreadyVotedForThisGroup() - public - { - WhenTheVoterHasAlreadyVotedForThisGroup(); - assertEq(lockedGold.nonvotingAccountBalance(voter), 0); - } - - function test_ShouldEmitValidatorGroupVoteCast_WhenTheVoterHasAlreadyVotedForThisGroup() public { - WhenTheVoterHasNotAlreadyVotedForThisGroup(); - lockedGold.incrementNonvotingAccountBalance(voter, value); - vm.expectEmit(true, true, false, false); - emit ValidatorGroupVoteCast(voter, group, value); - election.vote(group, value, address(0), address(0)); - } -} - -contract ElectionTest_Vote_GroupNotEligible is ElectionTest { - address voter = address(this); - address group = account1; - uint256 value = 1000; - - uint256 originallyNotVotedWithAmount = 1; - uint256 voterFirstGroupVote = value - maxNumGroupsVotedFor - originallyNotVotedWithAmount; - uint256 rewardValue = 1000000; - - function setUp() public { - super.setUp(); - - address[] memory members = new address[](1); - members[0] = account9; - validators.setMembers(group, members); - } - - function test_ShouldRevert_WhenTheGroupIsNotEligible() public { - vm.expectRevert("Group not eligible"); - election.vote(group, value, address(0), address(0)); - } -} - -contract ElectionTest_Activate is ElectionTest { - address voter = address(this); - address group = account1; - uint256 value = 1000; - - address voter2 = account2; - uint256 value2 = 573; - - function setUp() public { - super.setUp(); - - address[] memory members = new address[](1); - members[0] = account9; - validators.setMembers(group, members); - - vm.prank(address(validators)); - election.markGroupEligible(group, address(0), address(0)); - - lockedGold.setTotalLockedGold(value); - validators.setMembers(group, members); - validators.setNumRegisteredValidators(1); - lockedGold.incrementNonvotingAccountBalance(voter, value); - } - - function WhenVoterHasPendingVotes() public { - election.vote(group, value, address(0), address(0)); - } - - function WhenEpochBoundaryHasPassed() public { - WhenVoterHasPendingVotes(); - blockTravel(ph.epochSize() + 1); - election.activate(group); - } - - function test_ShouldDecrementTheAccountsPendingVotesForTheGroup_WhenEpochBoundaryHasPassed() - public - { - WhenEpochBoundaryHasPassed(); - assertEq(election.getPendingVotesForGroupByAccount(group, voter), 0); - } - - function test_ShouldIncrementTheAccountsActiveVotesForTheGroup_WhenEpochBoundaryHasPassed() - public - { - WhenEpochBoundaryHasPassed(); - assertEq(election.getActiveVotesForGroupByAccount(group, voter), value); - } - - function test_ShouldNotModifyTheAccountsTotalVotesForTheGroup_WhenEpochBoundaryHasPassed() - public - { - WhenEpochBoundaryHasPassed(); - assertEq(election.getTotalVotesForGroupByAccount(group, voter), value); - } - - function test_ShouldNotModifyTheAccountsTotalVotes_WhenEpochBoundaryHasPassed() public { - WhenEpochBoundaryHasPassed(); - assertEq(election.getTotalVotesByAccount(voter), value); - } - - function test_ShouldNotModifyTotalVotesForGroup_WhenEpochBoundaryHasPassed() public { - WhenEpochBoundaryHasPassed(); - assertEq(election.getTotalVotesForGroup(group), value); - } - - function test_ShouldNotModifyTotalVotes_WhenEpochBoundaryHasPassed() public { - WhenEpochBoundaryHasPassed(); - assertEq(election.getTotalVotes(), value); - } - - function test_ShouldEmitValidatorGroupVoteActivatedEvent_WhenEpochBoundaryHasPassed() public { - WhenVoterHasPendingVotes(); - blockTravel(ph.epochSize() + 1); - vm.expectEmit(true, true, true, false); - emit ValidatorGroupVoteActivated(voter, group, value, value * 100000000000000000000); - election.activate(group); - } - - function test_Reverts_WhenBlocked() public { - WhenVoterHasPendingVotes(); - blockTravel(ph.epochSize() + 1); - - blocker.mockSetBlocked(true); - vm.expectRevert("Contract is blocked from performing this action"); - election.activate(group); - } - - function WhenAnotherVoterActivatesVotes() public { - WhenEpochBoundaryHasPassed(); - lockedGold.incrementNonvotingAccountBalance(voter2, value2); - vm.prank(voter2); - election.vote(group, value2, address(0), address(0)); - blockTravel(ph.epochSize() + 1); - vm.prank(voter2); - election.activate(group); - } - - function test_ShouldNotModifyTheFirstAccountActiveVotesForTheGroup_WhenAnotherVoterActivatesVotes() - public - { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getActiveVotesForGroupByAccount(group, voter), value); - } - - function test_ShouldNotModifyTheFirstAccountTotalVotesForTheGroup_WhenAnotherVoterActivatesVotes() - public - { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getTotalVotesForGroupByAccount(group, voter), value); - } - - function test_ShouldNotModifyTheFirstAccountTotalVotes_WhenAnotherVoterActivatesVotes() public { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getTotalVotesByAccount(voter), value); - } - - function test_ShouldDecrementTheSecondAccountsPendingVotesFOrTheGroup_WhenAnotherVoterActivatesVotes() - public - { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getPendingVotesForGroupByAccount(group, voter2), 0); - } - - function test_ShouldIncrementTheSecondAccountActiveVotesForTheGroup_WhenAnotherVoterActivatesVotes() - public - { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getActiveVotesForGroupByAccount(group, voter2), value2); - } - - function test_ShouldNotModifyTheSecondsAccountTotalVotesForTheGroup_WhenAnotherVoterActivatesVotes() - public - { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getTotalVotesForGroupByAccount(group, voter2), value2); - } - - function test_ShouldNotMOdifyTheSecondAccountTotalVotes_WhenAnotherVoterActivatesVotes() public { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getTotalVotesByAccount(voter2), value2); - } - - function test_ShouldNotModifyTotalVotesForGroup_WhenAnotherVoterActivatesVotes() public { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getTotalVotesForGroup(group), value + value2); - } - - function test_ShouldNotModifyTotalVotes_WhenAnotherVoterActivatesVotes() public { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getTotalVotes(), value + value2); - } + election.vote(newGroup, originallyNotVotedWithAmount, account4, group); - function test_ShouldRevert_WhenAnEpochBoundaryHadNotPassedSinceThePendingVotesWereMade() public { - WhenVoterHasPendingVotes(); - vm.expectRevert("Pending vote epoch not passed"); - election.activateForAccount(group, voter); + assertEq(election.getTotalVotes(), value + rewardValue); } - function test_ShouldRevert_WhenTheVoterDoesNotHavePendingVotes() public { - vm.expectRevert("Vote value cannot be zero"); - election.activate(group); + function test_ShouldRevert_WhenTheGroupCannotReceiveVotes() public { + lockedGold.setTotalLockedGold(value / 2 - 1); + address[] memory members = new address[](1); + members[0] = account9; + validators.setMembers(group, members); + validators.setNumRegisteredValidators(1); + assertEq(election.getNumVotesReceivable(group), value - 2); + + vm.expectRevert("Group cannot receive votes"); + election.vote(group, value, address(0), address(0)); } } -contract ElectionTest_Activate_L2 is ElectionTest { +contract ElectionTest_Vote_WhenGroupEligible_L2 is + TransitionToL2After, + ElectionTest_Vote_WhenGroupEligible +{} + +contract ElectionTest_Vote_WhenGroupEligible_WhenGroupCanReceiveVotes is ElectionTest { address voter = address(this); address group = account1; uint256 value = 1000; - address voter2 = account2; - uint256 value2 = 573; + uint256 originallyNotVotedWithAmount = 1; + uint256 voterFirstGroupVote = value - maxNumGroupsVotedFor - originallyNotVotedWithAmount; + uint256 rewardValue = 1000000; function setUp() public { super.setUp(); - _whenL2(); + address[] memory members = new address[](1); members[0] = account9; validators.setMembers(group, members); @@ -1323,143 +662,174 @@ contract ElectionTest_Activate_L2 is ElectionTest { election.markGroupEligible(group, address(0), address(0)); lockedGold.setTotalLockedGold(value); - validators.setMembers(group, members); validators.setNumRegisteredValidators(1); - lockedGold.incrementNonvotingAccountBalance(voter, value); } - function WhenVoterHasPendingVotes() public { - election.vote(group, value, address(0), address(0)); + function WhenTheVoterCanVoteForAnAdditionalGroup() public { + lockedGold.incrementNonvotingAccountBalance(voter, value); } - function WhenEpochBoundaryHasPassed() public { - WhenVoterHasPendingVotes(); - epochManager.setCurrentEpochNumber(epochManager.getCurrentEpochNumber() + 1); - election.activate(group); + function WhenTheVoterHasNotAlreadyVotedForThisGroup() public { + WhenTheVoterCanVoteForAnAdditionalGroup(); + election.vote(group, value, address(0), address(0)); } - function test_ShouldDecrementTheAccountsPendingVotesForTheGroup_WhenEpochBoundaryHasPassed() + function test_ShouldAddTheGroupToListOfGroupsTheAccountHasVotedFor_WhenTheVoterHasNotAlreadyVotedForThisGroup() public { - WhenEpochBoundaryHasPassed(); - assertEq(election.getPendingVotesForGroupByAccount(group, voter), 0); + WhenTheVoterHasNotAlreadyVotedForThisGroup(); + address[] memory groupsVotedFor = election.getGroupsVotedForByAccount(voter); + assertEq(groupsVotedFor.length, 1); + assertEq(groupsVotedFor[0], group); } - function test_ShouldIncrementTheAccountsActiveVotesForTheGroup_WhenEpochBoundaryHasPassed() + function test_ShouldIncrementTheAccountsPendingVotesForTheGroup_WhenTheVoterHasNotAlreadyVotedForThisGroup() public { - WhenEpochBoundaryHasPassed(); - assertEq(election.getActiveVotesForGroupByAccount(group, voter), value); + WhenTheVoterHasNotAlreadyVotedForThisGroup(); + assertEq(election.getPendingVotesForGroupByAccount(group, voter), value); } - function test_ShouldNotModifyTheAccountsTotalVotesForTheGroup_WhenEpochBoundaryHasPassed() + function test_ShouldIncrementTheAccountsTotalVotesForTheGroup_WhenTheVoterHasNotAlreadyVotedForThisGroup() public { - WhenEpochBoundaryHasPassed(); + WhenTheVoterHasNotAlreadyVotedForThisGroup(); assertEq(election.getTotalVotesForGroupByAccount(group, voter), value); } - function test_ShouldNotModifyTheAccountsTotalVotes_WhenEpochBoundaryHasPassed() public { - WhenEpochBoundaryHasPassed(); + function test_ShouldIncrementTheACcountsTotalVotes_WhenTheVoterHasNotAlreadyVotedForThisGroup() + public + { + WhenTheVoterHasNotAlreadyVotedForThisGroup(); assertEq(election.getTotalVotesByAccount(voter), value); } - function test_ShouldNotModifyTotalVotesForGroup_WhenEpochBoundaryHasPassed() public { - WhenEpochBoundaryHasPassed(); + function test_ShouldIncrementTheTotalVotesForTheGroup_WhenTheVoterHasNotAlreadyVotedForThisGroup() + public + { + WhenTheVoterHasNotAlreadyVotedForThisGroup(); assertEq(election.getTotalVotesForGroup(group), value); } - function test_ShouldNotModifyTotalVotes_WhenEpochBoundaryHasPassed() public { - WhenEpochBoundaryHasPassed(); + function test_ShouldIncrementTheTotalVotes_WhenTheVoterHasNotAlreadyVotedForThisGroup() public { + WhenTheVoterHasNotAlreadyVotedForThisGroup(); assertEq(election.getTotalVotes(), value); } - function test_ShouldEmitValidatorGroupVoteActivatedEvent_WhenEpochBoundaryHasPassed() public { - WhenVoterHasPendingVotes(); - epochManager.setCurrentEpochNumber(epochManager.getCurrentEpochNumber() + 1); - vm.expectEmit(true, true, true, false); - emit ValidatorGroupVoteActivated(voter, group, value, value * 100000000000000000000); - election.activate(group); - } - - function WhenAnotherVoterActivatesVotes() public { - WhenEpochBoundaryHasPassed(); - lockedGold.incrementNonvotingAccountBalance(voter2, value2); - vm.prank(voter2); - election.vote(group, value2, address(0), address(0)); - epochManager.setCurrentEpochNumber(epochManager.getCurrentEpochNumber() + 1); - vm.prank(voter2); - election.activate(group); + function test_ShouldDecrementTheAccountsNonVotingLockedGoldBalance_WhenTheVoterHasNotAlreadyVotedForThisGroup() + public + { + WhenTheVoterHasNotAlreadyVotedForThisGroup(); + assertEq(lockedGold.nonvotingAccountBalance(voter), 0); } - function test_ShouldNotModifyTheFirstAccountActiveVotesForTheGroup_WhenAnotherVoterActivatesVotes() + function test_ShouldEmitTheValidatorGroupVoteCastEvent_WhenTheVoterHasNotAlreadyVotedForThisGroup() public { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getActiveVotesForGroupByAccount(group, voter), value); + WhenTheVoterCanVoteForAnAdditionalGroup(); + vm.expectEmit(true, false, false, false); + emit ValidatorGroupVoteCast(voter, group, value); + election.vote(group, value, address(0), address(0)); } - function test_ShouldNotModifyTheFirstAccountTotalVotesForTheGroup_WhenAnotherVoterActivatesVotes() + function WhenTheVoterHasAlreadyVotedForThisGroup() public { + WhenTheVoterHasNotAlreadyVotedForThisGroup(); + lockedGold.incrementNonvotingAccountBalance(voter, value); + election.vote(group, value, address(0), address(0)); + } + + function test_ShouldNotChangeTheListOfGroupsTheAccountVotedFor_WhenTheVoterHasAlreadyVotedForThisGroup() public { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getTotalVotesForGroupByAccount(group, voter), value); + WhenTheVoterHasAlreadyVotedForThisGroup(); + address[] memory groupsVotedFor = election.getGroupsVotedForByAccount(voter); + assertEq(groupsVotedFor.length, 1); + assertEq(groupsVotedFor[0], group); } - function test_ShouldNotModifyTheFirstAccountTotalVotes_WhenAnotherVoterActivatesVotes() public { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getTotalVotesByAccount(voter), value); + function test_ShouldIncreaseAccountsPendingVotesForTheGroup_WhenTheVoterHasAlreadyVotedForThisGroup() + public + { + WhenTheVoterHasAlreadyVotedForThisGroup(); + assertEq(election.getPendingVotesForGroupByAccount(group, voter), value * 2); } - function test_ShouldDecrementTheSecondAccountsPendingVotesFOrTheGroup_WhenAnotherVoterActivatesVotes() + function test_ShouldIncrementAccountTotalVotesForTheGroup_WhenTheVoterHasAlreadyVotedForThisGroup() public { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getPendingVotesForGroupByAccount(group, voter2), 0); + WhenTheVoterHasAlreadyVotedForThisGroup(); + assertEq(election.getTotalVotesForGroupByAccount(group, voter), value * 2); } - function test_ShouldIncrementTheSecondAccountActiveVotesForTheGroup_WhenAnotherVoterActivatesVotes() + function test_ShouldIncrementTheAccountsTotalVotes_WhenTheVoterHasAlreadyVotedForThisGroup() public { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getActiveVotesForGroupByAccount(group, voter2), value2); + WhenTheVoterHasAlreadyVotedForThisGroup(); + assertEq(election.getTotalVotesByAccount(voter), value * 2); } - function test_ShouldNotModifyTheSecondsAccountTotalVotesForTheGroup_WhenAnotherVoterActivatesVotes() + function test_ShouldIncrementTotalVotesForTheGroup_WhenTheVoterHasAlreadyVotedForThisGroup() public { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getTotalVotesForGroupByAccount(group, voter2), value2); + WhenTheVoterHasAlreadyVotedForThisGroup(); + assertEq(election.getTotalVotesForGroup(group), value * 2); } - function test_ShouldNotMOdifyTheSecondAccountTotalVotes_WhenAnotherVoterActivatesVotes() public { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getTotalVotesByAccount(voter2), value2); + function test_ShouldIncrementTotalVotes_WhenTheVoterHasAlreadyVotedForThisGroup() public { + WhenTheVoterHasAlreadyVotedForThisGroup(); + assertEq(election.getTotalVotes(), value * 2); } - function test_ShouldNotModifyTotalVotesForGroup_WhenAnotherVoterActivatesVotes() public { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getTotalVotesForGroup(group), value + value2); + function test_ShouldDecrementAccountNonVotingBalance_WhenTheVoterHasAlreadyVotedForThisGroup() + public + { + WhenTheVoterHasAlreadyVotedForThisGroup(); + assertEq(lockedGold.nonvotingAccountBalance(voter), 0); } - function test_ShouldNotModifyTotalVotes_WhenAnotherVoterActivatesVotes() public { - WhenAnotherVoterActivatesVotes(); - assertEq(election.getTotalVotes(), value + value2); + function test_ShouldEmitValidatorGroupVoteCast_WhenTheVoterHasAlreadyVotedForThisGroup() public { + WhenTheVoterHasNotAlreadyVotedForThisGroup(); + lockedGold.incrementNonvotingAccountBalance(voter, value); + vm.expectEmit(true, true, false, false); + emit ValidatorGroupVoteCast(voter, group, value); + election.vote(group, value, address(0), address(0)); } +} - function test_ShouldRevert_WhenAnEpochBoundaryHadNotPassedSinceThePendingVotesWereMade() public { - WhenVoterHasPendingVotes(); - vm.expectRevert("Pending vote epoch not passed"); - election.activateForAccount(group, voter); +contract ElectionTest_Vote_WhenGroupEligible_WhenGroupCanReceiveVotes_L2 is + ElectionTest_Vote_WhenGroupEligible_WhenGroupCanReceiveVotes, + TransitionToL2After +{} + +contract ElectionTest_Vote_GroupNotEligible is ElectionTest { + address voter = address(this); + address group = account1; + uint256 value = 1000; + + uint256 originallyNotVotedWithAmount = 1; + uint256 voterFirstGroupVote = value - maxNumGroupsVotedFor - originallyNotVotedWithAmount; + uint256 rewardValue = 1000000; + + function setUp() public { + super.setUp(); + + address[] memory members = new address[](1); + members[0] = account9; + validators.setMembers(group, members); } - function test_ShouldRevert_WhenTheVoterDoesNotHavePendingVotes() public { - vm.expectRevert("Vote value cannot be zero"); - election.activate(group); + function test_ShouldRevert_WhenTheGroupIsNotEligible() public { + vm.expectRevert("Group not eligible"); + election.vote(group, value, address(0), address(0)); } } -contract ElectionTest_ActivateForAccount is ElectionTest { +contract ElectionTest_Vote_GroupNotEligible_L2 is + ElectionTest_Vote_GroupNotEligible, + TransitionToL2After +{} + +contract ElectionTest_Activate is ElectionTest { address voter = address(this); address group = account1; uint256 value = 1000; @@ -1489,8 +859,8 @@ contract ElectionTest_ActivateForAccount is ElectionTest { function WhenEpochBoundaryHasPassed() public { WhenVoterHasPendingVotes(); - blockTravel(ph.epochSize() + 1); - election.activateForAccount(group, voter); + travelNEpoch(1); + election.activate(group); } function test_ShouldDecrementTheAccountsPendingVotesForTheGroup_WhenEpochBoundaryHasPassed() @@ -1531,19 +901,29 @@ contract ElectionTest_ActivateForAccount is ElectionTest { function test_ShouldEmitValidatorGroupVoteActivatedEvent_WhenEpochBoundaryHasPassed() public { WhenVoterHasPendingVotes(); - blockTravel(ph.epochSize() + 1); + travelNEpoch(1); vm.expectEmit(true, true, true, false); emit ValidatorGroupVoteActivated(voter, group, value, value * 100000000000000000000); election.activate(group); } + function test_Reverts_WhenBlocked() public { + WhenVoterHasPendingVotes(); + travelNEpoch(1); + + blocker.mockSetBlocked(true); + vm.expectRevert("Contract is blocked from performing this action"); + election.activate(group); + } + function WhenAnotherVoterActivatesVotes() public { WhenEpochBoundaryHasPassed(); lockedGold.incrementNonvotingAccountBalance(voter2, value2); vm.prank(voter2); election.vote(group, value2, address(0), address(0)); - blockTravel(ph.epochSize() + 1); - election.activateForAccount(group, voter2); + travelNEpoch(1); + vm.prank(voter2); + election.activate(group); } function test_ShouldNotModifyTheFirstAccountActiveVotesForTheGroup_WhenAnotherVoterActivatesVotes() @@ -1601,7 +981,7 @@ contract ElectionTest_ActivateForAccount is ElectionTest { assertEq(election.getTotalVotes(), value + value2); } - function test_ShouldRevert_WhenEpochBoundaryHasNotPassedSinceThePendingVotesWereMade() public { + function test_ShouldRevert_WhenAnEpochBoundaryHadNotPassedSinceThePendingVotesWereMade() public { WhenVoterHasPendingVotes(); vm.expectRevert("Pending vote epoch not passed"); election.activateForAccount(group, voter); @@ -1609,11 +989,13 @@ contract ElectionTest_ActivateForAccount is ElectionTest { function test_ShouldRevert_WhenTheVoterDoesNotHavePendingVotes() public { vm.expectRevert("Vote value cannot be zero"); - election.activateForAccount(group, voter); + election.activate(group); } } -contract ElectionTest_ActivateForAccount_L2 is ElectionTest { +contract ElectionTest_Activate_L2 is TransitionToL2After, ElectionTest_Activate {} + +contract ElectionTest_ActivateForAccount is ElectionTest { address voter = address(this); address group = account1; uint256 value = 1000; @@ -1623,7 +1005,7 @@ contract ElectionTest_ActivateForAccount_L2 is ElectionTest { function setUp() public { super.setUp(); - _whenL2(); + address[] memory members = new address[](1); members[0] = account9; validators.setMembers(group, members); @@ -1643,7 +1025,7 @@ contract ElectionTest_ActivateForAccount_L2 is ElectionTest { function WhenEpochBoundaryHasPassed() public { WhenVoterHasPendingVotes(); - epochManager.setCurrentEpochNumber(epochManager.getCurrentEpochNumber() + 1); + travelNEpoch(1); election.activateForAccount(group, voter); } @@ -1685,7 +1067,7 @@ contract ElectionTest_ActivateForAccount_L2 is ElectionTest { function test_ShouldEmitValidatorGroupVoteActivatedEvent_WhenEpochBoundaryHasPassed() public { WhenVoterHasPendingVotes(); - epochManager.setCurrentEpochNumber(epochManager.getCurrentEpochNumber() + 1); + travelNEpoch(1); vm.expectEmit(true, true, true, false); emit ValidatorGroupVoteActivated(voter, group, value, value * 100000000000000000000); election.activate(group); @@ -1696,7 +1078,7 @@ contract ElectionTest_ActivateForAccount_L2 is ElectionTest { lockedGold.incrementNonvotingAccountBalance(voter2, value2); vm.prank(voter2); election.vote(group, value2, address(0), address(0)); - epochManager.setCurrentEpochNumber(epochManager.getCurrentEpochNumber() + 1); + travelNEpoch(1); election.activateForAccount(group, voter2); } @@ -1767,6 +1149,11 @@ contract ElectionTest_ActivateForAccount_L2 is ElectionTest { } } +contract ElectionTest_ActivateForAccount_L2 is + TransitionToL2After, + ElectionTest_ActivateForAccount +{} + contract ElectionTest_RevokePending is ElectionTest { address voter = address(this); address group = account1; @@ -1923,6 +1310,8 @@ contract ElectionTest_RevokePending is ElectionTest { } } +contract ElectionTest_RevokePending_L2 is ElectionTest_RevokePending, TransitionToL2After {} + contract ElectionTest_RevokeActive is ElectionTest { address voter0 = address(this); address voter1 = account1; @@ -1964,7 +1353,7 @@ contract ElectionTest_RevokeActive is ElectionTest { // Gives 1000 units to voter 0 election.vote(group, voteValue0, address(0), address(0)); assertConsistentSums(); - blockTravel(ph.epochSize() + 1); + travelNEpoch(1); election.activate(group); assertConsistentSums(); @@ -1976,7 +1365,7 @@ contract ElectionTest_RevokeActive is ElectionTest { vm.prank(voter1); election.vote(group, voteValue1, address(0), address(0)); assertConsistentSums(); - blockTravel(ph.epochSize() + 1); + travelNEpoch(1); vm.prank(voter1); election.activate(group); assertConsistentSums(); @@ -2201,6 +1590,8 @@ contract ElectionTest_RevokeActive is ElectionTest { } } +contract ElectionTest_RevokeActive_L2 is TransitionToL2After, ElectionTest_RevokeActive {} + contract ElectionTest_ElectValidatorsAbstract is ElectionTest { struct MemberWithVotes { address member; @@ -2622,7 +2013,7 @@ contract ElectionTest_GetGroupEpochRewards is ElectionTest { } function WhenOneGroupHasActiveVotes() public { - blockTravel(ph.epochSize() + 1); + travelNEpoch(1); election.activate(group1); } @@ -2661,7 +2052,7 @@ contract ElectionTest_GetGroupEpochRewards is ElectionTest { } function WhenTwoGroupsHaveActiveVotes() public { - blockTravel(ph.epochSize() + 1); + travelNEpoch(1); election.activate(group1); election.activate(group2); } @@ -2699,6 +2090,14 @@ contract ElectionTest_GetGroupEpochRewards is ElectionTest { uptimes[0] = FIXED1; assertEq(election.getGroupEpochRewards(group1, totalRewardValue, uptimes), 0); } + + function test_Reverts_WhenL2() public { + _whenL2(); + uint256[] memory uptimes = new uint256[](1); + uptimes[0] = FIXED1; + vm.expectRevert("This method is no longer supported in L2."); + election.getGroupEpochRewards(group1, totalRewardValue, uptimes); + } } contract ElectionTest_DistributeEpochRewards is ElectionTest { @@ -2734,7 +2133,7 @@ contract ElectionTest_DistributeEpochRewards is ElectionTest { lockedGold.incrementNonvotingAccountBalance(voter, voteValue); election.vote(group, voteValue, address(0), address(0)); - blockTravel(ph.epochSize() + 1); + travelNEpoch(1); election.activate(group); } @@ -2779,7 +2178,7 @@ contract ElectionTest_DistributeEpochRewards is ElectionTest { // Split voter2's vote between the two groups. election.vote(group, voteValue2 / 2, group2, address(0)); election.vote(group2, voteValue2 / 2, address(0), group); - blockTravel(ph.epochSize() + 1); + travelNEpoch(1); election.activate(group); election.activate(group2); vm.stopPrank(); @@ -2859,6 +2258,11 @@ contract ElectionTest_DistributeEpochRewards is ElectionTest { } } +contract ElectionTest_DistributeEpochRewards_L2 is + TransitionToL2After, + ElectionTest_DistributeEpochRewards +{} + contract ElectionTest_ForceDecrementVotes is ElectionTest { address voter = address(this); address group = account2; @@ -2942,7 +2346,7 @@ contract ElectionTest_ForceDecrementVotes is ElectionTest { function WhenAccountHasOnlyActiveVotes() public { WhenAccountHasVotedForOneGroup(); - blockTravel(ph.epochSize() + 1); + travelNEpoch(1); election.activate(group); vm.prank(account2); @@ -3073,9 +2477,9 @@ contract ElectionTest_ForceDecrementVotes is ElectionTest { public { WhenAccountHasVotedForMoreThanOneGroupInequally(); - blockTravel(ph.epochSize() + 1); + travelNEpoch(1); election.activate(group); - blockTravel(ph.epochSize() + 1); + travelNEpoch(1); election.activate(group2); election.vote(group2, value2 / 2, group, address(0)); @@ -3177,9 +2581,9 @@ contract ElectionTest_ForceDecrementVotes is ElectionTest { group1RemainingActiveVotes = value - slashedValue; election.vote(group, value / 2, group2, address(0)); - blockTravel(ph.epochSize() + 1); + travelNEpoch(1); election.activate(group); - blockTravel(ph.epochSize() + 1); + travelNEpoch(1); election.activate(group2); (initialOrdering, ) = election.getTotalVotesForEligibleValidatorGroups(); @@ -3284,6 +2688,11 @@ contract ElectionTest_ForceDecrementVotes is ElectionTest { } } +contract ElectionTest_ForceDecrementVotes_L2 is + TransitionToL2After, + ElectionTest_ForceDecrementVotes +{} + contract ElectionTest_ConsistencyChecks is ElectionTest { struct AccountStruct { address account; @@ -3410,7 +2819,12 @@ contract ElectionTest_ConsistencyChecks is ElectionTest { makeRandomAction(_accounts[j], j); checkVoterInvariants(_accounts[j], 0); checkGroupInvariants(0); - vm.roll((i + 1) * ph.epochSize() + (i + 1)); + + if (isL2()) { + epochManager.setCurrentEpochNumber(i + 1); + } else { + vm.roll((i + 1) * ph.epochSize() + (i + 1)); + } } } revokeAllAndCheckInvariants(0); @@ -3446,7 +2860,11 @@ contract ElectionTest_ConsistencyChecks is ElectionTest { } distributeEpochRewards(i); - vm.roll((i + 1) * ph.epochSize() + (i + 1)); + if (isL2()) { + epochManager.setCurrentEpochNumber(i + 1); + } else { + vm.roll((i + 1) * ph.epochSize() + (i + 1)); + } for (uint256 j = 0; j < _accounts.length; j++) { checkVoterInvariants(_accounts[j], 100); @@ -3503,6 +2921,8 @@ contract ElectionTest_ConsistencyChecks is ElectionTest { } } +contract ElectionTest_ConsistencyChecks_L2 is TransitionToL2After, ElectionTest_ConsistencyChecks {} + contract ElectionTest_HasActivatablePendingVotes is ElectionTest { address voter = address(this); address group = account1; @@ -3523,7 +2943,7 @@ contract ElectionTest_HasActivatablePendingVotes is ElectionTest { lockedGold.incrementNonvotingAccountBalance(voter, value); election.vote(group, value, address(0), address(0)); - blockTravel(ph.epochSize() + 1); + travelNEpoch(1); } function test_ReturnsTrue_WhenUserHasVoted() public { assertTrue(election.hasActivatablePendingVotes(voter, group)); @@ -3535,3 +2955,8 @@ contract ElectionTest_HasActivatablePendingVotes is ElectionTest { assertTrue(election.hasActivatablePendingVotes(voter, group)); } } + +contract ElectionTest_HasActivatablePendingVotes_L2 is + TransitionToL2After, + ElectionTest_HasActivatablePendingVotes +{}