Summary
rewardPerVoter is wrongly computed
Vulnerability Details
The reward per voter is calculated as if all voters received a reward, whereas only those who voted for are entitled to the reward.
File: src/VotingBooth.sol
192: uint256 rewardPerVoter = totalRewards / totalVotes;
Proof of Concept
Place the code for the following test function in test/VotingBoothTest.t.sol.
function test_WrongRewardPerVoter() public {
uint256 totalRewards = address(booth).balance;
uint256 votersFor;
uint256 votersAgainst;
vm.prank(address(0x1));
booth.vote(true);
votersFor += 1;
vm.prank(address(0x2));
booth.vote(true);
votersFor += 1;
vm.prank(address(0x3));
booth.vote(false);
votersAgainst += 1;
vm.prank(address(0x4));
booth.vote(true);
votersFor += 1;
uint256 totalVotes = votersFor + votersAgainst;
uint256 originalRewardPerVoter = totalRewards / votersFor;
uint256 currentRewardPerVoter = totalRewards / totalVotes;
assertGe(originalRewardPerVoter, currentRewardPerVoter);
}
In the terminal, run the following command:
Impact
For voters receive less than they should.
Tools Used
Manual review
Recommendations
Make following changes
- uint256 rewardPerVoter = totalRewards / totalVotes;
+ uint256 rewardPerVoter = totalRewards / totalVotesFor;