code-423n4 / 2022-01-openleverage-findings

0 stars 0 forks source link

Gas in `LPool.getBorrowRateInternal()`: `kink` should get cached #186

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

Dravee

Vulnerability details

Impact

SLOADs are expensive (~100 gas) compared to MLOADs/MSTOREs (~3 gas). Minimizing them can save gas.

Proof of Concept

The code is as such (see @audit-info tags):

File: LPool.sol
375:     function getBorrowRateInternal(uint cash, uint borrows, uint reserves) internal view returns (uint) {
376:         uint util = utilizationRate(cash, borrows, reserves);
377:         if (util <= kink) { //@audit-info SLOAD 1 kink
378:             return util.mul(multiplierPerBlock).div(1e18).add(baseRatePerBlock);
379:         } else {
380:             uint normalRate = kink.mul(multiplierPerBlock).div(1e18).add(baseRatePerBlock);//@audit-info SLOAD 2 kink
381:             uint excessUtil = util.sub(kink);//@audit-info SLOAD 3 kink
382:             return excessUtil.mul(jumpMultiplierPerBlock).div(1e18).add(normalRate);
383:         }
384:     }

It's possible to save these 2 SLOAD (~200 gas) by caching kink in a memory variable and use it instead of reading it repeatedly from storage.

Tools Used

VS Code

Recommended Mitigation Steps

Cache the storage value in a memory variable and use it instead of repeatedly reading it from storage.

ColaM12 commented 2 years ago

Duplicate to #137

0xleastwood commented 2 years ago

This warden has made a large number of submissions pointing to basically the same area in different parts of the code. Because of how similar issues are, I don't think its fair to other wardens to have these treated as separate. While I understand gas reports should fix this, I've decided for this contest I'll mark similar duplicates as invalid.