From 805a0dfd2a84fe7b4e9edf86d49f4baa70ca624d Mon Sep 17 00:00:00 2001 From: Daksh Date: Fri, 28 Oct 2022 10:55:34 +0530 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20chore:=20convert=20req?= =?UTF-8?q?uire=20to=20custom=20errors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 16 ++++++++-------- src/utils/SSTORE2.sol | 7 ++++--- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index b8e332a8..1e71db9c 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -325,20 +325,20 @@ RolesAuthorityTest:testSetRoleCapabilities(uint8,address,bytes4) (runs: 256, μ: RolesAuthorityTest:testSetRoles() (gas: 29005) RolesAuthorityTest:testSetRoles(address,uint8) (runs: 256, μ: 29122, ~: 29108) SSTORE2Test:testFailReadInvalidPointer() (gas: 2927) -SSTORE2Test:testFailReadInvalidPointer(address,bytes) (runs: 256, μ: 3889, ~: 3892) -SSTORE2Test:testFailReadInvalidPointerCustomBounds() (gas: 3099) -SSTORE2Test:testFailReadInvalidPointerCustomBounds(address,uint256,uint256,bytes) (runs: 256, μ: 4107, ~: 4130) +SSTORE2Test:testFailReadInvalidPointer(address,bytes) (runs: 256, μ: 3879, ~: 3892) +SSTORE2Test:testFailReadInvalidPointerCustomBounds() (gas: 3033) +SSTORE2Test:testFailReadInvalidPointerCustomBounds(address,uint256,uint256,bytes) (runs: 256, μ: 4050, ~: 4067) SSTORE2Test:testFailReadInvalidPointerCustomStartBound() (gas: 3004) -SSTORE2Test:testFailReadInvalidPointerCustomStartBound(address,uint256,bytes) (runs: 256, μ: 3980, ~: 3988) -SSTORE2Test:testFailWriteReadCustomBoundsOutOfRange(bytes,uint256,uint256,bytes) (runs: 256, μ: 46236, ~: 43603) +SSTORE2Test:testFailReadInvalidPointerCustomStartBound(address,uint256,bytes) (runs: 256, μ: 3979, ~: 3988) +SSTORE2Test:testFailWriteReadCustomBoundsOutOfRange(bytes,uint256,uint256,bytes) (runs: 256, μ: 46175, ~: 43540) SSTORE2Test:testFailWriteReadCustomStartBoundOutOfRange(bytes,uint256,bytes) (runs: 256, μ: 46017, ~: 43452) -SSTORE2Test:testFailWriteReadEmptyOutOfBounds() (gas: 34470) -SSTORE2Test:testFailWriteReadOutOfBounds() (gas: 34426) +SSTORE2Test:testFailWriteReadEmptyOutOfBounds() (gas: 34407) +SSTORE2Test:testFailWriteReadOutOfBounds() (gas: 34363) SSTORE2Test:testFailWriteReadOutOfStartBound() (gas: 34362) SSTORE2Test:testWriteRead() (gas: 53497) SSTORE2Test:testWriteRead(bytes,bytes) (runs: 256, μ: 44019, ~: 41555) SSTORE2Test:testWriteReadCustomBounds() (gas: 34869) -SSTORE2Test:testWriteReadCustomBounds(bytes,uint256,uint256,bytes) (runs: 256, μ: 29324, ~: 44495) +SSTORE2Test:testWriteReadCustomBounds(bytes,uint256,uint256,bytes) (runs: 256, μ: 28531, ~: 41976) SSTORE2Test:testWriteReadCustomStartBound() (gas: 34740) SSTORE2Test:testWriteReadCustomStartBound(bytes,uint256,bytes) (runs: 256, μ: 46484, ~: 44053) SSTORE2Test:testWriteReadEmptyBound() (gas: 34677) diff --git a/src/utils/SSTORE2.sol b/src/utils/SSTORE2.sol index 23d69803..d2f109bc 100644 --- a/src/utils/SSTORE2.sol +++ b/src/utils/SSTORE2.sol @@ -6,7 +6,8 @@ pragma solidity >=0.8.0; /// @author Modified from 0xSequence (https://github.com/0xSequence/sstore2/blob/master/contracts/SSTORE2.sol) library SSTORE2 { uint256 internal constant DATA_OFFSET = 1; // We skip the first byte as it's a STOP opcode to ensure the contract can't be called. - + error DeploymentFail(); + error OutOfBounds(); /*////////////////////////////////////////////////////////////// WRITE LOGIC //////////////////////////////////////////////////////////////*/ @@ -41,7 +42,7 @@ library SSTORE2 { pointer := create(0, add(creationCode, 32), mload(creationCode)) } - require(pointer != address(0), "DEPLOYMENT_FAILED"); + if(pointer == address(0)) revert DeploymentFail(); } /*////////////////////////////////////////////////////////////// @@ -66,7 +67,7 @@ library SSTORE2 { start += DATA_OFFSET; end += DATA_OFFSET; - require(pointer.code.length >= end, "OUT_OF_BOUNDS"); + if(pointer.code.length < end) revert OutOfBounds(); return readBytecode(pointer, start, end - start); } From 4105c061d2e2fcdb3dc0684ba94486baa0d5b4d1 Mon Sep 17 00:00:00 2001 From: Daksh Date: Fri, 28 Oct 2022 11:03:03 +0530 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=8E=A8=20chore:=20run=20linter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/SSTORE2.sol | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/utils/SSTORE2.sol b/src/utils/SSTORE2.sol index d2f109bc..811ee8e5 100644 --- a/src/utils/SSTORE2.sol +++ b/src/utils/SSTORE2.sol @@ -8,6 +8,7 @@ library SSTORE2 { uint256 internal constant DATA_OFFSET = 1; // We skip the first byte as it's a STOP opcode to ensure the contract can't be called. error DeploymentFail(); error OutOfBounds(); + /*////////////////////////////////////////////////////////////// WRITE LOGIC //////////////////////////////////////////////////////////////*/ @@ -42,7 +43,7 @@ library SSTORE2 { pointer := create(0, add(creationCode, 32), mload(creationCode)) } - if(pointer == address(0)) revert DeploymentFail(); + if (pointer == address(0)) revert DeploymentFail(); } /*////////////////////////////////////////////////////////////// @@ -67,7 +68,7 @@ library SSTORE2 { start += DATA_OFFSET; end += DATA_OFFSET; - if(pointer.code.length < end) revert OutOfBounds(); + if (pointer.code.length < end) revert OutOfBounds(); return readBytecode(pointer, start, end - start); } From 921182f5d1c9c23c87729dea551f970d59434714 Mon Sep 17 00:00:00 2001 From: Daksh Kulshrestha <20741933+dawksh@users.noreply.github.com> Date: Sun, 30 Oct 2022 23:17:13 +0530 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20feat:=20custom=20error?= =?UTF-8?q?s=20for=20CREATE3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 10 +++++----- src/utils/CREATE3.sol | 7 +++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 1e71db9c..43d76860 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -28,11 +28,11 @@ AuthTest:testTransferOwnershipWithPermissiveAuthority(address,address) (runs: 25 Bytes32AddressLibTest:testFillLast12Bytes() (gas: 223) Bytes32AddressLibTest:testFromLast20Bytes() (gas: 191) CREATE3Test:testDeployERC20() (gas: 853111) -CREATE3Test:testDeployERC20(bytes32,string,string,uint8) (runs: 256, μ: 922796, ~: 921961) -CREATE3Test:testFailDoubleDeployDifferentBytecode() (gas: 9079256848778914174) -CREATE3Test:testFailDoubleDeployDifferentBytecode(bytes32,bytes,bytes) (runs: 256, μ: 5062195514745832485, ~: 8937393460516727435) -CREATE3Test:testFailDoubleDeploySameBytecode() (gas: 9079256848778906218) -CREATE3Test:testFailDoubleDeploySameBytecode(bytes32,bytes) (runs: 256, μ: 5027837975401088878, ~: 8937393460516728677) +CREATE3Test:testDeployERC20(bytes32,string,string,uint8) (runs: 256, μ: 923485, ~: 921961) +CREATE3Test:testFailDoubleDeployDifferentBytecode() (gas: 9079256848778914128) +CREATE3Test:testFailDoubleDeployDifferentBytecode(bytes32,bytes,bytes) (runs: 256, μ: 5062195514745832424, ~: 8937393460516727378) +CREATE3Test:testFailDoubleDeploySameBytecode() (gas: 9079256848778906162) +CREATE3Test:testFailDoubleDeploySameBytecode(bytes32,bytes) (runs: 256, μ: 5062749668606232028, ~: 8937393460516728632) DSTestPlusTest:testBound() (gas: 14214) DSTestPlusTest:testBound(uint256,uint256,uint256) (runs: 256, μ: 2787, ~: 2793) DSTestPlusTest:testBrutalizeMemory() (gas: 823) diff --git a/src/utils/CREATE3.sol b/src/utils/CREATE3.sol index 0d5b341e..7751f30c 100644 --- a/src/utils/CREATE3.sol +++ b/src/utils/CREATE3.sol @@ -7,6 +7,9 @@ import {Bytes32AddressLib} from "./Bytes32AddressLib.sol"; /// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/CREATE3.sol) /// @author Modified from 0xSequence (https://github.com/0xSequence/create3/blob/master/contracts/Create3.sol) library CREATE3 { + error DeploymentFailed(); + error InitializationFailed(); + using Bytes32AddressLib for bytes32; //--------------------------------------------------------------------------------// @@ -48,11 +51,11 @@ library CREATE3 { // We start 32 bytes into the code to avoid copying the byte length. proxy := create2(0, add(proxyChildBytecode, 32), mload(proxyChildBytecode), salt) } - require(proxy != address(0), "DEPLOYMENT_FAILED"); + if (proxy == address(0)) revert DeploymentFailed(); deployed = getDeployed(salt); (bool success, ) = proxy.call{value: value}(creationCode); - require(success && deployed.code.length != 0, "INITIALIZATION_FAILED"); + if (success && deployed.code.length == 0) revert InitializationFailed(); } function getDeployed(bytes32 salt) internal view returns (address) {