From 5e6e3dc8046114c471baab34d2ae3c1152d54bc2 Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt <3esmit@gmail.com> Date: Sun, 29 Sep 2024 19:06:59 -0300 Subject: [PATCH] refactor(StakeManager): remove unused startTime field in Epoch struct and optimize epoch finalization and execution --- .gas-report | 40 ++++++++++++++++++------------------ .gas-snapshot | 42 +++++++++++++++++++------------------- contracts/StakeManager.sol | 14 ++++++------- 3 files changed, 47 insertions(+), 49 deletions(-) diff --git a/.gas-report b/.gas-report index 72eb99d..3901a22 100644 --- a/.gas-report +++ b/.gas-report @@ -1,21 +1,21 @@ | contracts/StakeManager.sol:StakeManager contract | | | | | | |--------------------------------------------------|-----------------|--------|--------|--------|---------| | Deployment Cost | Deployment Size | | | | | -| 2465702 | 13041 | | | | | +| 2458962 | 13010 | | | | | | Function Name | min | avg | median | max | # calls | | 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 | 264 | 264 | 264 | 264 | 12 | | YEAR | 263 | 263 | 263 | 263 | 637 | -| accounts | 1597 | 1597 | 1597 | 1597 | 144273 | +| accounts | 1597 | 1597 | 1597 | 1597 | 144285 | | calculateMPToMint | 740 | 740 | 740 | 740 | 1276 | | currentEpoch | 384 | 1050 | 384 | 2384 | 54 | -| epochEnd | 627 | 627 | 627 | 2627 | 23675 | +| epochEnd | 627 | 627 | 627 | 2627 | 23677 | | 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 | +| executeAccount(address) | 149284 | 149284 | 149284 | 149284 | 2 | +| executeAccount(address,uint256) | 26562 | 72246 | 74122 | 200111 | 141872 | +| executeEpoch() | 23458 | 120683 | 121843 | 900358 | 23566 | | executeEpoch(uint256) | 23861 | 24497 | 23861 | 26090 | 7 | | isVault | 540 | 948 | 540 | 2540 | 680 | | lock | 23862 | 23862 | 23862 | 23862 | 1 | @@ -24,7 +24,7 @@ | migrationInitialize | 24602 | 24602 | 24602 | 24602 | 1 | | newEpoch | 441 | 441 | 441 | 441 | 5 | | owner | 2432 | 2432 | 2432 | 2432 | 13 | -| pendingMPToBeMinted | 364 | 364 | 364 | 364 | 46432 | +| pendingMPToBeMinted | 364 | 364 | 364 | 364 | 46436 | | pendingReward | 386 | 1420 | 2386 | 2386 | 29 | | previousManager | 275 | 275 | 275 | 275 | 13 | | setVault | 46239 | 46239 | 46239 | 46239 | 139 | @@ -35,7 +35,7 @@ | startTime | 306 | 306 | 306 | 306 | 21 | | totalSupply | 762 | 1943 | 2762 | 2762 | 22 | | totalSupplyBalance | 407 | 1807 | 2407 | 2407 | 20 | -| totalSupplyMP | 362 | 362 | 362 | 2362 | 46453 | +| totalSupplyMP | 362 | 362 | 362 | 2362 | 46457 | | unstake | 23819 | 23819 | 23819 | 23819 | 1 | @@ -44,7 +44,7 @@ | Deployment Cost | Deployment Size | | | | | | 0 | 0 | | | | | | Function Name | min | avg | median | max | # calls | -| getExpiredMP | 2427 | 2427 | 2427 | 2427 | 23725 | +| getExpiredMP | 2427 | 2427 | 2427 | 2427 | 23727 | | transferOwnership | 28533 | 28533 | 28533 | 28533 | 1 | @@ -55,11 +55,11 @@ | Function Name | min | avg | median | max | # calls | | acceptMigration | 35280 | 35280 | 35280 | 35280 | 2 | | leave | 35266 | 35266 | 35266 | 35266 | 1 | -| lock | 43329 | 96146 | 64421 | 204745 | 7 | +| lock | 43329 | 95741 | 64421 | 204239 | 7 | | owner | 362 | 362 | 362 | 362 | 679 | -| stake | 27265 | 282079 | 265719 | 351866 | 684 | +| stake | 27265 | 282022 | 265707 | 351866 | 684 | | stakedToken | 212 | 212 | 212 | 212 | 2 | -| unstake | 40167 | 95818 | 78688 | 233549 | 11 | +| unstake | 40167 | 95099 | 78688 | 229591 | 11 | | contracts/VaultFactory.sol:VaultFactory contract | | | | | | @@ -77,24 +77,24 @@ | Deployment Cost | Deployment Size | | | | | | 0 | 0 | | | | | | Function Name | min | avg | median | max | # calls | -| approve | 46175 | 46239 | 46199 | 46367 | 679 | -| balanceOf | 561 | 2107 | 2561 | 2561 | 30744 | +| approve | 46175 | 46237 | 46199 | 46367 | 679 | +| balanceOf | 561 | 2107 | 2561 | 2561 | 30746 | | script/Deploy.s.sol:Deploy contract | | | | | | |-------------------------------------|-----------------|---------|---------|---------|---------| | Deployment Cost | Deployment Size | | | | | -| 6118940 | 29538 | | | | | +| 6112199 | 29507 | | | | | | Function Name | min | avg | median | max | # calls | -| run | 5316305 | 5316305 | 5316305 | 5316305 | 66 | +| run | 5310090 | 5310090 | 5310090 | 5310090 | 66 | | script/DeployMigrationStakeManager.s.sol:DeployMigrationStakeManager contract | | | | | | |-------------------------------------------------------------------------------|-----------------|---------|---------|---------|---------| | Deployment Cost | Deployment Size | | | | | -| 3282471 | 16306 | | | | | +| 3275739 | 16275 | | | | | | Function Name | min | avg | median | max | # calls | -| run | 2302633 | 2302633 | 2302633 | 2302633 | 19 | +| run | 2296418 | 2296418 | 2296418 | 2296418 | 19 | | script/DeploymentConfig.s.sol:DeploymentConfig contract | | | | | | @@ -117,9 +117,9 @@ | test/script/DeployBroken.s.sol:DeployBroken contract | | | | | | |------------------------------------------------------|-----------------|---------|---------|---------|---------| | Deployment Cost | Deployment Size | | | | | -| 4803693 | 23336 | | | | | +| 4796952 | 23305 | | | | | | Function Name | min | avg | median | max | # calls | -| run | 4156127 | 4156127 | 4156127 | 4156127 | 1 | +| run | 4149911 | 4149911 | 4149911 | 4149911 | 1 | diff --git a/.gas-snapshot b/.gas-snapshot index b013b9b..5fc8c19 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,24 +1,24 @@ CreateVaultTest:testDeployment() (gas: 9774) 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) +ExecuteAccountTest:test_ExecuteAccountLimit() (gas: 1577409) +ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 5293445) +ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1785612) +ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 320951251) ExecuteEpochTest:testDeployment() (gas: 28786) ExecuteEpochTest:testNewDeployment() (gas: 30858) ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterEpochEnd() (gas: 1366211) -ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1385045) +ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1383643) 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_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1393798) +ExecuteEpochTest:test_ExecuteEpochExecuteEpochAfterEnd() (gas: 1926644) +ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochs() (gas: 2509097) +ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1477157) +ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 2519273) +ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1487289) +ExecuteEpochTest:test_ExecuteEpochNewEpoch() (gas: 1083599) +ExecuteEpochTest:test_ExecuteEpochShouldIncreaseEpoch() (gas: 92300) +ExecuteEpochTest:test_ExecuteEpochShouldIncreasePendingReward() (gas: 256356) ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochBeforeEnd() (gas: 38984) ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 149638) LeaveTest:testDeployment() (gas: 28763) @@ -27,15 +27,15 @@ 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_InvalidUpdateLockupPeriod() (gas: 1542150) LockTest:test_RevertWhen_SenderIsNotVault() (gas: 31856) LockTest:test_ShouldIncreaseBonusMP() (gas: 1311575) -LockTest:test_UpdateLockupPeriod() (gas: 1634540) +LockTest:test_UpdateLockupPeriod() (gas: 1632634) 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) +MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 5167897) MigrationStakeManagerTest:testDeployment() (gas: 28763) MigrationStakeManagerTest:testNewDeployment() (gas: 30902) MigrationStakeManagerTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 149603) @@ -57,11 +57,11 @@ 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: 6526945) +UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 6447784) UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1319573) -UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1440205) +UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1437237) UserFlowsTest:testDeployment() (gas: 28763) -UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8,uint128) (runs: 106, μ: 131402731, ~: 130856521) -UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1481301) +UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8,uint128) (runs: 106, μ: 130900616, ~: 130347517) +UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1478333) UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 2494771) VaultFactoryTest:testDeployment() (gas: 9774) \ No newline at end of file diff --git a/contracts/StakeManager.sol b/contracts/StakeManager.sol index 696bed9..ab9c5d2 100644 --- a/contracts/StakeManager.sol +++ b/contracts/StakeManager.sol @@ -55,7 +55,6 @@ contract StakeManager is Ownable { } struct Epoch { - uint256 startTime; uint256 epochReward; uint256 totalSupply; uint256 estimatedMP; @@ -149,18 +148,17 @@ contract StakeManager is Ownable { totalMPPerEpoch -= expiredMP; stakeRewardEstimate.deleteExpiredMP(tempCurrentEpoch); } - thisEpoch.estimatedMP = totalMPPerEpoch - currentEpochTotalExpiredMP; - delete currentEpochTotalExpiredMP; - pendingMPToBeMinted += thisEpoch.estimatedMP; - - //finalize current epoch + uint256 epochEstimatedMP = totalMPPerEpoch; if (tempCurrentEpoch == currentEpoch) { + epochEstimatedMP -= currentEpochTotalExpiredMP; + currentEpochTotalExpiredMP = 0; thisEpoch.epochReward = epochReward(); pendingReward += thisEpoch.epochReward; } - thisEpoch.totalSupply = totalSupply(); - //create new epoch + pendingMPToBeMinted += epochEstimatedMP; + thisEpoch.estimatedMP = epochEstimatedMP; + thisEpoch.totalSupply = totalSupply(); tempCurrentEpoch++; } currentEpoch = tempCurrentEpoch;