Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,5 @@ jobs:
- name: Run tests @ 0.8.13
run: forge test -vvv --use 0.8.13

- name: Run tests @ 0.7.6
run: forge test -vvv --use 0.7.6

- name: Run snapshot
run: forge snapshot
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "lib/ds-test"]
path = lib/ds-test
url = https://github.com/dapphub/ds-test
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
1 change: 0 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
src = 'src'
out = 'out'
libs = ['lib']
remappings = ['ds-test/=lib/ds-test/src/']
1 change: 0 additions & 1 deletion lib/ds-test
Submodule ds-test deleted from 9310e8
1 change: 1 addition & 0 deletions lib/forge-std
Submodule forge-std added at dcb0d5
10 changes: 10 additions & 0 deletions node_modules/.yarn-integrity

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 45 additions & 2 deletions src/test/ExcessivelySafeCall.t.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.7.6;

import "ds-test/test.sol";
import {Test} from "forge-std/Test.sol";
import "src/ExcessivelySafeCall.sol";

contract ContractTest is DSTest {
contract ContractTest is Test {
using ExcessivelySafeCall for address;

address target;
Intermediary intermediary;
CallTarget t;

function returnSize() internal pure returns (uint256 _bytes) {
Expand All @@ -19,6 +20,7 @@ contract ContractTest is DSTest {
function setUp() public {
t = new CallTarget();
target = address(t);
intermediary = new Intermediary();
}

function testCall() public {
Expand Down Expand Up @@ -177,9 +179,39 @@ contract ContractTest is DSTest {
assertEq(_ret.length, 32, "revert didn't truncate");
}

function test_drain() public {
vm.expectRevert();
intermediary.getDrained{gas: 1_000_000}(target, 10_000);
}

function test_drain_safe() public {
intermediary.getSafeDrained{gas: 1_000_000}(target, 10_000);
}
}


contract Intermediary {
using ExcessivelySafeCall for address;
function getDrained(address target, uint256 drainTo) public {
bool _success;
bytes memory _ret;
(_success, _ret) = target.call(
abi.encodeWithSelector(CallTarget.drainTo.selector, drainTo)
);
}

function getSafeDrained(address target, uint256 drainTo) public {
bool _success;
bytes memory _ret;
(_success, _ret) = target.excessivelySafeCall(
gasleft(),
0,
0,
abi.encodeWithSelector(CallTarget.drainTo.selector, drainTo)
);
}
}

contract CallTarget {
uint256 public called;
constructor () {}
Expand Down Expand Up @@ -219,4 +251,15 @@ contract CallTarget {
function badRev() external pure {
revBytes(1_000_000);
}

function drainTo(uint256 gas) public view {
uint256 len;
while (gasleft() > gas) {
bytes memory a = new bytes(0);
len += 32 + a.length;
}
assembly {
return (0, len)
}
}
}
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1