code-423n4 / 2022-07-axelar-findings

0 stars 0 forks source link

Gas Optimizations #197

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

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

GalloDaSballo commented 2 years ago

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 commented 2 years ago

Will penalize this submission down to 100 gas

GalloDaSballo commented 2 years ago
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