Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SPDX-License-Identifier: UNLICENSED
- pragma solidity ^0.8.13;
- import {Test} from "forge-std/Test.sol";
- import {SimpleGame} from "../src/SimpleGame.sol";
- import {GameMonitor} from "../src/GameMonitor.sol";
- contract SimpleGameTest is Test {
- SimpleGame public simpleGame;
- GameMonitor public gameMonitor;
- address realUser1;
- address realUser2;
- address attacker;
- function setUp() public {
- simpleGame = new SimpleGame();
- gameMonitor = new GameMonitor(address(simpleGame));
- (realUser1, realUser2, attacker) = (vm.addr(1), vm.addr(2), vm.addr(3));
- // prefund all users
- vm.deal(realUser1, 1 ether);
- vm.deal(realUser2, 1 ether);
- vm.deal(attacker, 1 ether);
- }
- function test_DepositSuccessAttackerAlwaysClaim() public {
- address[] memory users = new address[](2);
- users[0] = realUser1;
- users[1] = realUser2;
- for (uint256 i = 0; i < 10; i++) {
- if (simpleGame.isFinished()) {
- break;
- }
- vm.startPrank(users[i % 2]);
- simpleGame.deposit{value: 0.1 ether}();
- vm.roll(block.number + 1);
- // attacker interupts
- if (i == 8) {
- vm.startPrank(attacker);
- gameMonitor.monitorAndClaim{value: 0.1 ether}();
- vm.roll(block.number + 1);
- }
- }
- }
- function test_DepositFailAttackerUnableToClaim() public {
- address[] memory users = new address[](2);
- users[0] = realUser1;
- users[1] = realUser2;
- gameMonitor.setWhenFixedStatus(true);
- for (uint256 i = 0; i < 10; i++) {
- if (simpleGame.isFinished()) {
- break;
- }
- vm.startPrank(users[i % 2]);
- simpleGame.deposit{value: 0.1 ether}();
- vm.roll(block.number + 1);
- // attacker interupts
- if (i == 8) {
- vm.startPrank(attacker);
- // expect the below to revert with "Cant claim on the same block as deposit"
- vm.expectRevert(bytes("Cant claim on the same block as deposit"));
- gameMonitor.monitorAndClaim{value: 0.1 ether}();
- vm.roll(block.number + 1);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement