Skip to content

Commit

Permalink
feat(StakeManager): implement multiplier points estimation
Browse files Browse the repository at this point in the history
This commit introduces the internal accounting logic for accrueing
multiplier points, that will later be used to determine how many
experience points an account is eligible to.

The majority of the work here was done by @3esmit.
  • Loading branch information
0x-r4bbit committed Sep 5, 2024
1 parent dbe6f08 commit 44d16c4
Show file tree
Hide file tree
Showing 13 changed files with 350 additions and 258 deletions.
98 changes: 0 additions & 98 deletions .gas-report
Original file line number Diff line number Diff line change
@@ -1,98 +0,0 @@
| contracts/StakeManager.sol:StakeManager contract | | | | | |
|--------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost | Deployment Size | | | | |
| 2058079 | 10495 | | | | |
| Function Name | min | avg | median | max | # calls |
| EPOCH_SIZE | 285 | 285 | 285 | 285 | 9 |
| MAX_LOCKUP_PERIOD | 405 | 405 | 405 | 405 | 2 |
| MIN_LOCKUP_PERIOD | 264 | 264 | 264 | 264 | 3 |
| accounts | 1406 | 1406 | 1406 | 1406 | 22 |
| currentEpoch | 341 | 1571 | 2341 | 2341 | 13 |
| epochEnd | 627 | 627 | 627 | 627 | 56 |
| executeAccount | 1311 | 54054 | 58730 | 104630 | 63 |
| executeEpoch | 87833 | 95166 | 87833 | 109833 | 3 |
| isVault | 517 | 2117 | 2517 | 2517 | 15 |
| lock | 2614 | 2614 | 2614 | 2614 | 1 |
| migrateTo | 1041 | 1713 | 1041 | 2721 | 5 |
| oldManager | 240 | 240 | 240 | 240 | 8 |
| owner | 2341 | 2341 | 2341 | 2341 | 8 |
| pendingReward | 386 | 1243 | 386 | 2386 | 21 |
| setVault | 22606 | 22606 | 22606 | 22606 | 12 |
| stake | 2638 | 136864 | 188409 | 189128 | 17 |
| stakedToken | 260 | 260 | 260 | 260 | 26 |
| totalSupply | 561 | 561 | 561 | 561 | 17 |
| totalSupplyBalance | 362 | 1592 | 2362 | 2362 | 13 |
| totalSupplyMP | 384 | 1614 | 2384 | 2384 | 13 |
| unstake | 1730 | 20008 | 8086 | 127550 | 9 |


| contracts/StakeVault.sol:StakeVault contract | | | | | |
|----------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost | Deployment Size | | | | |
| 635445 | 3370 | | | | |
| Function Name | min | avg | median | max | # calls |
| acceptMigration | 1726 | 1726 | 1726 | 1726 | 2 |
| leave | 1712 | 1712 | 1712 | 1712 | 1 |
| owner | 362 | 362 | 362 | 362 | 14 |
| stake | 3433 | 165544 | 219184 | 219903 | 17 |
| stakedToken | 212 | 212 | 212 | 212 | 2 |
| unstake | 2588 | 28122 | 14407 | 131871 | 8 |


| contracts/VaultFactory.sol:VaultFactory contract | | | | | |
|--------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost | Deployment Size | | | | |
| 1043406 | 5305 | | | | |
| Function Name | min | avg | median | max | # calls |
| createVault | 670954 | 674204 | 675454 | 675454 | 18 |
| setStakeManager | 2518 | 5317 | 4644 | 8790 | 3 |
| stakeManager | 368 | 1868 | 2368 | 2368 | 4 |


| lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol:ERC20 contract | | | | | |
|---------------------------------------------------------------------------|-----------------|-------|--------|-------|---------|
| Deployment Cost | Deployment Size | | | | |
| 649818 | 3562 | | | | |
| Function Name | min | avg | median | max | # calls |
| approve | 24603 | 24603 | 24603 | 24603 | 15 |
| balanceOf | 561 | 819 | 561 | 2561 | 139 |
| transfer | 3034 | 8340 | 3034 | 22934 | 15 |
| transferFrom | 27530 | 27530 | 27530 | 27530 | 16 |


| script/Deploy.s.sol:Deploy contract | | | | | |
|-------------------------------------|-----------------|---------|---------|---------|---------|
| Deployment Cost | Deployment Size | | | | |
| 5142589 | 26992 | | | | |
| Function Name | min | avg | median | max | # calls |
| run | 4837698 | 4837698 | 4837698 | 4837698 | 32 |


| script/DeploymentConfig.s.sol:DeploymentConfig contract | | | | | |
|---------------------------------------------------------|-----------------|-----|--------|-----|---------|
| Deployment Cost | Deployment Size | | | | |
| 1634091 | 8548 | | | | |
| Function Name | min | avg | median | max | # calls |
| activeNetworkConfig | 455 | 455 | 455 | 455 | 64 |


| test/mocks/BrokenERC20.s.sol:BrokenERC20 contract | | | | | |
|---------------------------------------------------|-----------------|-------|--------|-------|---------|
| Deployment Cost | Deployment Size | | | | |
| 475642 | 2660 | | | | |
| Function Name | min | avg | median | max | # calls |
| approve | 24603 | 24603 | 24603 | 24603 | 1 |
| balanceOf | 561 | 1227 | 561 | 2561 | 3 |
| transferFrom | 511 | 511 | 511 | 511 | 1 |


| test/script/DeployBroken.s.sol:DeployBroken contract | | | | | |
|------------------------------------------------------|-----------------|---------|---------|---------|---------|
| Deployment Cost | Deployment Size | | | | |
| 3915327 | 20790 | | | | |
| Function Name | min | avg | median | max | # calls |
| run | 3677521 | 3677521 | 3677521 | 3677521 | 1 |




67 changes: 43 additions & 24 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
CreateVaultTest:testDeployment() (gas: 9774)
CreateVaultTest:test_createVault() (gas: 692923)
ExecuteAccountTest:testDeployment() (gas: 26335)
ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 3633587)
ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1051631)
LeaveTest:testDeployment() (gas: 26335)
LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 1052239)
LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 10794)
LockTest:testDeployment() (gas: 26335)
LockTest:test_RevertWhen_InvalidLockupPeriod() (gas: 865463)
CreateVaultTest:test_createVault() (gas: 692936)
ExecuteAccountTest:testDeployment() (gas: 28720)
ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 3856669)
ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1154868)
ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 78782687)
ExecuteEpochTest:testDeployment() (gas: 28720)
ExecuteEpochTest:testNewDeployment() (gas: 30815)
ExecuteEpochTest:test_ExecuteEpochShouldIncreaseEpoch() (gas: 94832)
ExecuteEpochTest:test_ExecuteEpochShouldIncreasePendingReward() (gas: 253059)
ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochBeforeEnd() (gas: 17994)
ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 105720)
LeaveTest:testDeployment() (gas: 28720)
LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 1154740)
LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 10750)
LockTest:testDeployment() (gas: 28720)
LockTest:test_NewLockupPeriod() (gas: 1143590)
LockTest:test_RevertWhen_InvalidNewLockupPeriod() (gas: 1135184)
LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1231816)
LockTest:test_RevertWhen_SenderIsNotVault() (gas: 10630)
MigrateTest:testDeployment() (gas: 26335)
MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 1049846)
MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 10794)
LockTest:test_ShouldIncreaseBonusMP() (gas: 1123712)
LockTest:test_UpdateLockupPeriod() (gas: 1281226)
MigrateTest:testDeployment() (gas: 28720)
MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 1152379)
MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 10750)
MigrationInitializeTest:testDeployment() (gas: 28720)
MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 5719768)
MigrationStakeManagerTest:testDeployment() (gas: 28720)
MigrationStakeManagerTest:testNewDeployment() (gas: 30859)
MigrationStakeManagerTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 105708)
SetStakeManagerTest:testDeployment() (gas: 9774)
SetStakeManagerTest:test_RevertWhen_InvalidStakeManagerAddress() (gas: 20481)
SetStakeManagerTest:test_SetStakeManager() (gas: 19869)
StakeManagerTest:testDeployment() (gas: 26107)
StakeTest:testDeployment() (gas: 26335)
StakeTest:test_RevertWhen_InvalidLockupPeriod() (gas: 883366)
StakeTest:test_RevertWhen_SenderIsNotVault() (gas: 10650)
StakeManagerTest:testDeployment() (gas: 28492)
StakeTest:testDeployment() (gas: 28720)
StakeTest:test_RevertWhen_InvalidLockupPeriod() (gas: 892157)
StakeTest:test_RevertWhen_SenderIsNotVault() (gas: 10680)
StakeTest:test_RevertWhen_StakeTokenTransferFails() (gas: 175040)
StakeTest:test_StakeWithoutLockUpTimeMintsMultiplierPoints() (gas: 948728)
StakeTest:test_StakeWithoutLockUpTimeMintsMultiplierPoints() (gas: 1029276)
StakedTokenTest:testStakeToken() (gas: 7616)
UnstakeTest:testDeployment() (gas: 26357)
UnstakeTest:test_RevertWhen_FundsLocked() (gas: 1051813)
UnstakeTest:testDeployment() (gas: 28742)
UnstakeTest:test_RevertWhen_AmountMoreThanBalance() (gas: 1132994)
UnstakeTest:test_RevertWhen_FundsLocked() (gas: 1158740)
UnstakeTest:test_RevertWhen_SenderIsNotVault() (gas: 10653)
UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 3573382)
UnstakeTest:test_UnstakeShouldReturnFunds() (gas: 946825)
UserFlowsTest:testDeployment() (gas: 26335)
UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1046480)
UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 1825625)
UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 5498441)
UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1026676)
UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1115802)
UserFlowsTest:testDeployment() (gas: 28720)
UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8) (runs: 1001, μ: 67853584, ~: 29218023)
UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1116690)
UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 1951132)
VaultFactoryTest:testDeployment() (gas: 9774)
7 changes: 5 additions & 2 deletions certora/confs/StakeManager.conf
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
{
"files":
"files":
["contracts/StakeManager.sol",
"certora/helpers/StakeRewardEstimateA.sol",
"certora/helpers/ERC20A.sol"
],
"link" : [
"StakeManager:stakedToken=ERC20A"
"StakeManager:stakedToken=ERC20A",
"StakeManager:stakeRewardEstimate=StakeRewardEstimateA"
],
"msg": "Verifying StakeManager.sol",
"rule_sanity": "basic",
"verify": "StakeManager:certora/specs/StakeManager.spec",
"optimistic_loop": true,
"loop_iter": "3",
"packages": [
"forge-std=lib/forge-std/src",
"@openzeppelin=lib/openzeppelin-contracts"
]
}
Expand Down
9 changes: 6 additions & 3 deletions certora/confs/StakeManagerProcess.conf
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
{
"files":
"files":
["contracts/StakeManager.sol",
"certora/helpers/ERC20A.sol"
"certora/helpers/ERC20A.sol",
"certora/helpers/StakeRewardEstimateA.sol"
],
"link" : [
"StakeManager:stakedToken=ERC20A"
"StakeManager:stakedToken=ERC20A",
"StakeManager:stakeRewardEstimate=StakeRewardEstimateA"
],
"msg": "Verifying StakeManager ProcessAccount",
"rule_sanity": "basic",
"verify": "StakeManager:certora/specs/StakeManagerProcessAccount.spec",
"optimistic_loop": true,
"loop_iter": "3",
"packages": [
"forge-std=lib/forge-std/src",
"@openzeppelin=lib/openzeppelin-contracts"
]
}
Expand Down
4 changes: 3 additions & 1 deletion certora/confs/StakeManagerStartMigration.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
"files":
[ "contracts/StakeManager.sol",
"certora/harness/StakeManagerNew.sol",
"certora/helpers/StakeRewardEstimateA.sol",
"certora/helpers/ERC20A.sol"
],
"link" : [
"StakeManager:stakedToken=ERC20A"
"StakeManager:stakedToken=ERC20A",
"StakeManager:stakeRewardEstimate=StakeRewardEstimateA",
],
"msg": "Verifying StakeManager.sol",
"rule_sanity": "basic",
Expand Down
2 changes: 2 additions & 0 deletions certora/confs/StakeVault.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"files": [
"contracts/StakeManager.sol",
"contracts/StakeVault.sol",
"certora/helpers/StakeRewardEstimateA.sol",
"certora/helpers/ERC20A.sol"
],
"link" : [
"StakeVault:STAKED_TOKEN=ERC20A",
"StakeManager:stakedToken=ERC20A",
"StakeManager:stakeRewardEstimate=StakeRewardEstimateA",
"StakeVault:stakeManager=StakeManager"
],
"msg": "Verifying StakeVault.sol",
Expand Down
8 changes: 8 additions & 0 deletions certora/helpers/StakeRewardEstimateA.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.19;

import { StakeRewardEstimate } from "./../../contracts/StakeManager.sol";

contract StakeRewardEstimateA is StakeRewardEstimate {}

Check warning on line 7 in certora/helpers/StakeRewardEstimateA.sol

View workflow job for this annotation

GitHub Actions / lint

Code contains empty blocks

15 changes: 8 additions & 7 deletions certora/specs/StakeManager.spec
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ methods {
function previousManager() external returns (address) envfree;
function _.migrateFrom(address, bool, StakeManager.Account) external => NONDET;
function _.increaseTotalMP(uint256) external => NONDET;
function _.migrationInitialize(uint256,uint256,uint256,uint256) external => NONDET;
function accounts(address) external returns(address, uint256, uint256, uint256, uint256, uint256, uint256) envfree;
function _.migrationInitialize(uint256,uint256,uint256,uint256,uint256,uint256,uint256) external => NONDET;
function accounts(address) external returns(address, uint256, uint256, uint256, uint256, uint256, uint256, uint256) envfree;
function Math.mulDiv(uint256 a, uint256 b, uint256 c) internal returns uint256 => mulDivSummary(a,b,c);
function _._ external => DISPATCH [] default NONDET;
}

function mulDivSummary(uint256 a, uint256 b, uint256 c) returns uint256 {
Expand All @@ -18,28 +19,28 @@ function mulDivSummary(uint256 a, uint256 b, uint256 c) returns uint256 {

function getAccountBalance(address addr) returns uint256 {
uint256 balance;
_, balance, _, _, _, _, _ = accounts(addr);
_, balance, _, _, _, _, _, _ = accounts(addr);

return balance;
}

function getAccountBonusMultiplierPoints(address addr) returns uint256 {
uint256 bonusMP;
_, _, bonusMP, _, _, _, _ = accounts(addr);
_, _, bonusMP, _, _, _, _, _ = accounts(addr);

return bonusMP;
}

function getAccountCurrentMultiplierPoints(address addr) returns uint256 {
uint256 totalMP;
_, _, _, totalMP, _, _, _ = accounts(addr);
_, _, _, totalMP, _, _, _, _ = accounts(addr);

return totalMP;
}

function getAccountLockUntil(address addr) returns uint256 {
uint256 lockUntil;
_, _, _, _, _, lockUntil, _ = accounts(addr);
_, _, _, _, _, lockUntil, _, _ = accounts(addr);

return lockUntil;
}
Expand All @@ -59,7 +60,7 @@ function simplification(env e) {
}

definition requiresPreviousManager(method f) returns bool = (
f.selector == sig:migrationInitialize(uint256,uint256,uint256,uint256).selector ||
f.selector == sig:migrationInitialize(uint256,uint256,uint256,uint256,uint256,uint256,uint256).selector ||
f.selector == sig:migrateFrom(address,bool,StakeManager.Account).selector ||
f.selector == sig:increaseTotalMP(uint256).selector
);
Expand Down
11 changes: 7 additions & 4 deletions certora/specs/StakeManagerProcessAccount.spec
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ using ERC20A as staked;
methods {
function staked.balanceOf(address) external returns (uint256) envfree;
function totalSupplyBalance() external returns (uint256) envfree;
function accounts(address) external returns(address, uint256, uint256, uint256, uint256, uint256, uint256) envfree;
function totalSupplyMP() external returns (uint256) envfree;
function totalMPPerEpoch() external returns (uint256) envfree;
function accounts(address) external returns(address, uint256, uint256, uint256, uint256, uint256, uint256, uint256) envfree;

function _processAccount(StakeManager.Account storage account, uint256 _limitEpoch) internal with(env e) => markAccountProccessed(e.msg.sender, _limitEpoch);
function _.migrationInitialize(uint256,uint256,uint256,uint256) external => NONDET;
function _.migrationInitialize(uint256,uint256,uint256,uint256,uint256,uint256,uint256) external => NONDET;
function pendingMPToBeMinted() external returns (uint256) envfree;
}

// keeps track of the last epoch an account was processed
Expand All @@ -19,7 +22,7 @@ function markAccountProccessed(address account, uint256 _limitEpoch) {

function getAccountLockUntil(address addr) returns uint256 {
uint256 lockUntil;
_, _, _, _, _, lockUntil, _ = accounts(addr);
_, _, _, _, _, lockUntil, _, _ = accounts(addr);

return lockUntil;
}
Expand All @@ -29,7 +32,7 @@ hook Sstore accounts[KEY address addr].balance uint256 newValue (uint256 oldValu
}

definition requiresPreviousManager(method f) returns bool = (
f.selector == sig:migrationInitialize(uint256,uint256,uint256,uint256).selector ||
f.selector == sig:migrationInitialize(uint256,uint256,uint256,uint256,uint256,uint256,uint256).selector ||
f.selector == sig:migrateFrom(address,bool,StakeManager.Account).selector ||
f.selector == sig:increaseTotalMP(uint256).selector
);
Expand Down
Loading

0 comments on commit 44d16c4

Please sign in to comment.