From cc3d315cc4d6f9c454215c8c74dd126d1aa44ae8 Mon Sep 17 00:00:00 2001 From: Santiago Sanchez Avalos Date: Tue, 4 Jun 2024 00:39:16 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20rewards:=20avoid=20update?= =?UTF-8?q?=20after=20period=20ends?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/khaki-geese-heal.md | 5 ++ .gas-snapshot | 143 ++++++++++++++++---------------- contracts/RewardsController.sol | 3 +- test/RewardsController.t.sol | 25 ++++++ 4 files changed, 104 insertions(+), 72 deletions(-) create mode 100644 .changeset/khaki-geese-heal.md diff --git a/.changeset/khaki-geese-heal.md b/.changeset/khaki-geese-heal.md new file mode 100644 index 00000000..dc695f99 --- /dev/null +++ b/.changeset/khaki-geese-heal.md @@ -0,0 +1,5 @@ +--- +"@exactly/protocol": patch +--- + +⚡️ rewards: avoid update after period ends diff --git a/.gas-snapshot b/.gas-snapshot index d83f3005..179315e6 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -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: 7782644) +PreviewerTest:testEmptyExactly() (gas: 5674536) PreviewerTest:testExactlyReturningInterestRateModelData() (gas: 688149) PreviewerTest:testFixedAvailableLiquidityProjectingNewFloatingDebt() (gas: 13312154) PreviewerTest:testFixedPoolsA() (gas: 19328899) @@ -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: 7204085) PreviewerTest:testMaxBorrowAssetsCapacity() (gas: 2469700) PreviewerTest:testMaxBorrowAssetsCapacityForAccountWithShortfall() (gas: 11002333) PreviewerTest:testMaxBorrowAssetsCapacityPerMarket() (gas: 13192458) @@ -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: 6726433) +PreviewerTest:testRewardsRateAfterDistributionEnd() (gas: 7510428) +PreviewerTest:testRewardsRateOnlyWithFixedBorrows() (gas: 6817536) +PreviewerTest:testRewardsRateWithDifferentRewardLengths() (gas: 19281131) +PreviewerTest:testRewardsRateWithMarketWithDifferentDecimals() (gas: 18412141) +PreviewerTest:testRewardsRateX() (gas: 8157031) PriceFeedDoubleTest:testPriceFeedDoubleReturningAccurateDecimals() (gas: 597567) PriceFeedDoubleTest:testPriceFeedDoubleReturningPrice() (gas: 53190) PriceFeedDoubleTest:testPriceFeedDoubleWithActualOnChainValues() (gas: 76310) @@ -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: 1245635) +RewardsControllerTest:testAccrueRewardsWithBadDebtClearingOfFixedBorrow() (gas: 3339954) +RewardsControllerTest:testAccrueRewardsWithRepayOfBorrowBalance() (gas: 1603667) +RewardsControllerTest:testAccrueRewardsWithRepayOfFixedBorrowBalance() (gas: 1792728) +RewardsControllerTest:testAccrueRewardsWithSeizeOfAllDepositShares() (gas: 1992188) +RewardsControllerTest:testAfterDistributionPeriodEnd() (gas: 1818635) +RewardsControllerTest:testAllClaimableUSDCWithAnotherAccountInPool() (gas: 2274664) +RewardsControllerTest:testAllClaimableUSDCWithDeposit() (gas: 1628946) +RewardsControllerTest:testAllClaimableUSDCWithFloatingBorrow() (gas: 1560934) +RewardsControllerTest:testAllClaimableUSDCWithFloatingRefund() (gas: 1667893) +RewardsControllerTest:testAllClaimableUSDCWithFloatingRepay() (gas: 1674516) +RewardsControllerTest:testAllClaimableUSDCWithMint() (gas: 1285186) +RewardsControllerTest:testAllClaimableUSDCWithRedeem() (gas: 1641227) +RewardsControllerTest:testAllClaimableUSDCWithTransfer() (gas: 2221856) +RewardsControllerTest:testAllClaimableUSDCWithTransferFrom() (gas: 2136751) +RewardsControllerTest:testAllClaimableUSDCWithWithdraw() (gas: 1642240) +RewardsControllerTest:testAllClaimableWETH() (gas: 1248508) +RewardsControllerTest:testAllClaimableWithMaturedFixedPool() (gas: 1128268) +RewardsControllerTest:testAllClaimableWithTimeElapsedZero() (gas: 1625202) +RewardsControllerTest:testClaim() (gas: 1192555) +RewardsControllerTest:testClaimAll() (gas: 2189351) +RewardsControllerTest:testClaimMarketWithoutRewards() (gas: 1241206) +RewardsControllerTest:testClaimWithNotEnabledRewardAsset() (gas: 1223053) +RewardsControllerTest:testConfigSettingNewStartWithOnGoingDistributionShouldNotUpdate() (gas: 430363) +RewardsControllerTest:testConfigWithDistributionNotYetStartedShouldNotFail() (gas: 613312) 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: 2026019) +RewardsControllerTest:testEmitAccrue() (gas: 1318364) +RewardsControllerTest:testEmitClaimRewards() (gas: 1112732) +RewardsControllerTest:testEmitConfigUpdate() (gas: 439433) +RewardsControllerTest:testEmitIndexUpdate() (gas: 1446160) +RewardsControllerTest:testLastUndistributed() (gas: 2190858) +RewardsControllerTest:testLastUpdateAfterDistributionPeriodEnd() (gas: 1847049) +RewardsControllerTest:testOperationAfterDistributionEnded() (gas: 723043) +RewardsControllerTest:testOperationsBeforeDistributionStart() (gas: 1675130) +RewardsControllerTest:testPermitClaim() (gas: 1275545) +RewardsControllerTest:testSetDistributionConfigWithDifferentDecimals() (gas: 11456227) 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: 1202426) +RewardsControllerTest:testSetHigherTotalDistribution() (gas: 1832279) +RewardsControllerTest:testSetLowerAndEqualDistributionPeriodThanCurrentTimestampShouldRevert() (gas: 1275136) +RewardsControllerTest:testSetLowerAndEqualTotalDistributionThanReleasedShouldRevert() (gas: 1268161) +RewardsControllerTest:testSetLowerDistributionPeriod() (gas: 2285715) +RewardsControllerTest:testSetLowerDistributionPeriodAndLowerTotalDistribution() (gas: 2288440) +RewardsControllerTest:testSetLowerTotalDistribution() (gas: 1832214) +RewardsControllerTest:testSetNewDistributionPeriod() (gas: 3146204) +RewardsControllerTest:testSetNewDistributionPeriodAfterDistributionEnds() (gas: 1406940) +RewardsControllerTest:testSetNewTargetDebt() (gas: 1672317) +RewardsControllerTest:testSetNewTargetDebtAfterDistributionEnds() (gas: 1735924) +RewardsControllerTest:testSetNewTargetDebtWithClaimOnlyAtEnd() (gas: 1390162) +RewardsControllerTest:testSetNewTreasuryFeeShouldImpactAllocation() (gas: 659032) +RewardsControllerTest:testSetTargetDebtMultipleTimes() (gas: 2720674) +RewardsControllerTest:testSetTargetDebtMultipleTimesAfterEnd() (gas: 2757539) +RewardsControllerTest:testSetTotalDistributionMultipleTimes() (gas: 1838931) +RewardsControllerTest:testTriggerHandleBorrowHookBeforeUpdatingFloatingDebt() (gas: 1879646) +RewardsControllerTest:testUpdateConfig() (gas: 1329306) +RewardsControllerTest:testUpdateConfigIncreaseRewardDistribution() (gas: 405592) +RewardsControllerTest:testUpdateIndexesWithUtilizationEqualToOne() (gas: 1258133) +RewardsControllerTest:testUpdateIndexesWithUtilizationHigherThanOne() (gas: 1352874) +RewardsControllerTest:testUpdateWithTotalDebtZeroShouldUpdateLastUndistributed() (gas: 575771) +RewardsControllerTest:testUtilizationEqualZero() (gas: 922119) +RewardsControllerTest:testWithTwelveFixedPools() (gas: 8057870) +RewardsControllerTest:testWithdrawAllRewardBalance() (gas: 71935) +RewardsControllerTest:testWithdrawOnlyAdminRole() (gas: 122353) SwapperTest:testSwapBasic() (gas: 216831) SwapperTest:testSwapWithAllowance() (gas: 481530) SwapperTest:testSwapWithInaccurateSlippageSendsETHToAccount() (gas: 297968) diff --git a/contracts/RewardsController.sol b/contracts/RewardsController.sol index 3e8c4c47..3e498d65 100644 --- a/contracts/RewardsController.sol +++ b/contracts/RewardsController.sol @@ -322,8 +322,9 @@ contract RewardsController is Initializable, AccessControlUpgradeable { RewardData storage rewardData = distribution[market].rewards[reward]; { uint256 lastUpdate = rewardData.lastUpdate; + uint256 end = rewardData.end; // `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 < end || rewardData.lastUndistributed > 0)) { (uint256 borrowIndex, uint256 depositIndex, uint256 newUndistributed) = previewAllocation( rewardData, market, diff --git a/test/RewardsController.t.sol b/test/RewardsController.t.sol index 3e411a2a..b702fe63 100644 --- a/test/RewardsController.t.sol +++ b/test/RewardsController.t.sol @@ -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));