From ae25e4d48eae12591dfce3a604d0fa688ce50a81 Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt <3esmit@gmail.com> Date: Sun, 22 Sep 2024 14:36:24 -0300 Subject: [PATCH] fix(StakeManager): fix finalizeEpoch to use last epoch epochEnd --- .gas-report | 60 ++++++++++----------- .gas-snapshot | 106 ++++++++++++++++++------------------- contracts/StakeManager.sol | 52 ++++++++---------- 3 files changed, 106 insertions(+), 112 deletions(-) diff --git a/.gas-report b/.gas-report index 30ce1ab..70dca8f 100644 --- a/.gas-report +++ b/.gas-report @@ -1,34 +1,34 @@ | contracts/StakeManager.sol:StakeManager contract | | | | | | |--------------------------------------------------|-----------------|--------|--------|--------|---------| | Deployment Cost | Deployment Size | | | | | -| 2931383 | 14953 | | | | | +| 2469345 | 12797 | | | | | | Function Name | min | avg | median | max | # calls | | EPOCH_SIZE | 307 | 307 | 307 | 307 | 816 | | MAX_BOOST | 285 | 285 | 285 | 285 | 1 | | MAX_LOCKUP_PERIOD | 361 | 361 | 361 | 361 | 4 | | MIN_LOCKUP_PERIOD | 287 | 287 | 287 | 287 | 12 | | YEAR | 263 | 263 | 263 | 263 | 1 | -| accounts | 1597 | 1597 | 1597 | 1597 | 23368 | +| accounts | 1561 | 1561 | 1561 | 1561 | 22681 | | calculateMPToMint | 740 | 740 | 740 | 740 | 4 | | currentEpoch | 406 | 1691 | 2406 | 2406 | 28 | | epochEnd | 627 | 639 | 627 | 4627 | 635 | -| epochReward | 1403 | 2903 | 1403 | 5903 | 3 | -| executeAccount | 28897 | 95321 | 95004 | 171987 | 20929 | -| executeEpoch | 23436 | 169158 | 164483 | 204283 | 574 | -| isVault | 540 | 570 | 540 | 2540 | 18426 | +| epochReward | 1391 | 2891 | 1391 | 5891 | 3 | +| executeAccount | 30958 | 95378 | 95053 | 171350 | 20242 | +| executeEpoch | 23424 | 168403 | 163724 | 203524 | 574 | +| isVault | 540 | 571 | 540 | 2540 | 17739 | | lock | 23840 | 23840 | 23840 | 23840 | 1 | | migrateTo | 23869 | 23875 | 23875 | 23881 | 2 | -| migration | 439 | 1439 | 1439 | 2439 | 4 | -| migrationInitialize | 24602 | 24602 | 24602 | 24602 | 1 | -| owner | 2432 | 2432 | 2432 | 2432 | 13 | +| migration | 415 | 1415 | 1415 | 2415 | 4 | +| migrationInitialize | 24578 | 24578 | 24578 | 24578 | 1 | +| owner | 2408 | 2408 | 2408 | 2408 | 13 | | pendingMPToBeMinted | 364 | 364 | 364 | 364 | 512 | | pendingReward | 364 | 1398 | 2364 | 2364 | 29 | -| previousManager | 275 | 275 | 275 | 275 | 13 | -| setVault | 46239 | 46239 | 46239 | 46239 | 280 | +| previousManager | 263 | 263 | 263 | 263 | 13 | +| setVault | 46227 | 46227 | 46227 | 46227 | 280 | | stake | 23983 | 23983 | 23983 | 23983 | 1 | -| stakeRewardEstimate | 436 | 2269 | 2436 | 2436 | 12 | -| stakedToken | 273 | 273 | 273 | 273 | 18442 | -| startMigration | 108037 | 108045 | 108049 | 108049 | 3 | +| stakeRewardEstimate | 412 | 2245 | 2412 | 2412 | 12 | +| stakedToken | 261 | 261 | 261 | 261 | 17755 | +| startMigration | 107990 | 107998 | 108002 | 108002 | 3 | | totalSupply | 740 | 1921 | 2740 | 2740 | 22 | | totalSupplyBalance | 385 | 1785 | 2385 | 2385 | 20 | | totalSupplyMP | 385 | 437 | 385 | 2385 | 533 | @@ -40,7 +40,7 @@ | Deployment Cost | Deployment Size | | | | | | 0 | 0 | | | | | | Function Name | min | avg | median | max | # calls | -| getExpiredMP | 2427 | 2427 | 2427 | 2427 | 630 | +| getExpiredMP | 2427 | 2427 | 2427 | 2427 | 631 | | transferOwnership | 28533 | 28533 | 28533 | 28533 | 1 | @@ -49,13 +49,13 @@ | Deployment Cost | Deployment Size | | | | | | 0 | 0 | | | | | | Function Name | min | avg | median | max | # calls | -| acceptMigration | 35258 | 35258 | 35258 | 35258 | 2 | -| leave | 35244 | 35244 | 35244 | 35244 | 1 | -| lock | 45192 | 73900 | 66395 | 158442 | 7 | -| owner | 362 | 362 | 362 | 362 | 18425 | -| stake | 27265 | 268849 | 267631 | 353862 | 18430 | +| acceptMigration | 35246 | 35246 | 35246 | 35246 | 2 | +| leave | 35232 | 35232 | 35232 | 35232 | 1 | +| lock | 45376 | 99625 | 66468 | 193557 | 7 | +| owner | 362 | 362 | 362 | 362 | 17738 | +| stake | 27265 | 268933 | 267668 | 353899 | 17743 | | stakedToken | 212 | 212 | 212 | 212 | 2 | -| unstake | 42163 | 92598 | 80285 | 207456 | 11 | +| unstake | 42248 | 92540 | 80334 | 206828 | 11 | | contracts/VaultFactory.sol:VaultFactory contract | | | | | | @@ -63,7 +63,7 @@ | Deployment Cost | Deployment Size | | | | | | 0 | 0 | | | | | | Function Name | min | avg | median | max | # calls | -| createVault | 696531 | 696531 | 696531 | 696531 | 18429 | +| createVault | 696519 | 696519 | 696519 | 696519 | 17742 | | setStakeManager | 23710 | 26669 | 26076 | 30222 | 3 | | stakeManager | 368 | 1868 | 2368 | 2368 | 4 | @@ -73,24 +73,24 @@ | Deployment Cost | Deployment Size | | | | | | 0 | 0 | | | | | | Function Name | min | avg | median | max | # calls | -| approve | 46175 | 46198 | 46199 | 46211 | 18425 | -| balanceOf | 561 | 603 | 561 | 2561 | 43385 | +| approve | 46175 | 46198 | 46199 | 46211 | 17738 | +| balanceOf | 561 | 605 | 561 | 2561 | 42012 | | script/Deploy.s.sol:Deploy contract | | | | | | |-------------------------------------|-----------------|---------|---------|---------|---------| | Deployment Cost | Deployment Size | | | | | -| 6532373 | 31450 | | | | | +| 6070229 | 29294 | | | | | | Function Name | min | avg | median | max | # calls | -| run | 5752636 | 5752636 | 5752636 | 5752636 | 56 | +| run | 5320405 | 5320405 | 5320405 | 5320405 | 56 | | script/DeployMigrationStakeManager.s.sol:DeployMigrationStakeManager contract | | | | | | |-------------------------------------------------------------------------------|-----------------|---------|---------|---------|---------| | Deployment Cost | Deployment Size | | | | | -| 3695815 | 18218 | | | | | +| 3233781 | 16062 | | | | | | Function Name | min | avg | median | max | # calls | -| run | 2738306 | 2738306 | 2738306 | 2738306 | 9 | +| run | 2306051 | 2306051 | 2306051 | 2306051 | 9 | | script/DeploymentConfig.s.sol:DeploymentConfig contract | | | | | | @@ -113,9 +113,9 @@ | test/script/DeployBroken.s.sol:DeployBroken contract | | | | | | |------------------------------------------------------|-----------------|---------|---------|---------|---------| | Deployment Cost | Deployment Size | | | | | -| 5217091 | 25248 | | | | | +| 4754997 | 23092 | | | | | | Function Name | min | avg | median | max | # calls | -| run | 4592458 | 4592458 | 4592458 | 4592458 | 1 | +| run | 4160227 | 4160227 | 4160227 | 4160227 | 1 | diff --git a/.gas-snapshot b/.gas-snapshot index 3b05fd0..ff15667 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,57 +1,57 @@ CreateVaultTest:testDeployment() (gas: 9774) -CreateVaultTest:test_createVault() (gas: 692936) -ExecuteAccountTest:testDeployment() (gas: 28742) -ExecuteAccountTest:test_ExecuteAccountLimit() (gas: 1318328) -ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 3861122) -ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1154383) -ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 110701476) -ExecuteEpochTest:testDeployment() (gas: 28720) -ExecuteEpochTest:testNewDeployment() (gas: 30815) -ExecuteEpochTest:test_ExecuteEpochShouldIncreaseEpoch() (gas: 94810) -ExecuteEpochTest:test_ExecuteEpochShouldIncreasePendingReward() (gas: 253041) -ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochBeforeEnd() (gas: 17972) -ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 105698) -LeaveTest:testDeployment() (gas: 28720) -LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 1154639) -LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 10750) -LockTest:testDeployment() (gas: 28720) -LockTest:test_NewLockupPeriod() (gas: 1143197) -LockTest:test_RevertWhen_InvalidNewLockupPeriod() (gas: 1134814) -LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1229677) -LockTest:test_RevertWhen_SenderIsNotVault() (gas: 10630) -LockTest:test_ShouldIncreaseBonusMP() (gas: 1123297) -LockTest:test_UpdateLockupPeriod() (gas: 1260010) -MigrateTest:testDeployment() (gas: 28720) -MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 1152278) -MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 10750) -MigrationInitializeTest:testDeployment() (gas: 28720) -MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 5506950) -MigrationStakeManagerTest:testDeployment() (gas: 28720) -MigrationStakeManagerTest:testNewDeployment() (gas: 30859) -MigrationStakeManagerTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 105686) +CreateVaultTest:test_createVault() (gas: 713988) +ExecuteAccountTest:testDeployment() (gas: 28694) +ExecuteAccountTest:test_ExecuteAccountLimit() (gas: 1623530) +ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 5407775) +ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1294709) +ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 337956933) +ExecuteEpochTest:testDeployment() (gas: 28672) +ExecuteEpochTest:testNewDeployment() (gas: 30767) +ExecuteEpochTest:test_ExecuteEpochShouldIncreaseEpoch() (gas: 119103) +ExecuteEpochTest:test_ExecuteEpochShouldIncreasePendingReward() (gas: 277647) +ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochBeforeEnd() (gas: 43109) +ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 154075) +LeaveTest:testDeployment() (gas: 28672) +LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 1329912) +LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 31942) +LockTest:testDeployment() (gas: 28672) +LockTest:test_NewLockupPeriod() (gas: 1333043) +LockTest:test_RevertWhen_InvalidNewLockupPeriod() (gas: 1305408) +LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1588267) +LockTest:test_RevertWhen_SenderIsNotVault() (gas: 31834) +LockTest:test_ShouldIncreaseBonusMP() (gas: 1315583) +LockTest:test_UpdateLockupPeriod() (gas: 1662367) +MigrateTest:testDeployment() (gas: 28672) +MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 1293999) +MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 31954) +MigrationInitializeTest:testDeployment() (gas: 28672) +MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 5192974) +MigrationStakeManagerTest:testDeployment() (gas: 28672) +MigrationStakeManagerTest:testNewDeployment() (gas: 30811) +MigrationStakeManagerTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 154063) SetStakeManagerTest:testDeployment() (gas: 9774) -SetStakeManagerTest:test_RevertWhen_InvalidStakeManagerAddress() (gas: 20481) -SetStakeManagerTest:test_SetStakeManager() (gas: 19869) -StakeManagerTest:testDeployment() (gas: 28492) -StakeTest:testDeployment() (gas: 28698) -StakeTest:test_RevertWhen_InvalidLockupPeriod() (gas: 892168) -StakeTest:test_RevertWhen_Restake() (gas: 1157929) -StakeTest:test_RevertWhen_RestakeWithLock() (gas: 1159442) -StakeTest:test_RevertWhen_SenderIsNotVault() (gas: 10674) -StakeTest:test_RevertWhen_StakeIsTooLow() (gas: 745276) -StakeTest:test_RevertWhen_StakeTokenTransferFails() (gas: 175040) -StakeTest:test_StakeWithLockBonusMP() (gas: 2023009) -StakeTest:test_StakeWithoutLockUpTimeMintsMultiplierPoints() (gas: 1028803) +SetStakeManagerTest:test_RevertWhen_InvalidStakeManagerAddress() (gas: 63105) +SetStakeManagerTest:test_SetStakeManager() (gas: 41301) +StakeManagerTest:testDeployment() (gas: 28444) +StakeTest:testDeployment() (gas: 28650) +StakeTest:test_RevertWhen_InvalidLockupPeriod() (gas: 1084774) +StakeTest:test_RevertWhen_Restake() (gas: 1320915) +StakeTest:test_RevertWhen_RestakeWithLock() (gas: 1324952) +StakeTest:test_RevertWhen_SenderIsNotVault() (gas: 32018) +StakeTest:test_RevertWhen_StakeIsTooLow() (gas: 819797) +StakeTest:test_RevertWhen_StakeTokenTransferFails() (gas: 211363) +StakeTest:test_StakeWithLockBonusMP() (gas: 2363512) +StakeTest:test_StakeWithoutLockUpTimeMintsMultiplierPoints() (gas: 1325199) StakedTokenTest:testStakeToken() (gas: 7616) -UnstakeTest:testDeployment() (gas: 28742) -UnstakeTest:test_RevertWhen_AmountMoreThanBalance() (gas: 1132893) -UnstakeTest:test_RevertWhen_FundsLocked() (gas: 1158045) -UnstakeTest:test_RevertWhen_SenderIsNotVault() (gas: 10653) -UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 5535329) -UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1026305) -UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1113953) -UserFlowsTest:testDeployment() (gas: 28720) -UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8) (runs: 1000, μ: 70728281, ~: 30142776) -UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1114841) -UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 1950367) +UnstakeTest:testDeployment() (gas: 28694) +UnstakeTest:test_RevertWhen_AmountMoreThanBalance() (gas: 1301475) +UnstakeTest:test_RevertWhen_FundsLocked() (gas: 1348316) +UnstakeTest:test_RevertWhen_SenderIsNotVault() (gas: 31857) +UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 7382927) +UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1322773) +UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1422152) +UserFlowsTest:testDeployment() (gas: 28672) +UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8) (runs: 1001, μ: 85684344, ~: 40568918) +UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1465329) +UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 2502005) VaultFactoryTest:testDeployment() (gas: 9774) \ No newline at end of file diff --git a/contracts/StakeManager.sol b/contracts/StakeManager.sol index d6201d8..5544067 100644 --- a/contracts/StakeManager.sol +++ b/contracts/StakeManager.sol @@ -138,27 +138,30 @@ contract StakeManager is Ownable { /** * @notice Process epoch if it has ended */ - modifier finalizeEpoch() { - if (block.timestamp >= epochEnd() && address(migration) == address(0)) { + function finalizeEpoch() private { + if (address(migration) != address(0)) { + return; + } + Epoch storage thisEpoch = epochs[currentEpoch]; + if (block.timestamp >= thisEpoch.startTime + EPOCH_SIZE) { uint256 expiredMP = stakeRewardEstimate.getExpiredMP(currentEpoch); if (expiredMP > 0) { totalMPPerEpoch -= expiredMP; stakeRewardEstimate.deleteExpiredMP(currentEpoch); } - epochs[currentEpoch].estimatedMP = totalMPPerEpoch - currentEpochTotalExpiredMP; + thisEpoch.estimatedMP = totalMPPerEpoch - currentEpochTotalExpiredMP; delete currentEpochTotalExpiredMP; - pendingMPToBeMinted += epochs[currentEpoch].estimatedMP; + pendingMPToBeMinted += thisEpoch.estimatedMP; //finalize current epoch - epochs[currentEpoch].epochReward = epochReward(); - epochs[currentEpoch].totalSupply = totalSupply(); - pendingReward += epochs[currentEpoch].epochReward; + thisEpoch.epochReward = epochReward(); + thisEpoch.totalSupply = totalSupply(); + pendingReward += thisEpoch.epochReward; //create new epoch currentEpoch++; - epochs[currentEpoch].startTime = block.timestamp; + epochs[currentEpoch].startTime = thisEpoch.startTime + EPOCH_SIZE; } - _; } constructor(address _stakedToken, address _previousManager) { @@ -181,7 +184,8 @@ contract StakeManager is Ownable { * @dev Reverts when account has already staked funds. * @dev Reverts when amount staked results in less than 1 MP per epoch. */ - function stake(uint256 _amount, uint256 _secondsToLock) external onlyVault noPendingMigration finalizeEpoch { + function stake(uint256 _amount, uint256 _secondsToLock) external onlyVault noPendingMigration { + finalizeEpoch(); Account storage account = accounts[msg.sender]; if (account.balance > 0 || account.lockUntil != 0) { revert StakeManager__AlreadyStaked(); @@ -220,13 +224,8 @@ contract StakeManager is Ownable { /** * leaves the staking pool and withdraws all funds; */ - function unstake(uint256 _amount) - external - onlyVault - onlyAccountInitialized(msg.sender) - noPendingMigration - finalizeEpoch - { + function unstake(uint256 _amount) external onlyVault onlyAccountInitialized(msg.sender) noPendingMigration { + finalizeEpoch(); Account storage account = accounts[msg.sender]; if (_amount > account.balance) { revert StakeManager__InsufficientFunds(); @@ -266,8 +265,8 @@ contract StakeManager is Ownable { onlyVault onlyAccountInitialized(msg.sender) noPendingMigration - finalizeEpoch { + finalizeEpoch(); Account storage account = accounts[msg.sender]; _processAccount(account, currentEpoch); uint256 lockUntil = account.lockUntil; @@ -291,8 +290,8 @@ contract StakeManager is Ownable { * @notice Release rewards for current epoch and increase epoch. * @dev only executes the prerequisite modifier finalizeEpoch */ - function executeEpoch() external noPendingMigration finalizeEpoch { - return; //see modifier finalizeEpoch + function executeEpoch() external noPendingMigration { + finalizeEpoch(); } /** @@ -300,14 +299,8 @@ contract StakeManager is Ownable { * @param _vault Referred account * @param _limitEpoch Until what epoch it should be executed */ - function executeAccount( - address _vault, - uint256 _limitEpoch - ) - external - onlyAccountInitialized(_vault) - finalizeEpoch - { + function executeAccount(address _vault, uint256 _limitEpoch) external onlyAccountInitialized(_vault) { + finalizeEpoch(); _processAccount(accounts[_vault], _limitEpoch); } @@ -323,7 +316,8 @@ contract StakeManager is Ownable { * @notice starts migration to new StakeManager * @param _migration new StakeManager */ - function startMigration(StakeManager _migration) external onlyOwner noPendingMigration finalizeEpoch { + function startMigration(StakeManager _migration) external onlyOwner noPendingMigration { + finalizeEpoch(); if (_migration == this || address(_migration) == address(0)) { revert StakeManager__InvalidMigration(); }