fix(StakeManager): fix finalizeEpoch to use last epoch epochEnd and add tests to catch error

This commit is contained in:
Ricardo Guilherme Schmidt 2024-09-25 12:45:59 -03:00
parent 4a545a0ed4
commit 142137dee7
4 changed files with 167 additions and 110 deletions

View File

@ -1,37 +1,37 @@
| contracts/StakeManager.sol:StakeManager contract | | | | | | | contracts/StakeManager.sol:StakeManager contract | | | | | |
|--------------------------------------------------|-----------------|--------|--------|--------|---------| |--------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 2931383 | 14953 | | | | | | 2469345 | 12797 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| EPOCH_SIZE | 307 | 307 | 307 | 307 | 1452 | | EPOCH_SIZE | 307 | 307 | 307 | 307 | 1476 |
| MAX_BOOST | 285 | 285 | 285 | 285 | 637 | | MAX_BOOST | 285 | 285 | 285 | 285 | 637 |
| MAX_LOCKUP_PERIOD | 361 | 361 | 361 | 361 | 4 | | MAX_LOCKUP_PERIOD | 361 | 361 | 361 | 361 | 4 |
| MIN_LOCKUP_PERIOD | 287 | 287 | 287 | 287 | 12 | | MIN_LOCKUP_PERIOD | 287 | 287 | 287 | 287 | 12 |
| YEAR | 263 | 263 | 263 | 263 | 637 | | YEAR | 263 | 263 | 263 | 263 | 637 |
| accounts | 1597 | 1597 | 1597 | 1597 | 144321 | | accounts | 1561 | 1561 | 1561 | 1561 | 144375 |
| calculateMPToMint | 740 | 740 | 740 | 740 | 1276 | | calculateMPToMint | 740 | 740 | 740 | 740 | 1276 |
| currentEpoch | 406 | 1691 | 2406 | 2406 | 28 | | currentEpoch | 406 | 1188 | 406 | 2406 | 46 |
| epochEnd | 627 | 627 | 627 | 4627 | 23603 | | epochEnd | 627 | 627 | 627 | 4627 | 23648 |
| epochReward | 1403 | 2903 | 1403 | 5903 | 3 | | epochReward | 1391 | 2891 | 1391 | 5891 | 3 |
| executeAccount | 28897 | 76117 | 77904 | 171987 | 141882 | | executeAccount | 30958 | 76171 | 77953 | 240555 | 141954 |
| executeEpoch | 23436 | 146208 | 147383 | 204283 | 23542 | | executeEpoch | 23424 | 145441 | 146624 | 203524 | 23574 |
| isVault | 540 | 927 | 540 | 2540 | 671 | | isVault | 540 | 939 | 540 | 2540 | 676 |
| lock | 23840 | 23840 | 23840 | 23840 | 1 | | lock | 23840 | 23840 | 23840 | 23840 | 1 |
| migrateTo | 23869 | 23875 | 23875 | 23881 | 2 | | migrateTo | 23869 | 23875 | 23875 | 23881 | 2 |
| migration | 439 | 1439 | 1439 | 2439 | 4 | | migration | 415 | 1415 | 1415 | 2415 | 4 |
| migrationInitialize | 24602 | 24602 | 24602 | 24602 | 1 | | migrationInitialize | 24578 | 24578 | 24578 | 24578 | 1 |
| owner | 2432 | 2432 | 2432 | 2432 | 13 | | owner | 2408 | 2408 | 2408 | 2408 | 13 |
| pendingMPToBeMinted | 364 | 364 | 364 | 364 | 46448 | | pendingMPToBeMinted | 364 | 364 | 364 | 364 | 46466 |
| pendingReward | 364 | 1398 | 2364 | 2364 | 29 | | pendingReward | 364 | 1398 | 2364 | 2364 | 29 |
| previousManager | 275 | 275 | 275 | 275 | 13 | | previousManager | 263 | 263 | 263 | 263 | 13 |
| setVault | 46239 | 46239 | 46239 | 46239 | 130 | | setVault | 46227 | 46227 | 46227 | 46227 | 135 |
| stake | 23983 | 23983 | 23983 | 23983 | 1 | | stake | 23983 | 23983 | 23983 | 23983 | 1 |
| stakeRewardEstimate | 436 | 2269 | 2436 | 2436 | 12 | | stakeRewardEstimate | 412 | 2294 | 2412 | 2412 | 17 |
| stakedToken | 273 | 273 | 273 | 273 | 687 | | stakedToken | 261 | 261 | 261 | 261 | 692 |
| startMigration | 108037 | 108045 | 108049 | 108049 | 3 | | startMigration | 107990 | 107998 | 108002 | 108002 | 3 |
| totalSupply | 740 | 1921 | 2740 | 2740 | 22 | | totalSupply | 740 | 1921 | 2740 | 2740 | 22 |
| totalSupplyBalance | 385 | 1785 | 2385 | 2385 | 20 | | totalSupplyBalance | 385 | 1785 | 2385 | 2385 | 20 |
| totalSupplyMP | 385 | 385 | 385 | 2385 | 46469 | | totalSupplyMP | 385 | 385 | 385 | 2385 | 46487 |
| unstake | 23819 | 23819 | 23819 | 23819 | 1 | | unstake | 23819 | 23819 | 23819 | 23819 | 1 |
@ -40,7 +40,7 @@
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 0 | 0 | | | | | | 0 | 0 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| getExpiredMP | 2427 | 2427 | 2427 | 2427 | 23598 | | getExpiredMP | 2427 | 2427 | 2427 | 2427 | 23645 |
| transferOwnership | 28533 | 28533 | 28533 | 28533 | 1 | | transferOwnership | 28533 | 28533 | 28533 | 28533 | 1 |
@ -49,13 +49,13 @@
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 0 | 0 | | | | | | 0 | 0 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| acceptMigration | 35258 | 35258 | 35258 | 35258 | 2 | | acceptMigration | 35246 | 35246 | 35246 | 35246 | 2 |
| leave | 35244 | 35244 | 35244 | 35244 | 1 | | leave | 35232 | 35232 | 35232 | 35232 | 1 |
| lock | 45192 | 73900 | 66395 | 158442 | 7 | | lock | 45376 | 99625 | 66468 | 193557 | 7 |
| owner | 362 | 362 | 362 | 362 | 670 | | owner | 362 | 362 | 362 | 362 | 675 |
| stake | 27265 | 283296 | 267703 | 353862 | 675 | | stake | 27265 | 283876 | 267740 | 353899 | 680 |
| stakedToken | 212 | 212 | 212 | 212 | 2 | | stakedToken | 212 | 212 | 212 | 212 | 2 |
| unstake | 42163 | 92598 | 80285 | 207456 | 11 | | unstake | 42248 | 92540 | 80334 | 206828 | 11 |
| contracts/VaultFactory.sol:VaultFactory contract | | | | | | | contracts/VaultFactory.sol:VaultFactory contract | | | | | |
@ -63,7 +63,7 @@
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 0 | 0 | | | | | | 0 | 0 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| createVault | 696531 | 696531 | 696531 | 696531 | 674 | | createVault | 696519 | 696519 | 696519 | 696519 | 679 |
| setStakeManager | 23710 | 26669 | 26076 | 30222 | 3 | | setStakeManager | 23710 | 26669 | 26076 | 30222 | 3 |
| stakeManager | 368 | 1868 | 2368 | 2368 | 4 | | stakeManager | 368 | 1868 | 2368 | 2368 | 4 |
@ -73,24 +73,24 @@
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 0 | 0 | | | | | | 0 | 0 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| approve | 46175 | 46237 | 46199 | 46367 | 670 | | approve | 46175 | 46236 | 46199 | 46367 | 675 |
| balanceOf | 561 | 2108 | 2561 | 2561 | 30693 | | balanceOf | 561 | 2108 | 2561 | 2561 | 30755 |
| script/Deploy.s.sol:Deploy contract | | | | | | | script/Deploy.s.sol:Deploy contract | | | | | |
|-------------------------------------|-----------------|---------|---------|---------|---------| |-------------------------------------|-----------------|---------|---------|---------|---------|
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 6532373 | 31450 | | | | | | 6070229 | 29294 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| run | 5752636 | 5752636 | 5752636 | 5752636 | 56 | | run | 5320405 | 5320405 | 5320405 | 5320405 | 61 |
| script/DeployMigrationStakeManager.s.sol:DeployMigrationStakeManager contract | | | | | | | script/DeployMigrationStakeManager.s.sol:DeployMigrationStakeManager contract | | | | | |
|-------------------------------------------------------------------------------|-----------------|---------|---------|---------|---------| |-------------------------------------------------------------------------------|-----------------|---------|---------|---------|---------|
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 3695815 | 18218 | | | | | | 3233781 | 16062 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| run | 2738306 | 2738306 | 2738306 | 2738306 | 9 | | run | 2306051 | 2306051 | 2306051 | 2306051 | 14 |
| script/DeploymentConfig.s.sol:DeploymentConfig contract | | | | | | | script/DeploymentConfig.s.sol:DeploymentConfig contract | | | | | |
@ -98,7 +98,7 @@
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 0 | 0 | | | | | | 0 | 0 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| activeNetworkConfig | 455 | 455 | 455 | 455 | 112 | | activeNetworkConfig | 455 | 455 | 455 | 455 | 122 |
| test/mocks/BrokenERC20.s.sol:BrokenERC20 contract | | | | | | | test/mocks/BrokenERC20.s.sol:BrokenERC20 contract | | | | | |
@ -113,9 +113,9 @@
| test/script/DeployBroken.s.sol:DeployBroken contract | | | | | | | test/script/DeployBroken.s.sol:DeployBroken contract | | | | | |
|------------------------------------------------------|-----------------|---------|---------|---------|---------| |------------------------------------------------------|-----------------|---------|---------|---------|---------|
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 5217091 | 25248 | | | | | | 4754997 | 23092 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| run | 4592458 | 4592458 | 4592458 | 4592458 | 1 | | run | 4160227 | 4160227 | 4160227 | 4160227 | 1 |

View File

@ -1,57 +1,62 @@
CreateVaultTest:testDeployment() (gas: 9774) CreateVaultTest:testDeployment() (gas: 9774)
CreateVaultTest:test_createVault() (gas: 714000) CreateVaultTest:test_createVault() (gas: 713988)
ExecuteAccountTest:testDeployment() (gas: 28742) ExecuteAccountTest:testDeployment() (gas: 28694)
ExecuteAccountTest:test_ExecuteAccountLimit() (gas: 1624826) ExecuteAccountTest:test_ExecuteAccountLimit() (gas: 1623530)
ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 5410346) ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 5407775)
ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1292635) ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1294709)
ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 338282264) ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 337956933)
ExecuteEpochTest:testDeployment() (gas: 28720) ExecuteEpochTest:testDeployment() (gas: 28650)
ExecuteEpochTest:testNewDeployment() (gas: 30815) ExecuteEpochTest:testNewDeployment() (gas: 30812)
ExecuteEpochTest:test_ExecuteEpochShouldIncreaseEpoch() (gas: 119862) ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterEpochEnd() (gas: 1740114)
ExecuteEpochTest:test_ExecuteEpochShouldIncreasePendingReward() (gas: 278283) ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 2763201)
ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochBeforeEnd() (gas: 43024) ExecuteEpochTest:test_ExecuteEpochExecuteEpochAfterEnd() (gas: 1980675)
ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 154182) ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochs() (gas: 2789261)
LeaveTest:testDeployment() (gas: 28720) ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 2799415)
LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 1329923) ExecuteEpochTest:test_ExecuteEpochShouldIncreaseEpoch() (gas: 119081)
ExecuteEpochTest:test_ExecuteEpochShouldIncreasePendingReward() (gas: 277700)
ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochBeforeEnd() (gas: 43065)
ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 154098)
LeaveTest:testDeployment() (gas: 28672)
LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 1329912)
LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 31942) LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 31942)
LockTest:testDeployment() (gas: 28720) LockTest:testDeployment() (gas: 28672)
LockTest:test_NewLockupPeriod() (gas: 1332993) LockTest:test_NewLockupPeriod() (gas: 1333043)
LockTest:test_RevertWhen_InvalidNewLockupPeriod() (gas: 1305322) LockTest:test_RevertWhen_InvalidNewLockupPeriod() (gas: 1305408)
LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1444177) LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1588267)
LockTest:test_RevertWhen_SenderIsNotVault() (gas: 31834) LockTest:test_RevertWhen_SenderIsNotVault() (gas: 31834)
LockTest:test_ShouldIncreaseBonusMP() (gas: 1315569) LockTest:test_ShouldIncreaseBonusMP() (gas: 1315583)
LockTest:test_UpdateLockupPeriod() (gas: 1588106) LockTest:test_UpdateLockupPeriod() (gas: 1662367)
MigrateTest:testDeployment() (gas: 28720) MigrateTest:testDeployment() (gas: 28672)
MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 1293998) MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 1293999)
MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 31954) MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 31954)
MigrationInitializeTest:testDeployment() (gas: 28720) MigrationInitializeTest:testDeployment() (gas: 28672)
MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 6117874) MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 5192974)
MigrationStakeManagerTest:testDeployment() (gas: 28720) MigrationStakeManagerTest:testDeployment() (gas: 28672)
MigrationStakeManagerTest:testNewDeployment() (gas: 30859) MigrationStakeManagerTest:testNewDeployment() (gas: 30811)
MigrationStakeManagerTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 154170) MigrationStakeManagerTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 154063)
SetStakeManagerTest:testDeployment() (gas: 9774) SetStakeManagerTest:testDeployment() (gas: 9774)
SetStakeManagerTest:test_RevertWhen_InvalidStakeManagerAddress() (gas: 63105) SetStakeManagerTest:test_RevertWhen_InvalidStakeManagerAddress() (gas: 63105)
SetStakeManagerTest:test_SetStakeManager() (gas: 41301) SetStakeManagerTest:test_SetStakeManager() (gas: 41301)
StakeManagerTest:testDeployment() (gas: 28492) StakeManagerTest:testDeployment() (gas: 28444)
StakeTest:testDeployment() (gas: 28698) StakeTest:testDeployment() (gas: 28650)
StakeTest:test_RevertWhen_InvalidLockupPeriod() (gas: 1084628) StakeTest:test_RevertWhen_InvalidLockupPeriod() (gas: 1084774)
StakeTest:test_RevertWhen_Restake() (gas: 1320817) StakeTest:test_RevertWhen_Restake() (gas: 1320915)
StakeTest:test_RevertWhen_RestakeWithLock() (gas: 1324854) StakeTest:test_RevertWhen_RestakeWithLock() (gas: 1324952)
StakeTest:test_RevertWhen_SenderIsNotVault() (gas: 32018) StakeTest:test_RevertWhen_SenderIsNotVault() (gas: 32018)
StakeTest:test_RevertWhen_StakeIsTooLow() (gas: 819736) StakeTest:test_RevertWhen_StakeIsTooLow() (gas: 819797)
StakeTest:test_RevertWhen_StakeTokenTransferFails() (gas: 211363) StakeTest:test_RevertWhen_StakeTokenTransferFails() (gas: 211363)
StakeTest:test_StakeWithLockBonusMP() (gas: 2363509) StakeTest:test_StakeWithLockBonusMP() (gas: 2363512)
StakeTest:test_StakeWithoutLockUpTimeMintsMultiplierPoints() (gas: 1325221) StakeTest:test_StakeWithoutLockUpTimeMintsMultiplierPoints() (gas: 1325199)
StakedTokenTest:testStakeToken() (gas: 7616) StakedTokenTest:testStakeToken() (gas: 7616)
UnstakeTest:testDeployment() (gas: 28742) UnstakeTest:testDeployment() (gas: 28694)
UnstakeTest:test_RevertWhen_AmountMoreThanBalance() (gas: 1301377) UnstakeTest:test_RevertWhen_AmountMoreThanBalance() (gas: 1301475)
UnstakeTest:test_RevertWhen_FundsLocked() (gas: 1348133) UnstakeTest:test_RevertWhen_FundsLocked() (gas: 1348316)
UnstakeTest:test_RevertWhen_SenderIsNotVault() (gas: 31857) UnstakeTest:test_RevertWhen_SenderIsNotVault() (gas: 31857)
UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 7409934) UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 7382927)
UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1322723) UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1322773)
UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1422611) UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1422152)
UserFlowsTest:testDeployment() (gas: 28720) UserFlowsTest:testDeployment() (gas: 28672)
UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8,uint128) (runs: 106, μ: 141088719, ~: 140458057) UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8,uint128) (runs: 106, μ: 140983431, ~: 140305109)
UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1465703) UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1465329)
UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 2501847) UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 2501983)
VaultFactoryTest:testDeployment() (gas: 9774) VaultFactoryTest:testDeployment() (gas: 9774)

View File

@ -138,27 +138,30 @@ contract StakeManager is Ownable {
/** /**
* @notice Process epoch if it has ended * @notice Process epoch if it has ended
*/ */
modifier finalizeEpoch() { function finalizeEpoch() private {
if (block.timestamp >= epochEnd() && address(migration) == address(0)) { if (address(migration) != address(0)) {
return;
}
Epoch storage thisEpoch = epochs[currentEpoch];
if (block.timestamp >= thisEpoch.startTime + EPOCH_SIZE) {
uint256 expiredMP = stakeRewardEstimate.getExpiredMP(currentEpoch); uint256 expiredMP = stakeRewardEstimate.getExpiredMP(currentEpoch);
if (expiredMP > 0) { if (expiredMP > 0) {
totalMPPerEpoch -= expiredMP; totalMPPerEpoch -= expiredMP;
stakeRewardEstimate.deleteExpiredMP(currentEpoch); stakeRewardEstimate.deleteExpiredMP(currentEpoch);
} }
epochs[currentEpoch].estimatedMP = totalMPPerEpoch - currentEpochTotalExpiredMP; thisEpoch.estimatedMP = totalMPPerEpoch - currentEpochTotalExpiredMP;
delete currentEpochTotalExpiredMP; delete currentEpochTotalExpiredMP;
pendingMPToBeMinted += epochs[currentEpoch].estimatedMP; pendingMPToBeMinted += thisEpoch.estimatedMP;
//finalize current epoch //finalize current epoch
epochs[currentEpoch].epochReward = epochReward(); thisEpoch.epochReward = epochReward();
epochs[currentEpoch].totalSupply = totalSupply(); thisEpoch.totalSupply = totalSupply();
pendingReward += epochs[currentEpoch].epochReward; pendingReward += thisEpoch.epochReward;
//create new epoch //create new epoch
currentEpoch++; currentEpoch++;
epochs[currentEpoch].startTime = block.timestamp; epochs[currentEpoch].startTime = thisEpoch.startTime + EPOCH_SIZE;
} }
_;
} }
constructor(address _stakedToken, address _previousManager) { constructor(address _stakedToken, address _previousManager) {
@ -181,7 +184,8 @@ contract StakeManager is Ownable {
* @dev Reverts when account has already staked funds. * @dev Reverts when account has already staked funds.
* @dev Reverts when amount staked results in less than 1 MP per epoch. * @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]; Account storage account = accounts[msg.sender];
if (account.balance > 0 || account.lockUntil != 0) { if (account.balance > 0 || account.lockUntil != 0) {
revert StakeManager__AlreadyStaked(); revert StakeManager__AlreadyStaked();
@ -220,13 +224,8 @@ contract StakeManager is Ownable {
/** /**
* leaves the staking pool and withdraws all funds; * leaves the staking pool and withdraws all funds;
*/ */
function unstake(uint256 _amount) function unstake(uint256 _amount) external onlyVault onlyAccountInitialized(msg.sender) noPendingMigration {
external finalizeEpoch();
onlyVault
onlyAccountInitialized(msg.sender)
noPendingMigration
finalizeEpoch
{
Account storage account = accounts[msg.sender]; Account storage account = accounts[msg.sender];
if (_amount > account.balance) { if (_amount > account.balance) {
revert StakeManager__InsufficientFunds(); revert StakeManager__InsufficientFunds();
@ -266,8 +265,8 @@ contract StakeManager is Ownable {
onlyVault onlyVault
onlyAccountInitialized(msg.sender) onlyAccountInitialized(msg.sender)
noPendingMigration noPendingMigration
finalizeEpoch
{ {
finalizeEpoch();
Account storage account = accounts[msg.sender]; Account storage account = accounts[msg.sender];
_processAccount(account, currentEpoch); _processAccount(account, currentEpoch);
uint256 lockUntil = account.lockUntil; uint256 lockUntil = account.lockUntil;
@ -291,8 +290,8 @@ contract StakeManager is Ownable {
* @notice Release rewards for current epoch and increase epoch. * @notice Release rewards for current epoch and increase epoch.
* @dev only executes the prerequisite modifier finalizeEpoch * @dev only executes the prerequisite modifier finalizeEpoch
*/ */
function executeEpoch() external noPendingMigration finalizeEpoch { function executeEpoch() external noPendingMigration {
return; //see modifier finalizeEpoch finalizeEpoch();
} }
/** /**
@ -300,14 +299,8 @@ contract StakeManager is Ownable {
* @param _vault Referred account * @param _vault Referred account
* @param _limitEpoch Until what epoch it should be executed * @param _limitEpoch Until what epoch it should be executed
*/ */
function executeAccount( function executeAccount(address _vault, uint256 _limitEpoch) external onlyAccountInitialized(_vault) {
address _vault, finalizeEpoch();
uint256 _limitEpoch
)
external
onlyAccountInitialized(_vault)
finalizeEpoch
{
_processAccount(accounts[_vault], _limitEpoch); _processAccount(accounts[_vault], _limitEpoch);
} }
@ -323,7 +316,8 @@ contract StakeManager is Ownable {
* @notice starts migration to new StakeManager * @notice starts migration to new StakeManager
* @param _migration 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)) { if (_migration == this || address(_migration) == address(0)) {
revert StakeManager__InvalidMigration(); revert StakeManager__InvalidMigration();
} }

View File

@ -744,6 +744,64 @@ contract MigrationStakeManagerTest is StakeManagerTest {
} }
contract ExecuteEpochTest is MigrationStakeManagerTest { contract ExecuteEpochTest is MigrationStakeManagerTest {
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_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_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 { function test_ExecuteEpochShouldNotIncreaseEpochBeforeEnd() public {
assertEq(stakeManager.currentEpoch(), 0); assertEq(stakeManager.currentEpoch(), 0);