Skip to content

Instantly share code, notes, and snippets.

@IllIllI000
Last active June 24, 2023 13:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save IllIllI000/ad1bd0d29a0101b25e57c293b4b0c746 to your computer and use it in GitHub Desktop.
Save IllIllI000/ad1bd0d29a0101b25e57c293b4b0c746 to your computer and use it in GitHub Desktop.

RequireVsCustomError_r.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4; // optimize 200

/**
 * @title RequireVsCustomError
 * @author IllIllI
 */
contract RequireVsCustomError {
    function test() public view {
        require(uint256(uint160(msg.sender)) > block.number, "Err");
    }
    function ignore() external pure {
        revert Err();
    }
    error Err();
}

RequireVsCustomError_c.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4; // optimize 200

/**
 * @title RequireVsCustomError
 * @author IllIllI
 */
contract RequireVsCustomError {
    function test() public view {
        if (uint256(uint160(msg.sender)) >= block.number) { revert Err(); }
    }
    function ignore() external pure {
        revert Err();
    }
    error Err();
}

Diff

diff --git a/RequireVsCustomError_r.sol b/RequireVsCustomError_c.sol
index 169d1d1..abf3a64 100644
--- a/RequireVsCustomError_r.sol
+++ b/RequireVsCustomError_c.sol
@@ -7,7 +7,7 @@ pragma solidity ^0.8.4; // optimize 200
  */
 contract RequireVsCustomError {
     function test() public view {
-        require(uint256(uint160(msg.sender)) > block.number, "Err");
+        if (uint256(uint160(msg.sender)) >= block.number) { revert Err(); }
     }
     function ignore() external pure {
         revert Err();

Compiled binary

$ solc RequireVsCustomError_r.sol --bin --optimize --optimize-runs 200 | egrep "^60[0-9]+"
608060405234801561001057600080fd5b5060ce8061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c8063ed6f8663146037578063f8a8fd6d14603f575b600080fd5b603d6045565b005b603d605e565b604051636327e1b960e11b815260040160405180910390fd5b43331160965760405162461bcd60e51b815260206004820152600360248201526222b93960e91b604482015260640160405180910390fd5b56fea2646970667358221220a2f3cf5bbd0c3c1806dba84640032c876a6b960c78f17f05c2197296dff4fb9a64736f6c63430008040033

$ solc RequireVsCustomError_c.sol --bin --optimize --optimize-runs 200 | egrep "^60[0-9]+"
608060405234801561001057600080fd5b5060b58061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c8063ed6f8663146037578063f8a8fd6d14603f575b600080fd5b603d6045565b005b603d605e565b604051636327e1b960e11b815260040160405180910390fd5b433310607d57604051636327e1b960e11b815260040160405180910390fd5b56fea26469706673582212209f434c6b7dd3b9c5a19edbc55b544e7832bdcb6f49110723cc881c0d9ca1d22b64736f6c63430008040033

Opcodes

PC.Req Operation.Req Gas.Req PC.Cus Operation.Cus Gas.Cus
126(0x7e) NUMBER(0x43) 2 126(0x7e) NUMBER(0x43) 2
127(0x7f) CALLER(0x33) 2 127(0x7f) CALLER(0x33) 2
128(0x80) LT(0x10) 3
128(0x80) GT(0x11) 3
129(0x81) PUSH1(0x60) ["0x96"] 3 129(0x81) PUSH1(0x60) ["0x7d"] 3
131(0x83) JUMPI(0x57) 10 131(0x83) JUMPI(0x57) 10
132(0x84) PUSH1(0x60) ["0x40"] 3 132(0x84) PUSH1(0x60) ["0x40"] 3
134(0x86) MLOAD(0x51) 3 134(0x86) MLOAD(0x51) 3
135(0x87) PUSH4(0x63) ["0x63","0x27",... 3
135(0x87) PUSH3(0x62) ["0x46","0x1b",... 3
139(0x8b) PUSH1(0x60) ["0xe5"] 3 140(0x8c) PUSH1(0x60) ["0xe1"] 3
141(0x8d) SHL(0x1b) 3 142(0x8e) SHL(0x1b) 3
142(0x8e) DUP2(0x81) 3 143(0x8f) DUP2(0x81) 3
143(0x8f) MSTORE(0x52) 3 144(0x90) MSTORE(0x52) 3
144(0x90) PUSH1(0x60) ["0x20"] 3 145(0x91) PUSH1(0x60) ["0x04"] 3
146(0x92) PUSH1(0x60) ["0x04"] 3
148(0x94) DUP3(0x82) 3
149(0x95) ADD(0x1) 3 147(0x93) ADD(0x1) 3
150(0x96) MSTORE(0x52) 3
151(0x97) PUSH1(0x60) ["0x03"] 3 148(0x94) PUSH1(0x60) ["0x40"] 3
153(0x99) PUSH1(0x60) ["0x24"] 3
155(0x9b) DUP3(0x82) 3
156(0x9c) ADD(0x1) 3
157(0x9d) MSTORE(0x52) 3
158(0x9e) PUSH3(0x62) ["0x22","0xb9",... 3
162(0xa2) PUSH1(0x60) ["0xe9"] 3
164(0xa4) SHL(0x1b) 3
165(0xa5) PUSH1(0x60) ["0x44"] 3
167(0xa7) DUP3(0x82) 3
168(0xa8) ADD(0x1) 3
169(0xa9) MSTORE(0x52) 3
170(0xaa) PUSH1(0x60) ["0x64"] 3
172(0xac) ADD(0x1) 3
173(0xad) PUSH1(0x60) ["0x40"] 3
175(0xaf) MLOAD(0x51) 3 150(0x96) MLOAD(0x51) 3
176(0xb0) DUP1(0x80) 3 151(0x97) DUP1(0x80) 3
177(0xb1) SWAP2(0x91) 3 152(0x98) SWAP2(0x91) 3
178(0xb2) SUB(0x3) 3 153(0x99) SUB(0x3) 3
179(0xb3) SWAP1(0x90) 3 154(0x9a) SWAP1(0x90) 3
180(0xb4) REVERT(0xfd) 0 155(0x9b) REVERT(0xfd) 0
181(0xb5) JUMPDEST(0x5b)@0xb5 1 156(0x9c) JUMPDEST(0x5b)@0x9c 1
182(0xb6) JUMP(0x56) 8 157(0x9d) JUMP(0x56) 8
Total.Req: 125 Total.Cus: 74

The change provides a savings of 51 gas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment