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

0 stars 0 forks source link

Gas: `DelegatorPool.sol:claim()`, a repetitive arithmetic operation should be cached #146

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Handle

Dravee

Vulnerability details

Impact

Increased gas cost (2 SLOADs and 1 SUB vs 1 MSTORE and 2 MLOADs)

Proof of Concept

In DelegatorPool.sol:claim(), the following calculation is done twice :

(X * _stake) / (initialStake - claimedInitialStake);

where X is either currTotalStake or currTotalFees

While I understand a loss of precision could occur by caching the whole calculation, it's possible to save some gas (here, 2 SLOADs and 1 SUB) by caching the result of the denominator's substraction in a variable (initialStake - claimedInitialStake) and using this instead of computing the substraction twice.

Tools Used

VS Code

Recommended Mitigation Steps

Apply the refacto

yondonfu commented 2 years ago

Fixed in https://github.com/livepeer/arbitrum-lpt-bridge/commit/a1dfd5ea61a5977adf19df688aa4d1647506ac6b