Skip to content

Commit

Permalink
Merge pull request #3 from matter-labs/update-scripts-for-new-upgrade
Browse files Browse the repository at this point in the history
Update scripts for new upgrade
  • Loading branch information
vladbochok authored Jan 16, 2023
2 parents 5c204f2 + 892f36b commit 279fb41
Show file tree
Hide file tree
Showing 15 changed files with 539 additions and 41 deletions.
1 change: 1 addition & 0 deletions ethereum/contracts/common/L2ContractHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface IContractDeployer {
struct ForceDeployment {
bytes32 bytecodeHash;
address newAddress;
bool callConstructor;
uint256 value;
bytes input;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

pragma solidity ^0.8.0;

import "./Config.sol";
import "./facets/Mailbox.sol";
import "./libraries/Diamond.sol";
import "../common/L2ContractHelper.sol";
import "../Config.sol";
import "../facets/Mailbox.sol";
import "../libraries/Diamond.sol";
import "../../common/L2ContractHelper.sol";

interface IOldContractDeployer {
function forceDeployOnAddress(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

pragma solidity ^0.8.0;

import "./facets/Mailbox.sol";
import "./libraries/Diamond.sol";
import "../common/L2ContractHelper.sol";
import "../facets/Mailbox.sol";
import "../libraries/Diamond.sol";
import "../../common/L2ContractHelper.sol";

/// @author Matter Labs
contract DiamondUpgradeInit is MailboxFacet {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "../libraries/Diamond.sol";
import "../facets/Base.sol";

interface IOldDiamondCut {
function proposeDiamondCut(Diamond.FacetCut[] calldata _facetCuts, address _initAddress) external;

function cancelDiamondCutProposal() external;

function executeDiamondCutProposal(Diamond.DiamondCutData calldata _diamondCut) external;

function emergencyFreezeDiamond() external;

function unfreezeDiamond() external;

function approveEmergencyDiamondCutAsSecurityCouncilMember(bytes32 _diamondCutHash) external;

// FIXME: token holders should have the ability to cancel the upgrade

event DiamondCutProposal(Diamond.FacetCut[] _facetCuts, address _initAddress);

event DiamondCutProposalCancelation(uint256 currentProposalId, bytes32 indexed proposedDiamondCutHash);

event DiamondCutProposalExecution(Diamond.DiamondCutData _diamondCut);

event EmergencyFreeze();

event Unfreeze(uint256 lastDiamondFreezeTimestamp);

event EmergencyDiamondCutApproved(
address indexed _address,
uint256 currentProposalId,
uint256 securityCouncilEmergencyApprovals,
bytes32 indexed proposedDiamondCutHash
);
}

/// @author Matter Labs
contract DiamondUpgradeInit3 is Base {
function upgrade(
uint256 _priorityTxMaxErgsLimit,
IAllowList _allowList,
Verifier _verifier
) external payable returns (bytes32) {
// Zero out the deprecated storage slots
delete s.__DEPRECATED_diamondCutStorage;

s.priorityTxMaxErgsLimit = _priorityTxMaxErgsLimit;
s.allowList = _allowList;
s.verifier = _verifier;

return Diamond.DIAMOND_INIT_SUCCESS_RETURN_VALUE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "../Config.sol";
import "../facets/Mailbox.sol";
import "../libraries/Diamond.sol";
import "../../common/L2ContractHelper.sol";

interface IOldContractDeployer {
struct ForceDeployment {
bytes32 bytecodeHash;
address newAddress;
uint256 value;
bytes input;
}

function forceDeployOnAddresses(ForceDeployment[] calldata _deployParams) external;
}

/// @author Matter Labs
contract DiamondUpgradeInit4 is MailboxFacet {
function forceDeploy2(
bytes calldata _upgradeDeployerCalldata,
bytes calldata _upgradeSystemContractsCalldata,
bytes[] calldata _factoryDeps
) external payable returns (bytes32) {
// 1. Update bytecode for the deployer smart contract
_requestL2Transaction(
FORCE_DEPLOYER,
DEPLOYER_SYSTEM_CONTRACT_ADDRESS,
0,
_upgradeDeployerCalldata,
$(PRIORITY_TX_MAX_ERGS_LIMIT),
_factoryDeps
);

// 2. Redeploy other contracts by one transaction
_requestL2Transaction(
FORCE_DEPLOYER,
DEPLOYER_SYSTEM_CONTRACT_ADDRESS,
0,
_upgradeSystemContractsCalldata,
$(PRIORITY_TX_MAX_ERGS_LIMIT),
_factoryDeps
);

return Diamond.DIAMOND_INIT_SUCCESS_RETURN_VALUE;
}
}
6 changes: 4 additions & 2 deletions ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@
"read-variable": "ts-node scripts/read-variable.ts",
"initialize-bridges": "zk f ts-node scripts/initialize-bridges.ts",
"initialize-allow-list": "zk f ts-node scripts/initialize-l1-allow-list.ts",
"upgrade-l2-force-deploy": "zk f ts-node scripts/upgrade-l2-force-deploy.ts",
"force-deploy-upgrade-2": "zk f ts-node scripts/force-deploy-upgrade-2.ts"
"upgrade-1": "zk f ts-node scripts/upgrades/upgrade-1.ts",
"upgrade-2": "zk f ts-node scripts/upgrades/upgrade-2.ts",
"upgrade-3": "zk f ts-node scripts/upgrades/upgrade-3.ts",
"upgrade-4": "zk f ts-node scripts/upgrades/upgrade-4.ts"
},
"dependencies": {
"dotenv": "^16.0.3"
Expand Down
13 changes: 10 additions & 3 deletions ethereum/scripts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ async function main() {
.option('--nonce <nonce>')
.option('--governor-address <governor-address>')
.option('--create2-salt <create2-salt>')
.option('--diamond-upgrade-init <version>')
.action(async (cmd) => {
const deployWallet = cmd.privateKey
? new Wallet(cmd.privateKey, provider)
Expand Down Expand Up @@ -52,10 +53,16 @@ async function main() {
await deployer.deployCreate2Factory({ gasPrice, nonce });
nonce++;
}

// Deploy diamond upgrade init contract if needed
const diamondUpgradeContractVersion = cmd.diamondUpgradeInit;
if (diamondUpgradeContractVersion) {
await deployer.deployDiamondUpgradeInit(create2Salt, diamondUpgradeContractVersion, { gasPrice, nonce });
nonce++;
}

await deployer.deployDiamondInit2(create2Salt, { gasPrice, nonce });
await deployer.deployAllowList(create2Salt, { gasPrice, nonce: nonce + 1 });
await deployer.deployZkSyncContract(create2Salt, gasPrice, nonce + 2);
await deployer.deployAllowList(create2Salt, { gasPrice, nonce });
await deployer.deployZkSyncContract(create2Salt, gasPrice, nonce + 1);
await deployer.deployBridgeContracts(create2Salt, gasPrice); // Do not pass nonce, since it was increment after deploying zkSync contracts
});

Expand Down
67 changes: 51 additions & 16 deletions ethereum/scripts/diamond-upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import { FacetCut, getAllSelectors } from '../src.ts/diamondCut';
const provider = web3Provider();
const ZERO_ADDRESS = ethers.constants.AddressZero;

function getZkSyncContract() {
// Create the dummy wallet with provider to get contracts from `Deployer`
const dummyWallet = ethers.Wallet.createRandom().connect(provider);
const deployer = new Deployer({ deployWallet: dummyWallet });

return deployer.zkSyncContract(dummyWallet);
}

async function main() {
const program = new Command();

Expand All @@ -22,20 +30,46 @@ async function main() {
});

program.command('diamond-loupe-view').action(async () => {
// Create the dummy wallet with provider to get contracts from `Deployer`
const dummyWallet = ethers.Wallet.createRandom().connect(provider);
const deployer = new Deployer({ deployWallet: dummyWallet });

const zkSync = deployer.zkSyncContract(dummyWallet);
const facets = await zkSync.facets();
const facets = await getZkSyncContract().facets();

print('Facets', facets);
});

program.command('legacy-prepare-upgrade-calldata <facetCuts>')
.option('--init-address <init-address>')
.option('--init-data <init-data>')
.action(async (facetCutsData: string, cmd) => {
const diamondCutFacet = await hardhat.ethers.getContractAt('IOldDiamondCut', ZERO_ADDRESS);

// Encode data for the upgrade call
const facetCuts: Array<FacetCut> = JSON.parse(facetCutsData);

const initAddress = cmd.initAddress ? cmd.initAddress : ZERO_ADDRESS;
const initData = cmd.initData ? cmd.initData : '0x';

const upgradeParam = diamondCut(facetCuts, initAddress, initData);
print('DiamondCutData', upgradeParam);

// Get transaction data of the `proposeDiamondCut`
const proposeUpgrade = await diamondCutFacet.interface.encodeFunctionData('proposeDiamondCut', [
upgradeParam.facetCuts,
upgradeParam.initAddress
]);

// Get transaction data of the `executeDiamondCutProposal`
const executeUpgrade = await diamondCutFacet.interface.encodeFunctionData('executeDiamondCutProposal', [
upgradeParam
]);

print('proposeUpgrade', proposeUpgrade);
print('executeUpgrade', executeUpgrade);
});

program
.command('prepare-upgrade-calldata <facetCuts>')
.option('--init-address <init-address>')
.option('--init-data <init-data>')
.option('--proposal-id <proposal-id>')
.action(async (facetCutsData: string, cmd) => {
const diamondCutFacet = await hardhat.ethers.getContractAt('DiamondCutFacet', ZERO_ADDRESS);

Expand All @@ -44,24 +78,25 @@ async function main() {

const initAddress = cmd.initAddress ? cmd.initAddress : ZERO_ADDRESS;
const initData = cmd.initData ? cmd.initData : '0x';
const proposalId = cmd.proposalId ? cmd.proposalId : await getZkSyncContract().getCurrentProposalId();

const upgradeParam = diamondCut(facetCuts, initAddress, initData);
print('DiamondCut', upgradeParam);

// Get transaction data of the `proposeDiamondCut`
const proposeDiamondCut = await diamondCutFacet.interface.encodeFunctionData('proposeDiamondCut', [
upgradeParam.facetCuts,
upgradeParam.initAddress
// Get transaction data of the `proposeTransparentUpgrade`
const proposeUpgrade = await diamondCutFacet.interface.encodeFunctionData('proposeTransparentUpgrade', [
upgradeParam,
proposalId
]);

// Get transaction data of the `executeDiamondCutProposal`
const executeDiamondCutProposal = await diamondCutFacet.interface.encodeFunctionData(
'executeDiamondCutProposal',
[upgradeParam]
);
const executeUpgrade = await diamondCutFacet.interface.encodeFunctionData('executeUpgrade', [
upgradeParam,
ethers.constants.HashZero
]);

print('proposeDiamondCut', proposeDiamondCut);
print('executeDiamondCutProposal', executeDiamondCutProposal);
print('proposeUpgrade', proposeUpgrade);
print('executeUpgrade', executeUpgrade);
});

await program.parseAsync(process.argv);
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 279fb41

Please sign in to comment.