code-423n4 / 2022-02-skale-findings

0 stars 0 forks source link

Gas Optimizations #79

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

skalenetwork/ima-c4-audit gas optimization

1 use cache for storage in registerExtraContractForAll and removeExtraContractForAll.

https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/MessageProxy.sol#L174-L175 https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/MessageProxy.sol#L188-L189

EnumerableSetUpgradeable.AddressSet storage registryContracts = _getRegistryContracts()[bytes32(0)]; require(!registryContracts.contains(extraContract), "Extra contract is already registered"); registryContracts.add(extraContract);

EnumerableSetUpgradeable.AddressSet storage registryContracts = _getRegistryContracts()[bytes32(0)]; require(registryContracts.contains(extraContract), "Extra contract is not registered"); registryContracts.remove(extraContract);

2 use unchecked. from < to is already checked so you can use unchecked for the following line to save gas.

https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/MessageProxy.sol#L220

Unchecked { contractsInRange = new address[](to -from); }

3 use cache for storage in postOutgoingMessage.

https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/MessageProxy.sol#L291-L302

ConnectedChainInfo storage _connectedChain = connectedChains[targetChainHash]; require(_connectedChain.inited, "Destination chain is not initialized"); _authorizeOutgoingMessageSender(targetChainHash);

    emit OutgoingMessage(
        targetChainHash,
        _connectedChain.outgoingMessageCounter,
        msg.sender,
        targetContract,
        data
    );

    _connectedChain.outgoingMessageCounter += 1;

4 use cache for storage in _registerExtraContract and _removeExtraContract

https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/MessageProxy.sol#L365-L371 https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/MessageProxy.sol#L389-L390

5 Input must be checked earlier to save gas. The following line can be checked at the beginning of the function to save gas.

https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/schain/TokenManager.sol#L192

6 Use initial value for uint256 and ++i in loop in TokenManagerLinker.sol

https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/schain/TokenManagerLinker.sol#L122 https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/schain/TokenManagerLinker.sol#L151 https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/schain/TokenManagerLinker.sol#L166 https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/schain/TokenManagerLinker.sol#L178 https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/schain/TokenManagerLinker.sol#L192

7 use unchecked. balance < MINIMUM_BALANCE is already checked in if sentence, so you can use unchecked.

https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/schain/MessageProxyForSchain.sol#L404

uint missingAmount; unchecked { missingAmount = MINIMUM_BALANCE - balance; }

8 No need to use cache. _outgoingMessageDataHash[message.dstChainHash][message.msgCounter] Is used only one time in function so you don’t use cache

https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/schain/MessageProxyForSchain.sol#L253

if (_outgoingMessageDataHash[message.dstChainHash][message.msgCounter] == _hashOfMessage(message)) isValidMessage = true;

yavrsky commented 2 years ago

Only marginal gas improvements.

GalloDaSballo commented 2 years ago

1 use cache for storage in registerExtraContractForAll and removeExtraContractForAll.

Not convinced this will save gas as you're still dealing with storage pointers

2 use unchecked.

Saves 20g

3 use cache for storage in postOutgoingMessage.

Same as 1, caching a storage pointer won't save gas

4 use cache for storage in _registerExtraContract and _removeExtraContract

Same as 1

5 Input must be checked earlier to save gas.

Don't think it makes much difference

6 Use initial value for uint256 and ++i in loop in TokenManagerLinker.sol

3 5 2 = 30

7 use unchecked. balance < MINIMUM_BALANCE is already checked in if sentence, so you can use unchecked.

This would save 20 gas

8 No need to use cache. _outgoingMessageDataHash[message.dstChainHash][message.msgCounter]

Would save 6 gas

Total Gas Saved 76