From 8bf9baaa2994d6c19e5ded0e4a5fa49b00b2cefb Mon Sep 17 00:00:00 2001 From: arbazkiraak <13177578+arbazkiraak@users.noreply.github.com> Date: Mon, 4 Dec 2023 18:23:28 +0530 Subject: [PATCH] mocks: add gasExhaust contract --- src/mocks/malicious/gasExhaust.sol | 16 ++++++++++++++++ test/mocks/malicious/gasExhaust.t.sol | 25 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/mocks/malicious/gasExhaust.sol create mode 100644 test/mocks/malicious/gasExhaust.t.sol diff --git a/src/mocks/malicious/gasExhaust.sol b/src/mocks/malicious/gasExhaust.sol new file mode 100644 index 0000000..d09d27d --- /dev/null +++ b/src/mocks/malicious/gasExhaust.sol @@ -0,0 +1,16 @@ +pragma solidity ^0.8.0; + +contract gasExhaust { + uint256 counter; + uint256 gasConsumeLimit; + + function setGasConsumeLimit(uint256 _gasConsume) public { + gasConsumeLimit = _gasConsume; + } + + fallback() external { + while (gasleft() > gasConsumeLimit) { + counter++; + } + } +} diff --git a/test/mocks/malicious/gasExhaust.t.sol b/test/mocks/malicious/gasExhaust.t.sol new file mode 100644 index 0000000..8f51d99 --- /dev/null +++ b/test/mocks/malicious/gasExhaust.t.sol @@ -0,0 +1,25 @@ +pragma solidity ^0.8.0; + +import "forge-std/Test.sol"; + +import {gasExhaust} from "../../../src/mocks/malicious/gasExhaust.sol"; + +contract gasExhaustTest is Test { + gasExhaust public attacker; + + function setUp() public { + attacker = new gasExhaust(); + } + + function testGasExhaustSuccess() public { + attacker.setGasConsumeLimit(6000); + (bool success, bytes memory returnData) = address(attacker).call{gas: 25000}(""); + assertEq(success, true); + } + + function testGasExhaustFail() public { + attacker.setGasConsumeLimit(100); + (bool success, bytes memory returnData) = address(attacker).call{gas: 25000}(""); + assertEq(success, false); + } +}