Skip to content

Commit

Permalink
feat: add fork test
Browse files Browse the repository at this point in the history
  • Loading branch information
ethyla committed Nov 9, 2024
1 parent 47e8738 commit 643def4
Show file tree
Hide file tree
Showing 7 changed files with 5,713 additions and 115 deletions.
5 changes: 4 additions & 1 deletion contracts/common/lib/MerklePatriciaProof.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ library MerklePatriciaProof {
);
pathPtr += 1;
} else if (currentNodeList.length == 2) {
// Alternative 1 start
bytes memory nodeValue = RLPReader.toBytes(currentNodeList[0]);
uint256 traversed = _nibblesToTraverse(
nodeValue,
Expand Down Expand Up @@ -96,8 +97,9 @@ library MerklePatriciaProof {

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

// // Alternative:
// // Alternative 2 start
// bytes memory nodeValue = RLPReader.toBytes(currentNodeList[0]);
// bytes1 prefix = _getNthNibbleOfBytes(0, nodeValue);
// // Extension node
Expand All @@ -118,6 +120,7 @@ library MerklePatriciaProof {
// } else {
// return false;
// }
// // Alternative 2 end
} else {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ optimizer_runs = 200
via_ir = true
cache_path = 'forge-cache'
verbosity = 2
memory_limit = 40003554432
gas_limit = "18446744073709551615"

ffi = true
remappings = [
Expand Down
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.

98 changes: 98 additions & 0 deletions test/foundry/ForkupgradeMPT.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
pragma solidity ^0.8.4;

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

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

import "forge-std/Test.sol";

struct TxObject {
uint256 block_number;
address from;
bytes input_proof;
bytes modified_input_proof;
bytes32 timestamp;
address to;
bytes32 transaction_hash;
uint256 value;
}

struct FileObject {
TxObject[] txObjects;
}

contract ForkupgradeMPT is Test {
using stdJson for string;

uint256 mainnetFork;

function setUp() public {
mainnetFork = vm.createFork(vm.rpcUrl("mainnet"), 20900000);
vm.selectFork(mainnetFork);
}

function test_UpgradeMPT() public {
assertEq(vm.activeFork(), mainnetFork);

string memory input = vm.readFile("scripts/deployers/mpt-fix/input.json");
string memory chainIdSlug = string(abi.encodePacked('["', vm.toString(block.chainid), '"]'));
WithdrawManagerProxy withdrawManagerProxy = WithdrawManagerProxy(payable(input.readAddress(string.concat(chainIdSlug, ".withdrawManagerProxy"))));
Timelock timelock = Timelock(payable(input.readAddress(string.concat(chainIdSlug, ".timelock"))));
address exitNFT = input.readAddress(string.concat(chainIdSlug, ".exitNFT"));

address withdrawManagerImpl = deployCode("out/WithdrawManager.sol/WithdrawManager.json");

vm.prank(address(timelock));
withdrawManagerProxy.updateImplementation(withdrawManagerImpl);
WithdrawManager withdrawManager = WithdrawManager(payable(withdrawManagerProxy));

// load tx to be replayed
string memory txsJson = vm.readFile("test/foundry/batch1withdrawNew.json");
bytes memory txs = vm.parseJson(txsJson);
FileObject memory txBatch1 = abi.decode(txs, (FileObject));
uint256 successes = 0;
// loop
for (uint i = 0; i < txBatch1.txObjects.length; i++) {
console.log("tx num: ", i);
TxObject memory obj = txBatch1.txObjects[i];
console.log(obj.from);

// Get the index of the exit
uint256 index = withdrawManager.verifyInclusion(obj.modified_input_proof, 0, false);

// Calculate the storage slots we need to manipulate to replay the tx
bytes32 slotisKnownExit = keccak256(abi.encode(uint128(index << 1), 5));
bytes32 slotexits = bytes32(uint256(keccak256(abi.encode(index << 1, 6)))+ 3);
bytes32 slot_tokenOwner = keccak256(abi.encode((index << 1), 1));
// WithdrawManager
vm.store(address(withdrawManager), slotisKnownExit, 0);
vm.store(address(withdrawManager), slotexits, 0);
// ExitNFT
vm.store(exitNFT, slot_tokenOwner, 0);


// Expect the exit correctly happens, this is the last thing that happens in an orderly exit
vm.expectEmit(true, false, false, false, address(withdrawManager));
emit WithdrawManager.ExitStarted(obj.from, index, address(0), 0, true);
// Pretend to be the the exitor and replay tx
vm.prank(obj.from);
(bool successSchedule, bytes memory dataSchedule) = address(0x626fb210bf50e201ED62cA2705c16DE2a53DC966).call(obj.input_proof);
if (successSchedule == false) {
assembly {
revert(add(dataSchedule, 32), mload(dataSchedule))
}
} else {
successes += 1;
console.log("success");
}
}
console.log("Total successful tx: ", successes);
}
}
88 changes: 0 additions & 88 deletions test/foundry/ForkupgradeStakeManager.t.sol

This file was deleted.

Loading

0 comments on commit 643def4

Please sign in to comment.