code-423n4 / 2021-10-ambire-findings

0 stars 0 forks source link

Cache storage variables in the stack can save gas #30

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Handle

WatchPug

Vulnerability details

For the storage variables that will be accessed multiple times, cache them in the stack can save ~100 gas from each extra read (SLOAD after Berlin).

For example:

Ivshti commented 2 years ago

resolved in https://github.com/AmbireTech/adex-protocol-eth/commit/b2a799ea7b43039b3ac47fb27b395b8f8da378ee

GalloDaSballo commented 2 years ago

As per eip-2929 the cost of reading a cold storage slot is 2100, the cost of reading from a hot storage slot (already read once) is 100

While apparently minor, storing the result of the read in memory will end up saving 94 gas (3 gas to MSTORE, 3 gas to MLOAD) when compared to re-reading from Storage

GalloDaSballo commented 2 years ago

The sponsor has applied the improvement