The VootingBooth::_distributeRewards function in the voting smart contract has an issue with the calculation of rewards, resulting in incorrect fund distribution to voters and potential funds getting locked in the contract indefinitely.
The problematic code lies within the VootingBooth::_distributeRewards function, specifically in the calculation of rewardPerVoter. The current calculation divides the totalRewards by totalVotes, which includes all votes (both 'for' and 'against'). This miscalculation leads to incorrect reward distribution to 'for' voters.
Follow the steps below:
Import StdInvariant from forge-std/StdInvariant.sol into VotingBoothTest.t.sol contract for invariant testing.
Add the StdInvariant to the inheritance. Ensure that StdInvariant is the first in the inheritance chain to avoid Linearization of inheritance graph impossible error.
Introduce the following invariant test in the contract.
The assertion will fail, demonstrating incomplete fund distribution to voters.
The incorrect calculation in rewardPerVoter impacts the allocation of funds to voters who voted 'for'. Consequently, the remaining funds might not be correctly distributed, potentially resulting in funds getting locked within the contract, as the issue affects the one-time use nature of the contract.
Foundry
Revise the denominator of rewardPerVoter to totalVotesFor:
Handling Dust:
Adjust the last voter's reward to avoid leftover funds
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.