Skip to content

Commit

Permalink
feat(upgrade): update tool to generate calldata for setting new chain…
Browse files Browse the repository at this point in the history
… creation params (#3117)

## What ❔

<!-- What are the changes this PR brings about? -->
<!-- Example: This PR adds a PR template to the repo. -->
<!-- (For bigger PRs adding more context is appreciated) -->

## Why ❔

<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zkstack dev fmt` and `zkstack dev
lint`.
  • Loading branch information
koloz193 authored Oct 16, 2024
1 parent 30ddb29 commit 899ffc0
Showing 1 changed file with 56 additions and 2 deletions.
58 changes: 56 additions & 2 deletions infrastructure/protocol-upgrade/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,26 @@ import * as path from 'path';
const testConfigPath = path.join(process.env.ZKSYNC_HOME as string, `etc/test_config/constant`);
const ethTestConfig = JSON.parse(fs.readFileSync(`${testConfigPath}/eth.json`, { encoding: 'utf-8' }));

export enum Action {
Add = 0,
Replace = 1,
Remove = 2
}

export interface DiamondCutData {
facetCuts: FacetCut[];
initAddress: string;
initCalldata: string;
}

export interface ChainCreationParams {
genesisUpgrade: string;
genesisBatchHash: string;
genesisIndexRepeatedStorageChanges: number;
genesisBatchCommitment: string;
diamondCut: DiamondCutData;
}

export interface ForceDeployment {
// The bytecode hash to put on an address
bytecodeHash: BytesLike;
Expand Down Expand Up @@ -186,6 +200,10 @@ export function prepareUpgradeCalldata(
upgradeAddress: string,
facetCuts: FacetCut[],
zksyncAddress: string,
genesisUpgradeAddress: string,
genesisBatchHash: string,
genesisIndexRepeatedStorageChanges: number,
genesisBatchCommitment: string,
prepareDirectOperation?: boolean,
chainId?: string
) {
Expand All @@ -194,6 +212,21 @@ export function prepareUpgradeCalldata(
initAddress: upgradeAddress,
initCalldata
};

let chainCreationDiamondCut: DiamondCutData = {
facetCuts: facetCuts.filter((cut) => cut.action == Action.Add),
initAddress: genesisUpgradeAddress,
initCalldata: '0x'
};

let chainCreationParams: ChainCreationParams = {
genesisUpgrade: genesisUpgradeAddress,
genesisBatchHash,
genesisIndexRepeatedStorageChanges,
genesisBatchCommitment,
diamondCut: chainCreationDiamondCut
};

// Prepare calldata for STM
let stm = new StateTransitionManagerFactory();
const stmUpgradeCalldata = stm.interface.encodeFunctionData('setNewVersionUpgrade', [
Expand All @@ -203,6 +236,10 @@ export function prepareUpgradeCalldata(
newProtocolVersion
]);

const stmSetChainCreationCalldata = stm.interface.encodeFunctionData('setChainCreationParams', [
chainCreationParams
]);

// Prepare calldata for upgrading diamond proxy
let adminFacet = new AdminFacetFactory();
const diamondProxyUpgradeCalldata = adminFacet.interface.encodeFunctionData('upgradeChainFromVersion', [
Expand All @@ -215,7 +252,8 @@ export function prepareUpgradeCalldata(
let result: any = {
stmUpgradeCalldata,
chainAdminUpgradeCalldata,
diamondCut
diamondCut,
stmSetChainCreationCalldata
};

if (prepareDirectOperation) {
Expand All @@ -242,6 +280,10 @@ export function buildDefaultUpgradeTx(
upgradeTimestamp,
zksyncAddress,
postUpgradeCalldataFlag,
genesisUpgradeAddress,
genesisBatchHash,
genesisIndexRepeatedStorageChanges,
genesisBatchCommitment,
prepareDirectOperation?,
chainId?
) {
Expand Down Expand Up @@ -329,6 +371,10 @@ export function buildDefaultUpgradeTx(
upgradeAddress,
facetCuts,
zksyncAddress,
genesisUpgradeAddress,
genesisBatchHash,
genesisIndexRepeatedStorageChanges,
genesisBatchCommitment,
prepareDirectOperation,
chainId
);
Expand Down Expand Up @@ -376,7 +422,11 @@ command
.option('--zksync-address <zksyncAddress>')
.option('--chain-id <chainId>')
.option('--prepare-direct-operation <prepareDirectOperation>')
.option('--post-upgrade-calldata')
.option('--post-upgrade-calldata <postUpgradeCalldata>')
.option('--genesis-upgrade-address <genesisUpgradeAddress>')
.option('--genesis-batch-hash <genesisBatchHash>')
.option('--genesis-index-repeated-storage-changes <genesisIndexRepeatedStorageChanges>')
.option('--genesis-batch-commitment <genesisBatchCommitment>')
.action(async (options) => {
buildDefaultUpgradeTx(
options.environment,
Expand All @@ -386,6 +436,10 @@ command
options.upgradeTimestamp,
options.zksyncAddress,
options.postUpgradeCalldata,
options.genesisUpgradeAddress,
options.genesisBatchHash,
options.genesisIndexRepeatedStorageChanges,
options.genesisBatchCommitment,
options.prepareDirectOperation,
options.chainId
);
Expand Down

0 comments on commit 899ffc0

Please sign in to comment.