Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(RewardsStreamerMP): change account mp update time only if necessary #100

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions .gas-report
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
| script/DeployRewardsStreamerMP.s.sol:DeployRewardsStreamerMPScript contract | | | | | |
|-----------------------------------------------------------------------------|-----------------|---------|---------|---------|---------|
| Deployment Cost | Deployment Size | | | | |
| 6303045 | 30177 | | | | |
| 6307818 | 30199 | | | | |
| Function Name | min | avg | median | max | # calls |
| run | 5397428 | 5397428 | 5397428 | 5397428 | 63 |
| run | 5401845 | 5401845 | 5401845 | 5401845 | 63 |


| script/DeploymentConfig.s.sol:DeploymentConfig contract | | | | | |
Expand All @@ -17,9 +17,9 @@
| script/UpgradeRewardsStreamerMP.s.sol:UpgradeRewardsStreamerMPScript contract | | | | | |
|-------------------------------------------------------------------------------|-----------------|---------|---------|---------|---------|
| Deployment Cost | Deployment Size | | | | |
| 2948151 | 14552 | | | | |
| 2952925 | 14574 | | | | |
| Function Name | min | avg | median | max | # calls |
| run | 2478266 | 2478266 | 2478266 | 2478266 | 3 |
| run | 2482684 | 2482684 | 2482684 | 2482684 | 3 |


| src/RewardsStreamer.sol:RewardsStreamer contract | | | | | |
Expand All @@ -39,7 +39,7 @@
| src/RewardsStreamerMP.sol:RewardsStreamerMP contract | | | | | |
|------------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost | Deployment Size | | | | |
| 2628212 | 12113 | | | | |
| 2632986 | 12135 | | | | |
| Function Name | min | avg | median | max | # calls |
| MAX_LOCKUP_PERIOD | 349 | 349 | 349 | 349 | 6 |
| MAX_MULTIPLIER | 251 | 251 | 251 | 251 | 33 |
Expand All @@ -55,8 +55,8 @@
| getUserVaults | 5201 | 5201 | 5201 | 5201 | 4 |
| initialize | 115611 | 115611 | 115611 | 115611 | 65 |
| lastRewardTime | 395 | 1395 | 1395 | 2395 | 2 |
| leave | 59897 | 59897 | 59897 | 59897 | 1 |
| lock | 12063 | 35390 | 16480 | 77628 | 3 |
| leave | 59939 | 59939 | 59939 | 59939 | 1 |
| lock | 12063 | 35404 | 16480 | 77670 | 3 |
| mpBalanceOfUser | 9185 | 9185 | 9185 | 9185 | 1 |
| proxiableUUID | 353 | 353 | 353 | 353 | 3 |
| registerVault | 55866 | 72766 | 72966 | 72966 | 257 |
Expand All @@ -65,14 +65,14 @@
| rewardsBalanceOf | 3231 | 6627 | 7074 | 7341 | 8 |
| setReward | 2583 | 58371 | 86341 | 105731 | 7 |
| setTrustedCodehash | 24243 | 24304 | 24243 | 26243 | 65 |
| stake | 134799 | 172378 | 179192 | 199525 | 66 |
| stake | 134841 | 172420 | 179234 | 199567 | 66 |
| totalMPAccrued | 395 | 395 | 395 | 395 | 81 |
| totalMaxMP | 350 | 350 | 350 | 350 | 81 |
| totalRewardsAccrued | 351 | 351 | 351 | 351 | 3 |
| totalRewardsSupply | 1025 | 1984 | 1806 | 6765 | 30 |
| totalStaked | 374 | 374 | 374 | 374 | 82 |
| unstake | 64035 | 64572 | 64035 | 67530 | 13 |
| updateAccountMP | 15392 | 17634 | 17898 | 17898 | 19 |
| unstake | 64077 | 64613 | 64077 | 67567 | 13 |
| updateAccountMP | 12217 | 17332 | 17934 | 17934 | 19 |
| updateGlobalState | 14317 | 26623 | 28594 | 28594 | 19 |
| upgradeToAndCall | 3225 | 7901 | 8448 | 10936 | 5 |

Expand Down Expand Up @@ -107,7 +107,7 @@
| totalRewardsAccrued | 778 | 778 | 778 | 778 | 3 |
| totalRewardsSupply | 1452 | 2561 | 2233 | 11692 | 30 |
| totalStaked | 801 | 801 | 801 | 801 | 82 |
| updateAccountMP | 41751 | 43993 | 44257 | 44257 | 19 |
| updateAccountMP | 38576 | 43691 | 44293 | 44293 | 19 |
| updateGlobalState | 40305 | 52611 | 54582 | 54582 | 19 |
| upgradeToAndCall | 29868 | 33720 | 33720 | 37572 | 2 |

Expand All @@ -119,14 +119,14 @@
| Function Name | min | avg | median | max | # calls |
| STAKING_TOKEN | 216 | 216 | 216 | 216 | 1 |
| emergencyExit | 36375 | 48879 | 48113 | 65213 | 7 |
| leave | 33507 | 132169 | 62097 | 370978 | 4 |
| lock | 33245 | 61590 | 50845 | 111425 | 4 |
| leave | 33507 | 132178 | 62113 | 370978 | 4 |
| lock | 33245 | 61600 | 50845 | 111467 | 4 |
| owner | 2339 | 2339 | 2339 | 2339 | 257 |
| register | 87015 | 103915 | 104115 | 104115 | 257 |
| stake | 33411 | 243707 | 253696 | 274077 | 67 |
| stake | 33411 | 243748 | 253738 | 274119 | 67 |
| stakeManager | 368 | 368 | 368 | 368 | 257 |
| trustStakeManager | 28953 | 28953 | 28953 | 28953 | 1 |
| unstake | 33282 | 99918 | 105715 | 113562 | 14 |
| unstake | 33282 | 99955 | 105755 | 113604 | 14 |
| withdraw | 42289 | 42289 | 42289 | 42289 | 1 |


Expand Down
100 changes: 50 additions & 50 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
EmergencyExitTest:test_CannotEnableEmergencyModeTwice() (gas: 92690)
EmergencyExitTest:test_CannotLeaveBeforeEmergencyMode() (gas: 298988)
EmergencyExitTest:test_EmergencyExitBasic() (gas: 385748)
EmergencyExitTest:test_EmergencyExitMultipleUsers() (gas: 664285)
EmergencyExitTest:test_EmergencyExitToAlternateAddress() (gas: 393667)
EmergencyExitTest:test_EmergencyExitWithLock() (gas: 393055)
EmergencyExitTest:test_EmergencyExitWithRewards() (gas: 378609)
EmergencyExitTest:test_CannotLeaveBeforeEmergencyMode() (gas: 299030)
EmergencyExitTest:test_EmergencyExitBasic() (gas: 385790)
EmergencyExitTest:test_EmergencyExitMultipleUsers() (gas: 664369)
EmergencyExitTest:test_EmergencyExitToAlternateAddress() (gas: 393709)
EmergencyExitTest:test_EmergencyExitWithLock() (gas: 393097)
EmergencyExitTest:test_EmergencyExitWithRewards() (gas: 378651)
EmergencyExitTest:test_OnlyOwnerCanEnableEmergencyMode() (gas: 39426)
IntegrationTest:testStakeFoo() (gas: 1212412)
LeaveTest:test_LeaveShouldProperlyUpdateAccounting() (gas: 5827658)
LeaveTest:test_RevertWhenStakeManagerIsTrusted() (gas: 296119)
LeaveTest:test_TrustNewStakeManager() (gas: 5898100)
LockTest:test_LockFailsWithInvalidPeriod() (gas: 311204)
IntegrationTest:testStakeFoo() (gas: 1212612)
LeaveTest:test_LeaveShouldProperlyUpdateAccounting() (gas: 5836926)
LeaveTest:test_RevertWhenStakeManagerIsTrusted() (gas: 296161)
LeaveTest:test_TrustNewStakeManager() (gas: 5907344)
LockTest:test_LockFailsWithInvalidPeriod() (gas: 311246)
LockTest:test_LockFailsWithNoStake() (gas: 63730)
LockTest:test_LockWithoutPriorLock() (gas: 390861)
MaliciousUpgradeTest:test_UpgradeStackOverflowStakeManager() (gas: 1746626)
LockTest:test_LockWithoutPriorLock() (gas: 390945)
MaliciousUpgradeTest:test_UpgradeStackOverflowStakeManager() (gas: 1746668)
MathTest:test_CalcAbsoluteMaxTotalMP() (gas: 18908)
MathTest:test_CalcAccrueMP() (gas: 22207)
MathTest:test_CalcBonusMP() (gas: 17713)
MathTest:test_CalcInitialMP() (gas: 5395)
MathTest:test_CalcMaxAccruedMP() (gas: 15586)
MathTest:test_CalcMaxTotalMP() (gas: 23298)
MultipleVaultsStakeTest:test_StakeMultipleVaults() (gas: 725502)
MultipleVaultsStakeTest:test_StakeMultipleVaults() (gas: 725628)
NFTMetadataGeneratorSVGTest:testGenerateMetadata() (gas: 85934)
NFTMetadataGeneratorSVGTest:testSetImageStrings() (gas: 58332)
NFTMetadataGeneratorSVGTest:testSetImageStringsRevert() (gas: 35804)
NFTMetadataGeneratorURLTest:testGenerateMetadata() (gas: 102512)
NFTMetadataGeneratorURLTest:testSetBaseURL() (gas: 49555)
NFTMetadataGeneratorURLTest:testSetBaseURLRevert() (gas: 35979)
RewardsStreamerMP_RewardsTest:testRewardsBalanceOf() (gas: 486254)
RewardsStreamerMP_RewardsTest:testRewardsBalanceOf() (gas: 486296)
RewardsStreamerMP_RewardsTest:testSetRewards() (gas: 160703)
RewardsStreamerMP_RewardsTest:testSetRewards_RevertsBadAmount() (gas: 39404)
RewardsStreamerMP_RewardsTest:testSetRewards_RevertsBadDuration() (gas: 39340)
RewardsStreamerMP_RewardsTest:testSetRewards_RevertsNotAuthorized() (gas: 39375)
RewardsStreamerMP_RewardsTest:testTotalRewardsSupply() (gas: 618489)
RewardsStreamerMP_RewardsTest:testTotalRewardsSupply() (gas: 618531)
RewardsStreamerTest:testStake() (gas: 869181)
StakeTest:test_StakeMultipleAccounts() (gas: 499452)
StakeTest:test_StakeMultipleAccountsAndRewards() (gas: 505390)
StakeTest:test_StakeMultipleAccountsMPIncreasesMaxMPDoesNotChange() (gas: 842312)
StakeTest:test_StakeMultipleAccountsWithMinLockUp() (gas: 515852)
StakeTest:test_StakeMultipleAccountsWithRandomLockUp() (gas: 537895)
StakeTest:test_StakeOneAccount() (gas: 278226)
StakeTest:test_StakeOneAccountAndRewards() (gas: 284173)
StakeTest:test_StakeOneAccountMPIncreasesMaxMPDoesNotChange() (gas: 507547)
StakeTest:test_StakeOneAccountReachingMPLimit() (gas: 502149)
StakeTest:test_StakeOneAccountWithMaxLockUp() (gas: 298081)
StakeTest:test_StakeOneAccountWithMinLockUp() (gas: 299770)
StakeTest:test_StakeOneAccountWithRandomLockUp() (gas: 299837)
StakeTest:test_StakeMultipleAccounts() (gas: 499536)
StakeTest:test_StakeMultipleAccountsAndRewards() (gas: 505474)
StakeTest:test_StakeMultipleAccountsMPIncreasesMaxMPDoesNotChange() (gas: 842540)
StakeTest:test_StakeMultipleAccountsWithMinLockUp() (gas: 515936)
StakeTest:test_StakeMultipleAccountsWithRandomLockUp() (gas: 537979)
StakeTest:test_StakeOneAccount() (gas: 278268)
StakeTest:test_StakeOneAccountAndRewards() (gas: 284215)
StakeTest:test_StakeOneAccountMPIncreasesMaxMPDoesNotChange() (gas: 507661)
StakeTest:test_StakeOneAccountReachingMPLimit() (gas: 499052)
StakeTest:test_StakeOneAccountWithMaxLockUp() (gas: 298123)
StakeTest:test_StakeOneAccountWithMinLockUp() (gas: 299812)
StakeTest:test_StakeOneAccountWithRandomLockUp() (gas: 299879)
StakingTokenTest:testStakeToken() (gas: 10422)
UnstakeTest:test_StakeMultipleAccounts() (gas: 499474)
UnstakeTest:test_StakeMultipleAccountsAndRewards() (gas: 505412)
UnstakeTest:test_StakeMultipleAccountsMPIncreasesMaxMPDoesNotChange() (gas: 842334)
UnstakeTest:test_StakeMultipleAccountsWithMinLockUp() (gas: 515851)
UnstakeTest:test_StakeMultipleAccountsWithRandomLockUp() (gas: 537917)
UnstakeTest:test_StakeOneAccount() (gas: 278249)
UnstakeTest:test_StakeOneAccountAndRewards() (gas: 284217)
UnstakeTest:test_StakeOneAccountMPIncreasesMaxMPDoesNotChange() (gas: 507591)
UnstakeTest:test_StakeOneAccountReachingMPLimit() (gas: 502151)
UnstakeTest:test_StakeOneAccountWithMaxLockUp() (gas: 298081)
UnstakeTest:test_StakeOneAccountWithMinLockUp() (gas: 299770)
UnstakeTest:test_StakeOneAccountWithRandomLockUp() (gas: 299836)
UnstakeTest:test_UnstakeBonusMPAndAccuredMP() (gas: 546206)
UnstakeTest:test_UnstakeMultipleAccounts() (gas: 704915)
UnstakeTest:test_UnstakeMultipleAccountsAndRewards() (gas: 800743)
UnstakeTest:test_UnstakeOneAccount() (gas: 479994)
UnstakeTest:test_UnstakeOneAccountAndAccruedMP() (gas: 502856)
UnstakeTest:test_UnstakeOneAccountAndRewards() (gas: 409068)
UnstakeTest:test_UnstakeOneAccountWithLockUpAndAccruedMP() (gas: 531333)
UpgradeTest:test_RevertWhenNotOwner() (gas: 2704653)
UpgradeTest:test_UpgradeStakeManager() (gas: 5740239)
UnstakeTest:test_StakeMultipleAccounts() (gas: 499558)
UnstakeTest:test_StakeMultipleAccountsAndRewards() (gas: 505496)
UnstakeTest:test_StakeMultipleAccountsMPIncreasesMaxMPDoesNotChange() (gas: 842562)
UnstakeTest:test_StakeMultipleAccountsWithMinLockUp() (gas: 515935)
UnstakeTest:test_StakeMultipleAccountsWithRandomLockUp() (gas: 538001)
UnstakeTest:test_StakeOneAccount() (gas: 278291)
UnstakeTest:test_StakeOneAccountAndRewards() (gas: 284259)
UnstakeTest:test_StakeOneAccountMPIncreasesMaxMPDoesNotChange() (gas: 507705)
UnstakeTest:test_StakeOneAccountReachingMPLimit() (gas: 499054)
UnstakeTest:test_StakeOneAccountWithMaxLockUp() (gas: 298123)
UnstakeTest:test_StakeOneAccountWithMinLockUp() (gas: 299812)
UnstakeTest:test_StakeOneAccountWithRandomLockUp() (gas: 299878)
UnstakeTest:test_UnstakeBonusMPAndAccuredMP() (gas: 546326)
UnstakeTest:test_UnstakeMultipleAccounts() (gas: 705083)
UnstakeTest:test_UnstakeMultipleAccountsAndRewards() (gas: 800935)
UnstakeTest:test_UnstakeOneAccount() (gas: 480104)
UnstakeTest:test_UnstakeOneAccountAndAccruedMP() (gas: 502976)
UnstakeTest:test_UnstakeOneAccountAndRewards() (gas: 409152)
UnstakeTest:test_UnstakeOneAccountWithLockUpAndAccruedMP() (gas: 531453)
UpgradeTest:test_RevertWhenNotOwner() (gas: 2709437)
UpgradeTest:test_UpgradeStakeManager() (gas: 5749483)
VaultRegistrationTest:test_VaultRegistration() (gas: 62013)
WithdrawTest:test_CannotWithdrawStakedFunds() (gas: 311821)
WithdrawTest:test_CannotWithdrawStakedFunds() (gas: 311863)
XPNFTTokenTest:testApproveNotAllowed() (gas: 10500)
XPNFTTokenTest:testGetApproved() (gas: 10523)
XPNFTTokenTest:testIsApprovedForAll() (gas: 10698)
Expand Down
17 changes: 9 additions & 8 deletions src/RewardsStreamerMP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
error StakingManager__EmergencyModeEnabled();
error StakingManager__DurationCannotBeZero();

IERC20 public STAKING_TOKEN;

Check warning on line 36 in src/RewardsStreamerMP.sol

View workflow job for this annotation

GitHub Actions / lint

Variable name must be in mixedCase

uint256 public constant SCALE_FACTOR = 1e18;
uint256 public constant MP_RATE_PER_YEAR = 1;
Expand Down Expand Up @@ -198,7 +198,7 @@
}

_updateGlobalState();
_updateAccountMP(msg.sender);
_updateAccountMP(msg.sender, true);

Account storage account = accounts[msg.sender];
if (account.lockUntil != 0 && account.lockUntil > block.timestamp) {
Expand Down Expand Up @@ -253,7 +253,7 @@
}

_updateGlobalState();
_updateAccountMP(msg.sender);
_updateAccountMP(msg.sender, true);

uint256 additionalBonusMP = _calculateBonusMP(account.stakedBalance, lockPeriod);

Expand Down Expand Up @@ -289,7 +289,7 @@

function _unstake(uint256 amount, Account storage account, address accountAddress) internal {
_updateGlobalState();
_updateAccountMP(accountAddress);
_updateAccountMP(accountAddress, true);

uint256 previousStakedBalance = account.stakedBalance;

Expand Down Expand Up @@ -487,16 +487,17 @@
return accruedMP;
}

function _updateAccountMP(address accountAddress) internal {
function _updateAccountMP(address accountAddress, bool forceMPUpdate) internal {
Account storage account = accounts[accountAddress];
uint256 accruedMP = _getAccountPendingdMP(account);

account.mpAccrued += accruedMP;
account.lastMPUpdateTime = block.timestamp;
if (accruedMP > 0 || forceMPUpdate) {
account.mpAccrued += accruedMP;
account.lastMPUpdateTime = block.timestamp;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Different options here... I went with forceMPUpdate flag, instead of explicitly updating account.lastMPUpdateTime in places where necessary.

}

function updateAccountMP(address accountAddress) external onlyNotEmergencyMode {
_updateAccountMP(accountAddress);
_updateAccountMP(accountAddress, false);
}

function enableEmergencyMode() external onlyOwner {
Expand Down
Loading