diff --git a/.gas-report b/.gas-report index defbd73..72eb99d 100644 --- a/.gas-report +++ b/.gas-report @@ -1,37 +1,41 @@ | contracts/StakeManager.sol:StakeManager contract | | | | | | |--------------------------------------------------|-----------------|--------|--------|--------|---------| | Deployment Cost | Deployment Size | | | | | -| 2931383 | 14953 | | | | | +| 2465702 | 13041 | | | | | | Function Name | min | avg | median | max | # calls | -| EPOCH_SIZE | 307 | 307 | 307 | 307 | 1452 | +| EPOCH_SIZE | 307 | 307 | 307 | 307 | 1498 | | MAX_BOOST | 285 | 285 | 285 | 285 | 637 | | MAX_LOCKUP_PERIOD | 361 | 361 | 361 | 361 | 4 | -| MIN_LOCKUP_PERIOD | 287 | 287 | 287 | 287 | 12 | +| MIN_LOCKUP_PERIOD | 264 | 264 | 264 | 264 | 12 | | YEAR | 263 | 263 | 263 | 263 | 637 | -| accounts | 1597 | 1597 | 1597 | 1597 | 144321 | +| accounts | 1597 | 1597 | 1597 | 1597 | 144273 | | calculateMPToMint | 740 | 740 | 740 | 740 | 1276 | -| currentEpoch | 406 | 1691 | 2406 | 2406 | 28 | -| epochEnd | 627 | 627 | 627 | 4627 | 23603 | -| epochReward | 1403 | 2903 | 1403 | 5903 | 3 | -| executeAccount | 28897 | 76117 | 77904 | 171987 | 141882 | -| executeEpoch | 23436 | 146208 | 147383 | 204283 | 23542 | -| isVault | 540 | 927 | 540 | 2540 | 671 | -| lock | 23840 | 23840 | 23840 | 23840 | 1 | -| migrateTo | 23869 | 23875 | 23875 | 23881 | 2 | -| migration | 439 | 1439 | 1439 | 2439 | 4 | +| currentEpoch | 384 | 1050 | 384 | 2384 | 54 | +| epochEnd | 627 | 627 | 627 | 2627 | 23675 | +| epochReward | 1425 | 2925 | 1425 | 5925 | 3 | +| executeAccount(address) | 151152 | 151152 | 151152 | 151152 | 2 | +| executeAccount(address,uint256) | 26562 | 72616 | 74122 | 217879 | 141860 | +| executeEpoch() | 23458 | 120800 | 121956 | 938985 | 23564 | +| executeEpoch(uint256) | 23861 | 24497 | 23861 | 26090 | 7 | +| isVault | 540 | 948 | 540 | 2540 | 680 | +| lock | 23862 | 23862 | 23862 | 23862 | 1 | +| migrateTo | 23891 | 23897 | 23897 | 23903 | 2 | +| migration | 395 | 1395 | 1395 | 2395 | 4 | | migrationInitialize | 24602 | 24602 | 24602 | 24602 | 1 | +| newEpoch | 441 | 441 | 441 | 441 | 5 | | owner | 2432 | 2432 | 2432 | 2432 | 13 | -| pendingMPToBeMinted | 364 | 364 | 364 | 364 | 46448 | -| pendingReward | 364 | 1398 | 2364 | 2364 | 29 | +| pendingMPToBeMinted | 364 | 364 | 364 | 364 | 46432 | +| pendingReward | 386 | 1420 | 2386 | 2386 | 29 | | previousManager | 275 | 275 | 275 | 275 | 13 | -| setVault | 46239 | 46239 | 46239 | 46239 | 130 | +| setVault | 46239 | 46239 | 46239 | 46239 | 139 | | stake | 23983 | 23983 | 23983 | 23983 | 1 | -| stakeRewardEstimate | 436 | 2269 | 2436 | 2436 | 12 | -| stakedToken | 273 | 273 | 273 | 273 | 687 | -| startMigration | 108037 | 108045 | 108049 | 108049 | 3 | -| totalSupply | 740 | 1921 | 2740 | 2740 | 22 | -| totalSupplyBalance | 385 | 1785 | 2385 | 2385 | 20 | -| totalSupplyMP | 385 | 385 | 385 | 2385 | 46469 | +| stakeRewardEstimate | 436 | 2345 | 2436 | 2436 | 22 | +| stakedToken | 295 | 295 | 295 | 295 | 696 | +| startMigration | 103580 | 103588 | 103592 | 103592 | 3 | +| startTime | 306 | 306 | 306 | 306 | 21 | +| totalSupply | 762 | 1943 | 2762 | 2762 | 22 | +| totalSupplyBalance | 407 | 1807 | 2407 | 2407 | 20 | +| totalSupplyMP | 362 | 362 | 362 | 2362 | 46453 | | unstake | 23819 | 23819 | 23819 | 23819 | 1 | @@ -40,7 +44,7 @@ | Deployment Cost | Deployment Size | | | | | | 0 | 0 | | | | | | Function Name | min | avg | median | max | # calls | -| getExpiredMP | 2427 | 2427 | 2427 | 2427 | 23598 | +| getExpiredMP | 2427 | 2427 | 2427 | 2427 | 23725 | | transferOwnership | 28533 | 28533 | 28533 | 28533 | 1 | @@ -49,13 +53,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 | 670 | -| stake | 27265 | 283296 | 267703 | 353862 | 675 | +| acceptMigration | 35280 | 35280 | 35280 | 35280 | 2 | +| leave | 35266 | 35266 | 35266 | 35266 | 1 | +| lock | 43329 | 96146 | 64421 | 204745 | 7 | +| owner | 362 | 362 | 362 | 362 | 679 | +| stake | 27265 | 282079 | 265719 | 351866 | 684 | | stakedToken | 212 | 212 | 212 | 212 | 2 | -| unstake | 42163 | 92598 | 80285 | 207456 | 11 | +| unstake | 40167 | 95818 | 78688 | 233549 | 11 | | contracts/VaultFactory.sol:VaultFactory contract | | | | | | @@ -63,7 +67,7 @@ | Deployment Cost | Deployment Size | | | | | | 0 | 0 | | | | | | Function Name | min | avg | median | max | # calls | -| createVault | 696531 | 696531 | 696531 | 696531 | 674 | +| createVault | 696553 | 696553 | 696553 | 696553 | 683 | | setStakeManager | 23710 | 26669 | 26076 | 30222 | 3 | | stakeManager | 368 | 1868 | 2368 | 2368 | 4 | @@ -73,24 +77,24 @@ | Deployment Cost | Deployment Size | | | | | | 0 | 0 | | | | | | Function Name | min | avg | median | max | # calls | -| approve | 46175 | 46237 | 46199 | 46367 | 670 | -| balanceOf | 561 | 2108 | 2561 | 2561 | 30693 | +| approve | 46175 | 46239 | 46199 | 46367 | 679 | +| balanceOf | 561 | 2107 | 2561 | 2561 | 30744 | | script/Deploy.s.sol:Deploy contract | | | | | | |-------------------------------------|-----------------|---------|---------|---------|---------| | Deployment Cost | Deployment Size | | | | | -| 6532373 | 31450 | | | | | +| 6118940 | 29538 | | | | | | Function Name | min | avg | median | max | # calls | -| run | 5752636 | 5752636 | 5752636 | 5752636 | 56 | +| run | 5316305 | 5316305 | 5316305 | 5316305 | 66 | | script/DeployMigrationStakeManager.s.sol:DeployMigrationStakeManager contract | | | | | | |-------------------------------------------------------------------------------|-----------------|---------|---------|---------|---------| | Deployment Cost | Deployment Size | | | | | -| 3695815 | 18218 | | | | | +| 3282471 | 16306 | | | | | | Function Name | min | avg | median | max | # calls | -| run | 2738306 | 2738306 | 2738306 | 2738306 | 9 | +| run | 2302633 | 2302633 | 2302633 | 2302633 | 19 | | script/DeploymentConfig.s.sol:DeploymentConfig contract | | | | | | @@ -98,7 +102,7 @@ | Deployment Cost | Deployment Size | | | | | | 0 | 0 | | | | | | Function Name | min | avg | median | max | # calls | -| activeNetworkConfig | 455 | 455 | 455 | 455 | 112 | +| activeNetworkConfig | 455 | 455 | 455 | 455 | 132 | | test/mocks/BrokenERC20.s.sol:BrokenERC20 contract | | | | | | @@ -113,9 +117,9 @@ | test/script/DeployBroken.s.sol:DeployBroken contract | | | | | | |------------------------------------------------------|-----------------|---------|---------|---------|---------| | Deployment Cost | Deployment Size | | | | | -| 5217091 | 25248 | | | | | +| 4803693 | 23336 | | | | | | Function Name | min | avg | median | max | # calls | -| run | 4592458 | 4592458 | 4592458 | 4592458 | 1 | +| run | 4156127 | 4156127 | 4156127 | 4156127 | 1 | diff --git a/.gas-snapshot b/.gas-snapshot index 4a3aea6..b013b9b 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,57 +1,67 @@ CreateVaultTest:testDeployment() (gas: 9774) -CreateVaultTest:test_createVault() (gas: 714000) -ExecuteAccountTest:testDeployment() (gas: 28742) -ExecuteAccountTest:test_ExecuteAccountLimit() (gas: 1624826) -ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 5410346) -ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1292635) -ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 338282264) -ExecuteEpochTest:testDeployment() (gas: 28720) -ExecuteEpochTest:testNewDeployment() (gas: 30815) -ExecuteEpochTest:test_ExecuteEpochShouldIncreaseEpoch() (gas: 119862) -ExecuteEpochTest:test_ExecuteEpochShouldIncreasePendingReward() (gas: 278283) -ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochBeforeEnd() (gas: 43024) -ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 154182) -LeaveTest:testDeployment() (gas: 28720) -LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 1329923) -LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 31942) -LockTest:testDeployment() (gas: 28720) -LockTest:test_NewLockupPeriod() (gas: 1332993) -LockTest:test_RevertWhen_InvalidNewLockupPeriod() (gas: 1305322) -LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1444177) -LockTest:test_RevertWhen_SenderIsNotVault() (gas: 31834) -LockTest:test_ShouldIncreaseBonusMP() (gas: 1315569) -LockTest:test_UpdateLockupPeriod() (gas: 1588106) -MigrateTest:testDeployment() (gas: 28720) -MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 1293998) -MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 31954) -MigrationInitializeTest:testDeployment() (gas: 28720) -MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 6117874) -MigrationStakeManagerTest:testDeployment() (gas: 28720) -MigrationStakeManagerTest:testNewDeployment() (gas: 30859) -MigrationStakeManagerTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 154170) +CreateVaultTest:test_createVault() (gas: 714022) +ExecuteAccountTest:testDeployment() (gas: 28785) +ExecuteAccountTest:test_ExecuteAccountLimit() (gas: 1580255) +ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 5297777) +ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1788412) +ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 321397553) +ExecuteEpochTest:testDeployment() (gas: 28786) +ExecuteEpochTest:testNewDeployment() (gas: 30858) +ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterEpochEnd() (gas: 1366211) +ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1385045) +ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 1629507) +ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1395200) +ExecuteEpochTest:test_ExecuteEpochExecuteEpochAfterEnd() (gas: 1936210) +ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochs() (gas: 2523553) +ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1479491) +ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 2533729) +ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1489623) +ExecuteEpochTest:test_ExecuteEpochNewEpoch() (gas: 1122339) +ExecuteEpochTest:test_ExecuteEpochShouldIncreaseEpoch() (gas: 92413) +ExecuteEpochTest:test_ExecuteEpochShouldIncreasePendingReward() (gas: 256446) +ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochBeforeEnd() (gas: 38984) +ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 149638) +LeaveTest:testDeployment() (gas: 28763) +LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 1327993) +LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 31964) +LockTest:testDeployment() (gas: 28763) +LockTest:test_NewLockupPeriod() (gas: 1329045) +LockTest:test_RevertWhen_InvalidNewLockupPeriod() (gas: 1301374) +LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1545884) +LockTest:test_RevertWhen_SenderIsNotVault() (gas: 31856) +LockTest:test_ShouldIncreaseBonusMP() (gas: 1311575) +LockTest:test_UpdateLockupPeriod() (gas: 1634540) +MigrateTest:testDeployment() (gas: 28763) +MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 1292046) +MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 31976) +MigrationInitializeTest:testDeployment() (gas: 28763) +MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 5181388) +MigrationStakeManagerTest:testDeployment() (gas: 28763) +MigrationStakeManagerTest:testNewDeployment() (gas: 30902) +MigrationStakeManagerTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 149603) SetStakeManagerTest:testDeployment() (gas: 9774) SetStakeManagerTest:test_RevertWhen_InvalidStakeManagerAddress() (gas: 63105) SetStakeManagerTest:test_SetStakeManager() (gas: 41301) -StakeManagerTest:testDeployment() (gas: 28492) -StakeTest:testDeployment() (gas: 28698) -StakeTest:test_RevertWhen_InvalidLockupPeriod() (gas: 1084628) -StakeTest:test_RevertWhen_Restake() (gas: 1320817) -StakeTest:test_RevertWhen_RestakeWithLock() (gas: 1324854) +StakeManagerTest:testDeployment() (gas: 28535) +StakeTest:testDeployment() (gas: 28741) +StakeTest:test_RevertWhen_InvalidLockupPeriod() (gas: 1080635) +StakeTest:test_RevertWhen_Restake() (gas: 1316847) +StakeTest:test_RevertWhen_RestakeWithLock() (gas: 1320861) StakeTest:test_RevertWhen_SenderIsNotVault() (gas: 32018) -StakeTest:test_RevertWhen_StakeIsTooLow() (gas: 819736) +StakeTest:test_RevertWhen_StakeIsTooLow() (gas: 817762) StakeTest:test_RevertWhen_StakeTokenTransferFails() (gas: 211363) -StakeTest:test_StakeWithLockBonusMP() (gas: 2363509) -StakeTest:test_StakeWithoutLockUpTimeMintsMultiplierPoints() (gas: 1325221) +StakeTest:test_StakeWithLockBonusMP() (gas: 2357564) +StakeTest:test_StakeWithoutLockUpTimeMintsMultiplierPoints() (gas: 1322003) StakedTokenTest:testStakeToken() (gas: 7616) -UnstakeTest:testDeployment() (gas: 28742) -UnstakeTest:test_RevertWhen_AmountMoreThanBalance() (gas: 1301377) -UnstakeTest:test_RevertWhen_FundsLocked() (gas: 1348133) +UnstakeTest:testDeployment() (gas: 28785) +UnstakeTest:test_RevertWhen_AmountMoreThanBalance() (gas: 1297407) +UnstakeTest:test_RevertWhen_FundsLocked() (gas: 1342144) UnstakeTest:test_RevertWhen_SenderIsNotVault() (gas: 31857) -UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 7409934) -UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1322723) -UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1422611) -UserFlowsTest:testDeployment() (gas: 28720) -UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8,uint128) (runs: 106, μ: 141088719, ~: 140458057) -UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1465703) -UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 2501847) +UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 6526945) +UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1319573) +UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1440205) +UserFlowsTest:testDeployment() (gas: 28763) +UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8,uint128) (runs: 106, μ: 131402731, ~: 130856521) +UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1481301) +UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 2494771) VaultFactoryTest:testDeployment() (gas: 9774) \ No newline at end of file diff --git a/certora/specs/StakeManagerStartMigration.spec b/certora/specs/StakeManagerStartMigration.spec index c1f36f9..a57b5d3 100644 --- a/certora/specs/StakeManagerStartMigration.spec +++ b/certora/specs/StakeManagerStartMigration.spec @@ -19,6 +19,7 @@ definition blockedWhenMigrating(method f) returns bool = ( f.selector == sig:unstake(uint256).selector || f.selector == sig:lock(uint256).selector || f.selector == sig:executeEpoch().selector || + f.selector == sig:executeEpoch(uint256).selector || f.selector == sig:startMigration(address).selector || f.selector == sig:migrationInitialize(uint256,uint256,uint256,uint256,uint256,uint256,uint256).selector ); diff --git a/contracts/StakeManager.sol b/contracts/StakeManager.sol index d6201d8..696bed9 100644 --- a/contracts/StakeManager.sol +++ b/contracts/StakeManager.sol @@ -74,6 +74,7 @@ contract StakeManager is Ownable { uint256 public currentEpoch; uint256 public pendingReward; + uint256 public immutable startTime; uint256 public pendingMPToBeMinted; uint256 public totalSupplyMP; @@ -136,33 +137,37 @@ contract StakeManager is Ownable { } /** - * @notice Process epoch if it has ended + * @notice Release rewards for current epoch and increase epoch up to _limitEpoch + * @param _limitEpoch Until what epoch it should be executed */ - modifier finalizeEpoch() { - if (block.timestamp >= epochEnd() && address(migration) == address(0)) { - uint256 expiredMP = stakeRewardEstimate.getExpiredMP(currentEpoch); + function finalizeEpoch(uint256 _limitEpoch) private { + uint256 tempCurrentEpoch = currentEpoch; + while (tempCurrentEpoch < _limitEpoch) { + Epoch storage thisEpoch = epochs[tempCurrentEpoch]; + uint256 expiredMP = stakeRewardEstimate.getExpiredMP(tempCurrentEpoch); if (expiredMP > 0) { totalMPPerEpoch -= expiredMP; - stakeRewardEstimate.deleteExpiredMP(currentEpoch); + stakeRewardEstimate.deleteExpiredMP(tempCurrentEpoch); } - 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; + if (tempCurrentEpoch == currentEpoch) { + thisEpoch.epochReward = epochReward(); + pendingReward += thisEpoch.epochReward; + } + thisEpoch.totalSupply = totalSupply(); //create new epoch - currentEpoch++; - epochs[currentEpoch].startTime = block.timestamp; + tempCurrentEpoch++; } - _; + currentEpoch = tempCurrentEpoch; } constructor(address _stakedToken, address _previousManager) { - epochs[0].startTime = block.timestamp; + startTime = (_previousManager == address(0)) ? block.timestamp : StakeManager(_previousManager).startTime(); previousManager = StakeManager(_previousManager); stakedToken = ERC20(_stakedToken); if (address(previousManager) != address(0)) { @@ -181,7 +186,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(newEpoch()); Account storage account = accounts[msg.sender]; if (account.balance > 0 || account.lockUntil != 0) { revert StakeManager__AlreadyStaked(); @@ -220,13 +226,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(newEpoch()); Account storage account = accounts[msg.sender]; if (_amount > account.balance) { revert StakeManager__InsufficientFunds(); @@ -240,7 +241,6 @@ contract StakeManager is Ownable { uint256 reducedInitialMP = Math.mulDiv(_amount, account.bonusMP, account.balance); uint256 mpPerEpoch = _getMPToMint(account.balance, EPOCH_SIZE); - stakeRewardEstimate.decrementExpiredMP(account.mpLimitEpoch, mpPerEpoch); if (account.mpLimitEpoch < currentEpoch) { totalMPPerEpoch -= mpPerEpoch; @@ -266,8 +266,8 @@ contract StakeManager is Ownable { onlyVault onlyAccountInitialized(msg.sender) noPendingMigration - finalizeEpoch { + finalizeEpoch(newEpoch()); Account storage account = accounts[msg.sender]; _processAccount(account, currentEpoch); uint256 lockUntil = account.lockUntil; @@ -288,11 +288,32 @@ contract StakeManager is Ownable { } /** - * @notice Release rewards for current epoch and increase epoch. - * @dev only executes the prerequisite modifier finalizeEpoch + * @notice Release rewards for current epoch and increase epoch to latest epoch. + */ + function executeEpoch() external noPendingMigration { + finalizeEpoch(newEpoch()); + } + + /** + * @notice Release rewards for current epoch and increase epoch up to _limitEpoch + * @param _limitEpoch Until what epoch it should be executed */ - function executeEpoch() external noPendingMigration finalizeEpoch { - return; //see modifier finalizeEpoch + function executeEpoch(uint256 _limitEpoch) external noPendingMigration { + if (newEpoch() < _limitEpoch) { + revert StakeManager__InvalidLimitEpoch(); + } + finalizeEpoch(_limitEpoch); + } + + /** + * @notice Execute rewards for account until last possible epoch reached + * @param _vault Referred account + */ + function executeAccount(address _vault) external onlyAccountInitialized(_vault) { + if (address(migration) == address(0)) { + finalizeEpoch(newEpoch()); + } + _processAccount(accounts[_vault], currentEpoch); } /** @@ -300,14 +321,13 @@ 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) { + if (address(migration) == address(0)) { + if (newEpoch() < _limitEpoch) { + revert StakeManager__InvalidLimitEpoch(); + } + finalizeEpoch(_limitEpoch); + } _processAccount(accounts[_vault], _limitEpoch); } @@ -323,7 +343,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(newEpoch()); if (_migration == this || address(_migration) == address(0)) { revert StakeManager__InvalidMigration(); } @@ -334,7 +355,7 @@ contract StakeManager is Ownable { currentEpoch, totalSupplyMP, totalSupplyBalance, - epochs[currentEpoch].startTime, + startTime, totalMPPerEpoch, pendingMPToBeMinted, currentEpochTotalExpiredMP @@ -347,30 +368,30 @@ contract StakeManager is Ownable { * @param _currentEpoch epoch of old manager * @param _totalSupplyMP MP supply on old manager * @param _totalSupplyBalance stake supply on old manager - * @param _epochStartTime epoch start time of old manager + * @param _startTime start time of old manager */ function migrationInitialize( uint256 _currentEpoch, uint256 _totalSupplyMP, uint256 _totalSupplyBalance, - uint256 _epochStartTime, + uint256 _startTime, uint256 _totalMPPerEpoch, uint256 _pendingMPToBeMinted, uint256 _currentEpochExpiredMP ) external onlyPreviousManager + noPendingMigration { - if (address(migration) != address(0)) { - revert StakeManager__PendingMigration(); - } if (currentEpoch > 0) { revert StakeManager__AlreadyProcessedEpochs(); } + if (_startTime != startTime) { + revert StakeManager__InvalidMigration(); + } currentEpoch = _currentEpoch; totalSupplyMP = _totalSupplyMP; totalSupplyBalance = _totalSupplyBalance; - epochs[currentEpoch].startTime = _epochStartTime; totalMPPerEpoch = _totalMPPerEpoch; pendingMPToBeMinted = _pendingMPToBeMinted; currentEpochTotalExpiredMP = _currentEpochExpiredMP; @@ -443,7 +464,7 @@ contract StakeManager is Ownable { while (userEpoch < _limitEpoch) { Epoch storage iEpoch = epochs[userEpoch]; //mint multiplier points to that epoch - _mintMP(account, iEpoch.startTime + EPOCH_SIZE, iEpoch); + _mintMP(account, startTime + (EPOCH_SIZE * (userEpoch + 1)), iEpoch); uint256 userSupply = account.balance + account.totalMP; uint256 userEpochReward = Math.mulDiv(userSupply, iEpoch.epochReward, iEpoch.totalSupply); userReward += userEpochReward; @@ -593,6 +614,14 @@ contract StakeManager is Ownable { * @return _epochEnd end time of current epoch */ function epochEnd() public view returns (uint256 _epochEnd) { - return epochs[currentEpoch].startTime + EPOCH_SIZE; + return startTime + (EPOCH_SIZE * (currentEpoch + 1)); + } + + /** + * @notice Returns the last epoch that can be processed on current time + * @return _newEpoch the number of the epoch after all epochs that can be processed + */ + function newEpoch() public view returns (uint256 _newEpoch) { + _newEpoch = (block.timestamp - startTime) / EPOCH_SIZE; } } diff --git a/test/StakeManager.t.sol b/test/StakeManager.t.sol index 0d0431e..4224a47 100644 --- a/test/StakeManager.t.sol +++ b/test/StakeManager.t.sol @@ -454,6 +454,49 @@ contract ExecuteAccountTest is StakeManagerTest { uint256 currentEpoch = stakeManager.currentEpoch(); + vm.expectRevert(StakeManager.StakeManager__InvalidLimitEpoch.selector); + stakeManager.executeEpoch(currentEpoch + 1); + + vm.expectRevert(StakeManager.StakeManager__InvalidLimitEpoch.selector); + stakeManager.executeAccount(address(userVault), currentEpoch + 1); + + vm.warp(stakeManager.epochEnd() - 1); + + vm.expectRevert(StakeManager.StakeManager__InvalidLimitEpoch.selector); + stakeManager.executeEpoch(currentEpoch + 1); + + vm.expectRevert(StakeManager.StakeManager__InvalidLimitEpoch.selector); + stakeManager.executeAccount(address(userVault), currentEpoch + 1); + + vm.warp(stakeManager.epochEnd()); + + stakeManager.executeAccount(address(userVault), currentEpoch + 1); + stakeManager.executeEpoch(currentEpoch + 1); + + currentEpoch++; + + vm.expectRevert(StakeManager.StakeManager__InvalidLimitEpoch.selector); + stakeManager.executeEpoch(currentEpoch + 1); + + vm.expectRevert(StakeManager.StakeManager__InvalidLimitEpoch.selector); + stakeManager.executeAccount(address(userVault), currentEpoch + 1); + + vm.warp(stakeManager.epochEnd() + stakeManager.EPOCH_SIZE() - 1); + + vm.expectRevert(StakeManager.StakeManager__InvalidLimitEpoch.selector); + stakeManager.executeEpoch(currentEpoch + 2); + + vm.expectRevert(StakeManager.StakeManager__InvalidLimitEpoch.selector); + stakeManager.executeAccount(address(userVault), currentEpoch + 2); + + stakeManager.executeAccount(address(userVault), currentEpoch + 1); + stakeManager.executeEpoch(currentEpoch + 1); + + currentEpoch++; + + vm.expectRevert(StakeManager.StakeManager__InvalidLimitEpoch.selector); + stakeManager.executeEpoch(currentEpoch + 1); + vm.expectRevert(StakeManager.StakeManager__InvalidLimitEpoch.selector); stakeManager.executeAccount(address(userVault), currentEpoch + 1); } @@ -744,6 +787,130 @@ contract MigrationStakeManagerTest is StakeManagerTest { } contract ExecuteEpochTest is MigrationStakeManagerTest { + function test_ExecuteEpochNewEpoch() public { + uint256 firstEpochEnd = stakeManager.epochEnd(); + assertEq(stakeManager.currentEpoch(), 0, "Epoch not 0 at start of test"); + assertEq(stakeManager.newEpoch(), 0, "New epoch not 0 at start of test"); + stakeManager.executeEpoch(); + assertEq(stakeManager.currentEpoch(), 0, "Epoch should not increase if no time passed since start"); + + vm.warp(firstEpochEnd - 1); + assertEq(stakeManager.newEpoch(), 0, "New epoch not 0 if 1 second before epoch end"); + stakeManager.executeEpoch(); + assertEq(stakeManager.currentEpoch(), 0, "Epoch should not increase if 1 second before epoch end"); + + vm.warp(firstEpochEnd); + assertEq(stakeManager.newEpoch(), 1, "New epoch should be 1 if exactly at epochEnd"); + stakeManager.executeEpoch(); + assertEq(stakeManager.currentEpoch(), 1, "Current epoch should increased to 1 if exactly at epochEnd of 0"); + + vm.warp(firstEpochEnd + 1); + assertEq(stakeManager.newEpoch(), 1, "New epoch should be 1 if 1 second after epochend of 1"); + stakeManager.executeEpoch(); + assertEq(stakeManager.currentEpoch(), 1, "Current epoch should increase if 1 second after epochend of 1"); + + vm.warp(firstEpochEnd + (stakeManager.EPOCH_SIZE() * 99)); + assertEq(stakeManager.newEpoch(), 100); + stakeManager.executeEpoch(); + assertEq(stakeManager.currentEpoch(), 100); + } + + function test_ExecuteEpochExecuteEpochAfterEnd() public { + StakeVault userVault = _createStakingAccount(makeAddr("testUser"), 100_000, 0); + + vm.warp(stakeManager.epochEnd() + (stakeManager.EPOCH_SIZE() / 2)); + stakeManager.executeEpoch(); + stakeManager.executeAccount(address(userVault), stakeManager.currentEpoch()); + vm.warp(stakeManager.epochEnd()); + + stakeManager.executeEpoch(); + stakeManager.executeAccount(address(userVault), stakeManager.currentEpoch()); + + vm.warp(stakeManager.epochEnd() + (stakeManager.EPOCH_SIZE() * 2)); + stakeManager.executeEpoch(); + stakeManager.executeAccount(address(userVault), stakeManager.currentEpoch()); + } + + function test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochs() public { + StakeVault userVault = _createStakingAccount(makeAddr("testUser"), 100_000, 0); + + for (uint256 i = 0; i < 10; i++) { + vm.warp(stakeManager.epochEnd()); + stakeManager.executeEpoch(); + } + stakeManager.executeAccount(address(userVault), stakeManager.currentEpoch()); + } + + function test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsJumoMany() public { + StakeVault userVault = _createStakingAccount(makeAddr("testUser"), 100_000, 0); + + for (uint256 i = 0; i < 10; i++) { + vm.warp(stakeManager.epochEnd()); + } + stakeManager.executeEpoch(); + stakeManager.executeAccount(address(userVault), stakeManager.currentEpoch()); + } + + function test_ExecuteEpochExecuteAccountAfterManyEpochsJumoMany() public { + StakeVault userVault = _createStakingAccount(makeAddr("testUser"), 100_000, 0); + + for (uint256 i = 0; i < 10; i++) { + vm.warp(stakeManager.epochEnd()); + } + stakeManager.executeAccount(address(userVault)); + } + + function test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() public { + StakeVault userVault = _createStakingAccount(makeAddr("testUser"), 100_000, 0); + + for (uint256 i = 0; i < 10; i++) { + vm.warp(stakeManager.epochEnd() + (stakeManager.EPOCH_SIZE() / 10 - i)); + stakeManager.executeEpoch(); + } + stakeManager.executeAccount(address(userVault), stakeManager.currentEpoch()); + } + + function test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() public { + StakeVault userVault = _createStakingAccount(makeAddr("testUser"), 100_000, 0); + + for (uint256 i = 0; i < 10; i++) { + vm.warp(stakeManager.epochEnd() + (stakeManager.EPOCH_SIZE() / 10 - i)); + } + stakeManager.executeEpoch(); + stakeManager.executeAccount(address(userVault), stakeManager.currentEpoch()); + } + + function test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() public { + StakeVault userVault = _createStakingAccount(makeAddr("testUser"), 100_000, 0); + + for (uint256 i = 0; i < 10; i++) { + vm.warp(stakeManager.epochEnd() + (stakeManager.EPOCH_SIZE() / 10 - i)); + } + stakeManager.executeAccount(address(userVault)); + } + + function test_ExecuteEpochExecuteAccountAfterEpochEnd() public { + StakeVault userVault = _createStakingAccount(makeAddr("testUser"), 100_000, 0); + + vm.warp(stakeManager.epochEnd() + (stakeManager.EPOCH_SIZE() / 2)); + stakeManager.executeAccount(address(userVault), stakeManager.currentEpoch()); + + vm.warp(stakeManager.epochEnd()); + stakeManager.executeAccount(address(userVault), stakeManager.currentEpoch()); + + vm.warp(stakeManager.epochEnd() + (stakeManager.EPOCH_SIZE() * 2)); + stakeManager.executeAccount(address(userVault), stakeManager.currentEpoch()); + } + + function test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() public { + StakeVault userVault = _createStakingAccount(makeAddr("testUser"), 100_000, 0); + + for (uint256 i = 0; i < 10; i++) { + vm.warp(stakeManager.epochEnd() + (stakeManager.EPOCH_SIZE() / 10 - i)); + stakeManager.executeAccount(address(userVault), stakeManager.currentEpoch()); + } + } + function test_ExecuteEpochShouldNotIncreaseEpochBeforeEnd() public { assertEq(stakeManager.currentEpoch(), 0);