40,000 USDC
View results
Submission Details
Severity: gas

Optimize function names

Summary

When a call to a contract is made, the 4-byte function selectors are looked at sequentially (low to high) and matched with the one present in call data. As such, the most called functions should have the least method ids, and functions should be ordered with their method ids in ascending order (most used to least used, relatively)

Vulnerability Details

The called function is looked up with its method ID, not its placement. An extra ~22 gas is consumed for each next lookup. E.g., if there are 10 functions, and the called function is the 10th function, 220 extra gas will be consumed just to reach this function. As such, functions which the developers think will be called more often, should be given smaller method Ids.

Impact

Gas

Tools Used

Forge, Foundry Tooklit (gas snapshots and gas reports)

Recommendation

Give most called public functions, and public state variables, smaller methodIds. This tool can be used to optimise a function signature to have a smaller method Id. In addition, reduce public/ external functions, where possible.

The three escrow functions and their method Ids are as follows:

  • confirmReceipt():0x7d94ad98

  • initiateDispute():0x4acc296f

  • resolveDispute():0xc2b7b868

The methodIds, arranged in ascending order, place initiateDispute first, then confirmReceipt() and then resolveDispute().

As an example, confirmReceipt() is one function that can be said to bo called most often. Its method id is 0x7d94ad98. After changing its name to confirmReceipt_HgU() , it now has the least method id, 0x000063eb. This means, it will the first function in the contract, and all calls made to the contract will be first checked with this function. Making this change, the methodId sequence is as follows: confirmReceipt_HgU(), initiateDispute() and resolveDispute(). This is what we want.

Checking the gas difference, an improvement of 66 to 75 gas was seen. Optimising all public members of the contract can thus result in considerable gas savings.

Support

FAQs

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