40,000 USDC
View results
Submission Details
Severity: gas

Refactor a modifier to call a local function instead of directly having the code in the modifier, saving bytecode size and thereby deployment cost

Summary

Modifiers code is copied in all instances where it's used, increasing bytecode size.
The bytecode size is reduced, which saves a lot of deployment gas. The gas required to call the modifier increases slightly. However, in the current implementation, each function is called at most once, so reducing deploy gas is better than reducing call gas.

Vulnerability Details

modifier inState(State expectedState) {
if (s_state != expectedState) {
revert Escrow__InWrongState(s_state, expectedState);
}
_;
}

The code written in the modifier is then included in the code at deployment time. This increases the deploy gas.

  • original gas report

Deployment Cost Deployment Size
656473 4085
Function Name min avg median max # calls
initiateDispute 356 16991 23772 23772 10
resolveDispute 397 27562 22808 63620 8

You can replace the inState modifier with the following

modifier inState(State expectedState) {
_checkinState(expectedState);
_;
}
function _checkinState(State expectedState) internal view virtual {
if (s_state != expectedState) {
revert Escrow__InWrongState(s_state, expectedState);
}
}
  • optimized gas report

Deployment Cost Deployment Size
631032 3951
Function Name min avg median max # calls
initiateDispute 351 16971 23752 23752 10
resolveDispute 410 27564 22794 63638 8

Reduced the deployment gas by 25441, but the increased call gas is negligible, less than 100.

Impact

Reduce the deployment gas that the buyer has to pay.

Tools Used

VS Code

Recommendations

Refactor a modifier to call a local function instead of directly having the code in the modifier

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.