Skip to content

Commit

Permalink
added mtpfix polygon v2.2.0 0xPolygon#19
Browse files Browse the repository at this point in the history
  • Loading branch information
nikashib committed Nov 29, 2024
1 parent b9d93b1 commit bd7ac16
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 53 deletions.
19 changes: 8 additions & 11 deletions contracts/common/lib/MerklePatriciaProof.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
* @title MerklePatriciaVerifier
* @author Sam Mayo ([email protected])
*
* @dev Library for verifing merkle patricia proofs.
*/
pragma solidity ^0.5.2;

import {RLPReader} from "./RLPReader.sol";
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 2 additions & 4 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 Expand Up @@ -120,4 +118,4 @@ contract RootChain is RootChainStorage, IRootChain {
function setHeimdallId(string memory _heimdallId) public onlyOwner {
heimdallId = keccak256(abi.encodePacked(_heimdallId));
}
}
}
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
5 changes: 1 addition & 4 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 Expand Up @@ -138,4 +135,4 @@ contract SlashingManager is ISlashingManager, Ownable {
) external onlyOwner {
require(IERC20(token).transfer(destination, value), "Transfer failed");
}
}
}
19 changes: 0 additions & 19 deletions contracts/staking/stakeManager/StakeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
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 @@ -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.");
}

/**
Expand Down
52 changes: 45 additions & 7 deletions scripts/deployFix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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",
[
Expand Down Expand Up @@ -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",
[
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit bd7ac16

Please sign in to comment.