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 Feb 24, 2022 · 1 comment
Open

Gas Optimizations #2

code423n4 opened this issue Feb 24, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Title: Rearrange state variables
Severity: GAS

You can change the order of the storage variables to decrease memory uses.

In CrossAnchorBridge.sol,rearranging the storage fields can optimize to: 3 slots from: 4 slots.
The new order of types (you choose the actual variables):

    1. bytes32
    2. address
    3. uint32
    4. uint32
    5. uint16
    6. uint8
    7. uint8
    8. address
    9. uint8
    10. uint8
    11. uint8
    12. uint8
    13. uint8
    14. uint8
    15. uint8
    16. uint8
    17. uint8
    18. uint8

Title: uint8 index
Severity: GAS

Due to how the EVM natively works on 256 numbers, using a 8 bit number here introduces additional costs as the EVM has to properly enforce the limits of this smaller type.
See the warning at this link: https://docs.soliditylang.org/en/v0.8.0/internals/layout_in_storage.html#layout-of-state-variables-in-storage
We recommend to use uint256 for the index in every for loop instead using uint8:

    CrossAnchorBridge.sol, uint8 i, 149

Title: Public functions to external
Severity: GAS

The following functions could be set external to save gas and improve code quality.
External call cost is less expensive than of public functions.

    CrossAnchorBridge.sol, initialize

Title: Internal functions to private
Severity: GAS

The following functions could be set private to save gas and improve code quality:

    CrossAnchorBridge.sol, handleToken
    CrossAnchorBridge.sol, encodeAddress
    CrossAnchorBridge.sol, handleStableToken
    CrossAnchorBridge.sol, _authorizeUpgrade

Title: Unnecessary index init
Severity: GAS

In for loops you initialize the index to start from 0, but it already initialized to 0 in default and this assignment cost gas.
It is more clear and gas efficient to declare without assigning 0 and will have the same meaning:

    CrossAnchorBridge.sol, 149

Title: Use calldata instead of memory
Severity: GAS

Use calldata instead of memory for function parameters
In some cases, having function arguments in calldata instead of
memory is more optimal.

    CrossAnchorBridge.processTokenTransferInstruction (encodedIncomingTokenTransferInfo)
    CrossAnchorBridge.processTokenTransferInstruction (encodedTokenTransfer)

Title: Unused state variables
Severity: GAS

Unused state variables are gas consuming at deployment (since they are located in storage) and are
a bad code practice. Removing those variables will decrease deployment gas cost and improve code quality.
This is a full list of all the unused storage variables we found in your code base.

    CrossAnchorBridge.sol, OP_CODE_CLAIM_REWARDS
    CrossAnchorBridge.sol, FLAG_NO_ASSC_TRANSFER

Title: Prefix increments are cheaper than postfix increments
Severity: GAS

Prefix increments are cheaper than postfix increments.
Further more, using unchecked {++x} is even more gas efficient, and the gas saving accumulates every iteration and can make a real change
There is no risk of overflow caused by increamenting the iteration index in for loops (the ++i in for (uint256 i = 0; i < numIterations; ++i)).
But increments perform overflow checks that are not necessary in this case.
These functions use not using prefix increments (++x) or not using the unchecked keyword:

    change to prefix increment and unchecked: CrossAnchorBridge.sol, i, 149

Title: Caching array length can save gas
Severity: GAS

Caching the array length is more gas efficient.
This is because access to a local variable in solidity is more efficient than query storage / calldata / memory.
We recommend to change from:

for (uint256 i=0; i<array.length; i++) { ... }

to:

uint len = array.length  
for (uint256 i=0; i<len; i++) { ... }


    CrossAnchorBridge.sol, _collateralTokens, 149

Title: Consider inline the following functions to save gas
Severity: GAS

You can inline the following functions instead of writing a specific function to save gas.
(see https://github.com/code-423n4/2021-11-nested-findings/issues/167 for a similar issue.)


    CrossAnchorBridge.sol, encodeAddress, { return bytes32(uint256(uint160(addr))); }
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Feb 24, 2022
code423n4 added a commit that referenced this issue Feb 24, 2022
@code423n4 code423n4 mentioned this issue Mar 9, 2022
@GalloDaSballo
Copy link
Collaborator

Most likely less than 100 gas saved at runtime

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

2 participants