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 #192

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

Gas Optimizations #192

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

Comments

@code423n4
Copy link
Contributor

Gas report

[G-01] Avoid using SafeMath on solidity v8 and newer

The SafeMath library validates if an arithmetic operation would result in an integer overflow/underflow. If it would, the library throws an exception, effectively reverting the transaction.

Since Solidity 0.8, the overflow/underflow check is implemented on the language level - it adds the validation to the bytecode during compilation.

You don't need the SafeMath library for Solidity 0.8+

[G-02] On contract DNSSEC.sol the variable anchors should be immutable

[G-03] On BaseRegistrarImplementation ENS and basenode should be immutable

[G-04] Use unchecked in ethregistrar/StringUtils.sol

In the strlen function use unchecked to save gas:

    function strlen(string memory s) internal pure returns (uint) {
        uint len;
        uint i = 0;
        uint bytelength = bytes(s).length;
+       unchecked{
        for(len = 0; i < bytelength; len++) {
            bytes1 b = bytes(s)[i];
            if(b < 0x80) {
                i += 1;
            } else if (b < 0xE0) {
                i += 2;
            } else if (b < 0xF0) {
                i += 3;
            } else if (b < 0xF8) {
                i += 4;
            } else if (b < 0xFC) {
                i += 5;
            } else {
                i += 6;
            }
        }
+       }
        return len;
    }

[G-05] Take in mind you shoul only use unchecked when its safe to increment without overflow, like in loops iterator.

Use this pattern;

for (uint256 i; i < length;) {
    // code here
    unchecked { ++i; }
}

On

ontracts/dnssec-oracle/BytesUtils.sol:266:        for(uint i = 0; i < len; i++) {
contracts/dnssec-oracle/BytesUtils.sol:313:        for(uint256 idx = off; idx < off + len; idx++) {
contracts/dnssec-oracle/DNSSECImpl.sol:93:        for(uint i = 0; i < input.length; i++) {
contracts/dnssec-oracle/algorithms/EllipticCurve.sol:304:        for(uint i = 0; i < exp; i++) {
contracts/ethregistrar/BulkRenewal.sol:41:        for (uint256 i = 0; i < names.length; i++) {
contracts/ethregistrar/BulkRenewal.sol:56:        for (uint256 i = 0; i < names.length; i++) {
contracts/ethregistrar/ETHRegistrarController.sol:256:        for (uint256 i = 0; i < data.length; i++) {
contracts/ethregistrar/StringUtils.sol:14:        for(len = 0; i < bytelength; len++) {
contracts/resolvers/Multicallable.sol:10:        for(uint i = 0; i < data.length; i++) {
contracts/wrapper/ERC1155Fuse.sol:92:        for (uint256 i = 0; i < accounts.length; ++i) {
contracts/wrapper/ERC1155Fuse.sol:205:        for (uint256 i = 0; i < ids.length; ++i) {

[G-06] Use unchecked in resolvers/profiles/DNSResolver.sol

Change

contracts/resolvers/profiles/DNSResolver.sol:108:        versions[node]++;
contracts/resolvers/profiles/DNSResolver.sol:159:                nameEntriesCount[node][version][nameHash]++;

To

contracts/resolvers/profiles/DNSResolver.sol:108:        unchecked { versions[node]++; }
contracts/resolvers/profiles/DNSResolver.sol:159:        unchecked {        nameEntriesCount[node][version][nameHash]++; }

[L-07] The withdraw function should be external

The withdraw function of ETHRegistrarController.sol should be external

[G-08] There are two contracts with the same funcionality

Avoid using Owned onstead of that use the Ownable contract from openzepellin as you do with other contracts

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jul 19, 2022
code423n4 added a commit that referenced this issue Jul 19, 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