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

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

Gas Optimizations #197

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

Comments

@code423n4
Copy link
Contributor

#1 Cache the authModule.code.length and tokenDeployerImplementation.code.length

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/AxelarGateway.sol#L49-L50

cache the authModule.code.length and tokenDeployerImplementation.code.length to the memory can reduce the gas fee. because mload is cheaper than sload.

#2 Cache limits.length

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/AxelarGateway.sol#L205

cache the limits.length to the memory can reduce the gas fee. because mload is cheaper than sload.

#3 cache symbols.length

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/AxelarGateway.sol#L205-L207

cache the symbols.length because it use multiple times. read the mload are cheaper than sload

#4 Looping

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/AxelarGateway.sol#L205-L209

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/gas-service/AxelarGasService.sol#L123

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/deposit-service/AxelarDepositService.sol#L115

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/deposit-service/AxelarDepositService.sol#L204

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/auth/AxelarAuthWeighted.sol#L17

default uint is 0 so remove unnecassary explicit can reduce gas.
caching the array length can reduce gas it caused access to a local variable is more cheap than query storage / calldata / memory in solidity.
pre increment e.g ++i more cheaper gas than post increment e.g i++. i suggest to use pre increment.

#5 Cache setupParams.length

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/AxelarGateway.sol#L228

cache the setupParams.length to the memory can reduce the gas fee. because mload is cheaper than sload.

#6 Use calldata instead memory

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/auth/AxelarAuthWeighted.sol#L55

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/deposit-service/AxelarDepositService.sol#L220

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/AxelarGateway.sol#L447

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/AxelarGateway.sol#L271-L273

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/AxelarGateway.sol#L277-L279

In the external functions where the function argument is read-only, the function() has an inputed parameter that using memory, if this function didnt change the parameter, its cheaper to use calldata then memory. so we suggest to change it

#7 Cache commands.length and params.length

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/AxelarGateway.sol#L290

cache the commands.length and params.length to the memory can reduce the gas fee. because mload is cheaper than sload.

#8 Use storage instead memory

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/deposit-service/DepositBase.sol#L30

Use storage instead of memory to reduce the gas fee. i suggest to change this.

#9 Cache symbolBytes.length

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/deposit-service/DepositBase.sol#L32

cache the symbolBytes.length to the memory can reduce the gas fee. because mload is cheaper than sload.

#10 inefficient code Epoch

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/auth/AxelarAuthWeighted.sol#L78-L79

inefficient in coding epoch just change this to saving more gas

    uint256 epoch = currentEpoch + 1;
    currentEpoch = epoch;

to

    uint256 currentEpoch += 1;

#11 Default uint

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/contracts/auth/AxelarAuthWeighted.sol#L94-L95

the default value of uint is 0, so remove unnecassary explicit code initializations for default values e.g uint i = 0; to uint i;.

#12 Caching LocalAsset(xc20Token)

https://github.com/code-423n4/2022-07-axelar/blob/9c4c44b94cddbd48b9baae30051a4e13cbe39539/xc20/contracts/XC20Wrapper.sol#L61-L62

cache the LocalAsset(xc20Token) because it use multiple times. read the mload are cheaper than sload

@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
@GalloDaSballo
Copy link
Collaborator

Cache the authModule.code.length and tokenDeployerImplementation.code.length

This has got to be a bot submission, the length is the length of the code

300 for the loops, rest is honestly just wrong

@GalloDaSballo
Copy link
Collaborator

Will penalize this submission down to 100 gas

@GalloDaSballo
Copy link
Collaborator

Screenshot 2022-08-25 at 03 42 36

Like maybe you actually wrote this one, however it's wrong as the variable is actually from memory and doesn't save an SLOAD

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