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

extend utils & update compilation process #396

Merged
merged 4 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion system-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"compile-zasm": "ts-node scripts/compile-zasm.ts",
"deploy-preimages": "ts-node scripts/deploy-preimages.ts",
"preprocess:bootloader": "rm -rf ./bootloader/build && yarn ts-node scripts/preprocess-bootloader.ts",
"preprocess:system-contracts": "rm -rf ./contracts-preprocessed && ts-node scripts/preprocess-system-contracts.ts",
"preprocess:system-contracts": "ts-node scripts/preprocess-system-contracts.ts",
"test": "yarn build:test-system-contracts && hardhat test --network zkSyncTestNode",
"test-node": "hardhat node-zksync --tag v0.0.1-vm1.5.0",
"test:bootloader": "cd bootloader/test_infra && cargo run"
Expand Down
44 changes: 38 additions & 6 deletions system-contracts/scripts/compile-yul.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
// hardhat import should be the first import in the file
import type { CompilerPaths } from "./utils";
import { spawn, compilerLocation, prepareCompilerPaths, getSolcLocation } from "./utils";
import {
spawn,
compilerLocation,
prepareCompilerPaths,
getSolcLocation,
needsRecompilation,
setCompilationTime,
} from "./utils";
import * as fs from "fs";
import { Command } from "commander";
import * as _path from "path";

const COMPILER_VERSION = "1.3.18";
const IS_COMPILER_PRE_RELEASE = true;
const CONTRACTS_DIR = "contracts-preprocessed";
const BOOTLOADER_DIR = "bootloader";
const TIMESTAMP_FILE_YUL = "last_compilation_yul.timestamp";
const TIMESTAMP_FILE_BOOTLOADER = "last_compilation_bootloader.timestamp";

export async function compileYul(paths: CompilerPaths, file: string) {
const solcCompilerPath = await getSolcLocation();
Expand All @@ -30,14 +42,34 @@ async function main() {
program.version("0.1.0").name("compile yul").description("publish preimages for the L2 contracts");

program.command("compile-bootloader").action(async () => {
await compileYulFolder("bootloader/build");
await compileYulFolder("bootloader/tests");
const timestampFilePath = _path.join(process.cwd(), TIMESTAMP_FILE_BOOTLOADER);
const folderToCheck = _path.join(process.cwd(), BOOTLOADER_DIR);

if (needsRecompilation(folderToCheck, timestampFilePath)) {
console.log("Compilation needed.");
await compileYulFolder("bootloader/build");
await compileYulFolder("bootloader/tests");
setCompilationTime(timestampFilePath);
} else {
console.log("Compilation not needed.");
return;
}
});

program.command("compile-precompiles").action(async () => {
await compileYulFolder("contracts-preprocessed");
await compileYulFolder("contracts-preprocessed/precompiles");
await compileYulFolder("contracts-preprocessed/precompiles/test-contracts");
const timestampFilePath = _path.join(process.cwd(), TIMESTAMP_FILE_YUL);
const folderToCheck = _path.join(process.cwd(), CONTRACTS_DIR);

if (needsRecompilation(folderToCheck, timestampFilePath)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Checking the folder, rather than specific subfolders, like "contracts-preprocessed/precompiles/test-contracts".
If necessary, we can check each of the compilation targets separately

console.log("Compilation needed.");
await compileYulFolder("contracts-preprocessed");
await compileYulFolder("contracts-preprocessed/precompiles");
await compileYulFolder("contracts-preprocessed/precompiles/test-contracts");
setCompilationTime(timestampFilePath);
} else {
console.log("Compilation not needed.");
return;
}
});

await program.parseAsync(process.argv);
Expand Down
14 changes: 14 additions & 0 deletions system-contracts/scripts/preprocess-system-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import path from "path";
import { renderFile } from "template-file";
import { glob } from "fast-glob";
import { Command } from "commander";
import { needsRecompilation, deleteDir, setCompilationTime, isFolderEmpty } from "./utils";

const CONTRACTS_DIR = "contracts";
const OUTPUT_DIR = "contracts-preprocessed";
const TIMESTAMP_FILE = "last_compilation_preprocessing.timestamp"; // File to store the last compilation time
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we need to add it to .gitignore?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done


const params = {
SYSTEM_CONTRACTS_OFFSET: "0x8000",
Expand All @@ -17,6 +19,18 @@ async function preprocess(testMode: boolean) {
params.SYSTEM_CONTRACTS_OFFSET = "0x9000";
}

const timestampFilePath = path.join(process.cwd(), TIMESTAMP_FILE);
const folderToCheck = path.join(process.cwd(), CONTRACTS_DIR);

if ((await isFolderEmpty(OUTPUT_DIR)) || needsRecompilation(folderToCheck, timestampFilePath)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

not sure if it will work if:

  1. I first compile in normal mode
    2.I try to run the tests (for this the recompilation in testMode is done.

Seems like on (2) the recompilation wont happen since no changes have been made to the contracts

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

console.log("Preprocessing needed.");
await deleteDir("./contracts-preprocessed");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe use OUTPUT_DIR constant here?

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

setCompilationTime(timestampFilePath);
} else {
console.log("Preprocessing not needed.");
return;
}

const contracts = await glob(
[`${CONTRACTS_DIR}/**/*.sol`, `${CONTRACTS_DIR}/**/*.yul`, `${CONTRACTS_DIR}/**/*.zasm`],
{ onlyFiles: true }
Expand Down
79 changes: 78 additions & 1 deletion system-contracts/scripts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import type { Deployer } from "@matterlabs/hardhat-zksync-deploy";
import type { BigNumberish, BytesLike } from "ethers";
import { BigNumber, ethers } from "ethers";
import * as fs from "fs";
import * as fsPr from "fs/promises";
import { hashBytecode } from "zksync-web3/build/src/utils";
import type { YulContractDescription, ZasmContractDescription } from "./constants";
import { Language, SYSTEM_CONTRACTS } from "./constants";
import { getCompilersDir } from "hardhat/internal/util/global-dir";
import path from "path";
import { spawn as _spawn } from "child_process";
import { spawn as _spawn, exec } from "child_process";
import { createHash } from "crypto";
import { CompilerDownloader } from "hardhat/internal/solidity/compiler/downloader";

Expand Down Expand Up @@ -253,3 +254,79 @@ export function prepareCompilerPaths(path: string): CompilerPaths {

return new CompilerPaths(absolutePathSources, absolutePathArtifacts);
}

// Get the latest file modification time in the watched folder
function getLatestModificationTime(folder: string): Date | null {
Copy link
Collaborator

Choose a reason for hiding this comment

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

add a comment on when it can return null

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

comment added below

const files = fs.readdirSync(folder);
let latestTime: Date | null = null;

files.forEach((file) => {
const filePath = path.join(folder, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
const dirLatestTime = getLatestModificationTime(filePath);
if (dirLatestTime && (!latestTime || dirLatestTime > latestTime)) {
latestTime = dirLatestTime;
}
} else if (stats.isFile()) {
if (!latestTime || stats.mtime > latestTime) {
latestTime = stats.mtime;
}
}
});

return latestTime;
}

// Read the last compilation timestamp from the file
function getLastCompilationTime(timestampFile: string): Date | null {
try {
if (fs.existsSync(timestampFile)) {
const timestamp = fs.readFileSync(timestampFile, "utf-8");
return new Date(parseInt(timestamp, 10));
}
} catch (error) {
const err = error as Error; // Cast `error` to `Error`
console.error(`Error reading timestamp: ${err.message}`);
}
return null;
}

// Write the current time to the timestamp file
export function setCompilationTime(timestampFile: string) {
fs.writeFileSync(timestampFile, Date.now().toString());
}

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

if (!latestModificationTime || !lastCompilationTime) {
return true; // If there's no history, always recompile
}

return latestModificationTime > lastCompilationTime;
}

export function deleteDir(path: string): Promise<void> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

rmdirSync?

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

return new Promise((resolve, reject) => {
exec(`rm -rf ${path}`, (error) => {
if (error) {
reject(error); // If an error occurs, reject the promise
} else {
resolve(); // If successful, resolve the promise
}
});
});
}

export async function isFolderEmpty(folderPath: string): Promise<boolean> {
try {
const files = await fsPr.readdir(folderPath); // Get a list of files in the folder
return files.length === 0; // If there are no files, the folder is empty
} catch (error) {
console.error("No target folder with artifacts.");
return true; // Return true if an error, as folder doesn't exist.
}
}
Loading