The liquidation process in liquidateAccounts
repeatedly reads certain storage variables inside the loop for each account processed. This can result in higher gas consumption when multiple accounts are processed in a single transaction, indicating an area where gas optimization improvements can be made.
Within the loop inside the liquidateAccounts
function, repeated storage reads occur as part of the liquidation eligibility check. For instance, the liquidation fee stored in the protocol configuration is accessed each time an account is processed:
Since ctx.liquidationFeeUsdX18
(derived from the protocol configuration) is a constant value during the transaction, fetching this value repeatedly from storage increases the overall gas cost. This inefficiency is particularly relevant when processing a large number of accounts during a liquidation event.
Increased Gas Costs: Repeated storage reads result in higher gas consumption per transaction, reducing the economic efficiency of bulk liquidations.
Scalability Concerns: As the number of accounts processed increases, the cumulative gas cost rise may become significant, impacting the protocol's performance during high-stress liquidation events.
Operational Efficiency: Extra gas costs may deter liquidators from processing large batches, potentially delaying necessary liquidations.
Manual Code Review: A focused review of the liquidateAccounts
function identified repeated storage reads in critical loops.
Gas Profiling Tools: Tools such as Hardhat and Truffle’s gas reporter helped quantify the gas cost implications of repeated storage access.
Static Analysis: Automated analysis tools confirmed that certain state variables are read repeatedly within tight loops, suggesting an area for optimization.
Cache Immutable Values: Load values that do not change throughout the transaction (e.g., the liquidation fee and configuration parameters) into local memory variables prior to entering the loop. For example:
Optimize Loop Structure: Review the loop for any additional storage reads that can be minimized by caching values in memory.
Benchmark Gas Usage: Perform bench tests before and after optimization to assess the reduction in gas consumption, and adjust the logic accordingly.
Document Changes: Update documentation to reflect any changes in the gas optimization strategy, ensuring future developers are aware of the cost-saving measures employed.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.