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

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

Gas Optimizations #64

code423n4 opened this issue Aug 2, 2022 · 2 comments
Labels
bug Something isn't working G (Gas Optimization) sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons

Comments

@code423n4
Copy link
Contributor

Gas Optimization

++i costs less gas than i++ and i+=1 (--i/i--/i-=1 too)

    File: contracts/AxelarGateway.sol

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

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

    File: contracts/gas-service/AxelarGasService.sol

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

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

    File: contracts/deposit-service/AxelarDepositService.sol

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

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

    File: contracts/deposit-service/AxelarDepositService.sol

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

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

    File: contracts/deposit-service/AxelarDepositService.sol

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

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

Initializing a variable to its default value costs unnecessary gas.

    File: contracts/AxelarGateway.sol

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

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

    File: contracts/auth/AxelarAuthWeighted.sol

    uint256 totalWeight = 0;

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

    File: contracts/auth/AxelarAuthWeighted.sol

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

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

    File: contracts/auth/AxelarAuthWeighted.sol

    uint256 operatorIndex = 0;

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

    File: contracts/auth/AxelarAuthWeighted.sol

    uint256 weight = 0;

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

    File: contracts/auth/AxelarAuthWeighted.sol

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

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

Variable increment(e.g.++i/i++) for looping should be unchecked{++i} when they are not possible to overflow, to remove overflow checking to save gas.

    File: contracts/AxelarGateway.sol

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

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

    File: contracts/AxelarGateway.sol

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

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

    File: contracts/AxelarGateway.sol

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

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

    File: contracts/gas-service/AxelarGasService.sol

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

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

    File: contracts/deposit-service/AxelarDepositService.sol

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

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

    File: contracts/deposit-service/AxelarDepositService.sol

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

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

    File: contracts/deposit-service/AxelarDepositService.sol

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

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

    File: contracts/auth/AxelarAuthWeighted.sol

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

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

    File: contracts/auth/AxelarAuthWeighted.sol

        for (; operatorIndex < operatorsLength && signer != operators[operatorIndex]; ++operatorIndex) {}

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

    File: contracts/auth/AxelarAuthWeighted.sol

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

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

Array length should not be looked up in every loop. Instead, use a variable to store the length before the loop starts.

    File: contracts/AxelarGateway.sol

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

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

    File: contracts/AxelarGateway.sol

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

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

    File: contracts/deposit-service/AxelarDepositService.sol

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

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

    File: contracts/deposit-service/AxelarDepositService.sol

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

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

    File: contracts/deposit-service/AxelarDepositService.sol

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

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

    File: contracts/auth/AxelarAuthWeighted.sol

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

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

    File: contracts/auth/AxelarAuthWeighted.sol

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

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

Declare errors for revert, instead of using string, to reduce gas.

    File: xc20/contracts/XC20Wrapper.sol

    if (axelarToken == address(0)) revert('NotAxelarToken()');

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

    File: xc20/contracts/XC20Wrapper.sol

    if (xc20Token.codehash != xc20Codehash) revert('NotXc20Token()');

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

    File: xc20/contracts/XC20Wrapper.sol

    if (wrapped[axelarToken] != address(0)) revert('AlreadyWrappingAxelarToken()');

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

    File: xc20/contracts/XC20Wrapper.sol

    if (unwrapped[xc20Token] != address(0)) revert('AlreadyWrappingXC20Token()');

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

    File: xc20/contracts/XC20Wrapper.sol

    if (!LocalAsset(xc20Token).set_team(address(this), address(this), address(this))) revert('NotOwner()');

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

    File: xc20/contracts/XC20Wrapper.sol

    if (!LocalAsset(xc20Token).set_metadata(newName, newSymbol, IERC20(axelarToken).decimals())) revert('CannotSetMetadata()');

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

    File: xc20/contracts/XC20Wrapper.sol

    if (axelarToken == address(0)) revert('NotAxelarToken()');

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

    File: xc20/contracts/XC20Wrapper.sol

    if (xc20Token == address(0)) revert('NotWrappingToken()');

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

    File: xc20/contracts/XC20Wrapper.sol

    if (wrappedToken == address(0)) revert('NotAxelarToken()');

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

    File: xc20/contracts/XC20Wrapper.sol

    if (!LocalAsset(wrappedToken).mint(msg.sender, amount)) revert('CannotMint()');

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

    File: xc20/contracts/XC20Wrapper.sol

    if (axelarToken == address(0)) revert('NotXc20Token()');

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

    File: xc20/contracts/XC20Wrapper.sol

    if (IERC20(wrappedToken).balanceOf(msg.sender) < amount) revert('InsufficientBalance()');

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

    File: xc20/contracts/XC20Wrapper.sol

    if (!LocalAsset(wrappedToken).burn(msg.sender, amount)) revert('CannotBurn()');

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

    File: xc20/contracts/XC20Wrapper.sol

    if (!transferred || tokenAddress.code.length == 0) revert('TransferFailed()');

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

    File: xc20/contracts/XC20Wrapper.sol

    if (!transferred || tokenAddress.code.length == 0) revert('TransferFailed()');

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

Hardcode hash values instead of calculating the values with keccak256() in runtime, to reduce gas.

    File: contracts/gas-service/AxelarGasService.sol

    return keccak256('axelar-gas-service');

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

    File: contracts/deposit-service/AxelarDepositService.sol

    return keccak256('axelar-deposit-service');

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

    File: xc20/contracts/XC20Wrapper.sol
    
    return keccak256('xc20-wrapper');

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

@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

1, 2, 3, 4 : Dup #2

  1. Our targeted platform/compiler doesn't support custom errors.

  2. We prefer this for simplicity of verification. This function is only called on upgrades

@GalloDaSballo
Copy link
Collaborator

Less than 300 gas saved

For 6. you could use an hardcoded constant which would have the same usage but provide gas savings (the report that constants cost gas is false and has been debunked for ages)

@re1ro re1ro added the sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons label Aug 23, 2022
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) sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons
Projects
None yet
Development

No branches or pull requests

3 participants