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

chore(multi): use staking reinitializer and add upgrade tests #2465

Merged
merged 2 commits into from
Nov 12, 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
2 changes: 1 addition & 1 deletion contracts/bindings/admin.go

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions contracts/core/.gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Admin_Test:test_pause_unpause() (gas: 28603618)
Admin_Test:test_pause_unpause_bridge() (gas: 23633305)
Admin_Test:test_pause_unpause_xcall() (gas: 28557548)
Admin_Test:test_pause_unpause_xsubmit() (gas: 28557299)
Admin_Test:test_upgrade() (gas: 32638793)
Admin_Test:test_pause_unpause() (gas: 30662045)
Admin_Test:test_pause_unpause_bridge() (gas: 25691732)
Admin_Test:test_pause_unpause_xcall() (gas: 30615975)
Admin_Test:test_pause_unpause_xsubmit() (gas: 30615726)
Admin_Test:test_upgrade() (gas: 34697220)
AllocPredeploys_Test:test_num_allocs() (gas: 1181319043)
AllocPredeploys_Test:test_predeploys() (gas: 1181300853)
AllocPredeploys_Test:test_preinstalls() (gas: 1182017269)
Expand Down
13 changes: 12 additions & 1 deletion contracts/core/script/admin/Admin.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Script } from "forge-std/Script.sol";

import { BridgeL1PostUpgradeTest } from "./BridgeL1PostUpgradeTest.sol";
import { BridgeNativePostUpgradeTest } from "./BridgeNativePostUpgradeTest.sol";
import { StakingPostUpgradeTest } from "./StakingPostUpgradeTest.sol";

/**
* @title Admin
Expand Down Expand Up @@ -234,13 +235,23 @@ contract Admin is Script {
* @param deployer The address of the account that will deploy the new implementation.
*/
function upgradeStaking(address admin, address deployer, bytes calldata data) public {
Staking staking = Staking(Predeploys.Staking);

// read storage pre-upgrade
address owner = staking.owner();
bool isAllowlistEnabled = staking.isAllowlistEnabled();

vm.startBroadcast(deployer);
address impl = address(new Staking());
vm.stopBroadcast();

_upgradeProxy(admin, Predeploys.Staking, impl, data);

// TODO: add post upgrade tests
// assert storage unchanged
require(staking.owner() == owner, "owner changed");
require(staking.isAllowlistEnabled() == isAllowlistEnabled, "isAllowlistEnabled changed");

Zodomo marked this conversation as resolved.
Show resolved Hide resolved
new StakingPostUpgradeTest().run();
}

/**
Expand Down
98 changes: 98 additions & 0 deletions contracts/core/script/admin/StakingPostUpgradeTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity 0.8.24;

import { Predeploys } from "src/libraries/Predeploys.sol";
import { Staking } from "src/octane/Staking.sol";
import { Secp256k1 } from "src/libraries/Secp256k1.sol";
import { Test } from "forge-std/Test.sol";
import { VmSafe } from "forge-std/Vm.sol";

contract StakingPostUpgradeTest is Test {
Staking staking;
address owner;
address validator;

function run() public {
(VmSafe.CallerMode mode,,) = vm.readCallers();
require(mode == VmSafe.CallerMode.None, "no broadcast");

_setup();
_testEip712();
_testAllowlist();
_testCreateValidator();
_testDelegate();
}

function _setup() internal {
staking = Staking(Predeploys.Staking);
owner = staking.owner();
validator = makeAddr("validator");
}

function _testEip712() internal view {
(
bytes1 fields,
string memory name,
string memory version,
uint256 chainId,
address verifyingContract,
bytes32 salt,
uint256[] memory extensions
) = staking.eip712Domain();

assertEq(fields, hex"0f", "EIP-712 fields");
assertEq(name, "Staking", "EIP-712 name");
assertEq(version, "1", "EIP-712 version");
assertEq(chainId, block.chainid, "EIP-712 chainId");
assertEq(verifyingContract, address(staking), "EIP-712 verifyingContract");
assertEq(salt, bytes32(0), "EIP-712 salt");
assertEq(extensions.length, 0, "EIP-712 extensions");
}

function _testAllowlist() internal {
vm.startPrank(owner);
staking.enableAllowlist();
assertTrue(staking.isAllowlistEnabled(), "allowlist disabled");

address[] memory validators = new address[](1);
validators[0] = validator;
staking.allowValidators(validators);
assertTrue(staking.isAllowedValidator(validator), "validator not in allowlist");

staking.disallowValidators(validators);
assertFalse(staking.isAllowedValidator(validator), "validator in allowlist");

staking.disableAllowlist();
assertFalse(staking.isAllowlistEnabled(), "allowlist enabled");
vm.stopPrank();
}

function _testCreateValidator() internal {
bytes32 x = 0x3b12d750493ed6b12b390447f6dd38f587af12ed04ab8d6858e818cf0c63607c;
bytes32 y = 0x044e0321a3e57de51e95f2b230b9e4ffed2318578baab1a80652234fe0115d13;
bytes memory pubkey = Secp256k1.compressPublicKey(x, y);
bytes32 digest = 0x6b428e37b951e4f3858fde3c4f025278b0f2e1b8abf34fa295d26be1aeebf6e3;
bytes memory signature =
hex"ab1fc81491c244466777cc3edfbb5925cb2586c2f8d061edc09022cfc3f9ff6d06e8105a7228b964dd3a22349c9f853994db5ce47f76867ba43a641b7530559c1b";
uint256 deposit = staking.MinDeposit();
vm.deal(validator, deposit);

bytes32 validatorPubkeyDigest = staking.getValidatorPubkeyDigest(x, y);
assertEq(validatorPubkeyDigest, digest, "validator pubkey digest");

vm.expectEmit();
emit Staking.CreateValidator(validator, pubkey, deposit);
vm.prank(validator);
staking.createValidator{ value: deposit }(x, y, signature);
}

function _testDelegate() internal {
uint256 deposit = staking.MinDelegation();
vm.deal(validator, deposit);

vm.expectEmit();
emit Staking.Delegate(validator, validator, deposit);
vm.prank(validator);
staking.delegate{ value: deposit }(validator);
}
}
9 changes: 7 additions & 2 deletions e2e/app/admin/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,14 @@ func ugpradeSlashing(ctx context.Context, s shared, c chain) error {
return nil
}

var stakingABI = mustGetABI(bindings.StakingMetaData)

func upgradeStaking(ctx context.Context, s shared, c chain) error {
// TODO: replace if re-initialization is required
initializer := []byte{}
// Staking.sol's initializeV2() is called after upgrade
initializer, err := stakingABI.Pack("initializeV2")
if err != nil {
return errors.Wrap(err, "pack initializer")
}

calldata, err := adminABI.Pack("upgradeStaking", s.upgrader, s.deployer, initializer)
if err != nil {
Expand Down
Loading