Skip to content

Commit

Permalink
fix: download zkvyper if needed before compilation (#1484)
Browse files Browse the repository at this point in the history
* fix: download zkvyper if needed before compilation

* fix: delete project metadata and extra data from the compilation output

---------

Co-authored-by: Marko Arambasic <makiarambasic@gmail.com>
  • Loading branch information
kiriyaga-txfusion and kiriyaga authored Oct 21, 2024
1 parent be43a83 commit 9763aba
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/hardhat-zksync-vyper/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const ZKVYPER_BIN_REPOSITORY = 'https://github.com/matter-labs/zkvyper-bi
export const DEFAULT_TIMEOUT_MILISECONDS = 30000;
export const TASK_COMPILE_VYPER_CHECK_ERRORS = 'compile:vyper:check-errors';
export const TASK_COMPILE_VYPER_LOG_COMPILATION_ERRORS = 'compile:vyper:log:compilation-errors';

export const TASK_DOWNLOAD_ZKVYPER = 'compile:zkvyper:download';
export const ZKVYPER_COMPILER_PATH_VERSION = 'local_or_remote';

// User agent of MacOSX Chrome 120.0.0.0
Expand Down
56 changes: 38 additions & 18 deletions packages/hardhat-zksync-vyper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
TASK_COMPILE_VYPER_GET_BUILD,
TASK_COMPILE_VYPER_LOG_COMPILATION_RESULT,
TASK_COMPILE_VYPER_LOG_DOWNLOAD_COMPILER_START,
TASK_COMPILE_VYPER,
} from '@nomiclabs/hardhat-vyper/dist/src/task-names';
import {
TASK_COMPILE_SOLIDITY_LOG_COMPILATION_RESULT,
Expand All @@ -13,7 +14,7 @@ import { getCompilersDir } from 'hardhat/internal/util/global-dir';
import { Mutex } from 'hardhat/internal/vendor/await-semaphore';
import './type-extensions';
import chalk from 'chalk';
import { CompilationJob } from 'hardhat/types';
import { CompilationJob, TaskArguments } from 'hardhat/types';
import { PROXY_NAME, ProxtContractOutput, ZkArtifacts, proxyNames } from './artifacts';
import { compile } from './compile';
import { checkSupportedVyperVersions, pluralize } from './utils';
Expand All @@ -22,10 +23,13 @@ import {
defaultZkVyperConfig,
TASK_COMPILE_VYPER_CHECK_ERRORS,
TASK_COMPILE_VYPER_LOG_COMPILATION_ERRORS,
TASK_DOWNLOAD_ZKVYPER,
ZKVYPER_COMPILER_PATH_VERSION,
} from './constants';
import { ZkVyperCompilerDownloader } from './compile/downloader';

const zkVyperCompilerDownloaderMutex = new Mutex();

extendConfig((config, userConfig) => {
defaultZkVyperConfig.version = userConfig.zkvyper?.settings?.compilerPath
? ZKVYPER_COMPILER_PATH_VERSION
Expand Down Expand Up @@ -61,6 +65,37 @@ extendEnvironment((hre) => {
}
});

subtask(TASK_COMPILE_VYPER).setAction(async (args: TaskArguments, hre, runSuper) => {
if (hre.network.zksync) {
await hre.run(TASK_DOWNLOAD_ZKVYPER);
}

await runSuper(args);
});

subtask(TASK_DOWNLOAD_ZKVYPER, async (_args, hre) => {
if (!hre.network.zksync) {
return;
}

const compilersCache = await getCompilersDir();

await zkVyperCompilerDownloaderMutex.use(async () => {
const zkvyperDownloader = await ZkVyperCompilerDownloader.getDownloaderWithVersionValidated(
hre.config.zkvyper.version,
hre.config.zkvyper.settings.compilerPath ?? '',
compilersCache,
);

const isZksolcDownloaded = await zkvyperDownloader.isCompilerDownloaded();
if (!isZksolcDownloaded) {
await zkvyperDownloader.downloadCompiler();
}
hre.config.zkvyper.settings.compilerPath = zkvyperDownloader.getCompilerPath();
hre.config.zkvyper.version = zkvyperDownloader.getVersion();
});
});

// If there're no .sol files to compile - that's ok.
subtask(TASK_COMPILE_SOLIDITY_LOG_NOTHING_TO_COMPILE, async () => {});

Expand All @@ -81,6 +116,8 @@ subtask(TASK_COMPILE_VYPER_RUN_BINARY, async (args: { inputPaths: string[]; vype

delete compilerOutput.zk_version;
delete compilerOutput.long_version;
delete compilerOutput.project_metadata;
delete compilerOutput.extra_data;

proxyNames.forEach((proxyName) => {
if (compilerOutput[proxyName]) {
Expand Down Expand Up @@ -117,23 +154,6 @@ subtask(TASK_COMPILE_VYPER_GET_BUILD, async (args: { vyperVersion: string }, hre
}

const vyperBuild = await runSuper(args);
const compilersCache = await getCompilersDir();

const mutex = new Mutex();
await mutex.use(async () => {
const zkvyperDownloader = await ZkVyperCompilerDownloader.getDownloaderWithVersionValidated(
hre.config.zkvyper.version,
hre.config.zkvyper.settings.compilerPath ?? '',
compilersCache,
);

const isZksolcDownloaded = await zkvyperDownloader.isCompilerDownloaded();
if (!isZksolcDownloaded) {
await zkvyperDownloader.downloadCompiler();
}
hre.config.zkvyper.settings.compilerPath = zkvyperDownloader.getCompilerPath();
hre.config.zkvyper.version = zkvyperDownloader.getVersion();
});

console.info(chalk.yellow(COMPILING_INFO_MESSAGE(hre.config.zkvyper.version, args.vyperVersion)));

Expand Down

0 comments on commit 9763aba

Please sign in to comment.