FV-SOL-5-C3 Improper State Transitions

TLDR

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!
pragma solidity ^0.8.0;

contract StateTransitionGame {
    enum GameState { NotStarted, InProgress, Completed }
    GameState public state;

    constructor() {
        state = GameState.NotStarted;
    }

    function startGame() public {
        require(state == GameState.NotStarted, "Game already started or completed");
        state = GameState.InProgress;
    }

    function completeGame() public {
        state = GameState.Completed;
    }
}

Last updated