The FjordStaking::calculateReward and FjordStaking::_checkEpochRollover calculates rewards for users based on their staked tokens. Both functions use PRECISION_18 to maintain precision during calculation. However, if the staked amount of tokens is larger than PRECISION_18, then precision is lost, leading to stakers not getting their expected amount of rewards and tokens to be permanently locked in the contract.
Relevant Links
https://github.com/Cyfrin/2024-08-fjord/blob/main/src/FjordStaking.sol#L704
https://github.com/Cyfrin/2024-08-fjord/blob/main/src/FjordStaking.sol#L781
The FjordStaking::calculateReward and FjordStaking::_checkEpochRollover both use PRECISION_18 to maintain precision during the calculation of rewards for users based on their staked tokens. However if the total amount of staked tokens is much higher than PRECISION_18, it will cause a precision loss.
Stakers get slightly lower rewards due to precision loss
Small amount of tokens being permanently stuck inside the contract with no way of retrieving or claiming them and the amount of tokens stuck increasing slowly, possibly becoming a large value as more people stake overtime.
This causes a disruption in the functionality of calculating rewards for stakers.
The impact is demonstrated with the following test, which can be executed with forge test --mt testStakerRewardsAreDistributedCorrectly.
This test proves that stakers get slightly lower rewards that their expected amount and it also shows that tokens are being locked inside the contract.
Manual Review, Foundry
Increase the value of PRECISION_18 to 1e28 or higher to ensure that the new precision variable is always larger than any amount leading to accurate distribution. Also rename PRECISION_18 to reflect the updated value (e,g, PRECISION_28).
After adding this change, you can rerun the poc test provided earlier to verify that the rewards are being distributed correctly and stakers get their expected rewards.
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.