Open code423n4 opened 2 years ago
Only marginal gas improvements.
This does save gas on deploy but will not save gas on tx
True, saved 3 gas
Would save deployment cost but increase each call by 8 gas
21 * 3 = 63
Agree, that would save 2100 * 2 = 4200 gas
Agree but hard to quantify in lack of math
Agree, saves 3 gas
Saves 6 gas
Disagree, length is cached which is necessary
Costs an extra 3 gas
Disagree as it changes the functionality
Total Gas saved: 4286
Gas opt
1 Using 1 modifier to check ROLE
https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/MessageProxy.sol#L116-L135 currently this contract is using 3 modifier to check which address is validated to run the function. Just use 1 modifier and pass which role will run the function then pass the address into the modifier argument:
2 Using && spend more gashttps://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/mainnet/DepositBox.sol#L77-L81
instead of using &&, using multiple
require
check can save execution gas fee:3 Using new private function to validate
connectedChains[value].inited
https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/MessageProxy.sol#L240 https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/MessageProxy.sol#L258 https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/MessageProxy.sol#L291 https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/MessageProxy.sol#L315 create a new private function to run:
than call the code 4 times can save 18.018 gas when deploying the contract:
4 better increment for saving more gas
Prefix increment are cheaper than postfix increment, its a common that using ++i instead i++ for compiler ^0.8.* .So this f() are not using prefix increments (++i) or using the unchecked (++i).
it can be seen from here : https://github.com/code-423n4/2022-01-xdefi-findings/issues/9 and
Occurance :
5 state variables that could be set as immutable
the
erc721ContractOnSchain
&receiverContractOnMainnet
could be set asimmutable
to save more gasOccurance :
6 Custom errors from Solidity 0.8.4 are cheaper than revert strings.
https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/MessageProxy.sol#L173 instead of using string to revert the error message, use
error
to declare the custom error, then replace the revert string with it.7 using ++var instead += 1
https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/MessageProxy.sol#L302 change l 302 to:
8 Using
storage
can save gashttps://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/Messages.sol#L208 instead of caching
message
in memory, read it fromstorage
can save gas. Themessage
var is just called once inl L 2139 unnecessary
length
declarationhttps://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/mainnet/Linker.sol#L173
length
var is just used once in the L 175. Its unnecessary to chace it in memory. Remove the line and call_mainnetContracts.length()
directly:10 Dont set uint i = 0
https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/mainnet/MessageProxyForMainnet.sol#L118 by just declaring
uint i;
without set the value = 0 can save deployment gas fee. Do it on allfor()
loop11 Set the default value on vars declaration
https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/mainnet/MessageProxyForMainnet.sol#L73-L74 by setting the value of
headerMessageGasCost
andmessageGasCost
on their declaration, can save gas without writing storage ininitialize()
: https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/mainnet/MessageProxyForMainnet.sol#L298-L299