Skip to content

Commit da6cac4

Browse files
committed
refactor(StakeManager): remove unused startTime field in Epoch struct and optimize epoch finalization and execution
1 parent b6a95e3 commit da6cac4

File tree

3 files changed

+47
-49
lines changed

3 files changed

+47
-49
lines changed

.gas-report

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
| contracts/StakeManager.sol:StakeManager contract | | | | | |
22
|--------------------------------------------------|-----------------|--------|--------|--------|---------|
33
| Deployment Cost | Deployment Size | | | | |
4-
| 2489660 | 13152 | | | | |
4+
| 2483377 | 13123 | | | | |
55
| Function Name | min | avg | median | max | # calls |
66
| EPOCH_SIZE | 263 | 263 | 263 | 263 | 1498 |
77
| MAX_BOOST | 264 | 264 | 264 | 264 | 637 |
88
| MAX_LOCKUP_PERIOD | 383 | 383 | 383 | 383 | 4 |
99
| MIN_LOCKUP_PERIOD | 264 | 264 | 264 | 264 | 12 |
1010
| YEAR | 263 | 263 | 263 | 263 | 637 |
11-
| accounts | 1616 | 1616 | 1616 | 1616 | 144249 |
11+
| accounts | 1616 | 1616 | 1616 | 1616 | 144345 |
1212
| calculateMPToMint | 740 | 740 | 740 | 740 | 1276 |
1313
| currentEpoch | 384 | 1050 | 384 | 2384 | 54 |
14-
| epochEnd | 649 | 649 | 649 | 2649 | 23671 |
14+
| epochEnd | 649 | 649 | 649 | 2649 | 23687 |
1515
| epochReward | 1381 | 2881 | 1381 | 5881 | 3 |
16-
| executeAccount(address) | 151167 | 151167 | 151167 | 151167 | 2 |
17-
| executeAccount(address,uint256) | 26562 | 72617 | 74122 | 217855 | 141836 |
18-
| executeEpoch() | 23480 | 120821 | 121978 | 939007 | 23560 |
16+
| executeAccount(address) | 149300 | 149300 | 149300 | 149300 | 2 |
17+
| executeAccount(address,uint256) | 26562 | 72242 | 74122 | 200087 | 141932 |
18+
| executeEpoch() | 23480 | 120709 | 121865 | 900380 | 23576 |
1919
| executeEpoch(uint256) | 23861 | 24497 | 23861 | 26090 | 7 |
2020
| expiredStakeStorage | 437 | 2346 | 2437 | 2437 | 22 |
2121
| isTrustedCodehash | 541 | 949 | 541 | 2541 | 680 |
@@ -25,7 +25,7 @@
2525
| migrationInitialize | 24624 | 24624 | 24624 | 24624 | 1 |
2626
| newEpoch | 441 | 441 | 441 | 441 | 5 |
2727
| owner | 2432 | 2432 | 2432 | 2432 | 13 |
28-
| pendingMPToBeMinted | 363 | 363 | 363 | 363 | 46424 |
28+
| pendingMPToBeMinted | 363 | 363 | 363 | 363 | 46456 |
2929
| pendingReward | 408 | 1442 | 2408 | 2408 | 29 |
3030
| previousManager | 275 | 275 | 275 | 275 | 13 |
3131
| setTrustedCodehash | 47960 | 47960 | 47960 | 47960 | 139 |
@@ -35,7 +35,7 @@
3535
| startTime | 306 | 306 | 306 | 306 | 21 |
3636
| totalSupply | 784 | 1965 | 2784 | 2784 | 22 |
3737
| totalSupplyBalance | 407 | 1807 | 2407 | 2407 | 20 |
38-
| totalSupplyMP | 384 | 384 | 384 | 2384 | 46445 |
38+
| totalSupplyMP | 384 | 384 | 384 | 2384 | 46477 |
3939
| unstake | 23841 | 23841 | 23841 | 23841 | 1 |
4040

4141

@@ -46,11 +46,11 @@
4646
| Function Name | min | avg | median | max | # calls |
4747
| acceptMigration | 35311 | 35311 | 35311 | 35311 | 2 |
4848
| leave | 35297 | 35297 | 35297 | 35297 | 1 |
49-
| lock | 43285 | 96103 | 64379 | 204703 | 7 |
49+
| lock | 43285 | 95698 | 64379 | 204197 | 7 |
5050
| owner | 362 | 362 | 362 | 362 | 679 |
51-
| stake | 27265 | 282007 | 265700 | 351859 | 684 |
51+
| stake | 27265 | 282180 | 265700 | 351859 | 684 |
5252
| stakedToken | 212 | 212 | 212 | 212 | 2 |
53-
| unstake | 40180 | 97074 | 78700 | 233556 | 11 |
53+
| unstake | 40180 | 96354 | 78700 | 229598 | 11 |
5454

5555

5656
| contracts/VaultFactory.sol:VaultFactory contract | | | | | |
@@ -68,7 +68,7 @@
6868
| Deployment Cost | Deployment Size | | | | |
6969
| 0 | 0 | | | | |
7070
| Function Name | min | avg | median | max | # calls |
71-
| getExpiredMP | 2427 | 2427 | 2427 | 2427 | 23721 |
71+
| getExpiredMP | 2427 | 2427 | 2427 | 2427 | 23737 |
7272
| transferOwnership | 28533 | 28533 | 28533 | 28533 | 1 |
7373

7474

@@ -77,24 +77,24 @@
7777
| Deployment Cost | Deployment Size | | | | |
7878
| 0 | 0 | | | | |
7979
| Function Name | min | avg | median | max | # calls |
80-
| approve | 46175 | 46238 | 46199 | 46367 | 679 |
81-
| balanceOf | 561 | 2107 | 2561 | 2561 | 30740 |
80+
| approve | 46175 | 46236 | 46199 | 46367 | 679 |
81+
| balanceOf | 561 | 2107 | 2561 | 2561 | 30756 |
8282

8383

8484
| script/Deploy.s.sol:Deploy contract | | | | | |
8585
|-------------------------------------|-----------------|---------|---------|---------|---------|
8686
| Deployment Cost | Deployment Size | | | | |
87-
| 6142896 | 29649 | | | | |
87+
| 6136611 | 29620 | | | | |
8888
| Function Name | min | avg | median | max | # calls |
89-
| run | 5338557 | 5338557 | 5338557 | 5338557 | 66 |
89+
| run | 5332750 | 5332750 | 5332750 | 5332750 | 66 |
9090

9191

9292
| script/DeployMigrationStakeManager.s.sol:DeployMigrationStakeManager contract | | | | | |
9393
|-------------------------------------------------------------------------------|-----------------|---------|---------|---------|---------|
9494
| Deployment Cost | Deployment Size | | | | |
95-
| 3306430 | 16417 | | | | |
95+
| 3300146 | 16388 | | | | |
9696
| Function Name | min | avg | median | max | # calls |
97-
| run | 2324886 | 2324886 | 2324886 | 2324886 | 19 |
97+
| run | 2319079 | 2319079 | 2319079 | 2319079 | 19 |
9898

9999

100100
| script/DeploymentConfig.s.sol:DeploymentConfig contract | | | | | |
@@ -117,9 +117,9 @@
117117
| test/script/DeployBroken.s.sol:DeployBroken contract | | | | | |
118118
|------------------------------------------------------|-----------------|---------|---------|---------|---------|
119119
| Deployment Cost | Deployment Size | | | | |
120-
| 4827656 | 23447 | | | | |
120+
| 4821371 | 23418 | | | | |
121121
| Function Name | min | avg | median | max | # calls |
122-
| run | 4178379 | 4178379 | 4178379 | 4178379 | 1 |
122+
| run | 4172572 | 4172572 | 4172572 | 4172572 | 1 |
123123

124124

125125

.gas-snapshot

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
CreateVaultTest:testDeployment() (gas: 9774)
22
CreateVaultTest:test_createVault() (gas: 713999)
33
ExecuteAccountTest:testDeployment() (gas: 28828)
4-
ExecuteAccountTest:test_ExecuteAccountLimit() (gas: 1582018)
5-
ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 5299985)
6-
ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1790139)
7-
ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 321470949)
4+
ExecuteAccountTest:test_ExecuteAccountLimit() (gas: 1579172)
5+
ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 5295647)
6+
ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1787335)
7+
ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 321024135)
88
ExecuteEpochTest:testDeployment() (gas: 28829)
99
ExecuteEpochTest:testNewDeployment() (gas: 30901)
1010
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterEpochEnd() (gas: 1367896)
11-
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1386983)
11+
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1385583)
1212
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 1630994)
13-
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1396698)
14-
ExecuteEpochTest:test_ExecuteEpochExecuteEpochAfterEnd() (gas: 1937948)
15-
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochs() (gas: 2525682)
16-
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1481437)
17-
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 2535418)
18-
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1491129)
19-
ExecuteEpochTest:test_ExecuteEpochNewEpoch() (gas: 1122427)
20-
ExecuteEpochTest:test_ExecuteEpochShouldIncreaseEpoch() (gas: 92457)
21-
ExecuteEpochTest:test_ExecuteEpochShouldIncreasePendingReward() (gas: 256428)
13+
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1395298)
14+
ExecuteEpochTest:test_ExecuteEpochExecuteEpochAfterEnd() (gas: 1928384)
15+
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochs() (gas: 2511226)
16+
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1479103)
17+
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 2520962)
18+
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1488795)
19+
ExecuteEpochTest:test_ExecuteEpochNewEpoch() (gas: 1083687)
20+
ExecuteEpochTest:test_ExecuteEpochShouldIncreaseEpoch() (gas: 92344)
21+
ExecuteEpochTest:test_ExecuteEpochShouldIncreasePendingReward() (gas: 256338)
2222
ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochBeforeEnd() (gas: 39028)
2323
ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 149748)
2424
LeaveTest:testDeployment() (gas: 28806)
@@ -27,15 +27,15 @@ LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 31995)
2727
LockTest:testDeployment() (gas: 28806)
2828
LockTest:test_NewLockupPeriod() (gas: 1330751)
2929
LockTest:test_RevertWhen_InvalidNewLockupPeriod() (gas: 1303059)
30-
LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1547562)
30+
LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1543826)
3131
LockTest:test_RevertWhen_SenderIsNotVault() (gas: 31812)
3232
LockTest:test_ShouldIncreaseBonusMP() (gas: 1313344)
33-
LockTest:test_UpdateLockupPeriod() (gas: 1636200)
33+
LockTest:test_UpdateLockupPeriod() (gas: 1634292)
3434
MigrateTest:testDeployment() (gas: 28806)
3535
MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 1293784)
3636
MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 32007)
3737
MigrationInitializeTest:testDeployment() (gas: 28806)
38-
MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 5229403)
38+
MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 5216837)
3939
MigrationStakeManagerTest:testDeployment() (gas: 28806)
4040
MigrationStakeManagerTest:testNewDeployment() (gas: 30945)
4141
MigrationStakeManagerTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 149713)
@@ -57,11 +57,11 @@ UnstakeTest:testDeployment() (gas: 28828)
5757
UnstakeTest:test_RevertWhen_AmountMoreThanBalance() (gas: 1299127)
5858
UnstakeTest:test_RevertWhen_FundsLocked() (gas: 1343877)
5959
UnstakeTest:test_RevertWhen_SenderIsNotVault() (gas: 31879)
60-
UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 6529254)
60+
UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 6450051)
6161
UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1321289)
62-
UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1441917)
62+
UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1438949)
6363
UserFlowsTest:testDeployment() (gas: 28806)
64-
UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8,uint128) (runs: 106, μ: 131427031, ~: 130900495)
65-
UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1483026)
64+
UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8,uint128) (runs: 106, μ: 130995099, ~: 130391755)
65+
UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1480058)
6666
UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 2496473)
6767
VaultFactoryTest:testDeployment() (gas: 9774)

contracts/StakeManager.sol

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ contract StakeManager is TrustedCodehashAccess {
3535
}
3636

3737
struct Epoch {
38-
uint256 startTime;
3938
uint256 epochReward;
4039
uint256 totalSupply;
4140
uint256 estimatedMP;
@@ -118,18 +117,17 @@ contract StakeManager is TrustedCodehashAccess {
118117
totalMPPerEpoch -= expiredMP;
119118
expiredStakeStorage.deleteExpiredMP(tempCurrentEpoch);
120119
}
121-
thisEpoch.estimatedMP = totalMPPerEpoch - currentEpochTotalExpiredMP;
122-
delete currentEpochTotalExpiredMP;
123-
pendingMPToBeMinted += thisEpoch.estimatedMP;
124-
125-
//finalize current epoch
120+
uint256 epochEstimatedMP = totalMPPerEpoch;
126121
if (tempCurrentEpoch == currentEpoch) {
122+
epochEstimatedMP -= currentEpochTotalExpiredMP;
123+
currentEpochTotalExpiredMP = 0;
127124
thisEpoch.epochReward = epochReward();
128125
pendingReward += thisEpoch.epochReward;
129126
}
130-
thisEpoch.totalSupply = totalSupply();
131127

132-
//create new epoch
128+
pendingMPToBeMinted += epochEstimatedMP;
129+
thisEpoch.estimatedMP = epochEstimatedMP;
130+
thisEpoch.totalSupply = totalSupply();
133131
tempCurrentEpoch++;
134132
}
135133
currentEpoch = tempCurrentEpoch;

0 commit comments

Comments
 (0)