From 67b60012f113c742d775ffaa07d354c2df868eba Mon Sep 17 00:00:00 2001 From: Doug Hoyte Date: Tue, 30 Apr 2024 13:40:30 -0400 Subject: [PATCH] Restore encapsulation of distributions layout, broken by gas optimisations --- src/BaseRewardStreams.sol | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/BaseRewardStreams.sol b/src/BaseRewardStreams.sol index 65e12b0..fb143b7 100644 --- a/src/BaseRewardStreams.sol +++ b/src/BaseRewardStreams.sol @@ -396,8 +396,7 @@ abstract contract BaseRewardStreams is IRewardStreams, EVCUtil, ReentrancyGuard address reward, uint48 epoch ) public view virtual override returns (uint256) { - DistributionStorage storage distributionStorage = distributions[rewarded][reward]; - return distributionStorage.amounts[epoch / EPOCHS_PER_SLOT][epoch % EPOCHS_PER_SLOT]; + return rewardAmount(distributions[rewarded][reward], epoch); } /// @notice Returns the current epoch based on the block timestamp. @@ -444,6 +443,17 @@ abstract contract BaseRewardStreams is IRewardStreams, EVCUtil, ReentrancyGuard } } + /// @notice Returns the reward token amount for an epoch, given a pre-computed distribution storage pointer. + /// @param distributionStorage Pre-computed distribution storage pointer. + /// @param epoch The epoch to get the reward token amount for. + /// @return The reward token amount. + function rewardAmount( + DistributionStorage storage distributionStorage, + uint48 epoch + ) internal view returns (uint256) { + return distributionStorage.amounts[epoch / EPOCHS_PER_SLOT][epoch % EPOCHS_PER_SLOT]; + } + /// @notice Increases the reward token amounts for a specific rewarded token. /// @param rewarded The address of the rewarded token. /// @param reward The address of the reward token. @@ -574,7 +584,7 @@ abstract contract BaseRewardStreams is IRewardStreams, EVCUtil, ReentrancyGuard for (uint48 epoch = epochStart; epoch < epochEnd; ++epoch) { // Overflow safe because `totalRegistered * SCALER <= type(uint144).max < type(uint256).max`. unchecked { - uint256 amount = distributionStorage.amounts[epoch / EPOCHS_PER_SLOT][epoch % EPOCHS_PER_SLOT]; + uint256 amount = rewardAmount(distributionStorage, epoch); delta += SCALER * timeElapsedInEpoch(epoch, lastUpdated) * amount / EPOCH_DURATION; } }