pragma solidity ^0.8.20;
import {Test, console2} from "forge-std/Test.sol";
import {Game} from "../src/Game.sol";
contract RefinedCriticalInvertedLogicPoC is Test {
Game public game;
address public deployer;
address public alice;
address public bob;
address public attacker;
uint256 public constant INITIAL_CLAIM_FEE = 0.1 ether;
uint256 public constant GRACE_PERIOD = 1 days;
uint256 public constant FEE_INCREASE_PERCENTAGE = 10;
uint256 public constant PLATFORM_FEE_PERCENTAGE = 5;
function setUp() public {
deployer = makeAddr("deployer");
alice = makeAddr("alice");
bob = makeAddr("bob");
attacker = makeAddr("attacker");
vm.deal(deployer, 10 ether);
vm.deal(alice, 5 ether);
vm.deal(bob, 5 ether);
vm.deal(attacker, 5 ether);
vm.prank(deployer);
game = new Game(
INITIAL_CLAIM_FEE,
GRACE_PERIOD,
FEE_INCREASE_PERCENTAGE,
PLATFORM_FEE_PERCENTAGE
);
console2.log("=== CRITICAL-001 REFINED PoC: Complete Game DoS ===");
console2.log("Vulnerability: Game is broken from deployment");
console2.log("Impact: NOBODY can EVER claim the throne");
console2.log("");
}
* @notice Proves the game is completely broken from deployment
* @dev Shows that even the first claim attempt fails
*/
function test_CRITICAL_GameCompletelyBroken() public {
console2.log("=== DEMONSTRATING COMPLETE GAME FAILURE ===");
console2.log("Initial State Analysis:");
console2.log("currentKing:", game.currentKing());
console2.log("claimFee:", game.claimFee());
console2.log("gameEnded:", game.gameEnded());
assertEq(game.currentKing(), address(0), "currentKing should be address(0)");
console2.log("");
console2.log("ATTEMPT 1: Alice tries first claim");
console2.log("Expected: Should work (this is the INTENDED first claim)");
console2.log("Actual: FAILS due to inverted logic");
vm.prank(alice);
vm.expectRevert("Game: You are already the king. No need to re-claim.");
game.claimThrone{value: INITIAL_CLAIM_FEE}();
console2.log("Result: FIRST CLAIM FAILED");
console2.log("Reason: msg.sender (alice) != currentKing (address(0))");
console2.log("");
assertEq(game.currentKing(), address(0), "King should still be address(0)");
console2.log("ATTEMPT 2: Bob tries to claim");
vm.prank(bob);
vm.expectRevert("Game: You are already the king. No need to re-claim.");
game.claimThrone{value: INITIAL_CLAIM_FEE}();
console2.log("Result: SECOND CLAIM FAILED");
console2.log("");
console2.log("ATTEMPT 3: Even address(0) cannot claim (if it could)");
console2.log("Logic analysis: require(address(0) == address(0)) would pass");
console2.log("But address(0) cannot send transactions");
console2.log("");
console2.log("=== FINAL STATE ===");
console2.log("currentKing remains:", game.currentKing());
console2.log("Total successful claims:", game.totalClaims());
console2.log("Game functionality: COMPLETELY BROKEN");
}
* @notice Demonstrates the transaction revert behavior
* @dev Shows how the contract rejects all claim attempts
*/
function test_CRITICAL_TransactionReverts() public {
console2.log("=== TRANSACTION REVERT DEMONSTRATION ===");
console2.log("All claimThrone() attempts revert immediately");
console2.log("");
uint256 contractBalanceBefore = address(game).balance;
console2.log("Contract balance before any attempts:", contractBalanceBefore);
console2.log("");
console2.log("Alice attempting claim...");
vm.prank(alice);
vm.expectRevert("Game: You are already the king. No need to re-claim.");
game.claimThrone{value: INITIAL_CLAIM_FEE}();
console2.log("Alice's transaction REVERTED as expected");
console2.log("Bob attempting claim...");
vm.prank(bob);
vm.expectRevert("Game: You are already the king. No need to re-claim.");
game.claimThrone{value: INITIAL_CLAIM_FEE}();
console2.log("Bob's transaction REVERTED as expected");
uint256 contractBalanceAfter = address(game).balance;
console2.log("");
console2.log("Contract balance after attempts:", contractBalanceAfter);
assertEq(contractBalanceAfter, contractBalanceBefore, "Contract balance unchanged due to reverts");
console2.log("");
console2.log("IMPACT ANALYSIS:");
console2.log("- All user interactions fail immediately");
console2.log("- No ETH gets locked (transactions revert)");
console2.log("- Users waste gas on failed transactions");
console2.log("- Game appears broken to all users");
console2.log("- Reputation damage: Users think contract is scam");
}
* @notice Shows the exact logical error and its fix
* @dev Provides clear before/after comparison
*/
function test_CRITICAL_LogicalErrorAnalysis() public {
console2.log("=== LOGICAL ERROR BREAKDOWN ===");
console2.log("");
console2.log("CURRENT BROKEN CODE (Line 132):");
console2.log("require(msg.sender == currentKing, \"Game: You are already the king.\");");
console2.log("");
console2.log("EXECUTION ANALYSIS:");
console2.log("1. currentKing = address(0) (initial state)");
console2.log("2. Alice calls claimThrone(), msg.sender = alice's address");
console2.log("3. Condition: alice's address == address(0) -> FALSE");
console2.log("4. require(FALSE) -> REVERT");
console2.log("");
console2.log("INTENDED LOGIC:");
console2.log("- Prevent current king from claiming again");
console2.log("- Allow NEW players to claim throne");
console2.log("");
console2.log("CORRECT FIX:");
console2.log("require(msg.sender != currentKing, \"Game: You are already the king.\");");
console2.log("");
console2.log("FIXED EXECUTION FLOW:");
console2.log("1. currentKing = address(0) (initial state)");
console2.log("2. Alice calls claimThrone(), msg.sender = alice's address");
console2.log("3. Condition: alice's address != address(0) -> TRUE");
console2.log("4. require(TRUE) -> CONTINUE EXECUTION");
console2.log("5. Alice becomes the new king");
console2.log("");
assertEq(game.currentKing(), address(0), "King is address(0)");
vm.prank(alice);
vm.expectRevert("Game: You are already the king. No need to re-claim.");
game.claimThrone{value: INITIAL_CLAIM_FEE}();
console2.log("CONFIRMED: Current logic prevents ANY player from claiming");
}
* @notice Demonstrates this affects ALL possible users
* @dev Shows systematic failure across multiple actors
*/
function test_CRITICAL_SystematicFailure() public {
console2.log("=== SYSTEMATIC FAILURE ACROSS ALL USERS ===");
address[10] memory testUsers;
for (uint i = 0; i < 10; i++) {
testUsers[i] = makeAddr(string(abi.encodePacked("user", i)));
vm.deal(testUsers[i], 1 ether);
}
console2.log("Testing 10 different users attempting to claim...");
for (uint i = 0; i < 10; i++) {
console2.log("User", i, "attempting claim...");
vm.prank(testUsers[i]);
vm.expectRevert("Game: You are already the king. No need to re-claim.");
game.claimThrone{value: INITIAL_CLAIM_FEE}();
assertEq(game.currentKing(), address(0), "King should remain address(0)");
}
console2.log("");
console2.log("RESULT: All 10 users failed to claim throne");
console2.log("IMPACT: 100% failure rate across entire user base");
console2.log("SEVERITY: Complete system failure");
}
* @notice Shows this breaks all game mechanics, not just claiming
* @dev Demonstrates cascade failure across game features
*/
function test_CRITICAL_CascadeFailure() public {
console2.log("=== CASCADE FAILURE ANALYSIS ===");
console2.log("Since nobody can claim, ALL game features break:");
console2.log("");
console2.log("1. Testing declareWinner()...");
vm.expectRevert("Game: No one has claimed the throne yet.");
game.declareWinner();
console2.log(" FAILED: No king to declare as winner");
console2.log("2. Testing withdrawWinnings()...");
vm.prank(alice);
vm.expectRevert("Game: No winnings to withdraw.");
game.withdrawWinnings();
console2.log(" FAILED: No winnings accumulated");
console2.log("3. Checking game state progression...");
assertEq(game.totalClaims(), 0, "No claims ever succeeded");
assertEq(game.pot(), 0, "No pot accumulated");
assertEq(game.playerClaimCount(alice), 0, "Alice has zero successful claims");
console2.log(" CONFIRMED: Zero progression in game state");
console2.log("");
console2.log("=== FINAL IMPACT ASSESSMENT ===");
console2.log("Functionality Available: 0%");
console2.log("User Experience: Completely Broken");
console2.log("Financial Risk: Gas Loss on Every Interaction");
console2.log("Reputational Impact: Total System Failure");
console2.log("Classification: CRITICAL SEVERITY - IMMEDIATE FIX REQUIRED");
}
}