Skip to content

Commit

Permalink
Update bridge at create rollup for vanilla clients
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Oct 17, 2024
1 parent 97609ea commit 52e4397
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 64 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ typechain-types/
create_rollup_parameters.json

docker/deploymentOutput

.DS_Store
5 changes: 2 additions & 3 deletions deployment/v2/1_createGenesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ async function main() {

// Check if it's mainnet deployment
const isMainnet = deployParameters["isMainnet"] === true ? true : false;
let isSovereignChain = deployParameters["isSovereignChain"] === true ? true : false;

if (isMainnet === true) {
timelockAdminAddress = mainnetMultisig;
Expand Down Expand Up @@ -164,7 +163,7 @@ async function main() {
}

// Deploy implementation PolygonZkEVMBridge
const bridgeContractName = isSovereignChain ? "BridgeL2SovereignChain": "PolygonZkEVMBridgeV2";
const bridgeContractName = "PolygonZkEVMBridgeV2";
const polygonZkEVMBridgeFactory = await ethers.getContractFactory(bridgeContractName, deployer);
const deployTransactionBridge = (await polygonZkEVMBridgeFactory.getDeployTransaction()).data;
// Mandatory to override the gasLimit since the estimation with create are mess up D:
Expand Down Expand Up @@ -219,7 +218,7 @@ async function main() {
/*
*Deployment Global exit root manager
*/
const globalExitRootContractName = isSovereignChain ? "GlobalExitRootManagerL2SovereignChain": "PolygonZkEVMGlobalExitRootL2";
const globalExitRootContractName = "PolygonZkEVMGlobalExitRootL2";
const PolygonZkEVMGlobalExitRootL2Factory = await ethers.getContractFactory(
globalExitRootContractName,
deployer
Expand Down
150 changes: 90 additions & 60 deletions deployment/v2/4_createRollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ const pathGenesis = path.join(__dirname, "./genesis.json");
import {processorUtils, Constants} from "@0xpolygonhermez/zkevm-commonjs";

const createRollupParameters = require("./create_rollup_parameters.json");
const genesis = require("./genesis.json");
let genesis = require("./genesis.json");
const deployOutput = require("./deploy_output.json");
import "../helpers/utils";
import updateVanillaGenesis from "./utils/updateVanillaGenesis";

const pathOutputJson = path.join(__dirname, "./create_rollup_output.json");

Expand Down Expand Up @@ -65,6 +66,8 @@ async function main() {
adminZkEVM,
forkID,
consensusContract,
isVanillaClient,
sovereignParams,
} = createRollupParameters;

const supportedConsensus = ["PolygonZkEVMEtrog", "PolygonValidiumEtrog", "PolygonPessimisticConsensus"];
Expand Down Expand Up @@ -336,72 +339,99 @@ async function main() {
}
}

let batchData;
if (consensusContract === "PolygonPessimisticConsensus") {
// Add the first batch of the created rollup
const newZKEVMContract = (await PolygonconsensusFactory.attach(newZKEVMAddress)) as PolygonPessimisticConsensus;

// Get last GER
const lastGER = await globalExitRootManagerContract.getLastGlobalExitRoot();

const dataInjectedTx = await polygonZkEVMBridgeContract.interface.encodeFunctionData("initialize", [
rollupID,
let batchData = "";
// If is vanilla client, replace genesis by sovereign contracts, else, inject initialization batch
if (isVanillaClient) {
// Check sovereign params
const mandatorySovereignParams = ["bridgeManager", "sovereignWETHAddress", "sovereignWETHAddressIsNotMintable", "globalExitRootUpdater"];
for (const parameterName of mandatorySovereignParams) {
if (sovereignParams[parameterName] === undefined || sovereignParams[parameterName] === "") {
throw new Error(`Missing sovereign parameter: ${parameterName}`);
}
}
const initializeParams = {
rollupID: rollupID,
gasTokenAddress,
gasTokenNetwork,
Constants.ADDRESS_GLOBAL_EXIT_ROOT_MANAGER_L2, // Global exit root address on L2
ethers.ZeroAddress, // Rollup manager on L2 does not exist
gasTokenMetadata as any,
]);

// check maximum length is 65535
if ((dataInjectedTx.length - 2) / 2 > 0xffff) {
// throw error
throw new Error(`HugeTokenMetadataNotSupported`);
}

const injectedTx = {
type: 0, // force ethers to parse it as a legacy transaction
chainId: 0, // force ethers to parse it as a pre-EIP155 transaction
to: await newZKEVMContract.bridgeAddress(),
value: 0,
gasPrice: 0,
gasLimit: 30000000,
nonce: 0,
data: dataInjectedTx,
signature: {
v: "0x1b",
r: "0x00000000000000000000000000000000000000000000000000000005ca1ab1e0",
s: "0x000000000000000000000000000000000000000000000000000000005ca1ab1e",
},
};

// serialize transactions
const txObject = ethers.Transaction.from(injectedTx);

const customData = processorUtils.rawTxToCustomRawTx(txObject.serialized);
batchData = {
batchL2Data: customData,
globalExitRoot: lastGER,
timestamp: blockDeploymentRollup.timestamp,
sequencer: trustedSequencer,
l1BlockNumber: blockDeploymentRollup.number,
l1BlockHash: blockDeploymentRollup.hash,
l1ParentHash: blockDeploymentRollup.parentHash,
globalExitRootManager: Constants.ADDRESS_GLOBAL_EXIT_ROOT_MANAGER_L2,
polygonRollupManager: ethers.ZeroAddress,
gasTokenMetadata,
bridgeManager: sovereignParams.bridgeManager,
sovereignWETHAddress: sovereignParams.sovereignWETHAddress,
sovereignWETHAddressIsNotMintable: sovereignParams.sovereignWETHAddressIsNotMintable,
globalExitRootUpdater: sovereignParams.globalExitRootUpdater,
};
genesis = await updateVanillaGenesis(genesis, chainID, initializeParams);
fs.writeFileSync(pathGenesis, JSON.stringify(genesis, null, 1));
} else {
// Add the first batch of the created rollup
const newZKEVMContract = (await PolygonconsensusFactory.attach(newZKEVMAddress)) as PolygonZkEVMEtrog;
batchData = {
batchL2Data: await newZKEVMContract.generateInitializeTransaction(
if (consensusContract === "PolygonPessimisticConsensus") {
// Add the first batch of the created rollup
const newZKEVMContract = (await PolygonconsensusFactory.attach(
newZKEVMAddress
)) as PolygonPessimisticConsensus;

// Get last GER
const lastGER = await globalExitRootManagerContract.getLastGlobalExitRoot();

const dataInjectedTx = await polygonZkEVMBridgeContract.interface.encodeFunctionData("initialize", [
rollupID,
gasTokenAddress,
gasTokenNetwork,
gasTokenMetadata as any
),
globalExitRoot: globalExitRoot,
timestamp: timestampReceipt,
sequencer: trustedSequencer,
};
Constants.ADDRESS_GLOBAL_EXIT_ROOT_MANAGER_L2, // Global exit root address on L2
ethers.ZeroAddress, // Rollup manager on L2 does not exist
gasTokenMetadata as any,
]);

// check maximum length is 65535
if ((dataInjectedTx.length - 2) / 2 > 0xffff) {
// throw error
throw new Error(`HugeTokenMetadataNotSupported`);
}

const injectedTx = {
type: 0, // force ethers to parse it as a legacy transaction
chainId: 0, // force ethers to parse it as a pre-EIP155 transaction
to: await newZKEVMContract.bridgeAddress(),
value: 0,
gasPrice: 0,
gasLimit: 30000000,
nonce: 0,
data: dataInjectedTx,
signature: {
v: "0x1b",
r: "0x00000000000000000000000000000000000000000000000000000005ca1ab1e0",
s: "0x000000000000000000000000000000000000000000000000000000005ca1ab1e",
},
};

// serialize transactions
const txObject = ethers.Transaction.from(injectedTx);

const customData = processorUtils.rawTxToCustomRawTx(txObject.serialized);
batchData = {
batchL2Data: customData,
globalExitRoot: lastGER,
timestamp: blockDeploymentRollup.timestamp,
sequencer: trustedSequencer,
l1BlockNumber: blockDeploymentRollup.number,
l1BlockHash: blockDeploymentRollup.hash,
l1ParentHash: blockDeploymentRollup.parentHash,
};
} else {
// Add the first batch of the created rollup
const newZKEVMContract = (await PolygonconsensusFactory.attach(newZKEVMAddress)) as PolygonZkEVMEtrog;
batchData = {
batchL2Data: await newZKEVMContract.generateInitializeTransaction(
rollupID,
gasTokenAddress,
gasTokenNetwork,
gasTokenMetadata as any
),
globalExitRoot: globalExitRoot,
timestamp: timestampReceipt,
sequencer: trustedSequencer,
};
}
}
outputJson.firstBatchData = batchData;
outputJson.genesis = genesis.root;
Expand Down
9 changes: 8 additions & 1 deletion deployment/v2/create_rollup_parameters.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@
"deployerPvtKey": "",
"maxFeePerGas":"",
"maxPriorityFeePerGas":"",
"multiplierGas": ""
"multiplierGas": "",
"isVanillaClient": false,
"sovereignParams": {
"bridgeManager": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"sovereignWETHAddress": "0x0000000000000000000000000000000000000000",
"sovereignWETHAddressIsNotMintable": false,
"globalExitRootUpdater": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
}
}
Loading

0 comments on commit 52e4397

Please sign in to comment.