Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve zk init perfromance #1774

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Conversation

Raid5594
Copy link
Collaborator

@Raid5594 Raid5594 commented Apr 23, 2024

What ❔

Avoiding unnecessary compilation of smart contracts
Fixes EVM-551

Why ❔

Hardhat cache is not working as expected or scripts are being use for compilation

Checklist

  • 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 zk fmt and zk lint.
  • Spellcheck has been run via zk spellcheck.
  • Linkcheck has been run via zk linkcheck.

@Raid5594 Raid5594 changed the title Kl factory optimise init feat: improve zk init perfromance Apr 23, 2024
@Raid5594 Raid5594 requested a review from kelemeno April 23, 2024 15:42

const CONTRACTS_DIR = 'contracts';
const OUTPUT_DIR = 'artifacts-zk';
const TIMESTAMP_FILE = 'last_compilation.timestamp'; // File to store the last compilation time
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think is used in multiple places, maybe you can have them in utils and import them?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it follows the same logic as the lines above, then we'd want to put CONTRACTS_DIR and OUTPUT_DIR in utils as well

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, very good idea

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@kelemeno
Copy link
Contributor

Also kl-factory has been merged, please rebase on and merge main

@@ -46,20 +45,7 @@ export async function externalNode(reinit: boolean = false, args: string[]) {

async function create_genesis(cmd: string) {
await utils.confirmAction();
await utils.spawn(`${cmd} | tee genesis.log`);

const date = new Date();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not keep this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a discussion where it seems that we're not using these logs anymore

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mm-zk is it fine if we remove these log from here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK yes. @Deniallugo - FYI.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think we need this function at all

@Raid5594 Raid5594 changed the base branch from kl-factory to main April 23, 2024 16:57
@Raid5594 Raid5594 requested a review from a team as a code owner April 23, 2024 16:57
@Raid5594 Raid5594 changed the base branch from main to kl-factory April 23, 2024 17:08
@Raid5594 Raid5594 changed the base branch from kl-factory to main April 23, 2024 17:12
@Raid5594 Raid5594 force-pushed the kl-factory-optimise-init branch 2 times, most recently from 1aebd66 to 7f0d1e3 Compare April 24, 2024 11:35
@Raid5594 Raid5594 requested review from mm-zk and kelemeno April 24, 2024 12:16
@@ -46,20 +45,7 @@ export async function externalNode(reinit: boolean = false, args: string[]) {

async function create_genesis(cmd: string) {
await utils.confirmAction();
await utils.spawn(`${cmd} | tee genesis.log`);

const date = new Date();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK yes. @Deniallugo - FYI.

TIMESTAMP_FILE
} from './utils';

async function main() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add comment - describing what we're doing here (using some .timestamp file to record the last time that files were changed)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems that it was not done.
I'm looking for a 2-line comment, where reader can quickly see what's special about this 'build' script, without having to read the details of the code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's the right move.
We could solve in two ways:

  1. Move l2 contracts compilation to foundry zksync
  2. Use the prebuild contracts from the repo. we do pulbish built contracts

// Determine if recompilation is needed
export function needsRecompilation(folder: string, timestampFile: string): boolean {
const lastCompilationTime = getLastCompilationTime(timestampFile);
const latestModificationTime = getLatestModificationTime(folder);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do these sources have any dependencies on outside dirs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Raid5594 Raid5594 requested a review from mm-zk April 24, 2024 16:01
@@ -9,8 +9,8 @@
"fee-test": "RUN_FEE_TEST=1 zk f jest -- fees.test.ts",
"api-test": "zk f jest -- api/web3.test.ts api/debug.test.ts",
"contract-verification-test": "zk f jest -- api/contract-verification.test.ts",
"build": "hardhat compile",
"build-yul": "hardhat run scripts/compile-yul.ts"
"build": "yarn ts-node scripts/build.ts",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: do you actually have to say 'yarn' here? I thought that "ts-node XX" should be enough.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both will work, however 'yarn ts-node' is a safer choice in monorepo environment as it ensure execution in the context of yarn workspace.

TIMESTAMP_FILE
} from './utils';

async function main() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems that it was not done.
I'm looking for a 2-line comment, where reader can quickly see what's special about this 'build' script, without having to read the details of the code.

@@ -0,0 +1,159 @@
import * as fs from 'fs';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are repeating the same file twice, that's not good, as it will be harder to manage and update in the future.

Could it be some shared library instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch to sync call, so it's 1 library

import { exec } from 'child_process';

async function main() {
const timestampFilePath = path.join(process.cwd(), TIMESTAMP_FILE); // File stores the timestamp of last compilation
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are repeating this twice (as it already exists in the other build.rs) - maybe move it to a shared util instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, I was thinking of it, but as these are different workspaces, I was not sure if a shared util is a good idea

@Raid5594 Raid5594 requested a review from mm-zk April 25, 2024 09:11
@EmilLuta
Copy link
Contributor

EmilLuta commented May 3, 2024

Folks, what's the status of this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants