code-423n4 / 2021-11-streaming-findings

0 stars 0 forks source link

Avoid unnecessary storage reads can save gas #245

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Handle

WatchPug

Vulnerability details

In Stream#updateStreamInternal(), the result of rewardPerToken() can be cached to avoid unnecessary storage reads.

https://github.com/code-423n4/2021-11-streaming/blob/56d81204a00fc949d29ddd277169690318b36821/Streaming/src/Locke.sol#L216-L222

// accumulate reward per token info
cumulativeRewardPerToken = rewardPerToken();

// update user rewards
ts.rewards = earned(ts, cumulativeRewardPerToken);
// update users last cumulative reward per token
ts.lastCumulativeRewardPerToken = cumulativeRewardPerToken;

Can be changed to:

// accumulate reward per token info
uint256 _cumulativeRewardPerToken = rewardPerToken();
cumulativeRewardPerToken = _cumulativeRewardPerToken;

// update user rewards
ts.rewards = earned(ts, _cumulativeRewardPerToken);
// update users last cumulative reward per token
ts.lastCumulativeRewardPerToken = _cumulativeRewardPerToken;

https://github.com/code-423n4/2021-11-streaming/blob/56d81204a00fc949d29ddd277169690318b36821/Streaming/src/Locke.sol#L559-L565

// accumulate reward per token info
cumulativeRewardPerToken = rewardPerToken();

// update user rewards
ts.rewards = earned(ts, cumulativeRewardPerToken);
// update users last cumulative reward per token
ts.lastCumulativeRewardPerToken = cumulativeRewardPerToken;

Can be changed to:

// accumulate reward per token info
uint256 _cumulativeRewardPerToken = rewardPerToken();
cumulativeRewardPerToken = _cumulativeRewardPerToken;

// update user rewards
ts.rewards = earned(ts, _cumulativeRewardPerToken);
// update users last cumulative reward per token
ts.lastCumulativeRewardPerToken = _cumulativeRewardPerToken;