Last Man Standing

First Flight #45
Beginner FriendlyFoundrySolidity
100 EXP
View results
Submission Details
Severity: low
Valid

Grace Period expiration not the same in getRemainingTime and declareWinner

[L] Grace Period expiration not the same in GetRemainingTime and DeclareWinner

Description:
The function Game::getRemainingTime has a checking like below :

uint256 endTime = lastClaimTime + gracePeriod;
if (block.timestamp >= endTime) {
return 0; // Grace period has expired
}

and the function Game::declareWinner has a checking as following :

require(
block.timestamp > lastClaimTime + gracePeriod,
"Game: Grace period has not expired yet."
);

The condition in declareWinner is strict but the one in getRemainingTime is not.
Impact:
This little change can cause some problem if the currentKing want to perfectly time the end of the game with the function Game::getRemainingTime, before someone try to claim the throne again.

Proof of Concept:

Add the following test to Game.t.sol

function test_game_endTime() public {
// player1 become the first king
vm.prank(player1);
game.claimThrone{value: INITIAL_CLAIM_FEE}();
assertEq(player1, game.currentKing());
// player2 overthrow player1 and become king
vm.startPrank(player2);
game.claimThrone{value: game.claimFee()}();
assertEq(player2, game.currentKing());
// advance the game to the grace_period time
vm.warp(game.lastClaimTime() + GRACE_PERIOD);
assertEq(game.getRemainingTime(), 0);
// Grace has not expired yet even if getRemainingTime is 0.
vm.expectRevert("Game: Grace period has not expired yet.");
game.declareWinner();
vm.stopPrank();
}

Recommended Mitigation:

Change the condition in Game::getRemainingTime :

- if (block.timestamp >= endTime)
+ if (block.timestamp > endTime)
Updates

Appeal created

inallhonesty Lead Judge about 1 month ago
Submission Judgement Published
Validated
Assigned finding tags:

Discrepancy between getRemainingTime and declareWinner, one includes equality the other one doesn't

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.