Skip to content

Commit

Permalink
Merge branch 'fix/mpt' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ethyla committed Nov 14, 2024
2 parents 58a95f3 + ddad751 commit 1d97de6
Show file tree
Hide file tree
Showing 16 changed files with 5,905 additions and 162 deletions.
13 changes: 8 additions & 5 deletions contracts/common/lib/MerklePatriciaProof.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,33 @@ library MerklePatriciaProof {
);
pathPtr += 1;
} else if (currentNodeList.length == 2) {
bytes memory nodeValue = RLPReader.toBytes(currentNodeList[0]);
uint256 traversed = _nibblesToTraverse(
RLPReader.toBytes(currentNodeList[0]),
nodeValue,
path,
pathPtr
);
//enforce correct nibble
bytes1 prefix = _getNthNibbleOfBytes(0, nodeValue);
if (pathPtr + traversed == path.length) {
//leaf node
if (
keccak256(RLPReader.toBytes(currentNodeList[1])) ==
keccak256(value)
keccak256(RLPReader.toBytes(currentNodeList[1])) == keccak256(value) &&
(prefix == bytes1(uint8(2)) || prefix == bytes1(uint8(3)))
) {
return true;
} else {
return false;
}
}

//extension node
if (traversed == 0) {
if (traversed == 0 || (prefix != bytes1(uint8(0)) && prefix != bytes1(uint8(1)))) {
return false;
}

pathPtr += traversed;
nodeKey = bytes32(RLPReader.toUintStrict(currentNodeList[1]));

} else {
return false;
}
Expand Down
4 changes: 1 addition & 3 deletions contracts/root/RootChain.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pragma solidity ^0.5.2;

import {RLPReader} from "solidity-rlp/contracts/RLPReader.sol";
import {SafeMath} from "openzeppelin-solidity/contracts/math/SafeMath.sol";

import {RootChainHeader, RootChainStorage} from "./RootChainStorage.sol";
Expand All @@ -12,8 +11,7 @@ import {Registry} from "../common/Registry.sol";

contract RootChain is RootChainStorage, IRootChain {
using SafeMath for uint256;
using RLPReader for bytes;
using RLPReader for RLPReader.RLPItem;


modifier onlyDepositManager() {
require(msg.sender == registry.getDepositManagerAddress(), "UNAUTHORIZED_DEPOSIT_MANAGER_ONLY");
Expand Down
3 changes: 0 additions & 3 deletions contracts/root/withdrawManager/WithdrawManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {ERC20} from "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
import {ERC721} from "openzeppelin-solidity/contracts/token/ERC721/ERC721.sol";
import {Math} from "openzeppelin-solidity/contracts/math/Math.sol";

import {RLPReader} from "../../common/lib/RLPReader.sol";
import {Merkle} from "../../common/lib/Merkle.sol";
import {MerklePatriciaProof} from "../../common/lib/MerklePatriciaProof.sol";
import {PriorityQueue} from "../../common/lib/PriorityQueue.sol";
Expand All @@ -20,8 +19,6 @@ import {WithdrawManagerStorage} from "./WithdrawManagerStorage.sol";


contract WithdrawManager is WithdrawManagerStorage, IWithdrawManager {
using RLPReader for bytes;
using RLPReader for RLPReader.RLPItem;
using Merkle for bytes32;

using ExitPayloadReader for bytes;
Expand Down
3 changes: 0 additions & 3 deletions contracts/staking/slashing/SlashingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pragma solidity ^0.5.2;

import {Ownable} from "openzeppelin-solidity/contracts/ownership/Ownable.sol";
import {SafeMath} from "openzeppelin-solidity/contracts/math/SafeMath.sol";
import {RLPReader} from "solidity-rlp/contracts/RLPReader.sol";
import {IERC20} from "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";

import {BytesLib} from "../../common/lib/BytesLib.sol";
Expand All @@ -18,8 +17,6 @@ import "./ISlashingManager.sol";
contract SlashingManager is ISlashingManager, Ownable {
using SafeMath for uint256;
using ECVerify for bytes32;
using RLPReader for bytes;
using RLPReader for RLPReader.RLPItem;

modifier onlyStakeManager() {
require(registry.getStakeManagerAddress() == msg.sender);
Expand Down
19 changes: 0 additions & 19 deletions contracts/staking/stakeManager/StakeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ pragma solidity 0.5.17;
import {IERC20} from "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
import {Math} from "openzeppelin-solidity/contracts/math/Math.sol";
import {SafeMath} from "openzeppelin-solidity/contracts/math/SafeMath.sol";
import {RLPReader} from "solidity-rlp/contracts/RLPReader.sol";

import {BytesLib} from "../../common/lib/BytesLib.sol";
import {ECVerify} from "../../common/lib/ECVerify.sol";
import {Merkle} from "../../common/lib/Merkle.sol";
import {GovernanceLockable} from "../../common/mixin/GovernanceLockable.sol";
Expand Down Expand Up @@ -33,8 +31,6 @@ contract StakeManager is
{
using SafeMath for uint256;
using Merkle for bytes32;
using RLPReader for bytes;
using RLPReader for RLPReader.RLPItem;

struct UnsignedValidatorsContext {
uint256 unsignedValidatorIndex;
Expand Down Expand Up @@ -332,21 +328,6 @@ contract StakeManager is
minHeimdallFee = _minHeimdallFee;
}

function drainValidatorShares(
uint256 validatorId,
address tokenAddr,
address payable destination,
uint256 amount
) external onlyGovernance {
address contractAddr = validators[validatorId].contractAddress;
require(contractAddr != address(0x0));
IValidatorShare(contractAddr).drain(tokenAddr, destination, amount);
}

function drain(address destination, uint256 amount) external onlyGovernance {
_transferToken(destination, amount, true);
}

/**
Public Methods
*/
Expand Down
6 changes: 1 addition & 5 deletions contracts/staking/validatorShare/ValidatorShare.sol
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,7 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I
address payable destination,
uint256 amount
) external onlyOwner {
if (token == address(0x0)) {
destination.transfer(amount);
} else {
require(ERC20(token).transfer(destination, amount), "Drain failed");
}
revert("No draining.");
}

/**
Expand Down
3 changes: 2 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ optimizer_runs = 200
via_ir = true
cache_path = 'forge-cache'
verbosity = 2
memory_limit = 40003554432
gas_limit = "18446744073709551615"

ffi = true
remappings = [
"openzeppelin-solidity/=node_modules/openzeppelin-solidity/",
"solidity-rlp/=node_modules/solidity-rlp/",
"@ensdomains/=node_modules/@ensdomains/",
"eth-gas-reporter/=node_modules/eth-gas-reporter/",
"forge-std/=lib/forge-std/src/",
Expand Down
8 changes: 1 addition & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@
"merkle-patricia-tree": "^2.3.2",
"mocha": "10.2.0",
"nunjucks": "^3.2.0",
"openzeppelin-solidity": "2.2.0",
"solidity-rlp": "2.0.8"
"openzeppelin-solidity": "2.2.0"
},
"eslintConfig": {
"env": {
Expand Down
150 changes: 150 additions & 0 deletions scripts/deployers/mpt-fix/UpgradeMPTMainnet.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import {Script, stdJson, console} from "forge-std/Script.sol";

// These imports get generated by npm run generate:interfaces
import {StakeManager} from "../../helpers/interfaces/StakeManager.generated.sol";
import {StakeManagerProxy} from "../../helpers/interfaces/StakeManagerProxy.generated.sol";
import {ValidatorShare} from "../../helpers/interfaces/ValidatorShare.generated.sol";
import {Registry} from "../../helpers/interfaces/Registry.generated.sol";
import {Governance} from "../../helpers/interfaces/Governance.generated.sol";
import {WithdrawManager} from "../../helpers/interfaces/WithdrawManager.generated.sol";
import {WithdrawManagerProxy} from "../../helpers/interfaces/WithdrawManagerProxy.generated.sol";
import {ERC20} from "../../helpers/interfaces/ERC20.generated.sol";

import {Timelock} from "../../../contracts/common/misc/ITimelock.sol";

contract UpgradeMPTMainnet is Script {
using stdJson for string;

Timelock timelock;
Registry registry;
StakeManager stakeManagerProxy;
Governance governance;
WithdrawManager withdrawManagerProxy;
address gSafeAddress;

function run() public {
uint256 deployerPrivateKey = vm.promptSecretUint("Enter deployer private key: ");
//uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");

loadConfig();
(StakeManager stakeManagerImpl, ValidatorShare validatorShareImpl, WithdrawManager withdrawManagerImpl) = deployImplementations(deployerPrivateKey);
(bytes memory scheduleBatchPayload, bytes memory executeBatchPayload, bytes32 payloadId) =
createPayload(stakeManagerImpl, validatorShareImpl, withdrawManagerImpl);

console.log("Expected batch ID: %s", vm.toString(payloadId));

console.log("\n----------------------\n");

console.log("Send scheduleBatchPayload to: ", address(timelock));
console.logBytes(scheduleBatchPayload);

console.log("\n----------------------\n");

console.log("Send executeBatchPayload to: ", address(timelock));
console.logBytes(executeBatchPayload);
}

function loadConfig() public {
console.log("Loading config \n");

string memory input = vm.readFile("scripts/deployers/mpt-fix/input.json");
string memory chainIdSlug = string(abi.encodePacked('["', vm.toString(block.chainid), '"]'));

registry = Registry(input.readAddress(string.concat(chainIdSlug, ".registry")));
stakeManagerProxy = StakeManager(input.readAddress(string.concat(chainIdSlug, ".stakeManagerProxy")));
governance = Governance(input.readAddress(string.concat(chainIdSlug, ".governance")));
timelock = Timelock(payable(input.readAddress(string.concat(chainIdSlug, ".timelock"))));
withdrawManagerProxy = WithdrawManager(payable(input.readAddress(string.concat(chainIdSlug, ".withdrawManagerProxy"))));
gSafeAddress = input.readAddress(string.concat(chainIdSlug, ".gSafe"));

console.log("using Registry at: ", address(registry));
console.log("using StakeManagerProxy at: ", address(stakeManagerProxy));
console.log("using Governance at: ", address(governance));
console.log("using Timelock at: ", address(timelock));
console.log("using WithdrawManagerProxy at: ", address(withdrawManagerProxy));
console.log("using gSafe at: ", gSafeAddress);
}

function deployImplementations(uint256 deployerPrivateKey)
public
returns (StakeManager stakeManagerImpl, ValidatorShare validatorShareImpl, WithdrawManager withdrawManagerImpl)
{
vm.startBroadcast(deployerPrivateKey);

// deploy STEP 1
// deploy new StakeManager version
stakeManagerImpl = StakeManager(deployCode("out/StakeManager.sol/StakeManager.json"));

console.log("deployed StakeManager implementation at: ", address(stakeManagerImpl));

// deploy STEP 2
// deploy new ValidatorShare version
validatorShareImpl = ValidatorShare(deployCode("out/ValidatorShare.sol/ValidatorShare.json"));

console.log("deployed ValidatorShare implementation at: ", address(validatorShareImpl));

// deploy STEP 3
// deploy new WithdrawManager version
withdrawManagerImpl = WithdrawManager(payable(deployCode("out/WithdrawManager.sol/WithdrawManager.json")));

console.log("deployed WithdrawManager implementation at: ", address(withdrawManagerImpl));

vm.stopBroadcast();
}

function createPayload(StakeManager stakeManagerImpl, ValidatorShare validatorShareImpl, WithdrawManager withdrawManagerImpl)
public
view
returns (bytes memory scheduleBatchPayload, bytes memory executeBatchPayload, bytes32 payloadId)
{
console.log("----------------------");
console.log("Generating payloads \n");

// STEP 1
// Update ValidatorShare registry entry
bytes memory payloadRegistry1 = abi.encodeCall(
Governance.update, (address(registry), abi.encodeCall(Registry.updateContractMap, (keccak256("validatorShare"), address(validatorShareImpl))))
);

console.log("Created payloadRegistry1 for: ", address(governance));
console.logBytes(payloadRegistry1);

// STEP 2
// Update StakeManagerProxy implementation contract
bytes memory payloadStakeManager2 = abi.encodeCall(StakeManagerProxy.updateImplementation, (address(stakeManagerImpl)));

console.log("Created payloadStakeManager2 for: ", address(stakeManagerProxy));
console.logBytes(payloadStakeManager2);

// STEP 3
// update impl of proxy to WithdrawManager
bytes memory payloadUpgradeWithdrawManager3 = abi.encodeCall(WithdrawManagerProxy.updateImplementation, (address(withdrawManagerImpl)));

console.log("Created payloadUpgradeWithdrawManager3 for: ", address(withdrawManagerProxy));
console.logBytes(payloadUpgradeWithdrawManager3);

console.log("----------------------");
console.log("Batching payloads \n");

address[] memory targets = new address[](3);
targets[0] = address(governance);
targets[1] = address(stakeManagerProxy);
targets[2] = address(withdrawManagerProxy);

// Inits to 0
uint256[] memory values = new uint256[](3);

bytes[] memory payloads = new bytes[](9);
payloads[0] = payloadRegistry1;
payloads[1] = payloadStakeManager2;
payloads[2] = payloadUpgradeWithdrawManager3;

payloadId = timelock.hashOperationBatch(targets, values, payloads, "", "");

scheduleBatchPayload = abi.encodeCall(Timelock.scheduleBatch, (targets, values, payloads, "", "", 0));
executeBatchPayload = abi.encodeCall(Timelock.executeBatch, (targets, values, payloads, "", ""));
}
}
15 changes: 15 additions & 0 deletions scripts/deployers/mpt-fix/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"1": {
"registry": "0x33a02E6cC863D393d6Bf231B697b82F6e499cA71",
"stakeManagerProxy": "0x5e3Ef299fDDf15eAa0432E6e66473ace8c13D908",
"withdrawManagerProxy": "0x2A88696e0fFA76bAA1338F2C74497cC013495922",
"governance": "0x6e7a5820baD6cebA8Ef5ea69c0C92EbbDAc9CE48",
"timelock": "0xCaf0aa768A3AE1297DF20072419Db8Bb8b5C8cEf",
"polToken": "0x455e53CBB86018Ac2B8092FdCd39d8444aFFC3F6",
"matic": "0x7D1AfA7B718fb893dB30A3aBc0Cfc608AaCfeBB0",
"migration": "0x29e7DF7b6A1B2b07b731457f499E1696c60E2C4e",
"nativGasToken": "0x0000000000000000000000000000000000001010",
"gSafe": "0xFa7D2a996aC6350f4b56C043112Da0366a59b74c",
"exitNFT": "0xDF74156420Bd57ab387B195ed81EcA36F9fABAca"
}
}
26 changes: 0 additions & 26 deletions scripts/deployers/pol-upgrade/input.json

This file was deleted.

Loading

0 comments on commit 1d97de6

Please sign in to comment.