A contract that has a specific progression (e.g., setup, running, paused) may mistakenly allow state-changing functions to execute out of order, leading to potential exploitation
Game
startGame and completeGame are intended to control the game’s progress through its stages. However, due to an issue with the conditional logic in completeGame, the game can behave strangely. Can you spot the missing check?
// SPDX-License-Identifier: MIT// Open me in VSCode and really think before opening the hints!// Add @audit tags wherever suspicious// Go to the solidity docs to complete missing knowledge of what's happening here// Solve by drafting a fix!pragmasolidity ^0.8.0;contract StateTransitionGame {enumGameState { NotStarted, InProgress, Completed } GameState public state;constructor() { state = GameState.NotStarted; }functionstartGame() public {require(state == GameState.NotStarted,"Game already started or completed"); state = GameState.InProgress; }functioncompleteGame() public { state = GameState.Completed; }}
Look at the condition in completeGame.
Does it ensure that the game can only transition to Completed if it is currently InProgress?
Consider how adding specific checks for each state transition might make the progression more controlled and prevent unexpected transitions.
functioncompleteGame() public {require(state == GameState.InProgress,"Game must be in progress to complete"); // Fix: Check for InProgress state state = GameState.Completed;}