fix(VotingContract): voting rooms are considered active until evaluated (#89)

This commit fixes a bug in the `VotingContract` where any voting room
marked as `finalized` is not considered to be an active voting room,
even though, evaluation is still ongoing.

The fix adds a check that a voting room has to be either not marked as
finalized, or, if it is, it should not be evaluated, to be considered
an active voting room.

It also adds a test that ensures the fix is working as expected.
This commit is contained in:
r4bbit 2023-10-30 12:24:08 +01:00 committed by GitHub
parent ed5e16e647
commit 10cfa07bd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 3859 additions and 3730 deletions

View File

@ -157,7 +157,7 @@ contract VotingContract is Ownable2Step {
uint256[] memory returnVotingRooms = new uint256[](votingRooms.length);
uint256 count = 0;
for (uint256 i = 0; i < votingRooms.length; i++) {
if (!votingRooms[i].finalized) {
if (!votingRooms[i].finalized || !votingRooms[i].evaluated) {
returnVotingRooms[count] = votingRooms[i].roomNumber;
count++;
}
@ -284,7 +284,6 @@ contract VotingContract is Ownable2Step {
votingRoom.finalized = true;
votingRoom.endAt = block.timestamp;
votingRoom.endBlock = block.number;
activeRoomIDByCommunityID[votingRoom.community] = 0;
// resetting evaluation state as we're not entering finalization
// phase in which all votes have to be reevaluated
@ -298,6 +297,7 @@ contract VotingContract is Ownable2Step {
bool passed = _evaluateVotes(votingRoom, limit);
if (votingRoom.evaluated) {
activeRoomIDByCommunityID[votingRoom.community] = 0;
if (passed) {
_populateDirectory(votingRoom);
}

File diff suppressed because one or more lines are too long

View File

@ -187,6 +187,49 @@ contract GetActiveVotingRoomsTest is VotingContractTest {
assertEq(votingRooms.length, 1);
assertEq(votingRooms[0], 1);
}
function test_GetActiveVotingRooms_WhenFinalizationHasStarted() public {
// ensure there are not active voting rooms
uint256[] memory votingRooms = votingContract.getActiveVotingRooms();
assertEq(votingRooms.length, 0);
vm.prank(bob);
votingContract.initializeVotingRoom(VotingContract.VoteType.FOR, communityID1, 100);
// create another vote so we have more than one to evaluate
VotingContract.SignedVote[] memory votes = new VotingContract.SignedVote[](1);
votes[0] = _createSignedVote(alicesKey, alice, 1, VotingContract.VoteType.AGAINST, 200, block.timestamp);
// ensure users have funds
_ensureVoteTokens(alice, 1000);
votingContract.castVotes(votes);
// fast forward such that voting time has passed
skip(votingWithVerificationLength + 1);
// finalize voting room with a `limit` of `1`, meaning, 1 vote left to evaluate
votingContract.finalizeVotingRoom(1, 1);
// there should be one active voting room
votingRooms = votingContract.getActiveVotingRooms();
assertEq(votingRooms.length, 1);
assertEq(votingRooms[0], 1);
// current active voting room should be marked as finalized but not evaluated yet
VotingContract.VotingRoom memory votingRoom = votingContract.getActiveVotingRoom(communityID1);
assertEq(votingRoom.community, communityID1);
assertEq(votingRoom.finalized, true);
assertEq(votingRoom.evaluated, false);
// finalize voting room once more with a `limit` of `1`, this will close the voting room
votingContract.finalizeVotingRoom(1, 1);
// there should be no active voting room anymore
votingRooms = votingContract.getActiveVotingRooms();
assertEq(votingRooms.length, 0);
// sanity check, community should not be in directy due to vote result
assertFalse(directoryContract.isCommunityInDirectory(communityID1));
}
}
contract ListRoomVotersTest is VotingContractTest {