Skip to content

Commit

Permalink
update deployment scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
invocamanman committed Jan 25, 2024
1 parent 7247b54 commit 8e0d098
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 51 deletions.
26 changes: 22 additions & 4 deletions contracts/v2/PolygonRollupManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ contract PolygonRollupManager is

/**
* @notice Verify and reward batches internal function
* @param rollup Rollup Data struct that will be used to the verification
* @param rollup Rollup Data storage pointer that will be used to the verification
* @param pendingStateNum Init pending state, 0 if consolidated state is used
* @param initNumBatch Batch which the aggregator starts the verification
* @param finalNewBatch Last batch aggregator intends to verify
Expand Down Expand Up @@ -1085,6 +1085,7 @@ contract PolygonRollupManager is
/**
* @notice Allows to consolidate any pending state that has already exceed the pendingStateTimeout
* Can be called by the trusted aggregator, which can consolidate any state without the timeout restrictions
* @param rollupID Rollup identifier
* @param pendingStateNum Pending state to consolidate
*/
function consolidatePendingState(
Expand All @@ -1108,6 +1109,7 @@ contract PolygonRollupManager is

/**
* @notice Internal function to consolidate any pending state that has already exceed the pendingStateTimeout
* @param rollup Rollup data storage pointer
* @param pendingStateNum Pending state to consolidate
*/
function _consolidatePendingState(
Expand Down Expand Up @@ -1689,6 +1691,8 @@ contract PolygonRollupManager is

/**
* @notice Returns a boolean that indicates if the pendingStateNum is or not consolidable
* @param rollupID Rollup id
* @param pendingStateNum Pending state number to check
* Note that his function does not check if the pending state currently exists, or if it's consolidated already
*/
function isPendingStateConsolidable(
Expand All @@ -1704,6 +1708,8 @@ contract PolygonRollupManager is

/**
* @notice Returns a boolean that indicates if the pendingStateNum is or not consolidable
* @param rollup Rollup data storage pointer
* @param pendingStateNum Pending state number to check
* Note that his function does not check if the pending state currently exists, or if it's consolidated already
*/
function _isPendingStateConsolidable(
Expand Down Expand Up @@ -1747,6 +1753,7 @@ contract PolygonRollupManager is

/**
* @notice Function to calculate the input snark bytes
* @param rollupID Rollup id used to calculate the input snark bytes
* @param initNumBatch Batch which the aggregator starts the verification
* @param finalNewBatch Last batch aggregator intends to verify
* @param newLocalExitRoot New local exit root once the batch is processed
Expand Down Expand Up @@ -1774,6 +1781,7 @@ contract PolygonRollupManager is

/**
* @notice Function to calculate the input snark bytes
* @param rollup Rollup data storage pointer
* @param initNumBatch Batch which the aggregator starts the verification
* @param finalNewBatch Last batch aggregator intends to verify
* @param newLocalExitRoot New local exit root once the batch is processed
Expand Down Expand Up @@ -1826,6 +1834,10 @@ contract PolygonRollupManager is
);
}

/**
* @notice Function to check if the state root is inside of the prime field
* @param newStateRoot New State root once the batch is processed
*/
function _checkStateRootInsidePrime(
uint256 newStateRoot
) internal pure returns (bool) {
Expand All @@ -1843,7 +1855,9 @@ contract PolygonRollupManager is
}

/**
* @notice Get the last verified batch
* @notice Get rollup state root given a batch number
* @param rollupID Rollup identifier
* @param batchNum Batch number
*/
function getRollupBatchNumToStateRoot(
uint32 rollupID,
Expand All @@ -1853,7 +1867,9 @@ contract PolygonRollupManager is
}

/**
* @notice Get the last verified batch
* @notice Get rollup sequence batches struct given a batch number
* @param rollupID Rollup identifier
* @param batchNum Batch number
*/
function getRollupSequencedBatches(
uint32 rollupID,
Expand All @@ -1863,7 +1879,9 @@ contract PolygonRollupManager is
}

/**
* @notice Get the last verified batch
* @notice Get rollup sequence pending state struct given a batch number
* @param rollupID Rollup identifier
* @param batchNum Batch number
*/
function getRollupPendingStateTransitions(
uint32 rollupID,
Expand Down
2 changes: 1 addition & 1 deletion deployment/v2/3_deployContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ async function main() {
expect(precalculateRollupManager).to.be.equal(await polygonZkEVMGlobalExitRoot.rollupManager());
}

// deploy Rollup Managdr
// deploy Rollup Manager
console.log("\n#######################");
console.log("##### Deployment Polygon ZK-EVM #####");
console.log("#######################");
Expand Down
94 changes: 77 additions & 17 deletions deployment/v2/4_createRollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const genesis = require("./genesis.json");
const deployOutput = require("./deploy_output.json");
import "../helpers/utils";

import {PolygonRollupManager, PolygonZkEVMV2} from "../../typechain-types";
import {PolygonRollupManager, PolygonZkEVMV2, PolygonZkEVMBridgeV2} from "../../typechain-types";

async function main() {
/*
Expand All @@ -35,6 +35,7 @@ async function main() {
"chainID",
"adminZkEVM",
"forkID",
"consensusContract",
];

for (const parameterName of mandatoryDeploymentParameters) {
Expand All @@ -43,8 +44,28 @@ async function main() {
}
}

const {realVerifier, trustedSequencerURL, networkName, description, trustedSequencer, chainID, adminZkEVM, forkID} =
deployParameters;
const {
realVerifier,
trustedSequencerURL,
networkName,
description,
trustedSequencer,
chainID,
adminZkEVM,
forkID,
consensusContract,
} = deployParameters;

const supportedConensus = [
"PolygonZkEVMEtrog",
"PolygonZkEVMV2",
"PolygonDataComittee",
"PolygonDataComitteeEtrog",
];

if (!supportedConensus.includes(consensusContract)) {
throw new Error(`Consensus contract not supported, supported contracts are: ${supportedConensus}`);
}

// Load provider
let currentProvider = ethers.provider;
Expand Down Expand Up @@ -115,24 +136,28 @@ async function main() {
const ADD_ROLLUP_TYPE_ROLE = ethers.id("ADD_ROLLUP_TYPE_ROLE");
const CREATE_ROLLUP_ROLE = ethers.id("CREATE_ROLLUP_ROLE");

await rollupManagerContract.grantRole(ADD_ROLLUP_TYPE_ROLE, deployer.address);
await rollupManagerContract.grantRole(CREATE_ROLLUP_ROLE, deployer.address);
// Check role:
if ((await rollupManagerContract.hasRole(ADD_ROLLUP_TYPE_ROLE, deployer.address)) == false)
await rollupManagerContract.grantRole(ADD_ROLLUP_TYPE_ROLE, deployer.address);

if ((await rollupManagerContract.hasRole(CREATE_ROLLUP_ROLE, deployer.address)) == false)
await rollupManagerContract.grantRole(CREATE_ROLLUP_ROLE, deployer.address);

// Create zkEVM implementation
const PolygonZKEVMV2Factory = await ethers.getContractFactory("PolygonZkEVMEtrog");
const PolygonZKEVMV2Contract = await PolygonZKEVMV2Factory.deploy(
// Create consensus implementation
const PolygonconsensusFactory = (await ethers.getContractFactory(consensusContract)) as any;
const PolygonconsensusContract = await PolygonconsensusFactory.deploy(
deployOutput.polygonZkEVMGlobalExitRootAddress,
deployOutput.polTokenAddress,
deployOutput.polygonZkEVMBridgeAddress,
deployOutput.polygonRollupManager
);
await PolygonZKEVMV2Contract.waitForDeployment();
await PolygonconsensusContract.waitForDeployment();

// Add a new rollup type with timelock
const rollupCompatibilityID = 0;
await (
await rollupManagerContract.addNewRollupType(
PolygonZKEVMV2Contract.target,
PolygonconsensusContract.target,
verifierContract.target,
forkID,
rollupCompatibilityID,
Expand All @@ -147,9 +172,30 @@ async function main() {

let gasTokenAddress, gasTokenNetwork, gasTokenMetadata;

if (deployParameters.gasTokenAddress && deployParameters.gasTokenAddress != "") {
gasTokenAddress = deployParameters.gasTokenAddress;
gasTokenNetwork = deployParameters.gasTokenNetwork;
if (
deployParameters.gasTokenAddress &&
deployParameters.gasTokenAddress != "" &&
deployParameters.gasTokenAddress != ethers.ZeroAddress
) {
// Get bridge instance
const bridgeFactory = await ethers.getContractFactory("PolygonZkEVMBridgeV2", deployer);
const polygonZkEVMBridgeContract = bridgeFactory.attach(
deployOutput.polygonZkEVMBridgeAddress
) as PolygonZkEVMBridgeV2;

// Get token metadata
gasTokenMetadata = await polygonZkEVMBridgeContract.getTokenMetadata(deployParameters.gasTokenAddress);

const wrappedData = await polygonZkEVMBridgeContract.wrappedTokenToTokenInfo(deployParameters.gasTokenAddress);
if (wrappedData.originNetwork != 0n) {
// Wrapped token
gasTokenAddress = wrappedData.originTokenAddress;
gasTokenNetwork = wrappedData.originNetwork;
} else {
// Mainnet token
gasTokenAddress = deployParameters.gasTokenAddress;
gasTokenNetwork = 0n;
}
} else {
gasTokenAddress = ethers.ZeroAddress;
gasTokenNetwork = 0;
Expand All @@ -172,30 +218,44 @@ async function main() {
networkName
);

const receipt = await txDeployRollup.wait();
const receipt = (await txDeployRollup.wait()) as any;
const timestampReceipt = (await receipt?.getBlock())?.timestamp;
const rollupID = await rollupManagerContract.chainIDToRollupID(chainID);

console.log("#######################\n");
console.log("Created new Rollup:", newZKEVMAddress);

// Assert admin address
expect(await upgrades.erc1967.getAdminAddress(newZKEVMAddress)).to.be.equal(rollupManagerContract.target);
expect(await upgrades.erc1967.getImplementationAddress(newZKEVMAddress)).to.be.equal(PolygonZKEVMV2Contract.target);
expect(await upgrades.erc1967.getImplementationAddress(newZKEVMAddress)).to.be.equal(
PolygonconsensusContract.target
);

// Search added global exit root on the logs
let globalExitRoot;
for (const log of receipt?.logs) {
if (log.address == newZKEVMAddress) {
const parsedLog = PolygonconsensusFactory.interface.parseLog(log);
if (parsedLog != null && parsedLog.name == "InitialSequenceBatches") {
globalExitRoot = parsedLog.args.lastGlobalExitRoot;
}
}
}

deployOutput.genesis = genesis.root;
deployOutput.newZKEVMAddress = newZKEVMAddress;
deployOutput.verifierAddress = verifierContract.target;

// Add the first batch of the created rollup
const newZKEVMContract = (await PolygonZKEVMV2Factory.attach(newZKEVMAddress)) as PolygonZkEVMV2;
const newZKEVMContract = (await PolygonconsensusFactory.attach(newZKEVMAddress)) as PolygonZkEVMV2;
const batchData = {
transactions: await newZKEVMContract.generateInitializeTransaction(
rollupID,
gasTokenAddress,
gasTokenNetwork,
gasTokenMetadata as any
),
globalExitRoot: ethers.ZeroHash,
globalExitRoot: globalExitRoot,
timestamp: timestampReceipt,
sequencer: trustedSequencer,
};
Expand Down
58 changes: 31 additions & 27 deletions deployment/v2/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Requirements

- node version: 14.x
- npm version: 7.x
- node version: 14.x
- npm version: 7.x

## Deployment

Expand Down Expand Up @@ -55,34 +55,38 @@ A new folder will be created witth the following name `deployments/${network}_$(

## deploy-parameters.json

- `realVerifier`: bool, Indicates whether deploy a real verifier or not
- `trustedSequencerURL`: string, trustedSequencer URL
- `networkName`: string, networkName
- `version`:string, will just be emitted at initialization of the contract, usefull just for synchronizer
- `trustedSequencer`: address, trusted sequencer addresss
- `chainID`: uint64, chainID of the zkEVM
- `trustedAggregator`:address, Trusted aggregator address
- `trustedAggregatorTimeout`: uint64, If a sequence is not verified in this timeout everyone can verify it
- `pendingStateTimeout`: uint64, Once a pending state exceeds this timeout it can be consolidated
- `forkID`: uint64, Fork ID of the zkEVM, indicates the prover (zkROM/executor) version
- `admin`:address, Admin address, can adjust PolygonZkEVM parameters or stop the emergency state
- `zkEVMOwner`: address, Able to put the PolygonZkEVM into emergency state (kill switch)
- `timelockAddress`: address, Timelock owner address, able to send start an upgradability process via timelock
- `minDelayTimelock`: number, Minimum timelock delay,
- `salt`: bytes32, Salt used in `PolygonZkEVMDeployer` to deploy deterministic contracts, such as the PolygonZkEVMBridge
- `initialZkEVMDeployerOwner`: address, Initial owner of the `PolygonZkEVMDeployer`
- `maticTokenAddress`: address, Matic token address, only if deploy on testnet can be left blank and will fullfilled by the scripts.
- `zkEVMDeployerAddress`: address, Address of the `PolygonZkEVMDeployer`. Can be left blank, will be fullfilled automatically with the `deploy:deployer:ZkEVM:goerli` script.
- `timelockAdminAddress`: address, Timelock owner address, able to send start an upgradability process via timelock
- `minDelayTimelock`: number, Minimum timelock delay,
- `salt`: bytes32, Salt used in `PolygonZkEVMDeployer` to deploy deterministic contracts, such as the PolygonZkEVMBridge
- `initialZkEVMDeployerOwner`: address, Initial owner of the `PolygonZkEVMDeployer`
- `admin`:address, Admin address, can adjust RollupManager parameters or stop the emergency state
- `trustedAggregator`:address, Trusted aggregator address
- `trustedAggregatorTimeout`: uint64, If a sequence is not verified in this timeout everyone can verify it
- `pendingStateTimeout`: uint64, Once a pending state exceeds this timeout it can be consolidated
- `emergencyCouncilAddress`:address, Emergency council addres
<The following parameters refer to the new rollup created >
- `realVerifier`: bool, Indicates whether deploy a real verifier or not for the new created
- `trustedSequencerURL`: string, trustedSequencer URL
- `networkName`: string, networkName
- `description`:string, Description of the new rollup type
- `trustedSequencer`: address, trusted sequencer addresss
- `chainID`: uint64, chainID of the new rollup
- `admin`:address, Admin address, can adjust Rollup parameters
- `forkID`: uint64, Fork ID of the new rollup, indicates the prover (zkROM/executor) version
- `consensusContract`: string, Consensus contract name of the new rollup deployed, current options are: "PolygonZkEVMEtrog","PolygonZkEVMV2","PolygonDataComittee", "PolygonDataComitteeEtrog",
- `gasTokenAddress`:address, Gas token address, empty or address(0) for ether
- `polTokenAddress`: address, Matic token address, only if deploy on testnet can be left blank and will fullfilled by the scripts.
- `zkEVMDeployerAddress`: address, Address of the `PolygonZkEVMDeployer`. Can be left blank, will be fullfilled automatically with the `deploy:deployer:ZkEVM:goerli` script.

### Optional Parameters

- `deployerPvtKey`: string, pvtKey of the deployer, overrides the address in `MNEMONIC` of `.env` if exist
- `maxFeePerGas`:string, Set `maxFeePerGas`, must define aswell `maxPriorityFeePerGas` to use it
- `maxPriorityFeePerGas`:string, Set `maxPriorityFeePerGas`, must define aswell `maxFeePerGas` to use it
- `multiplierGas`: number, Gas multiplier with 3 decimals. If `maxFeePerGas` and `maxPriorityFeePerGas` are set, this will not take effect
- `deployerPvtKey`: string, pvtKey of the deployer, overrides the address in `MNEMONIC` of `.env` if exist
- `maxFeePerGas`:string, Set `maxFeePerGas`, must define aswell `maxPriorityFeePerGas` to use it
- `maxPriorityFeePerGas`:string, Set `maxPriorityFeePerGas`, must define aswell `maxFeePerGas` to use it
- `multiplierGas`: number, Gas multiplier with 3 decimals. If `maxFeePerGas` and `maxPriorityFeePerGas` are set, this will not take effect

## Notes

- Since there are deterministic address you cannot deploy twice on the same network using the same `salt` and `initialZkEVMDeployerOwner`. Changing one of them is enough to make a new deployment.
- It's mandatory to delete the `.openzeppelin` upgradebility information in order to make a new deployment
- `genesis.json` has been generated using the tool: `1_createGenesis`, this script depends on the `deploy_parameters` aswell.
- Since there are deterministic address you cannot deploy twice on the same network using the same `salt` and `initialZkEVMDeployerOwner`. Changing one of them is enough to make a new deployment.
- It's mandatory to delete the `.openzeppelin` upgradebility information in order to make a new deployment
- `genesis.json` has been generated using the tool: `1_createGenesis`, this script depends on the `deploy_parameters` aswell.
4 changes: 2 additions & 2 deletions deployment/v2/deploy_parameters.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"chainID": 1001,
"adminZkEVM":"0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D",
"forkID": 1,
"consensusContract": "PolygonZkEVMEtrog",
"gasTokenAddress":"",
"polTokenAddress":"0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D",
"zkEVMDeployerAddress":"",
"gasTokenAddress":"",
"gasTokenNetwork":0,
"deployerPvtKey": "",
"maxFeePerGas":"",
"maxPriorityFeePerGas":"",
Expand Down
1 change: 1 addition & 0 deletions docker/scripts/v2/deploy_parameters_docker.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"chainID": 1001,
"adminZkEVM": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"forkID": 5,
"consensusContract": "PolygonZkEVMEtrog",
"polTokenAddress": "",
"zkEVMDeployerAddress": "",
"gasTokenAddress": "",
Expand Down
10 changes: 10 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ const config: HardhatUserConfig = {
evmVersion: "shanghai",
},
},
"contracts/v2/mocks/PolygonRollupManagerMockInternalTest.sol": {
version: "0.8.20",
settings: {
optimizer: {
enabled: true,
runs: 99,
},
evmVersion: "shanghai",
}, // try yul optimizer
},
"contracts/v2/mocks/PolygonRollupManagerMock.sol": {
version: "0.8.20",
settings: {
Expand Down

0 comments on commit 8e0d098

Please sign in to comment.