Skip to content

Commit

Permalink
feat: check diamond cut hash locally (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimazhornyk authored Jun 12, 2024
1 parent 311b82e commit ab2f13d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
14 changes: 10 additions & 4 deletions l1-contracts/scripts/register-hyperchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,9 @@ async function main() {

program
.option("--private-key <private-key>")
.option("--chain-id <chain-id>")
.option("--gas-price <gas-price>")
.option("--nonce <nonce>")
.option("--governor-address <governor-address>")
.option("--create2-salt <create2-salt>")
.option("--diamond-upgrade-init <version>")
.option("--only-verifier")
.option("--validium-mode")
.option("--base-token-name <base-token-name>")
Expand Down Expand Up @@ -106,7 +103,16 @@ async function main() {
await deployer.registerToken(baseTokenAddress, useGovernance);
}

await deployer.registerHyperchain(baseTokenAddress, cmd.validiumMode, null, gasPrice, useGovernance);
await deployer.registerHyperchain(
baseTokenAddress,
cmd.validiumMode,
null,
gasPrice,
true,
null,
null,
useGovernance
);
await deployer.transferAdminFromDeployerToGovernance();
});

Expand Down
1 change: 1 addition & 0 deletions l1-contracts/src.ts/deploy-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export async function registerHyperchain(
validiumMode,
extraFacets,
gasPrice,
false,
null,
chainId,
useGovernance
Expand Down
59 changes: 44 additions & 15 deletions l1-contracts/src.ts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ import { L1SharedBridgeFactory } from "../typechain/L1SharedBridgeFactory";

import { SingletonFactoryFactory } from "../typechain/SingletonFactoryFactory";
import { ValidatorTimelockFactory } from "../typechain/ValidatorTimelockFactory";

import type { FacetCut } from "./diamondCut";
import { getCurrentFacetCutsForAdd } from "./diamondCut";

import { ERC20Factory } from "../typechain";
import { ERC20Factory, StateTransitionManagerFactory } from "../typechain";
import type { Contract, Overrides } from "@ethersproject/contracts";

let L2_BOOTLOADER_BYTECODE_HASH: string;
Expand Down Expand Up @@ -81,7 +82,7 @@ export class Deployer {
this.chainId = parseInt(process.env.CHAIN_ETH_ZKSYNC_NETWORK_ID!);
}

public async initialZkSyncHyperchainDiamondCut(extraFacets?: FacetCut[]) {
public async initialZkSyncHyperchainDiamondCut(extraFacets?: FacetCut[], compareDiamondCutHash: boolean = false) {
let facetCuts: FacetCut[] = Object.values(
await getCurrentFacetCutsForAdd(
this.addresses.StateTransition.AdminFacet,
Expand All @@ -99,7 +100,7 @@ export class Deployer {
};
const priorityTxMaxGasLimit = getNumberFromEnv("CONTRACTS_PRIORITY_TX_MAX_GAS_LIMIT");

return compileInitialCutHash(
const diamondCut = compileInitialCutHash(
facetCuts,
verifierParams,
L2_BOOTLOADER_BYTECODE_HASH,
Expand All @@ -110,6 +111,25 @@ export class Deployer {
this.addresses.StateTransition.DiamondInit,
false
);

if (compareDiamondCutHash) {
const hash = ethers.utils.keccak256(
ethers.utils.defaultAbiCoder.encode([DIAMOND_CUT_DATA_ABI_STRING], [diamondCut])
);

console.log(`Diamond cut hash: ${hash}`);
const stm = StateTransitionManagerFactory.connect(
this.addresses.StateTransition.StateTransitionProxy,
this.deployWallet
);

const hashFromSTM = await stm.initialCutHash();
if (hash != hashFromSTM) {
throw new Error(`Has from STM ${hashFromSTM} does not match the computed hash ${hash}`);
}
}

return diamondCut;
}

public async deployCreate2Factory(ethTxOptions?: ethers.providers.TransactionRequest) {
Expand Down Expand Up @@ -685,6 +705,7 @@ export class Deployer {
validiumMode: boolean,
extraFacets?: FacetCut[],
gasPrice?: BigNumberish,
compareDiamondCutHash: boolean = false,
nonce?,
predefinedChainId?: string,
useGovernance: boolean = false
Expand All @@ -698,7 +719,7 @@ export class Deployer {

const inputChainId = predefinedChainId || getNumberFromEnv("CHAIN_ETH_ZKSYNC_NETWORK_ID");
const admin = process.env.CHAIN_ADMIN_ADDRESS || this.ownerAddress;
const diamondCutData = await this.initialZkSyncHyperchainDiamondCut(extraFacets);
const diamondCutData = await this.initialZkSyncHyperchainDiamondCut(extraFacets, compareDiamondCutHash);
const initialDiamondCut = new ethers.utils.AbiCoder().encode([DIAMOND_CUT_DATA_ABI_STRING], [diamondCutData]);

const receipt = await this.executeDirectOrGovernance(
Expand Down Expand Up @@ -751,7 +772,8 @@ export class Deployer {
console.log(`CONTRACTS_DIAMOND_PROXY_ADDR=${diamondProxyAddress}`);
}
}
this.chainId = parseInt(chainId, 16);
const intChainId = parseInt(chainId, 16);
this.chainId = intChainId;

const validatorOneAddress = getAddressFromEnv("ETH_SENDER_SENDER_OPERATOR_COMMIT_ETH_ADDR");
const validatorTwoAddress = getAddressFromEnv("ETH_SENDER_SENDER_OPERATOR_BLOBS_ETH_ADDR");
Expand Down Expand Up @@ -783,18 +805,25 @@ export class Deployer {
}

const diamondProxy = this.stateTransitionContract(this.deployWallet);
const tx4 = await diamondProxy.setTokenMultiplier(1, 1);
const receipt4 = await tx4.wait();
if (this.verbose) {
console.log(`BaseTokenMultiplier set, gas used: ${receipt4.gasUsed.toString()}`);
}

if (validiumMode) {
const tx5 = await diamondProxy.setPubdataPricingMode(PubdataPricingMode.Validium);
const receipt5 = await tx5.wait();
// if we are using governance, the deployer will not be the admin, so we can't call the diamond proxy directly
if (admin == this.deployWallet.address) {
const tx4 = await diamondProxy.setTokenMultiplier(1, 1);
const receipt4 = await tx4.wait();
if (this.verbose) {
console.log(`Validium mode set, gas used: ${receipt5.gasUsed.toString()}`);
console.log(`BaseTokenMultiplier set, gas used: ${receipt4.gasUsed.toString()}`);
}

if (validiumMode) {
const tx5 = await diamondProxy.setPubdataPricingMode(PubdataPricingMode.Validium);
const receipt5 = await tx5.wait();
if (this.verbose) {
console.log(`Validium mode set, gas used: ${receipt5.gasUsed.toString()}`);
}
}
} else {
console.warn(
"BaseTokenMultiplier and Validium mode can't be set through the governance, please set it separately, using the admin account"
);
}
}

Expand Down

0 comments on commit ab2f13d

Please sign in to comment.