diff --git a/src/Assertions.sol b/src/Assertions.sol new file mode 100644 index 00000000..a52616a1 --- /dev/null +++ b/src/Assertions.sol @@ -0,0 +1,232 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.6.0 <0.9.0; + +import "ds-test/test.sol"; +import "./Math.sol"; + +contract Assertions is DSTest { + event log_array(uint256[] val); + event log_array(int256[] val); + event log_array(address[] val); + event log_named_array(string key, uint256[] val); + event log_named_array(string key, int256[] val); + event log_named_array(string key, address[] val); + + function fail(string memory err) internal virtual { + emit log_named_string("Error", err); + fail(); + } + + function assertFalse(bool data) internal virtual { + assertTrue(!data); + } + + function assertFalse(bool data, string memory err) internal virtual { + assertTrue(!data, err); + } + + function assertEq(bool a, bool b) internal virtual { + if (a != b) { + emit log ("Error: a == b not satisfied [bool]"); + emit log_named_string (" Expected", b ? "true" : "false"); + emit log_named_string (" Actual", a ? "true" : "false"); + fail(); + } + } + + function assertEq(bool a, bool b, string memory err) internal virtual { + if (a != b) { + emit log_named_string("Error", err); + assertEq(a, b); + } + } + + function assertEq(bytes memory a, bytes memory b) internal virtual { + assertEq0(a, b); + } + + function assertEq(bytes memory a, bytes memory b, string memory err) internal virtual { + assertEq0(a, b, err); + } + + function assertEq(uint256[] memory a, uint256[] memory b) internal virtual { + if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) { + emit log("Error: a == b not satisfied [uint[]]"); + emit log_named_array(" Expected", b); + emit log_named_array(" Actual", a); + fail(); + } + } + + function assertEq(int256[] memory a, int256[] memory b) internal virtual { + if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) { + emit log("Error: a == b not satisfied [int[]]"); + emit log_named_array(" Expected", b); + emit log_named_array(" Actual", a); + fail(); + } + } + + function assertEq(address[] memory a, address[] memory b) internal virtual { + if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) { + emit log("Error: a == b not satisfied [address[]]"); + emit log_named_array(" Expected", b); + emit log_named_array(" Actual", a); + fail(); + } + } + + function assertEq(uint256[] memory a, uint256[] memory b, string memory err) internal virtual { + if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) { + emit log_named_string("Error", err); + assertEq(a, b); + } + } + + function assertEq(int256[] memory a, int256[] memory b, string memory err) internal virtual { + if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) { + emit log_named_string("Error", err); + assertEq(a, b); + } + } + + + function assertEq(address[] memory a, address[] memory b, string memory err) internal virtual { + if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) { + emit log_named_string("Error", err); + assertEq(a, b); + } + } + + function assertApproxEqAbs( + uint256 a, + uint256 b, + uint256 maxDelta + ) internal virtual { + uint256 delta = stdMath.delta(a, b); + + if (delta > maxDelta) { + emit log ("Error: a ~= b not satisfied [uint]"); + emit log_named_uint (" Expected", b); + emit log_named_uint (" Actual", a); + emit log_named_uint (" Max Delta", maxDelta); + emit log_named_uint (" Delta", delta); + fail(); + } + } + + function assertApproxEqAbs( + uint256 a, + uint256 b, + uint256 maxDelta, + string memory err + ) internal virtual { + uint256 delta = stdMath.delta(a, b); + + if (delta > maxDelta) { + emit log_named_string ("Error", err); + assertApproxEqAbs(a, b, maxDelta); + } + } + + function assertApproxEqAbs( + int256 a, + int256 b, + uint256 maxDelta + ) internal virtual { + uint256 delta = stdMath.delta(a, b); + + if (delta > maxDelta) { + emit log ("Error: a ~= b not satisfied [int]"); + emit log_named_int (" Expected", b); + emit log_named_int (" Actual", a); + emit log_named_uint (" Max Delta", maxDelta); + emit log_named_uint (" Delta", delta); + fail(); + } + } + + function assertApproxEqAbs( + int256 a, + int256 b, + uint256 maxDelta, + string memory err + ) internal virtual { + uint256 delta = stdMath.delta(a, b); + + if (delta > maxDelta) { + emit log_named_string ("Error", err); + assertApproxEqAbs(a, b, maxDelta); + } + } + + function assertApproxEqRel( + uint256 a, + uint256 b, + uint256 maxPercentDelta // An 18 decimal fixed point number, where 1e18 == 100% + ) internal virtual { + if (b == 0) return assertEq(a, b); // If the expected is 0, actual must be too. + + uint256 percentDelta = stdMath.percentDelta(a, b); + + if (percentDelta > maxPercentDelta) { + emit log ("Error: a ~= b not satisfied [uint]"); + emit log_named_uint (" Expected", b); + emit log_named_uint (" Actual", a); + emit log_named_decimal_uint (" Max % Delta", maxPercentDelta, 18); + emit log_named_decimal_uint (" % Delta", percentDelta, 18); + fail(); + } + } + + function assertApproxEqRel( + uint256 a, + uint256 b, + uint256 maxPercentDelta, // An 18 decimal fixed point number, where 1e18 == 100% + string memory err + ) internal virtual { + if (b == 0) return assertEq(a, b); // If the expected is 0, actual must be too. + + uint256 percentDelta = stdMath.percentDelta(a, b); + + if (percentDelta > maxPercentDelta) { + emit log_named_string ("Error", err); + assertApproxEqRel(a, b, maxPercentDelta); + } + } + + function assertApproxEqRel( + int256 a, + int256 b, + uint256 maxPercentDelta + ) internal virtual { + if (b == 0) return assertEq(a, b); // If the expected is 0, actual must be too. + + uint256 percentDelta = stdMath.percentDelta(a, b); + + if (percentDelta > maxPercentDelta) { + emit log ("Error: a ~= b not satisfied [int]"); + emit log_named_int (" Expected", b); + emit log_named_int (" Actual", a); + emit log_named_decimal_uint(" Max % Delta", maxPercentDelta, 18); + emit log_named_decimal_uint(" % Delta", percentDelta, 18); + fail(); + } + } + + function assertApproxEqRel( + int256 a, + int256 b, + uint256 maxPercentDelta, + string memory err + ) internal virtual { + if (b == 0) return assertEq(a, b); // If the expected is 0, actual must be too. + + uint256 percentDelta = stdMath.percentDelta(a, b); + + if (percentDelta > maxPercentDelta) { + emit log_named_string ("Error", err); + assertApproxEqRel(a, b, maxPercentDelta); + } + } +} diff --git a/src/Cheats.sol b/src/Cheats.sol new file mode 100644 index 00000000..def74c57 --- /dev/null +++ b/src/Cheats.sol @@ -0,0 +1,180 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.6.0 <0.9.0; + +import "./Storage.sol"; +import "./Vm.sol"; + +// Wrappers around Cheats to avoid footguns +contract Cheats { + using stdStorage for StdStorage; + + StdStorage private stdstore; + Vm private constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code"))))); + + + // Skip forward or rewind time by the specified number of seconds + function skip(uint256 time) internal virtual { + vm.warp(block.timestamp + time); + } + + function rewind(uint256 time) internal virtual { + vm.warp(block.timestamp - time); + } + + // Setup a prank from an address that has some ether + function hoax(address who) internal virtual { + vm.deal(who, 1 << 128); + vm.prank(who); + } + + function hoax(address who, uint256 give) internal virtual { + vm.deal(who, give); + vm.prank(who); + } + + function hoax(address who, address origin) internal virtual { + vm.deal(who, 1 << 128); + vm.prank(who, origin); + } + + function hoax(address who, address origin, uint256 give) internal virtual { + vm.deal(who, give); + vm.prank(who, origin); + } + + // Start perpetual prank from an address that has some ether + function startHoax(address who) internal virtual { + vm.deal(who, 1 << 128); + vm.startPrank(who); + } + + function startHoax(address who, uint256 give) internal virtual { + vm.deal(who, give); + vm.startPrank(who); + } + + // Start perpetual prank from an address that has some ether + // tx.origin is set to the origin parameter + function startHoax(address who, address origin) internal virtual { + vm.deal(who, 1 << 128); + vm.startPrank(who, origin); + } + + function startHoax(address who, address origin, uint256 give) internal virtual { + vm.deal(who, give); + vm.startPrank(who, origin); + } + + function changePrank(address who) internal virtual { + vm.stopPrank(); + vm.startPrank(who); + } + + // The same as Vm's `deal` + // Use the alternative signature for ERC20 tokens + function deal(address to, uint256 give) internal virtual { + vm.deal(to, give); + } + + // Set the balance of an account for any ERC20 token + // Use the alternative signature to update `totalSupply` + function deal(address token, address to, uint256 give) internal virtual { + deal(token, to, give, false); + } + + function deal(address token, address to, uint256 give, bool adjust) internal virtual { + // get current balance + (, bytes memory balData) = token.call(abi.encodeWithSelector(0x70a08231, to)); + uint256 prevBal = abi.decode(balData, (uint256)); + + // update balance + stdstore + .target(token) + .sig(0x70a08231) + .with_key(to) + .checked_write(give); + + // update total supply + if (adjust) { + (, bytes memory totSupData) = token.call(abi.encodeWithSelector(0x18160ddd)); + uint256 totSup = abi.decode(totSupData, (uint256)); + if (give < prevBal) { + totSup -= (prevBal - give); + } else { + totSup += (give - prevBal); + } + stdstore + .target(token) + .sig(0x18160ddd) + .checked_write(totSup); + } + } + + // Deploy a contract by fetching the contract bytecode from + // the artifacts directory + // e.g. `deployCode(code, abi.encode(arg1,arg2,arg3))` + function deployCode(string memory what, bytes memory args) + internal virtual + returns (address addr) + { + bytes memory bytecode = abi.encodePacked(vm.getCode(what), args); + /// @solidity memory-safe-assembly + assembly { + addr := create(0, add(bytecode, 0x20), mload(bytecode)) + } + + require( + addr != address(0), + "Test deployCode(string,bytes): Deployment failed." + ); + } + + function deployCode(string memory what) + internal virtual + returns (address addr) + { + bytes memory bytecode = vm.getCode(what); + /// @solidity memory-safe-assembly + assembly { + addr := create(0, add(bytecode, 0x20), mload(bytecode)) + } + + require( + addr != address(0), + "Test deployCode(string): Deployment failed." + ); + } + + /// @dev deploy contract with value on construction + function deployCode(string memory what, bytes memory args, uint256 val) + internal virtual + returns (address addr) + { + bytes memory bytecode = abi.encodePacked(vm.getCode(what), args); + /// @solidity memory-safe-assembly + assembly { + addr := create(val, add(bytecode, 0x20), mload(bytecode)) + } + + require( + addr != address(0), + "Test deployCode(string,bytes,uint256): Deployment failed." + ); + } + + function deployCode(string memory what, uint256 val) + internal virtual + returns (address addr) + { + bytes memory bytecode = vm.getCode(what); + /// @solidity memory-safe-assembly + assembly { + addr := create(val, add(bytecode, 0x20), mload(bytecode)) + } + + require( + addr != address(0), + "Test deployCode(string,uint256): Deployment failed." + ); + } +} diff --git a/src/Components.sol b/src/Components.sol new file mode 100644 index 00000000..c5bfa3d5 --- /dev/null +++ b/src/Components.sol @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.6.0 <0.9.0; + +import "./Assertions.sol"; +import "./Cheats.sol"; +import "./console.sol"; +import "./console2.sol"; +import "./Errors.sol"; +import "./Math.sol"; +import "./Storage.sol"; +import "./Utils.sol"; +import "./Vm.sol"; diff --git a/src/Errors.sol b/src/Errors.sol new file mode 100644 index 00000000..7657862f --- /dev/null +++ b/src/Errors.sol @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: MIT +// Panics work for versions >=0.8.0, but we lowered the pragma to make this compatible with Test +pragma solidity >=0.6.0 <0.9.0; + +library stdError { + bytes public constant assertionError = abi.encodeWithSignature("Panic(uint256)", 0x01); + bytes public constant arithmeticError = abi.encodeWithSignature("Panic(uint256)", 0x11); + bytes public constant divisionError = abi.encodeWithSignature("Panic(uint256)", 0x12); + bytes public constant enumConversionError = abi.encodeWithSignature("Panic(uint256)", 0x21); + bytes public constant encodeStorageError = abi.encodeWithSignature("Panic(uint256)", 0x22); + bytes public constant popError = abi.encodeWithSignature("Panic(uint256)", 0x31); + bytes public constant indexOOBError = abi.encodeWithSignature("Panic(uint256)", 0x32); + bytes public constant memOverflowError = abi.encodeWithSignature("Panic(uint256)", 0x41); + bytes public constant zeroVarError = abi.encodeWithSignature("Panic(uint256)", 0x51); +} \ No newline at end of file diff --git a/src/Math.sol b/src/Math.sol new file mode 100644 index 00000000..5fb368e3 --- /dev/null +++ b/src/Math.sol @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.6.0 <0.9.0; + +library stdMath { + int256 private constant INT256_MIN = -57896044618658097711785492504343953926634992332820282019728792003956564819968; + + function abs(int256 a) internal pure returns (uint256) { + // Required or it will fail when `a = type(int256).min` + if (a == INT256_MIN) { + return 57896044618658097711785492504343953926634992332820282019728792003956564819968; + } + + return uint256(a > 0 ? a : -a); + } + + function delta(uint256 a, uint256 b) internal pure returns (uint256) { + return a > b + ? a - b + : b - a; + } + + function delta(int256 a, int256 b) internal pure returns (uint256) { + // a and b are of the same sign + // this works thanks to two's complement, the left-most bit is the sign bit + if ((a ^ b) > -1) { + return delta(abs(a), abs(b)); + } + + // a and b are of opposite signs + return abs(a) + abs(b); + } + + function percentDelta(uint256 a, uint256 b) internal pure returns (uint256) { + uint256 absDelta = delta(a, b); + + return absDelta * 1e18 / b; + } + + function percentDelta(int256 a, int256 b) internal pure returns (uint256) { + uint256 absDelta = delta(a, b); + uint256 absB = abs(b); + + return absDelta * 1e18 / absB; + } +} diff --git a/src/Script.sol b/src/Script.sol index 9bbd8599..8ebdbd80 100644 --- a/src/Script.sol +++ b/src/Script.sol @@ -1,39 +1,13 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.6.0 <0.9.0; -import "./Vm.sol"; -import "./console.sol"; -import "./console2.sol"; +import {Cheats, console, console2, stdMath, stdStorage, StdStorage, Utils, Vm} from "./Components.sol"; -abstract contract Script { - bool public IS_SCRIPT = true; - address constant private VM_ADDRESS = - address(bytes20(uint160(uint256(keccak256('hevm cheat code'))))); - - Vm public constant vm = Vm(VM_ADDRESS); - - /// @dev Compute the address a contract will be deployed at for a given deployer address and nonce - /// @notice adapated from Solmate implementation (https://github.com/transmissions11/solmate/blob/main/src/utils/LibRLP.sol) - function computeCreateAddress(address deployer, uint256 nonce) internal pure returns (address) { - // The integer zero is treated as an empty byte string, and as a result it only has a length prefix, 0x80, computed via 0x80 + 0. - // A one byte integer uses its own value as its length prefix, there is no additional "0x80 + length" prefix that comes before it. - if (nonce == 0x00) return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xd6), bytes1(0x94), deployer, bytes1(0x80)))); - if (nonce <= 0x7f) return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xd6), bytes1(0x94), deployer, uint8(nonce)))); - - // Nonces greater than 1 byte all follow a consistent encoding scheme, where each value is preceded by a prefix of 0x80 + length. - if (nonce <= 2**8 - 1) return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xd7), bytes1(0x94), deployer, bytes1(0x81), uint8(nonce)))); - if (nonce <= 2**16 - 1) return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xd8), bytes1(0x94), deployer, bytes1(0x82), uint16(nonce)))); - if (nonce <= 2**24 - 1) return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xd9), bytes1(0x94), deployer, bytes1(0x83), uint24(nonce)))); - - // More details about RLP encoding can be found here: https://eth.wiki/fundamentals/rlp - // 0xda = 0xc0 (short RLP prefix) + 0x16 (length of: 0x94 ++ proxy ++ 0x84 ++ nonce) - // 0x94 = 0x80 + 0x14 (0x14 = the length of an address, 20 bytes, in hex) - // 0x84 = 0x80 + 0x04 (0x04 = the bytes length of the nonce, 4 bytes, in hex) - // We assume nobody can have a nonce large enough to require more than 32 bytes. - return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xda), bytes1(0x94), deployer, bytes1(0x84), uint32(nonce)))); - } +abstract contract ScriptBase { + address internal constant VM_ADDRESS = address(uint160(uint256(keccak256("hevm cheat code")))); + Vm internal constant vm = Vm(VM_ADDRESS); +} - function addressFromLast20Bytes(bytes32 bytesValue) internal pure returns (address) { - return address(uint160(uint256(bytesValue))); - } +abstract contract Script is ScriptBase, Cheats, Utils { + bool public IS_SCRIPT = true; } diff --git a/src/Storage.sol b/src/Storage.sol new file mode 100644 index 00000000..7e4e260b --- /dev/null +++ b/src/Storage.sol @@ -0,0 +1,249 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.6.0 <0.9.0; + +import "./Vm.sol"; + +struct StdStorage { + mapping (address => mapping(bytes4 => mapping(bytes32 => uint256))) slots; + mapping (address => mapping(bytes4 => mapping(bytes32 => bool))) finds; + + bytes32[] _keys; + bytes4 _sig; + uint256 _depth; + address _target; + bytes32 _set; +} + +library stdStorage { + event SlotFound(address who, bytes4 fsig, bytes32 keysHash, uint slot); + event WARNING_UninitedSlot(address who, uint slot); + + Vm private constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code"))))); + + function sigs( + string memory sigStr + ) + internal + pure + returns (bytes4) + { + return bytes4(keccak256(bytes(sigStr))); + } + + /// @notice find an arbitrary storage slot given a function sig, input data, address of the contract and a value to check against + // slot complexity: + // if flat, will be bytes32(uint256(uint)); + // if map, will be keccak256(abi.encode(key, uint(slot))); + // if deep map, will be keccak256(abi.encode(key1, keccak256(abi.encode(key0, uint(slot))))); + // if map struct, will be bytes32(uint256(keccak256(abi.encode(key1, keccak256(abi.encode(key0, uint(slot)))))) + structFieldDepth); + function find( + StdStorage storage self + ) + internal + returns (uint256) + { + address who = self._target; + bytes4 fsig = self._sig; + uint256 field_depth = self._depth; + bytes32[] memory ins = self._keys; + + // calldata to test against + if (self.finds[who][fsig][keccak256(abi.encodePacked(ins, field_depth))]) { + return self.slots[who][fsig][keccak256(abi.encodePacked(ins, field_depth))]; + } + bytes memory cald = abi.encodePacked(fsig, flatten(ins)); + vm.record(); + bytes32 fdat; + { + (, bytes memory rdat) = who.staticcall(cald); + fdat = bytesToBytes32(rdat, 32*field_depth); + } + + (bytes32[] memory reads, ) = vm.accesses(address(who)); + if (reads.length == 1) { + bytes32 curr = vm.load(who, reads[0]); + if (curr == bytes32(0)) { + emit WARNING_UninitedSlot(who, uint256(reads[0])); + } + if (fdat != curr) { + require(false, "stdStorage find(StdStorage): Packed slot. This would cause dangerous overwriting and currently isn't supported."); + } + emit SlotFound(who, fsig, keccak256(abi.encodePacked(ins, field_depth)), uint256(reads[0])); + self.slots[who][fsig][keccak256(abi.encodePacked(ins, field_depth))] = uint256(reads[0]); + self.finds[who][fsig][keccak256(abi.encodePacked(ins, field_depth))] = true; + } else if (reads.length > 1) { + for (uint256 i = 0; i < reads.length; i++) { + bytes32 prev = vm.load(who, reads[i]); + if (prev == bytes32(0)) { + emit WARNING_UninitedSlot(who, uint256(reads[i])); + } + // store + vm.store(who, reads[i], bytes32(hex"1337")); + bool success; + bytes memory rdat; + { + (success, rdat) = who.staticcall(cald); + fdat = bytesToBytes32(rdat, 32*field_depth); + } + + if (success && fdat == bytes32(hex"1337")) { + // we found which of the slots is the actual one + emit SlotFound(who, fsig, keccak256(abi.encodePacked(ins, field_depth)), uint256(reads[i])); + self.slots[who][fsig][keccak256(abi.encodePacked(ins, field_depth))] = uint256(reads[i]); + self.finds[who][fsig][keccak256(abi.encodePacked(ins, field_depth))] = true; + vm.store(who, reads[i], prev); + break; + } + vm.store(who, reads[i], prev); + } + } else { + require(false, "stdStorage find(StdStorage): No storage use detected for target."); + } + + require(self.finds[who][fsig][keccak256(abi.encodePacked(ins, field_depth))], "stdStorage find(StdStorage): Slot(s) not found."); + + delete self._target; + delete self._sig; + delete self._keys; + delete self._depth; + + return self.slots[who][fsig][keccak256(abi.encodePacked(ins, field_depth))]; + } + + function target(StdStorage storage self, address _target) internal returns (StdStorage storage) { + self._target = _target; + return self; + } + + function sig(StdStorage storage self, bytes4 _sig) internal returns (StdStorage storage) { + self._sig = _sig; + return self; + } + + function sig(StdStorage storage self, string memory _sig) internal returns (StdStorage storage) { + self._sig = sigs(_sig); + return self; + } + + function with_key(StdStorage storage self, address who) internal returns (StdStorage storage) { + self._keys.push(bytes32(uint256(uint160(who)))); + return self; + } + + function with_key(StdStorage storage self, uint256 amt) internal returns (StdStorage storage) { + self._keys.push(bytes32(amt)); + return self; + } + function with_key(StdStorage storage self, bytes32 key) internal returns (StdStorage storage) { + self._keys.push(key); + return self; + } + + function depth(StdStorage storage self, uint256 _depth) internal returns (StdStorage storage) { + self._depth = _depth; + return self; + } + + function checked_write(StdStorage storage self, address who) internal { + checked_write(self, bytes32(uint256(uint160(who)))); + } + + function checked_write(StdStorage storage self, uint256 amt) internal { + checked_write(self, bytes32(amt)); + } + + function checked_write(StdStorage storage self, bool write) internal { + bytes32 t; + /// @solidity memory-safe-assembly + assembly { + t := write + } + checked_write(self, t); + } + + function checked_write( + StdStorage storage self, + bytes32 set + ) internal { + address who = self._target; + bytes4 fsig = self._sig; + uint256 field_depth = self._depth; + bytes32[] memory ins = self._keys; + + bytes memory cald = abi.encodePacked(fsig, flatten(ins)); + if (!self.finds[who][fsig][keccak256(abi.encodePacked(ins, field_depth))]) { + find(self); + } + bytes32 slot = bytes32(self.slots[who][fsig][keccak256(abi.encodePacked(ins, field_depth))]); + + bytes32 fdat; + { + (, bytes memory rdat) = who.staticcall(cald); + fdat = bytesToBytes32(rdat, 32*field_depth); + } + bytes32 curr = vm.load(who, slot); + + if (fdat != curr) { + require(false, "stdStorage find(StdStorage): Packed slot. This would cause dangerous overwriting and currently isn't supported."); + } + vm.store(who, slot, set); + delete self._target; + delete self._sig; + delete self._keys; + delete self._depth; + } + + function read(StdStorage storage self) private returns (bytes memory) { + address t = self._target; + uint256 s = find(self); + return abi.encode(vm.load(t, bytes32(s))); + } + + function read_bytes32(StdStorage storage self) internal returns (bytes32) { + return abi.decode(read(self), (bytes32)); + } + + + function read_bool(StdStorage storage self) internal returns (bool) { + int256 v = read_int(self); + if (v == 0) return false; + if (v == 1) return true; + revert("stdStorage read_bool(StdStorage): Cannot decode. Make sure you are reading a bool."); + } + + function read_address(StdStorage storage self) internal returns (address) { + return abi.decode(read(self), (address)); + } + + function read_uint(StdStorage storage self) internal returns (uint256) { + return abi.decode(read(self), (uint256)); + } + + function read_int(StdStorage storage self) internal returns (int256) { + return abi.decode(read(self), (int256)); + } + + function bytesToBytes32(bytes memory b, uint offset) public pure returns (bytes32) { + bytes32 out; + + uint256 max = b.length > 32 ? 32 : b.length; + for (uint i = 0; i < max; i++) { + out |= bytes32(b[offset + i] & 0xFF) >> (i * 8); + } + return out; + } + + function flatten(bytes32[] memory b) private pure returns (bytes memory) + { + bytes memory result = new bytes(b.length * 32); + for (uint256 i = 0; i < b.length; i++) { + bytes32 k = b[i]; + /// @solidity memory-safe-assembly + assembly { + mstore(add(result, add(32, mul(32, i))), k) + } + } + + return result; + } +} diff --git a/src/Test.sol b/src/Test.sol index da549594..e6f8aa7b 100644 --- a/src/Test.sol +++ b/src/Test.sol @@ -1,767 +1,14 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.6.0 <0.9.0; -import "./Script.sol"; +import "./Components.sol"; import "ds-test/test.sol"; -// Wrappers around Cheatcodes to avoid footguns -abstract contract Test is DSTest, Script { - using stdStorage for StdStorage; - - uint256 internal constant UINT256_MAX = - 115792089237316195423570985008687907853269984665640564039457584007913129639935; +abstract contract TestBase { + address internal constant VM_ADDRESS = address(uint160(uint256(keccak256("hevm cheat code")))); StdStorage internal stdstore; - - /*////////////////////////////////////////////////////////////////////////// - STD-LOGS - //////////////////////////////////////////////////////////////////////////*/ - - event log_array(uint256[] val); - event log_array(int256[] val); - event log_array(address[] val); - event log_named_array(string key, uint256[] val); - event log_named_array(string key, int256[] val); - event log_named_array(string key, address[] val); - - /*////////////////////////////////////////////////////////////////////////// - STD-CHEATS - //////////////////////////////////////////////////////////////////////////*/ - - // Skip forward or rewind time by the specified number of seconds - function skip(uint256 time) internal { - vm.warp(block.timestamp + time); - } - - function rewind(uint256 time) internal { - vm.warp(block.timestamp - time); - } - - // Setup a prank from an address that has some ether - function hoax(address who) internal { - vm.deal(who, 1 << 128); - vm.prank(who); - } - - function hoax(address who, uint256 give) internal { - vm.deal(who, give); - vm.prank(who); - } - - function hoax(address who, address origin) internal { - vm.deal(who, 1 << 128); - vm.prank(who, origin); - } - - function hoax(address who, address origin, uint256 give) internal { - vm.deal(who, give); - vm.prank(who, origin); - } - - // Start perpetual prank from an address that has some ether - function startHoax(address who) internal { - vm.deal(who, 1 << 128); - vm.startPrank(who); - } - - function startHoax(address who, uint256 give) internal { - vm.deal(who, give); - vm.startPrank(who); - } - - // Start perpetual prank from an address that has some ether - // tx.origin is set to the origin parameter - function startHoax(address who, address origin) internal { - vm.deal(who, 1 << 128); - vm.startPrank(who, origin); - } - - function startHoax(address who, address origin, uint256 give) internal { - vm.deal(who, give); - vm.startPrank(who, origin); - } - - function changePrank(address who) internal { - vm.stopPrank(); - vm.startPrank(who); - } - - // DEPRECATED: Use `deal` instead - function tip(address token, address to, uint256 give) internal { - emit log_named_string("WARNING", "Test tip(address,address,uint256): The `tip` stdcheat has been deprecated. Use `deal` instead."); - stdstore - .target(token) - .sig(0x70a08231) - .with_key(to) - .checked_write(give); - } - - // The same as Vm's `deal` - // Use the alternative signature for ERC20 tokens - function deal(address to, uint256 give) internal { - vm.deal(to, give); - } - - // Set the balance of an account for any ERC20 token - // Use the alternative signature to update `totalSupply` - function deal(address token, address to, uint256 give) internal { - deal(token, to, give, false); - } - - function deal(address token, address to, uint256 give, bool adjust) internal { - // get current balance - (, bytes memory balData) = token.call(abi.encodeWithSelector(0x70a08231, to)); - uint256 prevBal = abi.decode(balData, (uint256)); - - // update balance - stdstore - .target(token) - .sig(0x70a08231) - .with_key(to) - .checked_write(give); - - // update total supply - if(adjust){ - (, bytes memory totSupData) = token.call(abi.encodeWithSelector(0x18160ddd)); - uint256 totSup = abi.decode(totSupData, (uint256)); - if(give < prevBal) { - totSup -= (prevBal - give); - } else { - totSup += (give - prevBal); - } - stdstore - .target(token) - .sig(0x18160ddd) - .checked_write(totSup); - } - } - - function bound(uint256 x, uint256 min, uint256 max) internal virtual returns (uint256 result) { - require(min <= max, "Test bound(uint256,uint256,uint256): Max is less than min."); - - uint256 size = max - min; - - if (size == 0) - { - result = min; - } - else if (size == UINT256_MAX) - { - result = x; - } - else - { - ++size; // make `max` inclusive - uint256 mod = x % size; - result = min + mod; - } - - emit log_named_uint("Bound Result", result); - } - - // Deploy a contract by fetching the contract bytecode from - // the artifacts directory - // e.g. `deployCode(code, abi.encode(arg1,arg2,arg3))` - function deployCode(string memory what, bytes memory args) - internal - returns (address addr) - { - bytes memory bytecode = abi.encodePacked(vm.getCode(what), args); - /// @solidity memory-safe-assembly - assembly { - addr := create(0, add(bytecode, 0x20), mload(bytecode)) - } - - require( - addr != address(0), - "Test deployCode(string,bytes): Deployment failed." - ); - } - - function deployCode(string memory what) - internal - returns (address addr) - { - bytes memory bytecode = vm.getCode(what); - /// @solidity memory-safe-assembly - assembly { - addr := create(0, add(bytecode, 0x20), mload(bytecode)) - } - - require( - addr != address(0), - "Test deployCode(string): Deployment failed." - ); - } - - /// deploy contract with value on construction - function deployCode(string memory what, bytes memory args, uint256 val) - internal - returns (address addr) - { - bytes memory bytecode = abi.encodePacked(vm.getCode(what), args); - /// @solidity memory-safe-assembly - assembly { - addr := create(val, add(bytecode, 0x20), mload(bytecode)) - } - - require( - addr != address(0), - "Test deployCode(string,bytes,uint256): Deployment failed." - ); - } - - function deployCode(string memory what, uint256 val) - internal - returns (address addr) - { - bytes memory bytecode = vm.getCode(what); - /// @solidity memory-safe-assembly - assembly { - addr := create(val, add(bytecode, 0x20), mload(bytecode)) - } - - require( - addr != address(0), - "Test deployCode(string,uint256): Deployment failed." - ); - } - - /*////////////////////////////////////////////////////////////////////////// - STD-ASSERTIONS - //////////////////////////////////////////////////////////////////////////*/ - - function fail(string memory err) internal virtual { - emit log_named_string("Error", err); - fail(); - } - - function assertFalse(bool data) internal virtual { - assertTrue(!data); - } - - function assertFalse(bool data, string memory err) internal virtual { - assertTrue(!data, err); - } - - function assertEq(bool a, bool b) internal { - if (a != b) { - emit log ("Error: a == b not satisfied [bool]"); - emit log_named_string (" Expected", b ? "true" : "false"); - emit log_named_string (" Actual", a ? "true" : "false"); - fail(); - } - } - - function assertEq(bool a, bool b, string memory err) internal { - if (a != b) { - emit log_named_string("Error", err); - assertEq(a, b); - } - } - - function assertEq(bytes memory a, bytes memory b) internal { - assertEq0(a, b); - } - - function assertEq(bytes memory a, bytes memory b, string memory err) internal { - assertEq0(a, b, err); - } - - function assertEq(uint256[] memory a, uint256[] memory b) internal { - if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) { - emit log("Error: a == b not satisfied [uint[]]"); - emit log_named_array(" Expected", b); - emit log_named_array(" Actual", a); - fail(); - } - } - - function assertEq(int256[] memory a, int256[] memory b) internal { - if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) { - emit log("Error: a == b not satisfied [int[]]"); - emit log_named_array(" Expected", b); - emit log_named_array(" Actual", a); - fail(); - } - } - - function assertEq(address[] memory a, address[] memory b) internal { - if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) { - emit log("Error: a == b not satisfied [address[]]"); - emit log_named_array(" Expected", b); - emit log_named_array(" Actual", a); - fail(); - } - } - - function assertEq(uint256[] memory a, uint256[] memory b, string memory err) internal { - if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) { - emit log_named_string("Error", err); - assertEq(a, b); - } - } - - function assertEq(int256[] memory a, int256[] memory b, string memory err) internal { - if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) { - emit log_named_string("Error", err); - assertEq(a, b); - } - } - - - function assertEq(address[] memory a, address[] memory b, string memory err) internal { - if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) { - emit log_named_string("Error", err); - assertEq(a, b); - } - } - - function assertApproxEqAbs( - uint256 a, - uint256 b, - uint256 maxDelta - ) internal virtual { - uint256 delta = stdMath.delta(a, b); - - if (delta > maxDelta) { - emit log ("Error: a ~= b not satisfied [uint]"); - emit log_named_uint (" Expected", b); - emit log_named_uint (" Actual", a); - emit log_named_uint (" Max Delta", maxDelta); - emit log_named_uint (" Delta", delta); - fail(); - } - } - - function assertApproxEqAbs( - uint256 a, - uint256 b, - uint256 maxDelta, - string memory err - ) internal virtual { - uint256 delta = stdMath.delta(a, b); - - if (delta > maxDelta) { - emit log_named_string ("Error", err); - assertApproxEqAbs(a, b, maxDelta); - } - } - - function assertApproxEqAbs( - int256 a, - int256 b, - uint256 maxDelta - ) internal virtual { - uint256 delta = stdMath.delta(a, b); - - if (delta > maxDelta) { - emit log ("Error: a ~= b not satisfied [int]"); - emit log_named_int (" Expected", b); - emit log_named_int (" Actual", a); - emit log_named_uint (" Max Delta", maxDelta); - emit log_named_uint (" Delta", delta); - fail(); - } - } - - function assertApproxEqAbs( - int256 a, - int256 b, - uint256 maxDelta, - string memory err - ) internal virtual { - uint256 delta = stdMath.delta(a, b); - - if (delta > maxDelta) { - emit log_named_string ("Error", err); - assertApproxEqAbs(a, b, maxDelta); - } - } - - function assertApproxEqRel( - uint256 a, - uint256 b, - uint256 maxPercentDelta // An 18 decimal fixed point number, where 1e18 == 100% - ) internal virtual { - if (b == 0) return assertEq(a, b); // If the expected is 0, actual must be too. - - uint256 percentDelta = stdMath.percentDelta(a, b); - - if (percentDelta > maxPercentDelta) { - emit log ("Error: a ~= b not satisfied [uint]"); - emit log_named_uint (" Expected", b); - emit log_named_uint (" Actual", a); - emit log_named_decimal_uint (" Max % Delta", maxPercentDelta, 18); - emit log_named_decimal_uint (" % Delta", percentDelta, 18); - fail(); - } - } - - function assertApproxEqRel( - uint256 a, - uint256 b, - uint256 maxPercentDelta, // An 18 decimal fixed point number, where 1e18 == 100% - string memory err - ) internal virtual { - if (b == 0) return assertEq(a, b); // If the expected is 0, actual must be too. - - uint256 percentDelta = stdMath.percentDelta(a, b); - - if (percentDelta > maxPercentDelta) { - emit log_named_string ("Error", err); - assertApproxEqRel(a, b, maxPercentDelta); - } - } - - function assertApproxEqRel( - int256 a, - int256 b, - uint256 maxPercentDelta - ) internal virtual { - if (b == 0) return assertEq(a, b); // If the expected is 0, actual must be too. - - uint256 percentDelta = stdMath.percentDelta(a, b); - - if (percentDelta > maxPercentDelta) { - emit log ("Error: a ~= b not satisfied [int]"); - emit log_named_int (" Expected", b); - emit log_named_int (" Actual", a); - emit log_named_decimal_uint(" Max % Delta", maxPercentDelta, 18); - emit log_named_decimal_uint(" % Delta", percentDelta, 18); - fail(); - } - } - - function assertApproxEqRel( - int256 a, - int256 b, - uint256 maxPercentDelta, - string memory err - ) internal virtual { - if (b == 0) return assertEq(a, b); // If the expected is 0, actual must be too. - - uint256 percentDelta = stdMath.percentDelta(a, b); - - if (percentDelta > maxPercentDelta) { - emit log_named_string ("Error", err); - assertApproxEqRel(a, b, maxPercentDelta); - } - } -} - -/*////////////////////////////////////////////////////////////////////////// - STD-ERRORS -//////////////////////////////////////////////////////////////////////////*/ - -library stdError { - bytes public constant assertionError = abi.encodeWithSignature("Panic(uint256)", 0x01); - bytes public constant arithmeticError = abi.encodeWithSignature("Panic(uint256)", 0x11); - bytes public constant divisionError = abi.encodeWithSignature("Panic(uint256)", 0x12); - bytes public constant enumConversionError = abi.encodeWithSignature("Panic(uint256)", 0x21); - bytes public constant encodeStorageError = abi.encodeWithSignature("Panic(uint256)", 0x22); - bytes public constant popError = abi.encodeWithSignature("Panic(uint256)", 0x31); - bytes public constant indexOOBError = abi.encodeWithSignature("Panic(uint256)", 0x32); - bytes public constant memOverflowError = abi.encodeWithSignature("Panic(uint256)", 0x41); - bytes public constant zeroVarError = abi.encodeWithSignature("Panic(uint256)", 0x51); - // DEPRECATED: Use Vm's `expectRevert` without any arguments instead - bytes public constant lowLevelError = bytes(""); // `0x` -} - -/*////////////////////////////////////////////////////////////////////////// - STD-STORAGE -//////////////////////////////////////////////////////////////////////////*/ - -struct StdStorage { - mapping (address => mapping(bytes4 => mapping(bytes32 => uint256))) slots; - mapping (address => mapping(bytes4 => mapping(bytes32 => bool))) finds; - - bytes32[] _keys; - bytes4 _sig; - uint256 _depth; - address _target; - bytes32 _set; + Vm internal constant vm = Vm(VM_ADDRESS); } -library stdStorage { - event SlotFound(address who, bytes4 fsig, bytes32 keysHash, uint slot); - event WARNING_UninitedSlot(address who, uint slot); - - uint256 private constant UINT256_MAX = 115792089237316195423570985008687907853269984665640564039457584007913129639935; - int256 private constant INT256_MAX = 57896044618658097711785492504343953926634992332820282019728792003956564819967; - - Vm private constant vm_std_store = Vm(address(uint160(uint256(keccak256('hevm cheat code'))))); - - function sigs( - string memory sigStr - ) - internal - pure - returns (bytes4) - { - return bytes4(keccak256(bytes(sigStr))); - } - - /// @notice find an arbitrary storage slot given a function sig, input data, address of the contract and a value to check against - // slot complexity: - // if flat, will be bytes32(uint256(uint)); - // if map, will be keccak256(abi.encode(key, uint(slot))); - // if deep map, will be keccak256(abi.encode(key1, keccak256(abi.encode(key0, uint(slot))))); - // if map struct, will be bytes32(uint256(keccak256(abi.encode(key1, keccak256(abi.encode(key0, uint(slot)))))) + structFieldDepth); - function find( - StdStorage storage self - ) - internal - returns (uint256) - { - address who = self._target; - bytes4 fsig = self._sig; - uint256 field_depth = self._depth; - bytes32[] memory ins = self._keys; - - // calldata to test against - if (self.finds[who][fsig][keccak256(abi.encodePacked(ins, field_depth))]) { - return self.slots[who][fsig][keccak256(abi.encodePacked(ins, field_depth))]; - } - bytes memory cald = abi.encodePacked(fsig, flatten(ins)); - vm_std_store.record(); - bytes32 fdat; - { - (, bytes memory rdat) = who.staticcall(cald); - fdat = bytesToBytes32(rdat, 32*field_depth); - } - - (bytes32[] memory reads, ) = vm_std_store.accesses(address(who)); - if (reads.length == 1) { - bytes32 curr = vm_std_store.load(who, reads[0]); - if (curr == bytes32(0)) { - emit WARNING_UninitedSlot(who, uint256(reads[0])); - } - if (fdat != curr) { - require(false, "stdStorage find(StdStorage): Packed slot. This would cause dangerous overwriting and currently isn't supported."); - } - emit SlotFound(who, fsig, keccak256(abi.encodePacked(ins, field_depth)), uint256(reads[0])); - self.slots[who][fsig][keccak256(abi.encodePacked(ins, field_depth))] = uint256(reads[0]); - self.finds[who][fsig][keccak256(abi.encodePacked(ins, field_depth))] = true; - } else if (reads.length > 1) { - for (uint256 i = 0; i < reads.length; i++) { - bytes32 prev = vm_std_store.load(who, reads[i]); - if (prev == bytes32(0)) { - emit WARNING_UninitedSlot(who, uint256(reads[i])); - } - // store - vm_std_store.store(who, reads[i], bytes32(hex"1337")); - bool success; - bytes memory rdat; - { - (success, rdat) = who.staticcall(cald); - fdat = bytesToBytes32(rdat, 32*field_depth); - } - - if (success && fdat == bytes32(hex"1337")) { - // we found which of the slots is the actual one - emit SlotFound(who, fsig, keccak256(abi.encodePacked(ins, field_depth)), uint256(reads[i])); - self.slots[who][fsig][keccak256(abi.encodePacked(ins, field_depth))] = uint256(reads[i]); - self.finds[who][fsig][keccak256(abi.encodePacked(ins, field_depth))] = true; - vm_std_store.store(who, reads[i], prev); - break; - } - vm_std_store.store(who, reads[i], prev); - } - } else { - require(false, "stdStorage find(StdStorage): No storage use detected for target."); - } - - require(self.finds[who][fsig][keccak256(abi.encodePacked(ins, field_depth))], "stdStorage find(StdStorage): Slot(s) not found."); - - delete self._target; - delete self._sig; - delete self._keys; - delete self._depth; - - return self.slots[who][fsig][keccak256(abi.encodePacked(ins, field_depth))]; - } - - function target(StdStorage storage self, address _target) internal returns (StdStorage storage) { - self._target = _target; - return self; - } - - function sig(StdStorage storage self, bytes4 _sig) internal returns (StdStorage storage) { - self._sig = _sig; - return self; - } - - function sig(StdStorage storage self, string memory _sig) internal returns (StdStorage storage) { - self._sig = sigs(_sig); - return self; - } - - function with_key(StdStorage storage self, address who) internal returns (StdStorage storage) { - self._keys.push(bytes32(uint256(uint160(who)))); - return self; - } - - function with_key(StdStorage storage self, uint256 amt) internal returns (StdStorage storage) { - self._keys.push(bytes32(amt)); - return self; - } - function with_key(StdStorage storage self, bytes32 key) internal returns (StdStorage storage) { - self._keys.push(key); - return self; - } - - function depth(StdStorage storage self, uint256 _depth) internal returns (StdStorage storage) { - self._depth = _depth; - return self; - } - - function checked_write(StdStorage storage self, address who) internal { - checked_write(self, bytes32(uint256(uint160(who)))); - } - - function checked_write(StdStorage storage self, uint256 amt) internal { - checked_write(self, bytes32(amt)); - } - - function checked_write(StdStorage storage self, bool write) internal { - bytes32 t; - /// @solidity memory-safe-assembly - assembly { - t := write - } - checked_write(self, t); - } - - function checked_write( - StdStorage storage self, - bytes32 set - ) internal { - address who = self._target; - bytes4 fsig = self._sig; - uint256 field_depth = self._depth; - bytes32[] memory ins = self._keys; - - bytes memory cald = abi.encodePacked(fsig, flatten(ins)); - if (!self.finds[who][fsig][keccak256(abi.encodePacked(ins, field_depth))]) { - find(self); - } - bytes32 slot = bytes32(self.slots[who][fsig][keccak256(abi.encodePacked(ins, field_depth))]); - - bytes32 fdat; - { - (, bytes memory rdat) = who.staticcall(cald); - fdat = bytesToBytes32(rdat, 32*field_depth); - } - bytes32 curr = vm_std_store.load(who, slot); - - if (fdat != curr) { - require(false, "stdStorage find(StdStorage): Packed slot. This would cause dangerous overwriting and currently isn't supported."); - } - vm_std_store.store(who, slot, set); - delete self._target; - delete self._sig; - delete self._keys; - delete self._depth; - } - - function read(StdStorage storage self) private returns (bytes memory) { - address t = self._target; - uint256 s = find(self); - return abi.encode(vm_std_store.load(t, bytes32(s))); - } - - function read_bytes32(StdStorage storage self) internal returns (bytes32) { - return abi.decode(read(self), (bytes32)); - } - - - function read_bool(StdStorage storage self) internal returns (bool) { - int256 v = read_int(self); - if (v == 0) return false; - if (v == 1) return true; - revert("stdStorage read_bool(StdStorage): Cannot decode. Make sure you are reading a bool."); - } - - function read_address(StdStorage storage self) internal returns (address) { - return abi.decode(read(self), (address)); - } - - function read_uint(StdStorage storage self) internal returns (uint256) { - return abi.decode(read(self), (uint256)); - } - - function read_int(StdStorage storage self) internal returns (int256) { - return abi.decode(read(self), (int256)); - } - - function bytesToBytes32(bytes memory b, uint offset) public pure returns (bytes32) { - bytes32 out; - - uint256 max = b.length > 32 ? 32 : b.length; - for (uint i = 0; i < max; i++) { - out |= bytes32(b[offset + i] & 0xFF) >> (i * 8); - } - return out; - } - - function flatten(bytes32[] memory b) private pure returns (bytes memory) - { - bytes memory result = new bytes(b.length * 32); - for (uint256 i = 0; i < b.length; i++) { - bytes32 k = b[i]; - /// @solidity memory-safe-assembly - assembly { - mstore(add(result, add(32, mul(32, i))), k) - } - } - - return result; - } -} - -/*////////////////////////////////////////////////////////////////////////// - STD-MATH -//////////////////////////////////////////////////////////////////////////*/ - -library stdMath { - int256 private constant INT256_MIN = -57896044618658097711785492504343953926634992332820282019728792003956564819968; - - function abs(int256 a) internal pure returns (uint256) { - // Required or it will fail when `a = type(int256).min` - if (a == INT256_MIN) - return 57896044618658097711785492504343953926634992332820282019728792003956564819968; - - return uint256(a > 0 ? a : -a); - } - - function delta(uint256 a, uint256 b) internal pure returns (uint256) { - return a > b - ? a - b - : b - a; - } - - function delta(int256 a, int256 b) internal pure returns (uint256) { - // a and b are of the same sign - // this works thanks to two's complement, the left-most bit is the sign bit - if ((a ^ b) > -1) { - return delta(abs(a), abs(b)); - } - - // a and b are of opposite signs - return abs(a) + abs(b); - } - - function percentDelta(uint256 a, uint256 b) internal pure returns (uint256) { - uint256 absDelta = delta(a, b); - - return absDelta * 1e18 / b; - } - - function percentDelta(int256 a, int256 b) internal pure returns (uint256) { - uint256 absDelta = delta(a, b); - uint256 absB = abs(b); - - return absDelta * 1e18 / absB; - } -} +abstract contract Test is TestBase, DSTest, Assertions, Cheats, Utils {} diff --git a/src/Utils.sol b/src/Utils.sol new file mode 100644 index 00000000..0694f568 --- /dev/null +++ b/src/Utils.sol @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.6.0 <0.9.0; + +import "./console2.sol"; + +contract Utils { + uint256 internal constant UINT256_MAX = + 115792089237316195423570985008687907853269984665640564039457584007913129639935; + + function bound(uint256 x, uint256 min, uint256 max) internal view virtual returns (uint256 result) { + require(min <= max, "Test bound(uint256,uint256,uint256): Max is less than min."); + + uint256 size = max - min; + + if (size == 0) { + result = min; + } else if (size == UINT256_MAX) { + result = x; + } else { + ++size; // make `max` inclusive + uint256 mod = x % size; + result = min + mod; + } + + console2.log("Bound Result", result); + } + + /// @dev Compute the address a contract will be deployed at for a given deployer address and nonce + /// @notice adapated from Solmate implementation (https://github.com/Rari-Capital/solmate/blob/main/src/utils/LibRLP.sol) + function computeCreateAddress(address deployer, uint256 nonce) internal pure virtual returns (address) { + // The integer zero is treated as an empty byte string, and as a result it only has a length prefix, 0x80, computed via 0x80 + 0. + // A one byte integer uses its own value as its length prefix, there is no additional "0x80 + length" prefix that comes before it. + if (nonce == 0x00) return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xd6), bytes1(0x94), deployer, bytes1(0x80)))); + if (nonce <= 0x7f) return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xd6), bytes1(0x94), deployer, uint8(nonce)))); + + // Nonces greater than 1 byte all follow a consistent encoding scheme, where each value is preceded by a prefix of 0x80 + length. + if (nonce <= 2**8 - 1) return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xd7), bytes1(0x94), deployer, bytes1(0x81), uint8(nonce)))); + if (nonce <= 2**16 - 1) return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xd8), bytes1(0x94), deployer, bytes1(0x82), uint16(nonce)))); + if (nonce <= 2**24 - 1) return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xd9), bytes1(0x94), deployer, bytes1(0x83), uint24(nonce)))); + + // More details about RLP encoding can be found here: https://eth.wiki/fundamentals/rlp + // 0xda = 0xc0 (short RLP prefix) + 0x16 (length of: 0x94 ++ proxy ++ 0x84 ++ nonce) + // 0x94 = 0x80 + 0x14 (0x14 = the length of an address, 20 bytes, in hex) + // 0x84 = 0x80 + 0x04 (0x04 = the bytes length of the nonce, 4 bytes, in hex) + // We assume nobody can have a nonce large enough to require more than 32 bytes. + return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xda), bytes1(0x94), deployer, bytes1(0x84), uint32(nonce)))); + } + + function computeCreate2Address(bytes32 salt, bytes32 initcodeHash, address deployer) internal pure returns (address) { + return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xff), deployer, salt, initcodeHash))); + } + + function addressFromLast20Bytes(bytes32 bytesValue) internal pure virtual returns (address) { + return address(uint160(uint256(bytesValue))); + } +} \ No newline at end of file diff --git a/src/console2.sol b/src/console2.sol index 2edfda5b..cdde3d71 100644 --- a/src/console2.sol +++ b/src/console2.sol @@ -1,12 +1,11 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.4.22 <0.9.0; -// The orignal console.sol uses `int` and `uint` for computing function selectors, but it should -// use `int256` and `uint256`. This modified version fixes that. This version is recommended -// over `console.sol` if you don't need compatibility with Hardhat as the logs will show up in -// forge stack traces. If you do need compatibility with Hardhat, you must use `console.sol`. -// Reference: https://github.com/NomicFoundation/hardhat/issues/2178 - +/// @dev The orignal console.sol uses `int` and `uint` for computing function selectors, but it should +/// use `int256` and `uint256`. This modified version fixes that. This version is recommended +/// over `console.sol` if you don't need compatibility with Hardhat as the logs will show up in +/// forge stack traces. If you do need compatibility with Hardhat, you must use `console.sol`. +/// Reference: https://github.com/NomicFoundation/hardhat/issues/2178 library console2 { address constant CONSOLE_ADDRESS = address(0x000000000000000000636F6e736F6c652e6c6f67); diff --git a/src/test/Script.t.sol b/src/test/Script.t.sol deleted file mode 100644 index 595df7ba..00000000 --- a/src/test/Script.t.sol +++ /dev/null @@ -1,12 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity >=0.7.0 <0.9.0; - -import "../Test.sol"; - -contract ScriptTest is Test -{ - function testGenerateCorrectAddress() external { - address creation = computeCreateAddress(0x6C9FC64A53c1b71FB3f9Af64d1ae3A4931A5f4E9, 14); - assertEq(creation, 0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45); - } -} \ No newline at end of file diff --git a/src/test/StdAssertions.t.sol b/src/test/StdAssertions.t.sol index db90720a..beee6d23 100644 --- a/src/test/StdAssertions.t.sol +++ b/src/test/StdAssertions.t.sol @@ -3,8 +3,7 @@ pragma solidity >=0.7.0 <0.9.0; import "../Test.sol"; -contract StdAssertionsTest is Test -{ +contract StdAssertionsTest is Test { string constant CUSTOM_ERROR = "guh!"; bool constant EXPECT_PASS = false; @@ -443,8 +442,7 @@ contract StdAssertionsTest is Test } -contract TestTest is Test -{ +contract TestTest is Test { modifier expectFailure(bool expectFail) { bool preState = vm.load(HEVM_ADDRESS, bytes32("failed")) != bytes32(0x00); _; diff --git a/src/test/StdCheats.t.sol b/src/test/StdCheats.t.sol index 2b49e9e7..c891fedb 100644 --- a/src/test/StdCheats.t.sol +++ b/src/test/StdCheats.t.sol @@ -1,6 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.7.0 <0.9.0; +import "../Cheats.sol"; import "../Test.sol"; contract StdCheatsTest is Test { @@ -86,48 +87,6 @@ contract StdCheatsTest is Test { assertEq(barToken.totalSupply(), 10000e18); } - function testBound() public { - assertEq(bound(5, 0, 4), 0); - assertEq(bound(0, 69, 69), 69); - assertEq(bound(0, 68, 69), 68); - assertEq(bound(10, 150, 190), 160); - assertEq(bound(300, 2800, 3200), 3100); - assertEq(bound(9999, 1337, 6666), 6006); - } - - function testCannotBoundMaxLessThanMin() public { - vm.expectRevert(bytes("Test bound(uint256,uint256,uint256): Max is less than min.")); - bound(5, 100, 10); - } - - function testBound( - uint256 num, - uint256 min, - uint256 max - ) public { - if (min > max) (min, max) = (max, min); - - uint256 bounded = bound(num, min, max); - - assertGe(bounded, min); - assertLe(bounded, max); - } - - function testBoundUint256Max() public { - assertEq(bound(0, type(uint256).max - 1, type(uint256).max), type(uint256).max - 1); - assertEq(bound(1, type(uint256).max - 1, type(uint256).max), type(uint256).max); - } - - function testCannotBoundMaxLessThanMin( - uint256 num, - uint256 min, - uint256 max - ) public { - vm.assume(min > max); - vm.expectRevert(bytes("Test bound(uint256,uint256,uint256): Max is less than min.")); - bound(num, min, max); - } - function testDeployCode() public { address deployed = deployCode("StdCheats.t.sol:StdCheatsTest", bytes("")); assertEq(string(getCode(deployed)), string(getCode(address(this)))); @@ -138,19 +97,19 @@ contract StdCheatsTest is Test { assertEq(string(getCode(deployed)), string(getCode(address(this)))); } - // We need that payable constructor in order to send ETH on construction + // We need the payable constructor in order to send ETH on construction constructor() payable {} function testDeployCodeVal() public { address deployed = deployCode("StdCheats.t.sol:StdCheatsTest", bytes(""), 1 ether); assertEq(string(getCode(deployed)), string(getCode(address(this)))); - assertEq(deployed.balance, 1 ether); + assertEq(deployed.balance, 1 ether); } function testDeployCodeValNoArgs() public { address deployed = deployCode("StdCheats.t.sol:StdCheatsTest", 1 ether); assertEq(string(getCode(deployed)), string(getCode(address(this)))); - assertEq(deployed.balance, 1 ether); + assertEq(deployed.balance, 1 ether); } // We need this so we can call "this.deployCode" rather than "deployCode" directly diff --git a/src/test/StdError.t.sol b/src/test/StdError.t.sol index 0d6601e4..56bcf2a3 100644 --- a/src/test/StdError.t.sol +++ b/src/test/StdError.t.sol @@ -1,6 +1,7 @@ // SPDX-License-Identifier: MIT -pragma solidity >=0.8.10 <0.9.0; +pragma solidity >=0.8.0 <0.9.0; +import "../Errors.sol"; import "../Test.sol"; contract StdErrorsTest is Test { @@ -59,11 +60,6 @@ contract StdErrorsTest is Test { vm.expectRevert(stdError.zeroVarError); test.intern(); } - - function testExpectLowLvl() public { - vm.expectRevert(stdError.lowLevelError); - test.someArr(0); - } } contract ErrorsTest { diff --git a/src/test/StdMath.t.sol b/src/test/StdMath.t.sol index 9d09b810..23189047 100644 --- a/src/test/StdMath.t.sol +++ b/src/test/StdMath.t.sol @@ -1,10 +1,10 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.8.0 <0.9.0; +import "../Math.sol"; import "../Test.sol"; -contract StdMathTest is Test -{ +contract StdMathTest is Test { function testGetAbs() external { assertEq(stdMath.abs(-50), 50); assertEq(stdMath.abs(50), 50); diff --git a/src/test/StdStorage.t.sol b/src/test/StdStorage.t.sol index 6e238d04..1b828f8c 100644 --- a/src/test/StdStorage.t.sol +++ b/src/test/StdStorage.t.sol @@ -1,12 +1,13 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.7.0 <0.9.0; +import "../Storage.sol"; import "../Test.sol"; contract StdStorageTest is Test { using stdStorage for StdStorage; - StorageTest test; + StorageTest internal test; function setUp() public { test = new StorageTest(); @@ -296,7 +297,7 @@ contract StorageTest { uint256 two = (1<<128) | 1; map_packed[msg.sender] = two; - map_packed[address(bytes20(uint160(1337)))] = 1<<128; + map_packed[address(uint160(1337))] = 1<<128; } function read_struct_upper(address who) public view returns (uint256) { diff --git a/src/test/StdUtils.t.sol b/src/test/StdUtils.t.sol new file mode 100644 index 00000000..9e825692 --- /dev/null +++ b/src/test/StdUtils.t.sol @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.7.0 <0.9.0; + +import "../Test.sol"; + +contract StdUtilsTest is Test { + function testBound() public { + assertEq(bound(5, 0, 4), 0); + assertEq(bound(0, 69, 69), 69); + assertEq(bound(0, 68, 69), 68); + assertEq(bound(10, 150, 190), 160); + assertEq(bound(300, 2800, 3200), 3100); + assertEq(bound(9999, 1337, 6666), 6006); + } + + function testBound( + uint256 num, + uint256 min, + uint256 max + ) public { + if (min > max) (min, max) = (max, min); + + uint256 bounded = bound(num, min, max); + + assertGe(bounded, min); + assertLe(bounded, max); + } + + function testBoundUint256Max() public { + assertEq(bound(0, type(uint256).max - 1, type(uint256).max), type(uint256).max - 1); + assertEq(bound(1, type(uint256).max - 1, type(uint256).max), type(uint256).max); + } + + + function testCannotBoundMaxLessThanMin() public { + vm.expectRevert(bytes("Test bound(uint256,uint256,uint256): Max is less than min.")); + bound(5, 100, 10); + } + + function testCannotBoundMaxLessThanMin( + uint256 num, + uint256 min, + uint256 max + ) public { + vm.assume(min > max); + vm.expectRevert(bytes("Test bound(uint256,uint256,uint256): Max is less than min.")); + bound(num, min, max); + } + + function testGenerateCreateAddress() external { + address deployer = 0x6C9FC64A53c1b71FB3f9Af64d1ae3A4931A5f4E9; + uint256 nonce = 14; + address createAddress = computeCreateAddress(deployer, nonce); + assertEq(createAddress, 0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45); + } + + function testGenerateCreate2Address() external { + bytes32 salt = bytes32(uint256(31415)); + bytes32 initcodeHash = keccak256(abi.encode(0x6080)); + address deployer = 0x6C9FC64A53c1b71FB3f9Af64d1ae3A4931A5f4E9; + address create2Address = computeCreate2Address(salt, initcodeHash, deployer); + assertEq(create2Address, 0xB147a5d25748fda14b463EB04B111027C290f4d3); + } +} \ No newline at end of file