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

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

Gas Optimizations #69

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

Comments

@code423n4
Copy link
Contributor

++I/I++ SHOULD BE UNCHECKED{++I}/UNCHECKED{++I} WHEN IT IS NOT POSSIBLE FOR THEM TO OVERFLOW, AS IS THE CASE WHEN USED IN FOR- AND WHILE-LOOPS

1- File: contracts/AxelarGateway.sol (line 195)

        for (uint256 i; i < adminCount; ++i) {

2- File: contracts/AxelarGateway.sol (line 207)

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

3- File: contracts/AxelarGateway.sol (line 292)

        for (uint256 i; i < commandsLength; ++i) {

4- File: contracts/gas-service/AxelarGasService.sol (line 123)

        for (uint256 i; i < tokens.length; i++) {

5- File: contracts/deposit-service/AxelarDepositService.sol (line 114)

        for (uint256 i; i < refundTokens.length; i++) {

6- File: contracts/deposit-service/AxelarDepositService.sol (line 168)

        for (uint256 i; i < refundTokens.length; i++) {

7- File: contracts/deposit-service/AxelarDepositService.sol (line 204)

        for (uint256 i; i < refundTokens.length; i++) {

8- File: contracts/auth/AxelarAuthWeighted.sol (line 17)

        for (uint256 i; i < recentOperators.length; ++i) {

9- File: contracts/auth/AxelarAuthWeighted.sol (line 69)

        for (uint256 i = 0; i < weightsLength; ++i) {

10- File: contracts/auth/AxelarAuthWeighted.sol (line 98)

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

11- File: contracts/auth/AxelarAuthWeighted.sol (line 116)

        for (uint256 i; i < accounts.length - 1; ++i) {

++I COSTS LESS GAS THAN I++, ESPECIALLY WHEN IT’S USED IN FOR-LOOPS (--I/I-- TOO)

1- File: contracts/AxelarGateway.sol (line 207)

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

2- File: contracts/gas-service/AxelarGasService.sol (line 123)

        for (uint256 i; i < tokens.length; i++) {

3- File: contracts/deposit-service/AxelarDepositService.sol (line 114)

        for (uint256 i; i < refundTokens.length; i++) {

4- File: contracts/deposit-service/AxelarDepositService.sol (line 168)

        for (uint256 i; i < refundTokens.length; i++) {

5- File: contracts/deposit-service/AxelarDepositService.sol (line 204)

        for (uint256 i; i < refundTokens.length; i++) {

<ARRAY>.LENGTH SHOULD NOT BE LOOKED UP IN EVERY LOOP OF A FOR-LOOP

Even memory arrays incur the overhead of bit tests and bit shifts to calculate the array length

1- File: contracts/AxelarGateway.sol (line 207)

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

2- File: contracts/gas-service/AxelarGasService.sol (line 123)

        for (uint256 i; i < tokens.length; i++) {

3- File: contracts/deposit-service/AxelarDepositService.sol (line 114)

        for (uint256 i; i < refundTokens.length; i++) {

4- File: contracts/deposit-service/AxelarDepositService.sol (line 168)

        for (uint256 i; i < refundTokens.length; i++) {

5- File: contracts/deposit-service/AxelarDepositService.sol (line 204)

        for (uint256 i; i < refundTokens.length; i++) {

6- File: contracts/auth/AxelarAuthWeighted.sol (line 17)

        for (uint256 i; i < recentOperators.length; ++i) {

7- File: contracts/auth/AxelarAuthWeighted.sol (line 98)

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

8- File: contracts/auth/AxelarAuthWeighted.sol (line 116)

        for (uint256 i; i < accounts.length - 1; ++i) {

IT COSTS MORE GAS TO INITIALIZE NON-CONSTANT/NON-IMMUTABLE VARIABLES TO ZERO THAN TO LET THE DEFAULT OF ZERO BE APPLIED

Not overwriting the default for stack variables saves 8 gas. Storage and memory variables have larger savings

1- File: contracts/AxelarGateway.sol (line 207)

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

2- File: contracts/auth/AxelarAuthWeighted.sol (line 69)

        for (uint256 i = 0; i < weightsLength; ++i) {

3- File: contracts/auth/AxelarAuthWeighted.sol (line 98)

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

<X> += <Y> COSTS MORE GAS THAN <X> = <X> + <Y> FOR STATE VARIABLES

There are 2 instances of this issue:

File: contracts/auth/AxelarAuthWeighted.sol

70:             totalWeight += newWeights[i];

105:           weight += weights[operatorIndex];

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

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

re1ro commented Aug 5, 2022

Dup #2

@GalloDaSballo
Copy link
Collaborator

Less than 300

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