Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(StakeManager): optimize stake, account init and lock function #130

Merged
merged 5 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions .gas-report
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
| contracts/StakeManager.sol:StakeManager contract | | | | | |
|--------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost | Deployment Size | | | | |
| 2483377 | 13123 | | | | |
| 2495816 | 13179 | | | | |
| Function Name | min | avg | median | max | # calls |
| EPOCH_SIZE | 263 | 263 | 263 | 263 | 1498 |
| MAX_BOOST | 264 | 264 | 264 | 264 | 637 |
| MAX_LOCKUP_PERIOD | 383 | 383 | 383 | 383 | 4 |
| MIN_LOCKUP_PERIOD | 264 | 264 | 264 | 264 | 12 |
| YEAR | 263 | 263 | 263 | 263 | 637 |
| accounts | 1616 | 1616 | 1616 | 1616 | 144345 |
| accounts | 1616 | 1616 | 1616 | 1616 | 144285 |
| calculateMPToMint | 740 | 740 | 740 | 740 | 1276 |
| currentEpoch | 384 | 1050 | 384 | 2384 | 54 |
| epochEnd | 649 | 649 | 649 | 2649 | 23687 |
| epochEnd | 649 | 649 | 649 | 2649 | 23677 |
| epochReward | 1381 | 2881 | 1381 | 5881 | 3 |
| executeAccount(address) | 149300 | 149300 | 149300 | 149300 | 2 |
| executeAccount(address,uint256) | 26562 | 72242 | 74122 | 200087 | 141932 |
| executeEpoch() | 23480 | 120709 | 121865 | 900380 | 23576 |
| executeAccount(address,uint256) | 26562 | 72246 | 74122 | 200087 | 141872 |
| executeEpoch() | 23480 | 120708 | 121865 | 900380 | 23566 |
| executeEpoch(uint256) | 23861 | 24497 | 23861 | 26090 | 7 |
| expiredStakeStorage | 437 | 2346 | 2437 | 2437 | 22 |
| isTrustedCodehash | 541 | 949 | 541 | 2541 | 680 |
Expand All @@ -25,7 +25,7 @@
| migrationInitialize | 24624 | 24624 | 24624 | 24624 | 1 |
| newEpoch | 441 | 441 | 441 | 441 | 5 |
| owner | 2432 | 2432 | 2432 | 2432 | 13 |
| pendingMPToBeMinted | 363 | 363 | 363 | 363 | 46456 |
| pendingMPToBeMinted | 363 | 363 | 363 | 363 | 46436 |
| pendingReward | 408 | 1442 | 2408 | 2408 | 29 |
| previousManager | 275 | 275 | 275 | 275 | 13 |
| setTrustedCodehash | 47960 | 47960 | 47960 | 47960 | 139 |
Expand All @@ -35,7 +35,7 @@
| startTime | 306 | 306 | 306 | 306 | 21 |
| totalSupply | 784 | 1965 | 2784 | 2784 | 22 |
| totalSupplyBalance | 407 | 1807 | 2407 | 2407 | 20 |
| totalSupplyMP | 384 | 384 | 384 | 2384 | 46477 |
| totalSupplyMP | 384 | 384 | 384 | 2384 | 46457 |
| unstake | 23841 | 23841 | 23841 | 23841 | 1 |


Expand All @@ -46,9 +46,9 @@
| Function Name | min | avg | median | max | # calls |
| acceptMigration | 35311 | 35311 | 35311 | 35311 | 2 |
| leave | 35297 | 35297 | 35297 | 35297 | 1 |
| lock | 43285 | 95698 | 64379 | 204197 | 7 |
| lock | 43285 | 90487 | 61938 | 180284 | 7 |
| owner | 362 | 362 | 362 | 362 | 679 |
| stake | 27265 | 282180 | 265700 | 351859 | 684 |
| stake | 27265 | 282115 | 265681 | 351644 | 684 |
| stakedToken | 212 | 212 | 212 | 212 | 2 |
| unstake | 40180 | 96354 | 78700 | 229598 | 11 |

Expand All @@ -68,7 +68,7 @@
| Deployment Cost | Deployment Size | | | | |
| 0 | 0 | | | | |
| Function Name | min | avg | median | max | # calls |
| getExpiredMP | 2427 | 2427 | 2427 | 2427 | 23737 |
| getExpiredMP | 2427 | 2427 | 2427 | 2427 | 23727 |
| transferOwnership | 28533 | 28533 | 28533 | 28533 | 1 |


Expand All @@ -77,24 +77,24 @@
| Deployment Cost | Deployment Size | | | | |
| 0 | 0 | | | | |
| Function Name | min | avg | median | max | # calls |
| approve | 46175 | 46236 | 46199 | 46367 | 679 |
| balanceOf | 561 | 2107 | 2561 | 2561 | 30756 |
| approve | 46175 | 46239 | 46199 | 46367 | 679 |
| balanceOf | 561 | 2107 | 2561 | 2561 | 30746 |


| script/Deploy.s.sol:Deploy contract | | | | | |
|-------------------------------------|-----------------|---------|---------|---------|---------|
| Deployment Cost | Deployment Size | | | | |
| 6136611 | 29620 | | | | |
| 6149062 | 29676 | | | | |
| Function Name | min | avg | median | max | # calls |
| run | 5332750 | 5332750 | 5332750 | 5332750 | 66 |
| run | 5343965 | 5343965 | 5343965 | 5343965 | 66 |


| script/DeployMigrationStakeManager.s.sol:DeployMigrationStakeManager contract | | | | | |
|-------------------------------------------------------------------------------|-----------------|---------|---------|---------|---------|
| Deployment Cost | Deployment Size | | | | |
| 3300146 | 16388 | | | | |
| 3312594 | 16444 | | | | |
| Function Name | min | avg | median | max | # calls |
| run | 2319079 | 2319079 | 2319079 | 2319079 | 19 |
| run | 2330294 | 2330294 | 2330294 | 2330294 | 19 |


| script/DeploymentConfig.s.sol:DeploymentConfig contract | | | | | |
Expand All @@ -117,9 +117,9 @@
| test/script/DeployBroken.s.sol:DeployBroken contract | | | | | |
|------------------------------------------------------|-----------------|---------|---------|---------|---------|
| Deployment Cost | Deployment Size | | | | |
| 4821371 | 23418 | | | | |
| 4833800 | 23474 | | | | |
| Function Name | min | avg | median | max | # calls |
| run | 4172572 | 4172572 | 4172572 | 4172572 | 1 |
| run | 4183787 | 4183787 | 4183787 | 4183787 | 1 |



Expand Down
70 changes: 35 additions & 35 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
CreateVaultTest:testDeployment() (gas: 9774)
CreateVaultTest:test_createVault() (gas: 713999)
ExecuteAccountTest:testDeployment() (gas: 28828)
ExecuteAccountTest:test_ExecuteAccountLimit() (gas: 1579172)
ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 5295647)
ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1787335)
ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 321024135)
ExecuteAccountTest:test_ExecuteAccountLimit() (gas: 1579141)
ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 5295554)
ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1787120)
ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 321023887)
ExecuteEpochTest:testDeployment() (gas: 28829)
ExecuteEpochTest:testNewDeployment() (gas: 30901)
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterEpochEnd() (gas: 1367896)
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1385583)
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 1630994)
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1395298)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochAfterEnd() (gas: 1928384)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochs() (gas: 2511226)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1479103)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 2520962)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1488795)
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterEpochEnd() (gas: 1367865)
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1385552)
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 1630963)
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1395267)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochAfterEnd() (gas: 1928353)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochs() (gas: 2511195)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1479072)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 2520931)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1488764)
ExecuteEpochTest:test_ExecuteEpochNewEpoch() (gas: 1083687)
ExecuteEpochTest:test_ExecuteEpochShouldIncreaseEpoch() (gas: 92344)
ExecuteEpochTest:test_ExecuteEpochShouldIncreasePendingReward() (gas: 256338)
ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochBeforeEnd() (gas: 39028)
ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 149748)
LeaveTest:testDeployment() (gas: 28806)
LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 1329762)
LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 1329731)
LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 31995)
LockTest:testDeployment() (gas: 28806)
LockTest:test_NewLockupPeriod() (gas: 1330751)
LockTest:test_RevertWhen_InvalidNewLockupPeriod() (gas: 1303059)
LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1543826)
LockTest:test_NewLockupPeriod() (gas: 1328279)
LockTest:test_RevertWhen_InvalidNewLockupPeriod() (gas: 1303028)
LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1543611)
LockTest:test_RevertWhen_SenderIsNotVault() (gas: 31812)
LockTest:test_ShouldIncreaseBonusMP() (gas: 1313344)
LockTest:test_UpdateLockupPeriod() (gas: 1634292)
LockTest:test_ShouldIncreaseBonusMP() (gas: 1310872)
LockTest:test_UpdateLockupPeriod() (gas: 1579753)
MigrateTest:testDeployment() (gas: 28806)
MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 1293784)
MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 1293753)
MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 32007)
MigrationInitializeTest:testDeployment() (gas: 28806)
MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 5216837)
MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 5241726)
MigrationStakeManagerTest:testDeployment() (gas: 28806)
MigrationStakeManagerTest:testNewDeployment() (gas: 30945)
MigrationStakeManagerTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 149713)
Expand All @@ -44,24 +44,24 @@ SetStakeManagerTest:test_RevertWhen_InvalidStakeManagerAddress() (gas: 63105)
SetStakeManagerTest:test_SetStakeManager() (gas: 41301)
StakeManagerTest:testDeployment() (gas: 28578)
StakeTest:testDeployment() (gas: 28784)
StakeTest:test_RevertWhen_InvalidLockupPeriod() (gas: 1082353)
StakeTest:test_RevertWhen_Restake() (gas: 1318545)
StakeTest:test_RevertWhen_RestakeWithLock() (gas: 1322559)
StakeTest:test_RevertWhen_InvalidLockupPeriod() (gas: 1078067)
StakeTest:test_RevertWhen_Restake() (gas: 1318488)
StakeTest:test_RevertWhen_RestakeWithLock() (gas: 1322318)
StakeTest:test_RevertWhen_SenderIsNotVault() (gas: 32018)
StakeTest:test_RevertWhen_StakeIsTooLow() (gas: 819467)
StakeTest:test_RevertWhen_StakeIsTooLow() (gas: 817324)
StakeTest:test_RevertWhen_StakeTokenTransferFails() (gas: 211363)
StakeTest:test_StakeWithLockBonusMP() (gas: 2359257)
StakeTest:test_StakeWithoutLockUpTimeMintsMultiplierPoints() (gas: 1314761)
StakeTest:test_StakeWithLockBonusMP() (gas: 2356570)
StakeTest:test_StakeWithoutLockUpTimeMintsMultiplierPoints() (gas: 1314730)
StakedTokenTest:testStakeToken() (gas: 7616)
UnstakeTest:testDeployment() (gas: 28828)
UnstakeTest:test_RevertWhen_AmountMoreThanBalance() (gas: 1299127)
UnstakeTest:test_RevertWhen_FundsLocked() (gas: 1343877)
UnstakeTest:test_RevertWhen_AmountMoreThanBalance() (gas: 1299096)
UnstakeTest:test_RevertWhen_FundsLocked() (gas: 1343662)
UnstakeTest:test_RevertWhen_SenderIsNotVault() (gas: 31879)
UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 6450051)
UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1321289)
UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1438949)
UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 6450026)
UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1321258)
UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1438734)
UserFlowsTest:testDeployment() (gas: 28806)
UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8,uint128) (runs: 106, μ: 130995099, ~: 130391755)
UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1480058)
UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 2496473)
UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8,uint128) (runs: 106, μ: 130945931, ~: 130391929)
UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1479843)
UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 2496411)
VaultFactoryTest:testDeployment() (gas: 9774)
10 changes: 10 additions & 0 deletions certora/specs/StakeManager.spec
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ hook Sstore accounts[KEY address addr].totalMP uint256 newValue (uint256 oldValu
sumOfMultipliers = sumOfMultipliers - oldValue + newValue;
}

hook Sload uint256 newValue accounts[KEY address addr].totalMP {
require sumOfMultipliers >= to_mathint(newValue);
}

invariant sumOfBalancesIsTotalSupplyBalance()
sumOfBalances == to_mathint(totalSupplyBalance())
filtered {
Expand All @@ -71,6 +75,11 @@ invariant sumOfMultipliersIsMultiplierSupply()
filtered {
m -> !requiresPreviousManager(m) && !requiresNextManager(m)
}
{ preserved with (env e){
requireInvariant accountMPIsZeroIfBalanceIsZero(e.msg.sender);
requireInvariant accountBonusMPIsZeroIfBalanceIsZero(e.msg.sender);
}
}

invariant sumOfEpochRewardsIsPendingRewards()
sumOfEpochRewards == to_mathint(currentContract.pendingReward)
Expand Down Expand Up @@ -115,6 +124,7 @@ rule stakingMintsMultiplierPoints1To1Ratio {

requireInvariant InitialMPIsNeverSmallerThanBalance(e.msg.sender);
requireInvariant CurrentMPIsNeverSmallerThanInitialMP(e.msg.sender);
requireInvariant accountMPIsZeroIfBalanceIsZero(e.msg.sender);

require getAccountLockUntil(e.msg.sender) <= e.block.timestamp;

Expand Down
2 changes: 1 addition & 1 deletion certora/specs/StakeManagerProcessAccount.spec
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ hook Sstore accounts[KEY address addr].balance uint256 newValue (uint256 oldValu
https://prover.certora.com/output/40726/055d52bc67154e3fbea330fd7d68d36d/?anonymousKey=73030555b4cefe429d4eed6718b9a7e5be3a22c8
*/
rule checkAccountProcessedBeforeStoring(method f) filtered {
f -> !requiresPreviousManager(f) && !requiresNextManager(f)
f -> !requiresPreviousManager(f) && !requiresNextManager(f) && f.selector != sig:stake(uint256,uint256).selector
} {
address account;

Expand Down
Loading
Loading