From bd7ac16f72defae7c65ca6413af1bc7022053385 Mon Sep 17 00:00:00 2001 From: nikashib Date: Fri, 29 Nov 2024 14:27:28 +0530 Subject: [PATCH] added mtpfix polygon v2.2.0 #19 --- contracts/common/lib/MerklePatriciaProof.sol | 19 +++---- contracts/root/RootChain.sol | 6 +-- .../root/withdrawManager/WithdrawManager.sol | 3 -- .../staking/slashing/SlashingManager.sol | 5 +- .../staking/stakeManager/StakeManager.sol | 19 ------- .../staking/validatorShare/ValidatorShare.sol | 6 +-- scripts/deployFix.js | 52 ++++++++++++++++--- 7 files changed, 57 insertions(+), 53 deletions(-) diff --git a/contracts/common/lib/MerklePatriciaProof.sol b/contracts/common/lib/MerklePatriciaProof.sol index a891111a..73390813 100644 --- a/contracts/common/lib/MerklePatriciaProof.sol +++ b/contracts/common/lib/MerklePatriciaProof.sol @@ -1,9 +1,3 @@ -/* - * @title MerklePatriciaVerifier - * @author Sam Mayo (sammayo888@gmail.com) - * - * @dev Library for verifing merkle patricia proofs. - */ pragma solidity ^0.5.2; import {RLPReader} from "./RLPReader.sol"; @@ -69,30 +63,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; } diff --git a/contracts/root/RootChain.sol b/contracts/root/RootChain.sol index eefc8d7d..40df295a 100644 --- a/contracts/root/RootChain.sol +++ b/contracts/root/RootChain.sol @@ -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"; @@ -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"); @@ -120,4 +118,4 @@ contract RootChain is RootChainStorage, IRootChain { function setHeimdallId(string memory _heimdallId) public onlyOwner { heimdallId = keccak256(abi.encodePacked(_heimdallId)); } -} +} \ No newline at end of file diff --git a/contracts/root/withdrawManager/WithdrawManager.sol b/contracts/root/withdrawManager/WithdrawManager.sol index 6e5b24ad..dfbfce8a 100644 --- a/contracts/root/withdrawManager/WithdrawManager.sol +++ b/contracts/root/withdrawManager/WithdrawManager.sol @@ -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"; @@ -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; diff --git a/contracts/staking/slashing/SlashingManager.sol b/contracts/staking/slashing/SlashingManager.sol index a76428ca..fece5535 100644 --- a/contracts/staking/slashing/SlashingManager.sol +++ b/contracts/staking/slashing/SlashingManager.sol @@ -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"; @@ -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); @@ -138,4 +135,4 @@ contract SlashingManager is ISlashingManager, Ownable { ) external onlyOwner { require(IERC20(token).transfer(destination, value), "Transfer failed"); } -} +} \ No newline at end of file diff --git a/contracts/staking/stakeManager/StakeManager.sol b/contracts/staking/stakeManager/StakeManager.sol index 3899727e..c349f17f 100644 --- a/contracts/staking/stakeManager/StakeManager.sol +++ b/contracts/staking/stakeManager/StakeManager.sol @@ -3,8 +3,6 @@ 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"; @@ -31,8 +29,6 @@ contract StakeManager is { using SafeMath for uint256; using Merkle for bytes32; - using RLPReader for bytes; - using RLPReader for RLPReader.RLPItem; struct UnsignedValidatorsContext { @@ -298,21 +294,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); - } - function reinitialize( address _NFTContract, address _stakingLogger, diff --git a/contracts/staking/validatorShare/ValidatorShare.sol b/contracts/staking/validatorShare/ValidatorShare.sol index 8813edda..b7ac583e 100644 --- a/contracts/staking/validatorShare/ValidatorShare.sol +++ b/contracts/staking/validatorShare/ValidatorShare.sol @@ -224,11 +224,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."); } /** diff --git a/scripts/deployFix.js b/scripts/deployFix.js index c4daf218..393a3fc9 100644 --- a/scripts/deployFix.js +++ b/scripts/deployFix.js @@ -5,9 +5,10 @@ async function main() { // Contract addresses const REGISTRY = "0xC4d2Cf0423c326BC9d97A914B7FDf503805C37c7"; //puppynet const GOVERNANCE = "0x1FFEdE2984dd324C0E63EdFfc44d5b6795826bfC"; //puppynet - const DEPOSIT_MANAGER_PROXY = "0x78FB39bd541BD09c5e1D47b2bBB28A7279c9d196"; //puppynet + const DEPOSIT_MANAGER_PROXY = "0x78FB39bd541BD09c5e1D47b2BBB28A7279c9d196"; //puppynet const WITHDRAW_MANAGER_PROXY = "0x5475F5823168bAf8eBe0bC7A195ab0d7BebCAAC5"; //puppynet - + const STAKE_MANAGER_PROXY = "0xC0568572887E9687D7b57c1fC83332F8d1d38A6a"; // puppynet + console.log("Starting deployments..."); // Get the deployer @@ -20,7 +21,19 @@ async function main() { await withdrawManager.deployed(); console.log("Deployed WithdrawManager implementation at:", withdrawManager.address); - // STEP 2: Deploy new ERC20PredicateBurnOnly version + // STEP 2: Deploy new StakeManager version + const StakeManager = await ethers.getContractFactory("StakeManager"); + const stakeManager = await StakeManager.deploy(); + await stakeManager.deployed(); + console.log("Deployed StakeManager implementation at:", stakeManager.address); + + // STEP 3: Deploy new ValidatorShare version + const ValidatorShare = await ethers.getContractFactory("ValidatorShare"); + const validatorShare = await ValidatorShare.deploy(); + await validatorShare.deployed(); + console.log("Deployed ValidatorShare implementation at:", validatorShare.address); + + // STEP 4: Deploy new ERC20PredicateBurnOnly version const ERC20PredicateBurnOnly = await ethers.getContractFactory("ERC20PredicateBurnOnly"); const erc20PredicateBurnOnly = await ERC20PredicateBurnOnly.deploy( WITHDRAW_MANAGER_PROXY, @@ -29,7 +42,7 @@ async function main() { await erc20PredicateBurnOnly.deployed(); console.log("Deployed ERC20PredicateBurnOnly implementation at:", erc20PredicateBurnOnly.address); - // STEP 3: Deploy new ERC721PredicateBurnOnly version + // STEP 5: Deploy new ERC721PredicateBurnOnly version const ERC721PredicateBurnOnly = await ethers.getContractFactory("ERC721PredicateBurnOnly"); const erc721PredicateBurnOnly = await ERC721PredicateBurnOnly.deploy( WITHDRAW_MANAGER_PROXY, @@ -44,8 +57,33 @@ async function main() { const governance = await ethers.getContractAt("Governance", GOVERNANCE); const registry = await ethers.getContractAt("Registry", REGISTRY); const withdrawManagerProxy = await ethers.getContractAt("WithdrawManagerProxy", WITHDRAW_MANAGER_PROXY); + const stakeManagerProxy = await ethers.getContractAt("StakeManagerProxy", STAKE_MANAGER_PROXY); + + // STEP 1: Update ValidatorShare registry entry + const updateValidatorSharePayload = await governance.interface.encodeFunctionData( + "update", + [ + REGISTRY, + registry.interface.encodeFunctionData( + "updateContractMap", + [ethers.utils.keccak256(ethers.utils.toUtf8Bytes("validatorShare")), validatorShare.address] + ) + ] + ); + console.log("\nUpdate ValidatorShare Registry Payload:"); + console.log("Send to:", GOVERNANCE); + console.log("Data:", updateValidatorSharePayload); + + // STEP 2: Update StakeManager implementation + const updateStakeManagerPayload = await stakeManagerProxy.interface.encodeFunctionData( + "updateImplementation", + [stakeManager.address] + ); + console.log("\nUpdate StakeManager Implementation Payload:"); + console.log("Send to:", STAKE_MANAGER_PROXY); + console.log("Data:", updateStakeManagerPayload); - // STEP 1: Remove predicates payloads + // STEP 3: Remove old predicates payloads const removePredicatePayload1 = await governance.interface.encodeFunctionData( "update", [ @@ -74,7 +112,7 @@ async function main() { console.log("Send to:", GOVERNANCE); console.log("Data:", removePredicatePayload2); - // STEP 2: Add predicates payloads + // STEP 4: Add new predicates payloads const addErc20PredicatePayload = await governance.interface.encodeFunctionData( "update", [ @@ -103,7 +141,7 @@ async function main() { console.log("Send to:", GOVERNANCE); console.log("Data:", addErc721PredicatePayload); - // STEP 3: Update WithdrawManagerProxy implementation payload + // STEP 5: Update WithdrawManager implementation const updateWithdrawManagerPayload = await withdrawManagerProxy.interface.encodeFunctionData( "updateImplementation", [withdrawManager.address]