Skip to content

Commit

Permalink
⚡️ rewards: avoid update after period ends
Browse files Browse the repository at this point in the history
  • Loading branch information
santichez committed Jun 4, 2024
1 parent 982b589 commit befd927
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 72 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-geese-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@exactly/protocol": patch
---

⚡️ rewards: avoid update after period ends
143 changes: 72 additions & 71 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ PreviewerTest:testAccountsWithAccountOnlyDeposit() (gas: 862391)
PreviewerTest:testAccountsWithAccountThatHasBalances() (gas: 2265104)
PreviewerTest:testAccountsWithEmptyAccount() (gas: 690558)
PreviewerTest:testAccountsWithIntermediateOperationsReturningAccurateAmounts() (gas: 17628100)
PreviewerTest:testActualTimeBeforeStartDistributionRewards() (gas: 7774796)
PreviewerTest:testEmptyExactly() (gas: 5666767)
PreviewerTest:testActualTimeBeforeStartDistributionRewards() (gas: 7783836)
PreviewerTest:testEmptyExactly() (gas: 5675772)
PreviewerTest:testExactlyReturningInterestRateModelData() (gas: 688149)
PreviewerTest:testFixedAvailableLiquidityProjectingNewFloatingDebt() (gas: 13312154)
PreviewerTest:testFixedPoolsA() (gas: 19328899)
Expand All @@ -306,7 +306,7 @@ PreviewerTest:testFlexibleAvailableLiquidity() (gas: 17252956)
PreviewerTest:testFlexibleBorrowSharesAndAssets() (gas: 4401038)
PreviewerTest:testFloatingAvailableLiquidityProjectingNewFloatingDebt() (gas: 12564814)
PreviewerTest:testFloatingRateAndUtilization() (gas: 1128246)
PreviewerTest:testJustUpdatedRewardRatesShouldStillReturnRate() (gas: 7196163)
PreviewerTest:testJustUpdatedRewardRatesShouldStillReturnRate() (gas: 7205378)
PreviewerTest:testMaxBorrowAssetsCapacity() (gas: 2469700)
PreviewerTest:testMaxBorrowAssetsCapacityForAccountWithShortfall() (gas: 11002333)
PreviewerTest:testMaxBorrowAssetsCapacityPerMarket() (gas: 13192458)
Expand Down Expand Up @@ -353,12 +353,12 @@ PreviewerTest:testPreviewWithdrawAtMaturityWithOneUnit() (gas: 251718)
PreviewerTest:testPreviewWithdrawAtMaturityWithSameTimestamp() (gas: 233406)
PreviewerTest:testPreviewWithdrawAtMaturityWithZeroAmount() (gas: 251675)
PreviewerTest:testReserveFactor() (gas: 707280)
PreviewerTest:testReturnRewardAssetUsdPrice() (gas: 6718669)
PreviewerTest:testRewardsRateAfterDistributionEnd() (gas: 7502464)
PreviewerTest:testRewardsRateOnlyWithFixedBorrows() (gas: 6809656)
PreviewerTest:testRewardsRateWithDifferentRewardLengths() (gas: 19273115)
PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 18404224)
PreviewerTest:testRewardsRateX() (gas: 8149114)
PreviewerTest:testReturnRewardAssetUsdPrice() (gas: 6727669)
PreviewerTest:testRewardsRateAfterDistributionEnd() (gas: 7511699)
PreviewerTest:testRewardsRateOnlyWithFixedBorrows() (gas: 6818851)
PreviewerTest:testRewardsRateWithDifferentRewardLengths() (gas: 19282235)
PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 18413434)
PreviewerTest:testRewardsRateX() (gas: 8158324)
PriceFeedDoubleTest:testPriceFeedDoubleReturningAccurateDecimals() (gas: 597567)
PriceFeedDoubleTest:testPriceFeedDoubleReturningPrice() (gas: 53190)
PriceFeedDoubleTest:testPriceFeedDoubleWithActualOnChainValues() (gas: 76310)
Expand All @@ -375,70 +375,71 @@ PriceFeedWrapperTest:testPriceFeedWrapperReturningPriceAfterRebase() (gas: 48989
PriceFeedWrapperTest:testPriceFeedWrapperWithActualOnChainValues() (gas: 75210)
PriceFeedWrapperTest:testPriceFeedWrapperWithNegativePriceShouldRevert() (gas: 164216)
PriceFeedWrapperTest:testPriceFeedWrapperWithUsdPriceFeed() (gas: 1243191)
RewardsControllerTest:testAccrueRewardsForWholeDistributionPeriod() (gas: 1245322)
RewardsControllerTest:testAccrueRewardsWithBadDebtClearingOfFixedBorrow() (gas: 3339446)
RewardsControllerTest:testAccrueRewardsWithRepayOfBorrowBalance() (gas: 1603277)
RewardsControllerTest:testAccrueRewardsWithRepayOfFixedBorrowBalance() (gas: 1792346)
RewardsControllerTest:testAccrueRewardsWithSeizeOfAllDepositShares() (gas: 1991776)
RewardsControllerTest:testAfterDistributionPeriodEnd() (gas: 1818322)
RewardsControllerTest:testAllClaimableUSDCWithAnotherAccountInPool() (gas: 2274252)
RewardsControllerTest:testAllClaimableUSDCWithDeposit() (gas: 1628756)
RewardsControllerTest:testAllClaimableUSDCWithFloatingBorrow() (gas: 1560744)
RewardsControllerTest:testAllClaimableUSDCWithFloatingRefund() (gas: 1667674)
RewardsControllerTest:testAllClaimableUSDCWithFloatingRepay() (gas: 1674188)
RewardsControllerTest:testAllClaimableUSDCWithMint() (gas: 1284996)
RewardsControllerTest:testAllClaimableUSDCWithRedeem() (gas: 1640815)
RewardsControllerTest:testAllClaimableUSDCWithTransfer() (gas: 2221471)
RewardsControllerTest:testAllClaimableUSDCWithTransferFrom() (gas: 2136365)
RewardsControllerTest:testAllClaimableUSDCWithWithdraw() (gas: 1641850)
RewardsControllerTest:testAllClaimableWETH() (gas: 1248402)
RewardsControllerTest:testAllClaimableWithMaturedFixedPool() (gas: 1128046)
RewardsControllerTest:testAllClaimableWithTimeElapsedZero() (gas: 1624747)
RewardsControllerTest:testClaim() (gas: 1192249)
RewardsControllerTest:testClaimAll() (gas: 2188892)
RewardsControllerTest:testClaimMarketWithoutRewards() (gas: 1241095)
RewardsControllerTest:testClaimWithNotEnabledRewardAsset() (gas: 1222725)
RewardsControllerTest:testConfigSettingNewStartWithOnGoingDistributionShouldNotUpdate() (gas: 430252)
RewardsControllerTest:testConfigWithDistributionNotYetStartedShouldNotFail() (gas: 613248)
RewardsControllerTest:testAccrueRewardsForWholeDistributionPeriod() (gas: 1245894)
RewardsControllerTest:testAccrueRewardsWithBadDebtClearingOfFixedBorrow() (gas: 3340248)
RewardsControllerTest:testAccrueRewardsWithRepayOfBorrowBalance() (gas: 1603737)
RewardsControllerTest:testAccrueRewardsWithRepayOfFixedBorrowBalance() (gas: 1793088)
RewardsControllerTest:testAccrueRewardsWithSeizeOfAllDepositShares() (gas: 1992258)
RewardsControllerTest:testAfterDistributionPeriodEnd() (gas: 1818894)
RewardsControllerTest:testAllClaimableUSDCWithAnotherAccountInPool() (gas: 2274734)
RewardsControllerTest:testAllClaimableUSDCWithDeposit() (gas: 1628858)
RewardsControllerTest:testAllClaimableUSDCWithFloatingBorrow() (gas: 1560846)
RewardsControllerTest:testAllClaimableUSDCWithFloatingRefund() (gas: 1668007)
RewardsControllerTest:testAllClaimableUSDCWithFloatingRepay() (gas: 1674630)
RewardsControllerTest:testAllClaimableUSDCWithMint() (gas: 1285098)
RewardsControllerTest:testAllClaimableUSDCWithRedeem() (gas: 1641297)
RewardsControllerTest:testAllClaimableUSDCWithTransfer() (gas: 2221882)
RewardsControllerTest:testAllClaimableUSDCWithTransferFrom() (gas: 2136777)
RewardsControllerTest:testAllClaimableUSDCWithWithdraw() (gas: 1642310)
RewardsControllerTest:testAllClaimableWETH() (gas: 1248464)
RewardsControllerTest:testAllClaimableWithMaturedFixedPool() (gas: 1128426)
RewardsControllerTest:testAllClaimableWithTimeElapsedZero() (gas: 1625329)
RewardsControllerTest:testClaim() (gas: 1192669)
RewardsControllerTest:testClaimAll() (gas: 2189522)
RewardsControllerTest:testClaimMarketWithoutRewards() (gas: 1241285)
RewardsControllerTest:testClaimWithNotEnabledRewardAsset() (gas: 1223167)
RewardsControllerTest:testConfigSettingNewStartWithOnGoingDistributionShouldNotUpdate() (gas: 430442)
RewardsControllerTest:testConfigWithDistributionNotYetStartedShouldNotFail() (gas: 613290)
RewardsControllerTest:testConfigWithTransitionFactorHigherOrEqThanCap() (gas: 107077)
RewardsControllerTest:testConfigWithZeroDepositAllocationWeightFactorShouldRevert() (gas: 71475)
RewardsControllerTest:testDifferentDistributionTimeForDifferentRewards() (gas: 2025856)
RewardsControllerTest:testEmitAccrue() (gas: 1317982)
RewardsControllerTest:testEmitClaimRewards() (gas: 1112419)
RewardsControllerTest:testEmitConfigUpdate() (gas: 439411)
RewardsControllerTest:testEmitIndexUpdate() (gas: 1445640)
RewardsControllerTest:testLastUndistributed() (gas: 2189926)
RewardsControllerTest:testOperationAfterDistributionEnded() (gas: 723020)
RewardsControllerTest:testOperationsBeforeDistributionStart() (gas: 1674553)
RewardsControllerTest:testPermitClaim() (gas: 1275326)
RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 11455677)
RewardsControllerTest:testConfigWithZeroDepositAllocationWeightFactorShouldRevert() (gas: 71497)
RewardsControllerTest:testDifferentDistributionTimeForDifferentRewards() (gas: 2026300)
RewardsControllerTest:testEmitAccrue() (gas: 1318724)
RewardsControllerTest:testEmitClaimRewards() (gas: 1112991)
RewardsControllerTest:testEmitConfigUpdate() (gas: 439433)
RewardsControllerTest:testEmitIndexUpdate() (gas: 1446722)
RewardsControllerTest:testLastUndistributed() (gas: 2191681)
RewardsControllerTest:testLastUpdateAfterDistributionPeriodEnd() (gas: 1847918)
RewardsControllerTest:testOperationAfterDistributionEnded() (gas: 723122)
RewardsControllerTest:testOperationsBeforeDistributionStart() (gas: 1675525)
RewardsControllerTest:testPermitClaim() (gas: 1275659)
RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 11456499)
RewardsControllerTest:testSetDistributionOperationShouldUpdateIndex() (gas: 136066)
RewardsControllerTest:testSetDistributionWithOnGoingMarketOperations() (gas: 1202335)
RewardsControllerTest:testSetHigherTotalDistribution() (gas: 1831337)
RewardsControllerTest:testSetLowerAndEqualDistributionPeriodThanCurrentTimestampShouldRevert() (gas: 1274818)
RewardsControllerTest:testSetLowerAndEqualTotalDistributionThanReleasedShouldRevert() (gas: 1267821)
RewardsControllerTest:testSetLowerDistributionPeriod() (gas: 2284312)
RewardsControllerTest:testSetLowerDistributionPeriodAndLowerTotalDistribution() (gas: 2287059)
RewardsControllerTest:testSetLowerTotalDistribution() (gas: 1831250)
RewardsControllerTest:testSetNewDistributionPeriod() (gas: 3144152)
RewardsControllerTest:testSetNewDistributionPeriodAfterDistributionEnds() (gas: 1406553)
RewardsControllerTest:testSetNewTargetDebt() (gas: 1671639)
RewardsControllerTest:testSetNewTargetDebtAfterDistributionEnds() (gas: 1735009)
RewardsControllerTest:testSetNewTargetDebtWithClaimOnlyAtEnd() (gas: 1389753)
RewardsControllerTest:testSetNewTreasuryFeeShouldImpactAllocation() (gas: 658948)
RewardsControllerTest:testSetTargetDebtMultipleTimes() (gas: 2719552)
RewardsControllerTest:testSetTargetDebtMultipleTimesAfterEnd() (gas: 2756047)
RewardsControllerTest:testSetTotalDistributionMultipleTimes() (gas: 1838384)
RewardsControllerTest:testTriggerHandleBorrowHookBeforeUpdatingFloatingDebt() (gas: 1879533)
RewardsControllerTest:testUpdateConfig() (gas: 1328946)
RewardsControllerTest:testUpdateConfigIncreaseRewardDistribution() (gas: 405501)
RewardsControllerTest:testUpdateIndexesWithUtilizationEqualToOne() (gas: 1257815)
RewardsControllerTest:testUpdateIndexesWithUtilizationHigherThanOne() (gas: 1352623)
RewardsControllerTest:testUpdateWithTotalDebtZeroShouldUpdateLastUndistributed() (gas: 575527)
RewardsControllerTest:testUtilizationEqualZero() (gas: 921840)
RewardsControllerTest:testWithTwelveFixedPools() (gas: 8055817)
RewardsControllerTest:testWithdrawAllRewardBalance() (gas: 71913)
RewardsControllerTest:testWithdrawOnlyAdminRole() (gas: 122331)
RewardsControllerTest:testSetDistributionWithOnGoingMarketOperations() (gas: 1202527)
RewardsControllerTest:testSetHigherTotalDistribution() (gas: 1833068)
RewardsControllerTest:testSetLowerAndEqualDistributionPeriodThanCurrentTimestampShouldRevert() (gas: 1275518)
RewardsControllerTest:testSetLowerAndEqualTotalDistributionThanReleasedShouldRevert() (gas: 1268543)
RewardsControllerTest:testSetLowerDistributionPeriod() (gas: 2286809)
RewardsControllerTest:testSetLowerDistributionPeriodAndLowerTotalDistribution() (gas: 2289534)
RewardsControllerTest:testSetLowerTotalDistribution() (gas: 1833003)
RewardsControllerTest:testSetNewDistributionPeriod() (gas: 3147605)
RewardsControllerTest:testSetNewDistributionPeriodAfterDistributionEnds() (gas: 1407423)
RewardsControllerTest:testSetNewTargetDebt() (gas: 1672858)
RewardsControllerTest:testSetNewTargetDebtAfterDistributionEnds() (gas: 1736590)
RewardsControllerTest:testSetNewTargetDebtWithClaimOnlyAtEnd() (gas: 1390645)
RewardsControllerTest:testSetNewTreasuryFeeShouldImpactAllocation() (gas: 658988)
RewardsControllerTest:testSetTargetDebtMultipleTimes() (gas: 2721643)
RewardsControllerTest:testSetTargetDebtMultipleTimesAfterEnd() (gas: 2758511)
RewardsControllerTest:testSetTotalDistributionMultipleTimes() (gas: 1839616)
RewardsControllerTest:testTriggerHandleBorrowHookBeforeUpdatingFloatingDebt() (gas: 1879826)
RewardsControllerTest:testUpdateConfig() (gas: 1329666)
RewardsControllerTest:testUpdateConfigIncreaseRewardDistribution() (gas: 405693)
RewardsControllerTest:testUpdateIndexesWithUtilizationEqualToOne() (gas: 1258515)
RewardsControllerTest:testUpdateIndexesWithUtilizationHigherThanOne() (gas: 1353256)
RewardsControllerTest:testUpdateWithTotalDebtZeroShouldUpdateLastUndistributed() (gas: 575929)
RewardsControllerTest:testUtilizationEqualZero() (gas: 922110)
RewardsControllerTest:testWithTwelveFixedPools() (gas: 8060509)
RewardsControllerTest:testWithdrawAllRewardBalance() (gas: 71935)
RewardsControllerTest:testWithdrawOnlyAdminRole() (gas: 122353)
SwapperTest:testSwapBasic() (gas: 216831)
SwapperTest:testSwapWithAllowance() (gas: 481530)
SwapperTest:testSwapWithInaccurateSlippageSendsETHToAccount() (gas: 297968)
Expand Down
2 changes: 1 addition & 1 deletion contracts/RewardsController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ contract RewardsController is Initializable, AccessControlUpgradeable {
{
uint256 lastUpdate = rewardData.lastUpdate;
// `lastUpdate` can be greater than `block.timestamp` if distribution is set to start on a future date
if (block.timestamp > lastUpdate) {
if (block.timestamp > lastUpdate && (lastUpdate < rewardData.end || rewardData.lastUndistributed != 0)) {
(uint256 borrowIndex, uint256 depositIndex, uint256 newUndistributed) = previewAllocation(
rewardData,
market,
Expand Down
25 changes: 25 additions & 0 deletions test/RewardsController.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,31 @@ contract RewardsControllerTest is Test {
assertEq(opRewardAsset.balanceOf(address(this)), opRewards);
}

function testLastUpdateAfterDistributionPeriodEnd() external {
marketWETH.deposit(40_000 ether, address(this));
marketWETH.borrow(20_000 ether, address(this), address(this));

(, uint256 distributionEnd, ) = rewardsController.distributionTime(marketWETH, opRewardAsset);
vm.warp(distributionEnd / 2);
rewardsController.claimAll(address(this));

vm.warp(distributionEnd + 1);
rewardsController.claimAll(address(this));
(, , uint256 lastUndistributed) = rewardsController.rewardIndexes(marketWETH, opRewardAsset);
(, , uint256 lastUpdate) = rewardsController.distributionTime(marketWETH, opRewardAsset);
assertEq(lastUndistributed, 0);
assertEq(lastUpdate, distributionEnd + 1);

vm.warp(distributionEnd * 2);
rewardsController.claimAll(address(this));
(, , lastUpdate) = rewardsController.distributionTime(marketWETH, opRewardAsset);

(, , lastUndistributed) = rewardsController.rewardIndexes(marketWETH, opRewardAsset);
(, , lastUpdate) = rewardsController.distributionTime(marketWETH, opRewardAsset);
assertEq(lastUndistributed, 0);
assertEq(lastUpdate, distributionEnd + 1);
}

function testSetDistributionWithOnGoingMarketOperations() external {
vm.warp(1 days);
marketWBTC.deposit(10e8, address(this));
Expand Down

0 comments on commit befd927

Please sign in to comment.