40,000 USDC
View results
Submission Details
Severity: gas

Cache storage and reread variables in memory

Summary

Avoid rereading variables cache in memory

Vulnerability Details

When using variables some are being reread several times

  1. s_state not cached and reread

  2. msg.sender reread several times

Impact

Gas Savings: Caching especially storage variables leads to gas savings as reading from storage costs 100 gas whereas from memory will cost 3 gas.

Tools Used

Manual Analysis

Recommendations

  1. Escrow.sol line 66 -> this does not save as much gas as not reading from storage but avoids rereading msg.sender
    modifier onlyBuyerOrSeller() {
    address sender = msg.sender;
    if (sender != i_buyer && sender != i_seller) {
    revert Escrow__OnlyBuyerOrSeller();
    }
    _;
    }

  2. Escrow.sol line 82 -> s_state read twice from storage second time costs 100 gas if read from memory only 3 gas

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

  1. EscrowFactory.sol line 20-53 avoid rereading msg.sender and cache it as it is reused 4 times

It is recommended to ensure when reading and using storage variables they are cached. It is recommended to ensure msg.sender is not reread several times in a function but be cached.

Support

FAQs

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