Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #2

Open
code423n4 opened this issue Jul 12, 2022 · 0 comments
Open

Gas Optimizations #2

code423n4 opened this issue Jul 12, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

  1. Using Expression for constant values

Expressions for constant values such as a call to keccak256(), should use immutable rather than constant.

Code:
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/ethregistrar/BaseRegistrarImplementation.sol#L18-30
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/ethregistrar/LinearPremiumPriceOracle.sol#L14

Mitigation:
Make the variables immutable

  1. Splitting require() that use && can save gas

https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#L268

  1. Packed vs Unpacked structure
    In ethereum, you pay gas for every storage slot you use. A slot is of 256 bits, and you can pack as many variables as you want in it. Packing is done by solidity compiler and optimizer automatically, you just need to declare the packable functions consecutively.

Code:
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#L104
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#L186
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#L205
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#L72-82

Mitigation:
The above structure can be packed as

struct SignedSet {
uint8 algorithm;
uint8 labels;
uint16 typeCovered;
uint16 keytag;
uint32 ttl;
uint32 expiration;
uint32 inception;
bytes signerName;
bytes data;
bytes name;
}

  1. Use of bytes32
    Declaring variables of bytes32 type than byte is more cheaper.
    Code:
    https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#L72-82

  2. Use of custom error
    Starting from Solidity v0.8.4, there is a convenient and gas-efficient way to explain to users why an operation failed through the use of custom errors. Until now, you could already use strings to give more information about failures (e.g., revert("Insufficient funds.");), but they are rather expensive, especially when it comes to deploy cost, and it is difficult to use dynamic information in them.

Code references:
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#L12
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#L146
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#L159
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#L172
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#L185
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#L199-200
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#L235
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#L262
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#L268-270
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#L307

Mitigation:
Replace require with revert ERROR()

Mitigation:
replace require with if (a != b) revert ERROR()

  1. Every variable assignment in Solidity costs gas. When initializing variables, we often waste gas by assigning default values that will never be used.. uint need not be initialized to 0 , since default is 0

Code:
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/BytesUtils.sol#L264
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#L50
https://github.com/code-423n4/2022-07-ens/blob/main/contracts/dnssec-oracle/RRUtils.sol#L63

  1. Function visibility
    Decscription:
    For all the public functions, the input parameters are copied to memory automatically, and it costs gas. If your function is only called externally, then you should explicitly mark it as external. External function’s parameters are not copied into memory but are read from calldata directly. This small optimization in your solidity code can save you a lot of gas when the function input parameters are huge.

Code:
latestAnswer() should be declared external:
- DummyOracle.latestAnswer() (contracts/ethregistrar/DummyOracle.sol#14-16)
transferOwnership(address) should be declared external:
- Ownable.transferOwnership(address) (contracts/root/Ownable.sol#18-21)
uri(uint256) should be declared external:
- StaticMetadataService.uri(uint256) (contracts/wrapper/StaticMetadataService.sol#11-13)
setAlgorithm(uint8,Algorithm) should be declared external:
- DNSSECImpl.setAlgorithm(uint8,Algorithm) (contracts/dnssec-oracle/DNSSECImpl.sol#58-61)
setDigest(uint8,Digest) should be declared external:
- DNSSECImpl.setDigest(uint8,Digest) (contracts/dnssec-oracle/DNSSECImpl.sol#69-72)
setOwner(address) should be declared external:
- Owned.setOwner(address) (contracts/dnssec-oracle/Owned.sol#18-20)
commit(bytes32) should be declared external:
- ETHRegistrarController.commit(bytes32) (contracts/ethregistrar/ETHRegistrarController.sol#120-123)
register(string,address,uint256,bytes32,address,bytes[],bool,uint32,uint64) should be declared external:
- ETHRegistrarController.register(string,address,uint256,bytes32,address,bytes[],bool,uint32,uint64) (contracts/ethregistrar/ETHRegistrarController.sol#125-187)
withdraw() should be declared external:
- ETHRegistrarController.withdraw() (contracts/ethregistrar/ETHRegistrarController.sol#210-212)
setResolver(bytes32,address) should be declared external:
- ENSRegistry.setResolver(bytes32,address) (contracts/registry/ENSRegistry.sol#86-89)
setTTL(bytes32,uint64) should be declared external:
- ENSRegistry.setTTL(bytes32,uint64) (contracts/registry/ENSRegistry.sol#96-99)
register(bytes32,address) should be declared external:
- FIFSRegistrar.register(bytes32,address) (contracts/registry/FIFSRegistrar.sol#33-35)
setName(bytes32,string) should be declared external:
- NameResolver.setName(bytes32,string) (contracts/registry/ReverseRegistrar.sol#9)
setDefaultResolver(address) should be declared external:
- ReverseRegistrar.setDefaultResolver(address) (contracts/registry/ReverseRegistrar.sol#53-59)
claim(address) should be declared external:
- ReverseRegistrar.claim(address) (contracts/registry/ReverseRegistrar.sol#67-69)
claimWithResolver(address,address) should be declared external:
- ReverseRegistrar.claimWithResolver(address,address) (contracts/registry/ReverseRegistrar.sol#99-105)
setName(string) should be declared external:
- ReverseRegistrar.setName(string) (contracts/registry/ReverseRegistrar.sol#114-122)
node(address) should be declared external:
- ReverseRegistrar.node(address) (contracts/registry/ReverseRegistrar.sol#150-155)
register(bytes32,address) should be declared external:
- TestRegistrar.register(bytes32,address) (contracts/registry/TestRegistrar.sol#31-36)
dnsRecord(bytes32,bytes32,uint16) should be declared external:
- DNSResolver.dnsRecord(bytes32,bytes32,uint16) (contracts/resolvers/profiles/DNSResolver.sol#90-92)
hasDNSRecords(bytes32,bytes32) should be declared external:
- DNSResolver.hasDNSRecords(bytes32,bytes32) (contracts/resolvers/profiles/DNSResolver.sol#99-101)
clearDNSZone(bytes32) should be declared external:
- DNSResolver.clearDNSZone(bytes32) (contracts/resolvers/profiles/DNSResolver.sol#107-110)
setController(address,bool) should be declared external:
- Controllable.setController(address,bool) (contracts/root/Controllable.sol#18-21)
encodeName(string) should be declared external:
- TestNameEncoder.encodeName(string) (contracts/utils/TestNameEncoder.sol#9-15)
balanceOfBatch(address[],uint256[]) should be declared external:
- ERC1155Fuse.balanceOfBatch(address[],uint256[]) (contracts/wrapper/ERC1155Fuse.sol#78-97)
setApprovalForAll(address,bool) should be declared external:
- ERC1155Fuse.setApprovalForAll(address,bool) (contracts/wrapper/ERC1155Fuse.sol#102-114)
safeTransferFrom(address,address,uint256,uint256,bytes) should be declared external:
- ERC1155Fuse.safeTransferFrom(address,address,uint256,uint256,bytes) (contracts/wrapper/ERC1155Fuse.sol#169-183)
safeBatchTransferFrom(address,address,uint256[],uint256[],bytes) should be declared external:
- ERC1155Fuse.safeBatchTransferFrom(address,address,uint256[],uint256[],bytes) (contracts/wrapper/ERC1155Fuse.sol#188-232)
setMetadataService(IMetadataService) should be declared external:
- NameWrapper.setMetadataService(IMetadataService) (contracts/wrapper/NameWrapper.sol#105-110)
uri(uint256) should be declared external:
- NameWrapper.uri(uint256) (contracts/wrapper/NameWrapper.sol#117-119)
setUpgradeContract(INameWrapperUpgrade) should be declared external:
- NameWrapper.setUpgradeContract(INameWrapperUpgrade) (contracts/wrapper/NameWrapper.sol#128-143)
wrapETH2LD(string,address,uint32,uint64,address) should be declared external:
- NameWrapper.wrapETH2LD(string,address,uint32,uint64,address) (contracts/wrapper/NameWrapper.sol#210-237)
wrap(bytes,address,address) should be declared external:
- NameWrapper.wrap(bytes,address,address) (contracts/wrapper/NameWrapper.sol#295-325)
unwrapETH2LD(bytes32,address,address) should be declared external:
- NameWrapper.unwrapETH2LD(bytes32,address,address) (contracts/wrapper/NameWrapper.sol#335-346)
unwrap(bytes32,bytes32,address) should be declared external:
- NameWrapper.unwrap(bytes32,bytes32,address) (contracts/wrapper/NameWrapper.sol#356-365)
upgradeETH2LD(string,address,address) should be declared external:
- NameWrapper.upgradeETH2LD(string,address,address) (contracts/wrapper/NameWrapper.sol#401-417)
upgrade(bytes32,string,address,address) should be declared external:
- NameWrapper.upgrade(bytes32,string,address,address) (contracts/wrapper/NameWrapper.sol#429-447)
setRecord(bytes32,address,address,uint64) should be declared external:
- NameWrapper.setRecord(bytes32,address,address,uint64) (contracts/wrapper/NameWrapper.sol#584-601)
setResolver(bytes32,address) should be declared external:
- NameWrapper.setResolver(bytes32,address) (contracts/wrapper/NameWrapper.sol#609-616)
setTTL(bytes32,uint64) should be declared external:
- NameWrapper.setTTL(bytes32,uint64) (contracts/wrapper/NameWrapper.sol#624-631)
onERC721Received(address,address,uint256,bytes) should be declared external:
- NameWrapper.onERC721Received(address,address,uint256,bytes) (contracts/wrapper/NameWrapper.sol#693-725)

Mitigation:
Make above function external

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jul 12, 2022
code423n4 added a commit that referenced this issue Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

1 participant