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

Open
code423n4 opened this issue Aug 3, 2022 · 2 comments
Open

Gas Optimizations #151

code423n4 opened this issue Aug 3, 2022 · 2 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

[G-01] Functions guaranteed to revert when called normal users can be marked payable

If a function modifier such as onlyOwner is used, the function will revert if a normal user tries to pay the function. Marking the function as payable will lower the gas cost for legitimate callers because the compiler will not include checks for whether a payment was provided.

There are 5 instances of this issue:

AxelarGasService.sol#L120
AxelarGasService.sol#L140
AxelarAuthWeighted.sol#L47
XC20Wrapper.sol#L44
XC20Wrapper.sol#L66

[G-02] <array>.length should not be looked up in every loop of a for-loop

The overheads outlined below are PER LOOP, excluding the first loop

  • storage arrays incur a Gwarmaccess (100 gas)
  • memory arrays use MLOAD (3 gas)
  • calldata arrays use CALLDATALOAD (3 gas)

Caching the length changes each of these to a DUP<N> (3 gas), and gets rid of the extra DUP<N> needed to store the stack offset

There are 7 instances of this issue:

AxelarGasService.sol#L123
AxelarDepositService.sol#L114
AxelarDepositService.sol#L168
AxelarDepositService.sol#L204
AxelarAuthWeighted.sol#L17
AxelarAuthWeighted.sol#L98
AxelarAuthWeighted.sol#L116

[G-03] ++i costs less gas than i++, especially when it's used in for-loops (--i/i-- too)

Saves 6 gas per loop.

There are 4 instances of this issue:

AxelarGasService.sol#L123
AxelarDepositService.sol#L114
AxelarDepositService.sol#L168
AxelarDepositService.sol#L204

[G-04] Increments can be unchecked

In Solidity 0.8+, there’s a default overflow check on unsigned integers. It’s possible to uncheck this in for-loops and save some gas at each iteration, but at the cost of some code readability, as this uncheck cannot be made inline.

ethereum/solidity#10695

There are 10 instances of this issue:

AxelarGasService.sol#L123
AxelarDepositService.sol#L114
AxelarDepositService.sol#L168
AxelarDepositService.sol#L204
AxelarAuthWeighted.sol#L17
AxelarAuthWeighted.sol#L69
AxelarAuthWeighted.sol#L98
AxelarAuthWeighted.sol#L101
AxelarAuthWeighted.sol#L109
AxelarAuthWeighted.sol#L116

[G-05] No need to explicitly initialize variables with default values

If a variable is not set/initialized, it is assumed to have the default value (0 for uint, false for bool, address(0) for address…). Explicitly initializing it with its default value is an anti-pattern and wastes gas.
As an example: for (uint256 i = 0; i < numIterations; ++i) { should be replaced with for (uint256 i; i < numIterations; ++i) {

There are 5 instances of this issue:

AxelarAuthWeighted.sol#L68-L69
AxelarAuthWeighted.sol#L94-L98

[G-06] x = x + y is cheaper than x += y

There are 2 instances of this issue:

AxelarAuthWeighted.sol#L70
AxelarAuthWeighted.sol#L105

[G-07] Usage of uints/ints smaller than 32 bytes (256 bits) incurs overhead

When using elements that are smaller than 32 bytes, your contract’s gas usage may be higher. This is because the EVM operates on 32 bytes at a time. Therefore, if the element is smaller than that, the EVM must use more operations in order to reduce the size of the element from 32 bytes to the desired size.

https://docs.soliditylang.org/en/v0.8.11/internals/layout_in_storage.html Use a larger size then downcast where needed

There is 1 instance of this issue:

AxelarAuthWeighted.sol#L14

[G-08] Use custom error rather than revert()/require() strings to save deployment gas

Custom errors are available from solidity version 0.8.4. The instances below match or exceed that version.

There are 15 instances of this issue

XC20Wrapper.sol
Search for revert

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Aug 3, 2022
code423n4 added a commit that referenced this issue Aug 3, 2022
@re1ro
Copy link
Member

re1ro commented Aug 5, 2022

Dup #2 #3 #13

@GalloDaSballo
Copy link
Collaborator

Less than 300 gas

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

3 participants