Advertisement
Prosperity-Author

SimpleGameTest.t.sol

Jan 8th, 2025
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | Cryptocurrency | 0 0
  1. // SPDX-License-Identifier: UNLICENSED
  2. pragma solidity ^0.8.13;
  3.  
  4. import {Test} from "forge-std/Test.sol";
  5. import {SimpleGame} from "../src/SimpleGame.sol";
  6. import {GameMonitor} from "../src/GameMonitor.sol";
  7.  
  8. contract SimpleGameTest is Test {
  9. SimpleGame public simpleGame;
  10. GameMonitor public gameMonitor;
  11.  
  12. address realUser1;
  13. address realUser2;
  14. address attacker;
  15.  
  16. function setUp() public {
  17. simpleGame = new SimpleGame();
  18. gameMonitor = new GameMonitor(address(simpleGame));
  19. (realUser1, realUser2, attacker) = (vm.addr(1), vm.addr(2), vm.addr(3));
  20. // prefund all users
  21. vm.deal(realUser1, 1 ether);
  22. vm.deal(realUser2, 1 ether);
  23. vm.deal(attacker, 1 ether);
  24. }
  25.  
  26. function test_DepositSuccessAttackerAlwaysClaim() public {
  27. address[] memory users = new address[](2);
  28. users[0] = realUser1;
  29. users[1] = realUser2;
  30.  
  31. for (uint256 i = 0; i < 10; i++) {
  32. if (simpleGame.isFinished()) {
  33. break;
  34. }
  35. vm.startPrank(users[i % 2]);
  36. simpleGame.deposit{value: 0.1 ether}();
  37. vm.roll(block.number + 1);
  38. // attacker interupts
  39. if (i == 8) {
  40. vm.startPrank(attacker);
  41. gameMonitor.monitorAndClaim{value: 0.1 ether}();
  42. vm.roll(block.number + 1);
  43. }
  44. }
  45. }
  46.  
  47. function test_DepositFailAttackerUnableToClaim() public {
  48. address[] memory users = new address[](2);
  49. users[0] = realUser1;
  50. users[1] = realUser2;
  51.  
  52. gameMonitor.setWhenFixedStatus(true);
  53.  
  54. for (uint256 i = 0; i < 10; i++) {
  55. if (simpleGame.isFinished()) {
  56. break;
  57. }
  58. vm.startPrank(users[i % 2]);
  59. simpleGame.deposit{value: 0.1 ether}();
  60. vm.roll(block.number + 1);
  61. // attacker interupts
  62. if (i == 8) {
  63. vm.startPrank(attacker);
  64. // expect the below to revert with "Cant claim on the same block as deposit"
  65. vm.expectRevert(bytes("Cant claim on the same block as deposit"));
  66. gameMonitor.monitorAndClaim{value: 0.1 ether}();
  67. vm.roll(block.number + 1);
  68. }
  69. }
  70. }
  71.  
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement