Skip to content

Commit

Permalink
Remove system address from final genesis state
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Oct 30, 2024
1 parent 6b8255a commit 2e57261
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
8 changes: 3 additions & 5 deletions deployment/v2/4_createRollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import fs = require("fs");
import * as dotenv from "dotenv";
dotenv.config({path: path.resolve(__dirname, "../../.env")});
import {ethers, upgrades} from "hardhat";
import {HardhatEthersSigner} from "@nomicfoundation/hardhat-ethers/signers";
const {create2Deployment} = require("../helpers/deployment-helpers");

const pathGenesis = path.join(__dirname, "./genesis.json");
const pathGenesisSovereign = path.join(__dirname, "./genesis_sovereign.json");
import {processorUtils, Constants} from "@0xpolygonhermez/zkevm-commonjs";

const createRollupParameters = require("./create_rollup_parameters.json");
let genesis = require("./genesis.json");
let genesis = require(pathGenesis);
const deployOutput = require("./deploy_output.json");
import "../helpers/utils";
import updateVanillaGenesis from "./utils/updateVanillaGenesis";
Expand Down Expand Up @@ -375,7 +374,6 @@ async function main() {
rollupID: rollupID,
gasTokenAddress,
gasTokenNetwork,
globalExitRootManager: Constants.ADDRESS_GLOBAL_EXIT_ROOT_MANAGER_L2,
polygonRollupManager: ethers.ZeroAddress,
gasTokenMetadata,
bridgeManager: sovereignParams.bridgeManager,
Expand Down Expand Up @@ -463,7 +461,7 @@ async function main() {

// Rewrite updated genesis in case of vanilla client
if (isVanillaClient) {
fs.writeFileSync(pathGenesis, JSON.stringify(genesis, null, 1));
fs.writeFileSync(pathGenesisSovereign, JSON.stringify(genesis, null, 1));
}
fs.writeFileSync(pathOutputJson, JSON.stringify(outputJson, null, 1));
}
Expand Down
8 changes: 6 additions & 2 deletions deployment/v2/utils/deployment-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const padTo32Bytes = (value) => {
const hexValue = value.startsWith("0x") ? value.slice(2) : value; // Remove '0x'
return "0x" + hexValue.padStart(64, "0"); // Pad to 64 hex digits
};

const padTo20Bytes = (value) => {
const hexValue = value.startsWith("0x") ? value.slice(2) : value; // Remove '0x'
return "0x" + hexValue.padStart(40, "0"); // Pad to 40 hex digits
};
export {
padTo32Bytes
padTo32Bytes,
padTo20Bytes
}
36 changes: 25 additions & 11 deletions deployment/v2/utils/updateVanillaGenesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {ethers} from "hardhat";
const {getContractAddress} = require("@ethersproject/address");
const bridgeContractName = "BridgeL2SovereignChain";
import {expect} from "chai";
import {padTo32Bytes} from "./deployment-utils";
import {padTo32Bytes, padTo20Bytes} from "./deployment-utils";

async function updateVanillaGenesis(genesis, chainID, initializeParams) {
// Load genesis on a zkEVMDB
const poseidon = await getPoseidon();
Expand Down Expand Up @@ -31,8 +32,8 @@ async function updateVanillaGenesis(genesis, chainID, initializeParams) {
type: 11,
deltaTimestamp: 3,
l1Info: {
globalExitRoot: "0x090bcaf734c4f06c93954a827b45a6e8c67b8e0fd1e0a35a1c5982d6961828f9",
blockHash: "0x24a5871d68723340d9eadc674aa8ad75f3e33b61d5a9db7db92af856a19270bb",
globalExitRoot: ethers.ZeroAddress, // Can be any value
blockHash: "0x24a5871d68723340d9eadc674aa8ad75f3e33b61d5a9db7db92af856a19270bb", // Can be any value
timestamp: "42",
},
indexL1InfoTree: 0,
Expand Down Expand Up @@ -112,12 +113,14 @@ async function updateVanillaGenesis(genesis, chainID, initializeParams) {
);
// Add changeL2Block tx
batch2.addRawTx(`0x${rawChangeL2BlockTx}`);
const gerProxy = genesis.genesis.find(function (obj) {
return obj.contractName == "PolygonZkEVMGlobalExitRootL2 proxy";
});
// Initialize bridge
const {
rollupID,
gasTokenAddress,
gasTokenNetwork,
globalExitRootManager,
polygonRollupManager,
gasTokenMetadata,
bridgeManager,
Expand All @@ -131,7 +134,7 @@ async function updateVanillaGenesis(genesis, chainID, initializeParams) {
rollupID,
gasTokenAddress,
gasTokenNetwork,
globalExitRootManager,
gerProxy.address, // Global exit root manager address from base genesis
polygonRollupManager,
gasTokenMetadata,
bridgeManager,
Expand All @@ -152,9 +155,6 @@ async function updateVanillaGenesis(genesis, chainID, initializeParams) {
batch2.addRawTx(txInitializeBridge);

// Initialize GER Manager
const gerProxy = genesis.genesis.find(function (obj) {
return obj.contractName == "PolygonZkEVMGlobalExitRootL2 proxy";
});
const initializeGERData = gerFactory.interface.encodeFunctionData("initialize", [globalExitRootUpdater]);
// Update injectedTx to initialize GER
injectedTx.to = gerProxy.address;
Expand All @@ -175,7 +175,9 @@ async function updateVanillaGenesis(genesis, chainID, initializeParams) {
bridgeProxy.storage = await zkEVMDB2.dumpStorage(bridgeProxy.address);
// If bridge initialized with a zero sovereign weth address and a non zero gas token, we should add created erc20 weth contract to the genesis
if (gasTokenAddress !== ethers.ZeroAddress && sovereignWETHAddress === ethers.ZeroAddress) {
const wethAddress = bridgeProxy.storage["0x000000000000000000000000000000000000000000000000000000000000006f"];
const wethAddress = padTo20Bytes(
bridgeProxy.storage["0x000000000000000000000000000000000000000000000000000000000000006f"]
);
const wethGenesis = {
contractName: "WETH",
balance: "0",
Expand Down Expand Up @@ -225,7 +227,7 @@ async function updateVanillaGenesis(genesis, chainID, initializeParams) {
);
}
expect(bridgeProxy.storage["0x0000000000000000000000000000000000000000000000000000000000000068"]).to.include(
globalExitRootManager.toLowerCase().slice(2)
gerProxy.address.toLowerCase().slice(2)
);
expect(bridgeProxy.storage["0x00000000000000000000000000000000000000000000000000000000000000a3"]).to.include(
bridgeManager.toLowerCase().slice(2)
Expand All @@ -246,8 +248,20 @@ async function updateVanillaGenesis(genesis, chainID, initializeParams) {
expect(gerProxy.storage["0x0000000000000000000000000000000000000000000000000000000000000034"]).to.include(
globalExitRootUpdater.toLowerCase().slice(2)
);
// Create a new zkEVM to generate a genesis an empty system address storage
const zkEVMDB3 = await ZkEVMDB.newZkEVM(
new MemDB(F),
poseidon,
genesisRoot,
accHashInput,
genesis.genesis,
null,
null,
chainID
);
// update genesis root
genesis.root = smtUtils.h4toString(zkEVMDB2.getCurrentStateRoot());
genesis.root = smtUtils.h4toString(zkEVMDB3.getCurrentStateRoot());

return genesis;
}

Expand Down

0 comments on commit 2e57261

Please sign in to comment.