From b5ce251b8ed4632105a1fc5372c910b146b8afd4 Mon Sep 17 00:00:00 2001 From: r4bbit <445106+0x-r4bbit@users.noreply.github.com> Date: Mon, 9 Dec 2024 15:59:07 +0100 Subject: [PATCH] feat(scripts): add upgrade script This introduces a simple upgrade script template that can be used to upgrade the stake manager. --- .gas-report | 58 +++++++++++++++++++++------ .gas-snapshot | 6 +-- script/UpgradeRewardsStreamerMP.s.sol | 22 ++++++++++ test/RewardsStreamerMP.t.sol | 27 +++---------- 4 files changed, 77 insertions(+), 36 deletions(-) create mode 100644 script/UpgradeRewardsStreamerMP.s.sol diff --git a/.gas-report b/.gas-report index 8ddc0f4..bd20e13 100644 --- a/.gas-report +++ b/.gas-report @@ -9,11 +9,19 @@ | script/DeploymentConfig.s.sol:DeploymentConfig contract | | | | | | |---------------------------------------------------------|-----------------|-----|--------|-----|---------| | Deployment Cost | Deployment Size | | | | | -| 0 | 7132 | | | | | +| 0 | 0 | | | | | | Function Name | min | avg | median | max | # calls | | activeNetworkConfig | 454 | 454 | 454 | 454 | 126 | +| script/UpgradeRewardsStreamerMP.s.sol:UpgradeRewardsStreamerMPScript contract | | | | | | +|-------------------------------------------------------------------------------|-----------------|---------|---------|---------|---------| +| Deployment Cost | Deployment Size | | | | | +| 2845816 | 14079 | | | | | +| Function Name | min | avg | median | max | # calls | +| run | 2383391 | 2383391 | 2383391 | 2383391 | 3 | + + | src/RewardsStreamer.sol:RewardsStreamer contract | | | | | | |--------------------------------------------------|-----------------|--------|--------|--------|---------| | Deployment Cost | Deployment Size | | | | | @@ -66,16 +74,42 @@ | unstake | 60382 | 60919 | 60382 | 63877 | 13 | | updateAccountMP | 15396 | 18474 | 17898 | 34998 | 21 | | updateGlobalState | 11088 | 28045 | 25249 | 110284 | 21 | -| upgradeToAndCall | 3225 | 9387 | 10926 | 10936 | 5 | - - -| src/StakeManagerProxy.sol:StakeManagerProxy contract | | | | | | -|------------------------------------------------------|-----------------|------|--------|--------|---------| -| Deployment Cost | Deployment Size | | | | | -| 256467 | 1231 | | | | | -| Function Name | min | avg | median | max | # calls | -| fallback | 678 | 7210 | 2115 | 136272 | 823 | -| implementation | 343 | 775 | 343 | 2343 | 412 | +| upgradeToAndCall | 3225 | 7887 | 8426 | 10936 | 5 | + + +| src/StakeManagerProxy.sol:StakeManagerProxy contract | | | | | | +|------------------------------------------------------|-----------------|-------|--------|--------|---------| +| Deployment Cost | Deployment Size | | | | | +| 256467 | 1263 | | | | | +| Function Name | min | avg | median | max | # calls | +| MAX_LOCKUP_PERIOD | 776 | 3776 | 5276 | 5276 | 6 | +| MAX_MULTIPLIER | 678 | 1905 | 678 | 5178 | 33 | +| MIN_LOCKUP_PERIOD | 724 | 3424 | 5224 | 5224 | 15 | +| MP_RATE_PER_YEAR | 680 | 1180 | 680 | 5180 | 9 | +| STAKING_TOKEN | 855 | 6083 | 7355 | 7355 | 322 | +| emergencyModeEnabled | 7347 | 7347 | 7347 | 7347 | 7 | +| enableEmergencyMode | 28480 | 45381 | 50665 | 50665 | 8 | +| getAccount | 2115 | 2115 | 2115 | 2115 | 72 | +| getStakedBalance | 7559 | 7559 | 7559 | 7559 | 1 | +| getUserTotalMP | 9660 | 9660 | 9660 | 9660 | 1 | +| getUserTotalMaxMP | 3553 | 3553 | 3553 | 3553 | 1 | +| getUserTotalStakedBalance | 15548 | 15548 | 15548 | 15548 | 1 | +| getUserVaults | 5637 | 6762 | 5637 | 10137 | 4 | +| implementation | 343 | 775 | 343 | 2343 | 412 | +| lastRewardTime | 822 | 1822 | 1822 | 2822 | 2 | +| rewardEndTime | 800 | 1800 | 1800 | 2800 | 2 | +| rewardStartTime | 779 | 4029 | 4029 | 7279 | 2 | +| rewardsBalanceOf | 1747 | 1747 | 1747 | 1747 | 4 | +| setReward | 28863 | 77206 | 86636 | 128881 | 7 | +| setTrustedCodehash | 52889 | 52889 | 52889 | 52889 | 2 | +| totalMP | 800 | 800 | 800 | 800 | 81 | +| totalMaxMP | 777 | 777 | 777 | 777 | 81 | +| totalRewardsAccrued | 800 | 800 | 800 | 800 | 3 | +| totalRewardsSupply | 1387 | 2498 | 2151 | 11627 | 30 | +| totalStaked | 823 | 823 | 823 | 823 | 82 | +| updateAccountMP | 41755 | 44833 | 44257 | 61357 | 21 | +| updateGlobalState | 37076 | 54033 | 51237 | 136272 | 21 | +| upgradeToAndCall | 29868 | 33720 | 33720 | 37572 | 2 | | src/StakeVault.sol:StakeVault contract | | | | | | @@ -167,7 +201,7 @@ | test/mocks/MockToken.sol:MockToken contract | | | | | | |---------------------------------------------|-----------------|-------|--------|-------|---------| | Deployment Cost | Deployment Size | | | | | -| 625454 | 3260 | | | | | +| 625370 | 3260 | | | | | | Function Name | min | avg | median | max | # calls | | approve | 46330 | 46339 | 46342 | 46342 | 262 | | balanceOf | 558 | 989 | 558 | 2558 | 139 | diff --git a/.gas-snapshot b/.gas-snapshot index dfc4dbe..1f09771 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -7,9 +7,9 @@ EmergencyExitTest:test_EmergencyExitWithLock() (gas: 391762) EmergencyExitTest:test_EmergencyExitWithRewards() (gas: 377316) EmergencyExitTest:test_OnlyOwnerCanEnableEmergencyMode() (gas: 39426) IntegrationTest:testStakeFoo() (gas: 1178499) -LeaveTest:test_LeaveShouldProperlyUpdateAccounting() (gas: 2960876) +LeaveTest:test_LeaveShouldProperlyUpdateAccounting() (gas: 5626814) LeaveTest:test_RevertWhenStakeManagerIsTrusted() (gas: 294826) -LeaveTest:test_TrustNewStakeManager() (gas: 3036018) +LeaveTest:test_TrustNewStakeManager() (gas: 5699451) LockTest:test_LockFailsWithInvalidPeriod() (gas: 309911) LockTest:test_LockFailsWithNoStake() (gas: 63730) LockTest:test_LockWithoutPriorLock() (gas: 385915) @@ -67,7 +67,7 @@ UnstakeTest:test_UnstakeOneAccountAndAccruedMP() (gas: 494565) UnstakeTest:test_UnstakeOneAccountAndRewards() (gas: 404122) UnstakeTest:test_UnstakeOneAccountWithLockUpAndAccruedMP() (gas: 523042) UpgradeTest:test_RevertWhenNotOwner() (gas: 2602178) -UpgradeTest:test_UpgradeStakeManager() (gas: 2875664) +UpgradeTest:test_UpgradeStakeManager() (gas: 5541589) VaultRegistrationTest:test_VaultRegistration() (gas: 62013) WithdrawTest:test_CannotWithdrawStakedFunds() (gas: 310550) XPNFTTokenTest:testApproveNotAllowed() (gas: 10500) diff --git a/script/UpgradeRewardsStreamerMP.s.sol b/script/UpgradeRewardsStreamerMP.s.sol new file mode 100644 index 0000000..2fe1846 --- /dev/null +++ b/script/UpgradeRewardsStreamerMP.s.sol @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.26; + +import { UUPSUpgradeable } from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; +import { BaseScript } from "./Base.s.sol"; +import { RewardsStreamerMP } from "../src/RewardsStreamerMP.sol"; +import { IStakeManagerProxy } from "../src/interfaces/IStakeManagerProxy.sol"; + +contract UpgradeRewardsStreamerMPScript is BaseScript { + function run(address admin, IStakeManagerProxy currentImplProxy) public { + address deployer = broadcaster; + if (admin != address(0)) { + deployer = admin; + } + vm.startBroadcast(deployer); + // Replace this with actual new version of the contract + address nextImpl = address(new RewardsStreamerMP()); + bytes memory initializeData; + UUPSUpgradeable(address(currentImplProxy)).upgradeToAndCall(nextImpl, initializeData); + vm.stopBroadcast(); + } +} diff --git a/test/RewardsStreamerMP.t.sol b/test/RewardsStreamerMP.t.sol index 43cbfeb..cde458f 100644 --- a/test/RewardsStreamerMP.t.sol +++ b/test/RewardsStreamerMP.t.sol @@ -4,6 +4,7 @@ pragma solidity ^0.8.26; import { Test } from "forge-std/Test.sol"; import { Math } from "@openzeppelin/contracts/utils/math/Math.sol"; import { DeployRewardsStreamerMPScript } from "../script/DeployRewardsStreamerMP.s.sol"; +import { UpgradeRewardsStreamerMPScript } from "../script/UpgradeRewardsStreamerMP.s.sol"; import { DeploymentConfig } from "../script/DeploymentConfig.s.sol"; import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol"; import { UUPSUpgradeable } from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; @@ -172,6 +173,11 @@ contract RewardsStreamerMPTest is Test { uint256 mpPerYear = amount * streamer.MP_RATE_PER_YEAR(); return target * 365 days / mpPerYear; } + + function _upgradeStakeManager() internal { + UpgradeRewardsStreamerMPScript upgrade = new UpgradeRewardsStreamerMPScript(); + upgrade.run(admin, IStakeManagerProxy(address(streamer))); + } } contract MathTest is RewardsStreamerMPTest { @@ -1844,13 +1850,6 @@ contract EmergencyExitTest is RewardsStreamerMPTest { } contract UpgradeTest is RewardsStreamerMPTest { - function _upgradeStakeManager() internal { - address newImpl = address(new RewardsStreamerMP()); - bytes memory initializeData; - vm.prank(admin); - UUPSUpgradeable(streamer).upgradeToAndCall(newImpl, initializeData); - } - function setUp() public override { super.setUp(); } @@ -1897,13 +1896,6 @@ contract UpgradeTest is RewardsStreamerMPTest { } contract LeaveTest is RewardsStreamerMPTest { - function _upgradeStakeManager() internal { - address newImpl = address(new RewardsStreamerMP()); - bytes memory initializeData; - vm.prank(admin); - UUPSUpgradeable(streamer).upgradeToAndCall(newImpl, initializeData); - } - function setUp() public override { super.setUp(); } @@ -1996,13 +1988,6 @@ contract LeaveTest is RewardsStreamerMPTest { } contract MaliciousUpgradeTest is RewardsStreamerMPTest { - function _upgradeStakeManager() internal { - address newImpl = address(new RewardsStreamerMP()); - bytes memory initializeData; - vm.prank(admin); - UUPSUpgradeable(streamer).upgradeToAndCall(newImpl, initializeData); - } - function setUp() public override { super.setUp(); }