From 1f7880a5737a8d7faee84534dc200b2ad0d7f5ad Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt <3esmit@gmail.com> Date: Wed, 11 Sep 2024 09:05:18 -0300 Subject: [PATCH] fix(StakeManager): use while loop instead of hacky for loop in process account chore: lint again with foundry updated --- .gas-report | 122 +++++++++++++++++++++++++++++++++++++ .gas-snapshot | 35 +++++------ contracts/StakeManager.sol | 4 +- 3 files changed, 143 insertions(+), 18 deletions(-) diff --git a/.gas-report b/.gas-report index e69de29..ba89135 100644 --- a/.gas-report +++ b/.gas-report @@ -0,0 +1,122 @@ +| contracts/StakeManager.sol:StakeManager contract | | | | | | +|--------------------------------------------------|-----------------|--------|--------|--------|---------| +| Deployment Cost | Deployment Size | | | | | +| 3043249 | 15474 | | | | | +| Function Name | min | avg | median | max | # calls | +| EPOCH_SIZE | 307 | 307 | 307 | 307 | 816 | +| MAX_BOOST | 285 | 285 | 285 | 285 | 1 | +| MAX_LOCKUP_PERIOD | 361 | 361 | 361 | 361 | 4 | +| MIN_LOCKUP_PERIOD | 287 | 287 | 287 | 287 | 11 | +| YEAR | 263 | 263 | 263 | 263 | 1 | +| accounts | 1597 | 1597 | 1597 | 1597 | 21401 | +| calculateMPToMint | 740 | 740 | 740 | 740 | 3 | +| currentEpoch | 406 | 1691 | 2406 | 2406 | 28 | +| epochEnd | 627 | 639 | 627 | 4627 | 635 | +| epochReward | 1403 | 2903 | 1403 | 5903 | 3 | +| executeAccount | 28897 | 95353 | 95073 | 169064 | 18965 | +| executeEpoch | 23436 | 169158 | 164483 | 204283 | 574 | +| isVault | 540 | 573 | 540 | 2540 | 16460 | +| lock | 23840 | 23840 | 23840 | 23840 | 1 | +| migrateTo | 23869 | 23875 | 23875 | 23881 | 2 | +| migration | 439 | 1439 | 1439 | 2439 | 4 | +| migrationInitialize | 24602 | 24602 | 24602 | 24602 | 1 | +| owner | 2432 | 2432 | 2432 | 2432 | 13 | +| pendingMPToBeMinted | 364 | 364 | 364 | 364 | 512 | +| pendingReward | 364 | 1398 | 2364 | 2364 | 29 | +| previousManager | 275 | 275 | 275 | 275 | 13 | +| setVault | 46239 | 46239 | 46239 | 46239 | 279 | +| stake | 23983 | 23983 | 23983 | 23983 | 1 | +| stakeRewardEstimate | 436 | 2269 | 2436 | 2436 | 12 | +| stakedToken | 273 | 273 | 273 | 273 | 16476 | +| startMigration | 108037 | 108045 | 108049 | 108049 | 3 | +| totalSupply | 740 | 1921 | 2740 | 2740 | 22 | +| totalSupplyBalance | 385 | 1785 | 2385 | 2385 | 20 | +| totalSupplyMP | 385 | 437 | 385 | 2385 | 533 | +| unstake | 23819 | 23819 | 23819 | 23819 | 1 | + + +| contracts/StakeManager.sol:StakeRewardEstimate contract | | | | | | +|---------------------------------------------------------|-----------------|-------|--------|-------|---------| +| Deployment Cost | Deployment Size | | | | | +| 0 | 0 | | | | | +| Function Name | min | avg | median | max | # calls | +| getExpiredMP | 2427 | 2427 | 2427 | 2427 | 630 | +| transferOwnership | 28533 | 28533 | 28533 | 28533 | 1 | + + +| contracts/StakeVault.sol:StakeVault contract | | | | | | +|----------------------------------------------|-----------------|--------|--------|--------|---------| +| 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 | 45387 | 74829 | 66590 | 155519 | 6 | +| owner | 362 | 362 | 362 | 362 | 16459 | +| stake | 27265 | 269110 | 267752 | 354575 | 16464 | +| stakedToken | 212 | 212 | 212 | 212 | 2 | +| unstake | 42163 | 92145 | 80441 | 204532 | 11 | + + +| contracts/VaultFactory.sol:VaultFactory contract | | | | | | +|--------------------------------------------------|-----------------|--------|--------|--------|---------| +| Deployment Cost | Deployment Size | | | | | +| 0 | 0 | | | | | +| Function Name | min | avg | median | max | # calls | +| createVault | 696531 | 696531 | 696531 | 696531 | 16463 | +| setStakeManager | 23710 | 26669 | 26076 | 30222 | 3 | +| stakeManager | 368 | 1868 | 2368 | 2368 | 4 | + + +| lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol:ERC20 contract | | | | | | +|---------------------------------------------------------------------------|-----------------|-------|--------|-------|---------| +| Deployment Cost | Deployment Size | | | | | +| 0 | 0 | | | | | +| Function Name | min | avg | median | max | # calls | +| approve | 46175 | 46198 | 46199 | 46211 | 16459 | +| balanceOf | 561 | 607 | 561 | 2561 | 39452 | + + +| script/Deploy.s.sol:Deploy contract | | | | | | +|-------------------------------------|-----------------|---------|---------|---------|---------| +| Deployment Cost | Deployment Size | | | | | +| 6644281 | 31971 | | | | | +| Function Name | min | avg | median | max | # calls | +| run | 5857084 | 5857084 | 5857084 | 5857084 | 55 | + + +| script/DeployMigrationStakeManager.s.sol:DeployMigrationStakeManager contract | | | | | | +|-------------------------------------------------------------------------------|-----------------|---------|---------|---------|---------| +| Deployment Cost | Deployment Size | | | | | +| 3807695 | 18739 | | | | | +| Function Name | min | avg | median | max | # calls | +| run | 2842754 | 2842754 | 2842754 | 2842754 | 9 | + + +| script/DeploymentConfig.s.sol:DeploymentConfig contract | | | | | | +|---------------------------------------------------------|-----------------|-----|--------|-----|---------| +| Deployment Cost | Deployment Size | | | | | +| 0 | 0 | | | | | +| Function Name | min | avg | median | max | # calls | +| activeNetworkConfig | 455 | 455 | 455 | 455 | 110 | + + +| test/mocks/BrokenERC20.s.sol:BrokenERC20 contract | | | | | | +|---------------------------------------------------|-----------------|-------|--------|-------|---------| +| Deployment Cost | Deployment Size | | | | | +| 0 | 0 | | | | | +| Function Name | min | avg | median | max | # calls | +| approve | 46175 | 46175 | 46175 | 46175 | 1 | +| balanceOf | 561 | 1227 | 561 | 2561 | 3 | + + +| test/script/DeployBroken.s.sol:DeployBroken contract | | | | | | +|------------------------------------------------------|-----------------|---------|---------|---------|---------| +| Deployment Cost | Deployment Size | | | | | +| 5328977 | 25769 | | | | | +| Function Name | min | avg | median | max | # calls | +| run | 4696906 | 4696906 | 4696906 | 4696906 | 1 | + + + + diff --git a/.gas-snapshot b/.gas-snapshot index d925e79..304a49a 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,9 +1,10 @@ CreateVaultTest:testDeployment() (gas: 9774) CreateVaultTest:test_createVault() (gas: 692936) -ExecuteAccountTest:testDeployment() (gas: 28720) -ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 3856635) -ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1154886) -ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 110522900) +ExecuteAccountTest:testDeployment() (gas: 28742) +ExecuteAccountTest:test_ExecuteAccountLimit() (gas: 1338446) +ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 3857454) +ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1155096) +ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 110608299) ExecuteEpochTest:testDeployment() (gas: 28720) ExecuteEpochTest:testNewDeployment() (gas: 30815) ExecuteEpochTest:test_ExecuteEpochShouldIncreaseEpoch() (gas: 94810) @@ -14,17 +15,17 @@ LeaveTest:testDeployment() (gas: 28720) LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 1154760) LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 10750) LockTest:testDeployment() (gas: 28720) -LockTest:test_NewLockupPeriod() (gas: 1143587) -LockTest:test_RevertWhen_InvalidNewLockupPeriod() (gas: 1135204) -LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1231813) +LockTest:test_NewLockupPeriod() (gas: 1143513) +LockTest:test_RevertWhen_InvalidNewLockupPeriod() (gas: 1135130) +LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1231731) LockTest:test_RevertWhen_SenderIsNotVault() (gas: 10630) -LockTest:test_ShouldIncreaseBonusMP() (gas: 1123687) -LockTest:test_UpdateLockupPeriod() (gas: 1281200) +LockTest:test_ShouldIncreaseBonusMP() (gas: 1123613) +LockTest:test_UpdateLockupPeriod() (gas: 1281110) MigrateTest:testDeployment() (gas: 28720) MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 1152399) MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 10750) MigrationInitializeTest:testDeployment() (gas: 28720) -MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 5716968) +MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 5715768) MigrationStakeManagerTest:testDeployment() (gas: 28720) MigrationStakeManagerTest:testNewDeployment() (gas: 30859) MigrationStakeManagerTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 105686) @@ -39,17 +40,17 @@ StakeTest:test_RevertWhen_RestakeWithLock() (gas: 1160132) StakeTest:test_RevertWhen_SenderIsNotVault() (gas: 10651) StakeTest:test_RevertWhen_StakeIsTooLow() (gas: 745253) StakeTest:test_RevertWhen_StakeTokenTransferFails() (gas: 175040) -StakeTest:test_StakeWithoutLockUpTimeMintsMultiplierPoints() (gas: 1029215) +StakeTest:test_StakeWithoutLockUpTimeMintsMultiplierPoints() (gas: 1029141) StakedTokenTest:testStakeToken() (gas: 7616) UnstakeTest:testDeployment() (gas: 28742) UnstakeTest:test_RevertWhen_AmountMoreThanBalance() (gas: 1133014) UnstakeTest:test_RevertWhen_FundsLocked() (gas: 1158758) UnstakeTest:test_RevertWhen_SenderIsNotVault() (gas: 10653) -UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 5497531) -UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1026695) -UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1115820) +UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 5497132) +UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1026621) +UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1115812) UserFlowsTest:testDeployment() (gas: 28720) -UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8) (runs: 1001, μ: 69673434, ~: 32945058) -UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1116708) -UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 1951147) +UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8) (runs: 1001, μ: 68773621, ~: 28286723) +UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1116700) +UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 1950999) VaultFactoryTest:testDeployment() (gas: 9774) \ No newline at end of file diff --git a/contracts/StakeManager.sol b/contracts/StakeManager.sol index 3ea8d86..8e303c2 100644 --- a/contracts/StakeManager.sol +++ b/contracts/StakeManager.sol @@ -441,7 +441,8 @@ contract StakeManager is Ownable { uint256 userReward; uint256 userEpoch = account.epoch; uint256 mpDifference = account.totalMP; - for (Epoch storage iEpoch = epochs[userEpoch]; userEpoch < _limitEpoch; userEpoch++) { + while (userEpoch < _limitEpoch) { + Epoch storage iEpoch = epochs[userEpoch]; //mint multiplier points to that epoch _mintMP(account, iEpoch.startTime + EPOCH_SIZE, iEpoch); uint256 userSupply = account.balance + account.totalMP; @@ -450,6 +451,7 @@ contract StakeManager is Ownable { iEpoch.epochReward -= userEpochReward; iEpoch.totalSupply -= userSupply; //TODO: remove epoch when iEpoch.totalSupply reaches zero + userEpoch++; } account.epoch = userEpoch; if (userReward > 0) {