Skip to content

Commit

Permalink
timelock upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
invocamanman committed Dec 21, 2022
1 parent c328c7a commit 8c78b1d
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ docs/interfaces
docs/mocks
.vscode/launch.json
deploy_output.json
deploy_parameters.json
deploy_parameters.json
deployments
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ npm run lint:fix
## Deploy on hardhat

```
npm run deploy:PoE2_0:hardhat
npm run deploy:ZkEVM:hardhat
```

## Build dockers
Expand Down
8 changes: 4 additions & 4 deletions deployment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ cp deploy_parameters.json.example deploy_parameters.json

Fill created `deploy_parameters.json` with appropiate parameters.

To deploy contracts run `npm run deploy:PoE2_0:${network}`, for example:
To deploy contracts run `npm run deploy:ZkEVM:${network}`, for example:

> set `runs` parameter from the compiler settings in `hardhat.config.js` (i.e. `runs: 200`)
```
npm run deploy:PoE2_0:goerli
npm run deploy:ZkEVM:goerli
```

To verify contracts run `npm run verify:PoE2_0:${network}`, for example:
To verify contracts run `npm run verify:ZkEVM:${network}`, for example:

```
npm run verify:PoE2_0:goerli
npm run verify:ZkEVM:goerli
```

## deploy-parameters.json
Expand Down
31 changes: 20 additions & 11 deletions deployment/deployPoE_v2-0.js → deployment/deployContracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,19 @@ const pathOutputJson = path.join(__dirname, './deploy_output.json');
const deployParameters = require('./deploy_parameters.json');
const genesis = require('./genesis.json');

const pathOZUpgradability = path.join(__dirname, `../.openzeppelin/${process.env.HARDHAT_NETWORK}.json`);

async function main() {
// Check if it's .openzeppelin and revert if it exists
// Check that there0s no previous OZ deployment
if (fs.existsSync(pathOZUpgradability)) {
throw new Error(`Theres upggradability information from previous deployments, it's mandatory to erase them before start a new one, path: ${pathOZUpgradability}`);
}

// Constant variables
const networkIDMainnet = 0;
const attemptsDeployProxy = 20;

// Check deploy parameters
const forceBatchAllowed = Boolean(deployParameters.forceBatchAllowed);
const trustedSequencer = deployParameters.trustedSequencerAddress;
const trustedSequencerURL = deployParameters.trustedSequencerURL || 'http://zkevm-json-rpc:8123';
Expand All @@ -23,8 +33,7 @@ async function main() {
const pendingStateTimeout = deployParameters.pendingStateTimeout || (60 * 60 * 24 * 7 - 1);
const trustedAggregatorTimeout = deployParameters.trustedAggregatorTimeout || (60 * 60 * 24 * 7 - 1);

const attemptsDeployProxy = 20;

// Load provider
let currentProvider = ethers.provider;
if (deployParameters.multiplierGas || deployParameters.maxFeePerGas) {
if (process.env.HARDHAT_NETWORK !== 'hardhat') {
Expand All @@ -50,6 +59,7 @@ async function main() {
}
}

// Load deployer
let deployer;
if (deployParameters.privateKey) {
deployer = new ethers.Wallet(deployParameters.privateKey, currentProvider);
Expand All @@ -58,6 +68,7 @@ async function main() {
} else {
[deployer] = (await ethers.getSigners());
}
// Check trusted address from deploy parameters
const admin = deployParameters.admin || deployer.address;
const trustedAggregator = deployParameters.trustedAggregator || deployer.address;
const timelockAddress = deployParameters.timelockAddress || deployer.address;
Expand Down Expand Up @@ -149,7 +160,7 @@ async function main() {
console.log('#######################\n');
console.log('PolygonZkEVMBridge deployed to:', polygonZkEVMBridgeContract.address);

// deploy PoE
// deploy PolygonZkEVMMock
const PolygonZkEVMFactory = await ethers.getContractFactory('PolygonZkEVMMock', deployer);
let polygonZkEVMContract;
for (let i = 0; i < attemptsDeployProxy; i++) {
Expand Down Expand Up @@ -189,7 +200,7 @@ async function main() {
console.log('#######################');
console.log('PolygonZkEVMGlobalExitRootAddress:', await polygonZkEVMBridgeContract.globalExitRootManager());
console.log('networkID:', await polygonZkEVMBridgeContract.networkID());
console.log('zkEVMaddress:', await polygonZkEVMBridgeContract.zkEVMaddress());
console.log('zkEVMaddress:', await polygonZkEVMBridgeContract.polygonZkEVMaddress());

/*
* Initialize Polygon ZK-EVM
Expand Down Expand Up @@ -240,7 +251,7 @@ async function main() {
const deploymentBlockNumber = (await polygonZkEVMContract.deployTransaction.wait()).blockNumber;

console.log('\n#######################');
console.log('##### Checks PoE #####');
console.log('##### Checks PolygonZkEVMMock #####');
console.log('#######################');
console.log('PolygonZkEVMGlobalExitRootAddress:', await polygonZkEVMContract.globalExitRootManager());
console.log('maticTokenAddress:', await polygonZkEVMContract.matic());
Expand Down Expand Up @@ -300,7 +311,7 @@ async function main() {
console.log('timelockAddress:', timelockAddress);
console.log('zkEVMAddress:', polygonZkEVMContract.address);

const timelockContractFactory = await ethers.getContractFactory('PolygonZkEVMTimelock');
const timelockContractFactory = await ethers.getContractFactory('PolygonZkEVMTimelock', deployer);
const timelockContract = await timelockContractFactory.deploy(
minDelayTimelock,
[timelockAddress],
Expand All @@ -322,10 +333,8 @@ async function main() {
console.log('minDelayTimelock:', await timelockContract.getMinDelay());
console.log('polygonZkEVM:', polygonZkEVMContract.address);

/*
* Transfer ownership of the proxyAdmin to timelock
* await upgrades.admin.transferProxyAdminOwnership(timelockContract.address);
*/
// Transfer ownership of the proxyAdmin to timelock
await upgrades.admin.transferProxyAdminOwnership(timelockContract.address);

const outputJson = {
polygonZkEVMAddress: polygonZkEVMContract.address,
Expand Down
6 changes: 3 additions & 3 deletions deployment/genesis.json

Large diffs are not rendered by default.

108 changes: 108 additions & 0 deletions deployment/timeLockUpgrade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* eslint-disable no-console, no-unused-vars, no-use-before-define */
const hre = require('hardhat');
const { ethers, upgrades } = require('hardhat');
const path = require('path');
require('dotenv').config({ path: path.resolve(__dirname, '../.env') });

async function main() {
// Set multiplier Gas
const multiplierGas = 3;
const currentProvider = new ethers.providers.JsonRpcProvider(`https://${process.env.HARDHAT_NETWORK}.infura.io/v3/${process.env.INFURA_PROJECT_ID}`);
async function overrideFeeData() {
const feedata = await ethers.provider.getFeeData();
return {
maxFeePerGas: feedata.maxFeePerGas.mul(multiplierGas),
maxPriorityFeePerGas: feedata.maxPriorityFeePerGas.mul(multiplierGas),
};
}
currentProvider.getFeeData = overrideFeeData;

let deployer;
if (process.env.MNEMONIC) {
deployer = ethers.Wallet.fromMnemonic(process.env.MNEMONIC, 'm/44\'/60\'/0\'/0/0').connect(currentProvider);
} else {
[deployer] = (await ethers.getSigners());
}

// compìle contracts
await hre.run('compile');

const proxyPolygonZkEVMAddress = '0xFD44A8D8f28AadB1Ce916012c7C921f759056Ef7';
const polygonZkEVMFactory = await ethers.getContractFactory('PolygonZkEVMMock');

// Upgrade zkevm
const newImplPolygonZkEVMAddress = await upgrades.prepareUpgrade(proxyPolygonZkEVMAddress, polygonZkEVMFactory);
const proxyAdmin = await upgrades.admin.getInstance();

console.log({ newImplPolygonZkEVMAddress });

// Use timelock
const operation = genOperation(
proxyAdmin.address,
0, // value
proxyAdmin.interface.encodeFunctionData(
'upgrade',
[proxyPolygonZkEVMAddress,
newImplPolygonZkEVMAddress],
),
ethers.constants.HashZero, // predecesoor
ethers.constants.HashZero, // salt TODO
);

// Timelock operations
const TimelockFactory = await ethers.getContractFactory('PolygonZkEVMTimelock', deployer);
const minDelay = 10; // TODO upgrade parameter

// Schedule operation
const scheduleData = TimelockFactory.interface.encodeFunctionData(
'schedule',
[
operation.target,
operation.value,
operation.data,
operation.predecessor,
operation.salt,
minDelay,
],
);
// Executre operation
const executeData = TimelockFactory.interface.encodeFunctionData(
'execute',
[
operation.target,
operation.value,
operation.data,
operation.predecessor,
operation.salt,
],
);

console.log({ scheduleData });
console.log({ executeData });
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});

// OZ test functions
function genOperation(target, value, data, predecessor, salt) {
const id = ethers.utils.solidityKeccak256([
'address',
'uint256',
'bytes',
'uint256',
'bytes32',
], [
target,
value,
data,
predecessor,
salt,
]);
return {
id, target, value, data, predecessor, salt,
};
}
File renamed without changes.
2 changes: 1 addition & 1 deletion docker/scripts/deploy-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sleep 5
node docker/scripts/fund-accounts.js
cp docker/scripts/deploy_parameters_docker.json deployment/deploy_parameters.json
cp docker/scripts/genesis_docker.json deployment/genesis.json
npx hardhat run deployment/deployPoE_v2-0.js --network localhost
npx hardhat run deployment/deployContracts.js --network localhost
mkdir docker/deploymentOutput
mv deployment/deploy_output.json docker/deploymentOutput
docker-compose -f docker/docker-compose.geth.yml down
Expand Down
6 changes: 3 additions & 3 deletions docker/scripts/genesis_docker.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@
"solidity-docgen": "^0.5.16"
},
"scripts": {
"saveDeployment:goerli": "mkdir -p deployments/goerli_$(date +%s) && cp -r deployment/deploy_*.json deployments/goerli_$(date +%s) && cp .openzeppelin/goerli.json deployments/goerli_$(date +%s)",
"test": "npx hardhat test test/contracts/**.test.js",
"docgen": "npx solidity-docgen --solc-module solc-0.8 -t ./docs/templates -e ./contracts/verifiers,./contracts/mocks",
"deploy:PoE2_0:hardhat": "npx hardhat run deployment/deployPoE_v2-0.js --network hardhat",
"deploy:PoE2_0:goerli": "npx hardhat run deployment/deployPoE_v2-0.js --network goerli",
"verify:PoE2_0:goerli": "npx hardhat run deployment/verifyPoE_v2-0.js --network goerli",
"deploy:ZkEVM:hardhat": "npx hardhat run deployment/deployContracts.js --network hardhat",
"deploy:ZkEVM:goerli": "npx hardhat run deployment/deployContracts.js --network goerli && npm run saveDeployment:goerli",
"upgrade:timelock:goerli": "npx hardhat run deployment/timeLockUpgrade.js --network goerli",
"verify:ZkEVM:goerli": "npx hardhat run deployment/verifyContracts.js --network goerli",
"lint": "npx eslint ./test && npx eslint ./docker/scripts && npx eslint ./deployment && npx eslint ./src",
"lint:fix": "npx eslint ./test --fix && npx eslint ./docker/scripts --fix && npx eslint ./deployment --fix && npx eslint ./src --fix",
"compile": "npx hardhat compile",
"check:deploy:PoE": "npx hardhat node > /dev/null 2>&1 & npx hardhat run --network localhost deployment/testnet/deployPoETestnet.js && npx hardhat run --network localhost deployment/testnet/checkDeployment.js && pkill -f 'hardhat'",
"docker:contracts": "./docker/scripts/deploy-docker.sh",
"push:docker:contracts": "docker push hermeznetwork/geth-zkevm-contracts",
"update:genesis": "node src/create-genesis.js --gen ../deployment/genesis-gen.json --out ../deployment/genesis.json && node src/create-genesis.js --gen ../docker/scripts/genesis_gen_docker.json --out ../docker/scripts/genesis_docker.json"
Expand Down
2 changes: 1 addition & 1 deletion test/contracts/emergencyManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('Emergency mode test', () => {
const polygonZkEVMBridgeFactory = await ethers.getContractFactory('PolygonZkEVMBridge');
polygonZkEVMBridgeContract = await upgrades.deployProxy(polygonZkEVMBridgeFactory, [], { initializer: false });

// deploy PoE
// deploy PolygonZkEVMMock
const PolygonZkEVMFactory = await ethers.getContractFactory('PolygonZkEVMMock');
polygonZkEVMContract = await upgrades.deployProxy(PolygonZkEVMFactory, [], { initializer: false });

Expand Down
2 changes: 1 addition & 1 deletion test/contracts/proofOfEfficiency.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('Polygon ZK-EVM', () => {
const polygonZkEVMBridgeFactory = await ethers.getContractFactory('PolygonZkEVMBridge');
polygonZkEVMBridgeContract = await upgrades.deployProxy(polygonZkEVMBridgeFactory, [], { initializer: false });

// deploy PoE
// deploy PolygonZkEVMMock
const PolygonZkEVMFactory = await ethers.getContractFactory('PolygonZkEVMMock');
polygonZkEVMContract = await upgrades.deployProxy(PolygonZkEVMFactory, [], { initializer: false });

Expand Down
4 changes: 2 additions & 2 deletions test/contracts/real-prover/real-flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('Real flow test', () => {
const polygonZkEVMBridgeFactory = await ethers.getContractFactory('PolygonZkEVMBridge');
polygonZkEVMBridgeContract = await upgrades.deployProxy(polygonZkEVMBridgeFactory, [], { initializer: false });

// deploy PoE
// deploy PolygonZkEVMMock
const PolygonZkEVMFactory = await ethers.getContractFactory('PolygonZkEVMMock');
polygonZkEVMContract = await upgrades.deployProxy(PolygonZkEVMFactory, [], { initializer: false });

Expand Down Expand Up @@ -122,7 +122,7 @@ describe('Real flow test', () => {
maticTokenContract.connect(trustedSequencer).approve(polygonZkEVMContract.address, maticAmount.mul(batchesNum)),
).to.emit(maticTokenContract, 'Approval');

// prepare PoE
// prepare PolygonZkEVMMock
await polygonZkEVMContract.setVerifiedBatch(inputJson.oldNumBatch);
await polygonZkEVMContract.setSequencedBatch(inputJson.oldNumBatch);
const lastTimestamp = batchesData[batchesNum - 1].timestamp;
Expand Down
2 changes: 1 addition & 1 deletion test/contracts/timelockUpgradeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('Polygon ZK-EVM', () => {
const polygonZkEVMBridgeFactory = await ethers.getContractFactory('PolygonZkEVMBridge');
polygonZkEVMBridgeContract = await upgrades.deployProxy(polygonZkEVMBridgeFactory, [], { initializer: false });

// deploy PoE
// deploy PolygonZkEVMMock
const PolygonZkEVMFactory = await ethers.getContractFactory('PolygonZkEVMMock');
polygonZkEVMContract = await upgrades.deployProxy(PolygonZkEVMFactory, [], { initializer: false });

Expand Down

0 comments on commit 8c78b1d

Please sign in to comment.