Platform: Code4rena
Start Date: 20/01/2022
Pot Size: $80,000 USDC
Total HM: 5
Participants: 37
Period: 7 days
Judge: Jack the Pug
Total Solo HM: 1
Id: 76
League: ETH
Rank: 19/37
Findings: 1
Award: $224.51
🌟 Selected for report: 2
🚀 Solo Findings: 0
🌟 Selected for report: RamiRond
Also found by: Dravee, Ruhum, kirk-baird, p4st13r4
RamiRond
Gas saving.
Reading from storage (SLOAD) is quite expensive, so it makes sense to cache these values in memory if they are needed multiple times in the same scope. I verified most of these findings with hardhat-gas-reporter
; with the projects optimizer settings this saves at least 140 gas for each call and occurence. It can be applied to the mentioned state variables in the following functions:
SherlockClaimManager.sol
claim
in lines 197 and 198claimCallbacks.length
claim.timestamp
Sherlock.sol
yieldStrategy
yieldStrategy
, sherDistributionManager
, sherlockProtocolManager
, sherlockClaimManager
sherDistributionManager
vscode, solidity visual auditor, hardhat-gas-reporter
Use a local memory variable as cache to avoid SLOADs.
🌟 Selected for report: RamiRond
RamiRond
Gas saving.
An assignment also returns the new value, so it can be processed further. This can be used with storage variables to save gas. According to hardhat-gas-reporter
it's about 110 gas per call and occurence with the projects optimizer settings.
This can impair readability in many cases, for example in Sherlock.sol::enableStakingPeriod:
function enableStakingPeriod(uint256 _period) public override onlyOwner { if (_period == 0) revert ZeroArgument(); if (!(stakingPeriods[_period] = !stakingPeriods[_period])) revert InvalidArgument(); emit StakingPeriodEnabled(_period); }
In other cases, however, the impact is less severe. For example, Sherlock.sol::updateSherDistributionManager could be changed to this:
function updateSherDistributionManager(ISherDistributionManager _sherDistributionManager) external override onlyOwner { if (address(_sherDistributionManager) == address(0)) revert ZeroArgument(); if (sherDistributionManager == _sherDistributionManager) revert InvalidArgument(); emit SherDistributionManagerUpdated(sherDistributionManager, sherDistributionManager = _sherDistributionManager); }
Here's a list of cases where impact on readability would be acceptable:
Sherlock.sol
vscode, solidity visual auditor, hardhat-gas-reporter
Inline assignments to save gas.