Open code423n4 opened 2 years ago
Gas optimization for contract deployments is not our prior and 500K gas cost reduction is not that big relating to deployment all contracts, but good catch though.
Ultimately this will save a ton of deployment gas, for the sake of judging I'll rate it as if it saved 25 * 2500 (similar to the Messages Finding) 62500
Title
Massive gas reduction just by reusing common logic in library
Vulnerability details
Impact
Massive gas cost and contract size reductions. Since the total gas cost reduction is 518297 and the total size reduction is 2.397 KB, this report is shared as an individual report.
Proof of Concept
The function
keccak256(abi.encodePacked(...))
is called at many places in the codebase.Here are lists of files which uses
keccak256(abi.encodePacked(...))
function several times inmainnet
directory. By using the function in the library instead of calling `keccak256(abi.encodePacked(...))`` function reach time can reduce the gas cost hugely.Linker.sol and SkaleManagerClient.sol also use
keccak256(abi.encodePacked(...))
function, but they use only this function once, and in this case, using library increases the gas cost. So in this report, Linker.sol and SkaleManagerClient.sol keep usingkeccak256(abi.encodePacked(...))
function.Tools Used
To check the actual size of the reduction,
hardhat-gas-reporter
is used. ( https://www.npmjs.com/package/hardhat-gas-reporter ). At each result, it lists how many size of the gas is reduced after the change.Recommended Mitigation Steps
First, create library which contains function to convert
string calldata schainName
andstring memory schainName
intobytes32
usingkeccak256(abi.encodePacked(...))
function.Then use these functions in
HashLibrary
library in each codebase. Here is an example of the modification.Here are results of the gas reduction observed at the local environment.
Here are results of the contract size reduction at the local environment.
In total, the total contract size reduction is 2.397 KB after the change.
Here is an output of
git diff
that can reduce the above mentioned gas usage.https://gist.github.com/TerrierLover/9c80fe4099ea149b7e2fc559fa8a844e
The base commit of the above
git diff
output is https://github.com/skalenetwork/ima-c4-audit/commit/11d6a6ae5bf16af552edd75183791375e501915f .