40,000 USDC
View results
Submission Details
Severity: gas
Valid

Refactor `inState` modifier for high gas savings

Summary

Instead of housing the logic in a modifier, moving it into an internal function, and making a call to that function inside the modifier, can save a lot of gas.

Vulnerability Details

The modifier inState is used thrice. As such, its code is copied over to each modified function and is duplicated three times. If, instead, we create an internal view function with the state check, and invoke that function inside the modifier, we can save a lot of gas. What this does is that instead of the actual check being copied over in each modified function, just an invocation to the internal function is made.

Impact

Gas

Tools Used

Forge, Foundry Toolkit (gas report, gas snapshots)

Recommendation

Use the inState modifier in conjunction with an internal _inState function to reduce contract size and save on gas. Only the following change needs to be made:

modifier inState(State expectedState) {
_inState(expectedState);
_;
}
function _inState(State expectedState) internal view {
State currentState = s_state;
if (currentState != expectedState) {
revert Escrow__InWrongState(currentState, expectedState);
}
}

With this change in place, a gas saving of ~13000 gas was seen on each test through the gas snapshot. A total gas reduction of 303596 was seen across all tests. In addition, the deployment size for Escrow.sol reduced by 5 bytes and deployment cost reduced by 13012 gas. Saving ~13K gas on each deployment adds up to significant savings in the long-term.

Support

FAQs

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