Open code423n4 opened 2 years ago
Only marginal gas improvements.
Not convinced this will save gas as you're still dealing with storage pointers
Saves 20g
Same as 1, caching a storage pointer won't save gas
Same as 1
Don't think it makes much difference
3 5 2 = 30
This would save 20 gas
Would save 6 gas
Total Gas Saved 76
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);
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;