code-423n4 / 2021-05-88mph-findings

0 stars 0 forks source link

Gas optimizations - cache chainId #21

Closed code423n4 closed 3 years ago

code423n4 commented 3 years ago

Handle

a_delamo

Vulnerability details

Impact

In Sponsorable, the method _validateSponsorship is calling all the time

    uint256 chainId;
    assembly {
      chainId := chainid()
    }

Being the chainId "static", we could keep the value cached and just refresh it from time to time?

Running on Remix the following code gives 134 gas cost.

function chain() external {
        uint256 chainId;
        assembly {
          chainId := chainid()
        }
    }

Tools Used

None

ZeframLou commented 3 years ago

Storing the chainId in storage would require an SLOAD to access it, which is a lot more expensive.

ghoul-sol commented 3 years ago

SLOAD costs 200 gas. Closing as invalid.