Skip to content

Commit

Permalink
feat: switch to the default codegen with zksolc (#1062)
Browse files Browse the repository at this point in the history
* feat: switch to the default codegen with zksync solc

* fix: change input parametrs name and add validation

* fix: change tests and add change input parameters

* fix: add new settings params to the CI

* fix: revert new settings params at the CI

* fix: set detect missing libraries to the false as default

* fix: update examples hardhat config files

* fix: update tests with new solc required param

* fix: restore hardhat config

* fix: set viaEVMAssembly to true by default for solidity compiler version < 0.8

* chore: change readme file

* chore: change readme file

* fix: rename isSystem flag, add default eraVersion when evmla codegen is in use, change readme files and add debug output

* fix: change isSystem inside tests

* fix: change ci solidity version to 0.6.12

* fix: ignore windows runner for 0.4.22

* fix: update readme files with more info

* fix: change compiler parameters so they don't need specified codegen

* fix: change compiler parameters so they don't need specified codegen

* fix: run lint

* fix: update compiler era version update

* Merged 'main' into 'kiriayga-txfusion-zksolc-codegen'

* fix: update verify plugin to support zksolc with breakable changes

* fix: search blocks from latest-100

* fix: update hardhat-verify dependency

* fix: update constants and readme file

* fix: provide isSystem and forceEmvla to the be for older versions in the tests

* fix: revert unused changes

---------

Co-authored-by: Marko Arambasic <makiarambasic@gmail.com>
  • Loading branch information
kiriyaga-txfusion and kiriyaga authored Jun 19, 2024
1 parent c0703c7 commit 5ec997a
Show file tree
Hide file tree
Showing 42 changed files with 678 additions and 334 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ jobs:
solc: [0.8.16, 0.7.6, 0.4.22]
node: ["18"]
os: [ubuntu, macos, windows]
exclude:
- solc: 0.4.22
os: windows

runs-on: ${{ matrix.os }}-latest

Expand Down
2 changes: 1 addition & 1 deletion examples/basic-example/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const config: HardhatUserConfig = {
zksolc: {
compilerSource: 'binary',
settings: {
isSystem: true,
enableEraVMExtensions: true,
optimizer: {
enabled: true,
},
Expand Down
2 changes: 1 addition & 1 deletion examples/deploy-example/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const config: HardhatUserConfig = {
zksolc: {
compilerSource: 'binary',
settings: {
isSystem: true,
enableEraVMExtensions: true,
optimizer: {
enabled: true,
},
Expand Down
2 changes: 1 addition & 1 deletion examples/node-example/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const config: HardhatUserConfig = {
zksolc: {
compilerSource: 'binary',
settings: {
isSystem: true,
enableEraVMExtensions: true,
optimizer: {
enabled: true,
},
Expand Down
3 changes: 2 additions & 1 deletion examples/verify-example/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import '@matterlabs/hardhat-zksync-solc';
import '@matterlabs/hardhat-zksync-verify';
import '@matterlabs/hardhat-zksync-ethers';
import '@matterlabs/hardhat-zksync-deploy';
import '@matterlabs/hardhat-zksync-upgradable';
import '@matterlabs/hardhat-zksync-verify/dist/src/type-extensions'
import '@matterlabs/hardhat-zksync-ethers/dist/type-extensions'
Expand All @@ -12,7 +13,7 @@ const config: HardhatUserConfig = {
compilerSource: 'binary',
version: 'latest',
settings: {
isSystem: true,
enableEraVMExtensions: true,
optimizer: {
enabled: true,
},
Expand Down
2 changes: 1 addition & 1 deletion examples/zksync-ethers-example/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const config: HardhatUserConfig = {
zksolc: {
compilerSource: 'binary',
settings: {
isSystem: true,
enableEraVMExtensions: true,
optimizer: {
enabled: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { ETH_NETWORK_RPC_URL, ZKSYNC_NETWORK_NAME, ZKSYNC_NETWORK_RPC_URL } from
import '@matterlabs/hardhat-zksync-solc';

const config: HardhatUserConfig = {
zksolc: {},
zksolc: {
settings: {},
},
solidity: {
version: '0.8.17',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const config: HardhatUserConfig = {
zksolc: {
compilerSource: 'binary',
settings: {
isSystem: true,
enableEraVMExtensions: true,
optimizer: {
enabled: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const config: HardhatUserConfig = {
paths: {
deployPaths: ['./deploy-scripts', 'dependent-scripts'],
},
zksolc: {},
zksolc: {
settings: {},
},
defaultNetwork: 'zkSyncNetwork',
networks: {
ethNetwork: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const config: HardhatUserConfig = {
paths: {
deployPaths: './deploy-scripts',
},
zksolc: {},
zksolc: {
settings: {},
},
defaultNetwork: 'zkSyncNetwork',
networks: {
ethNetwork: {
Expand Down
1 change: 1 addition & 0 deletions packages/hardhat-zksync-node/test/common.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { HardhatUserConfig } from 'hardhat/config';
const config: HardhatUserConfig = {
zksolc: {
compilerSource: 'binary',
settings: {},
},
networks: {
hardhat: {
Expand Down
16 changes: 11 additions & 5 deletions packages/hardhat-zksync-solc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ zksolc: {
compilerPath: "zksolc", // optional. Ignored for compilerSource "docker". Can be used if compiler is located in a specific folder
libraries:{}, // optional. References to non-inlinable libraries
missingLibrariesPath: "./.zksolc-libraries-cache/missingLibraryDependencies.json" // optional. This path serves as a cache that stores all the libraries that are missing or have dependencies on other libraries. A `hardhat-zksync-deploy` plugin uses this cache later to compile and deploy the libraries, especially when the `deploy-zksync:libraries` task is executed
isSystem: false, // optional. Enables Yul instructions available only for zkSync system contracts and libraries
forceEvmla: false, // optional. Falls back to EVM legacy assembly if there is a bug with Yul
enableEraVMExtensions: false, // optional. Enables Yul instructions available only for zkSync system contracts and libraries. In the older versions of the plugin known as 'isSystem' flag
forceEVMLA: false, // Compile with EVM legacy assembly codegen. In the older versions of the plugin known as a 'forceEvmla' flag
optimizer: {
enabled: true, // optional. True by default
mode: '3' // optional. 3 by default, z to optimize bytecode size
Expand All @@ -43,22 +43,28 @@ zksolc: {
}
},
```

The `isSystema` and `forceEvmla` arguments are deprecated in favor of `enableEraVMExtensions` and `forceEVMLA`. If the deprecated arguments are used, a warning will be displayed and they will be automatically switched to the new naming with the provided values.

Starting from zksolc version 1.5.0, the zkSync Era Solidity compiler will be used by default with the latest version if not specified in hardhat.config.ts


| 🔧 Properties | 📄 Description |
|-----------------------------|----------------------------------------------------------------------------------------------------------------------|
| version | zksolc compiler version. |
| compilerSource | Indicates the compiler source and can be either binary (default) or docker (deprecated). |
| compilerPath | (optional) field with the path to the zksolc binary. By default, the binary in $PATH is used |
| libraries | If your contract uses non-inlinable libraries as dependencies, they have to be defined here. |
| missingLibrariesPath | (optional) serves as a cache that stores all the libraries that are missing or have dependencies on other libraries. |
| isSystem | Required if contracts use enables Yul instructions available only for zkSync system contracts and libraries |
| forceEvmla | Falls back to EVM legacy assembly if there is an issue with the Yul IR compilation pipeline. |
| enableEraVMExtensions | Required if contracts use enables Yul instructions available only for zkSync system contracts and libraries. In the older versions of the plugin known as 'isSystem' flag |
| forceEVMLA | Compile with EVM legacy assembly codegen. If the zksolc version is below 1.5.0, this argument will act as a 'forceEvmla' flag in the older versions of the plugin, attempting to fallback to EVM legacy assembly if there is a bug with Yul. |
| optimizer | Compiler optimizations (enabled: true (default) or false), mode: 3 (default), fallback_to_optimizing_for_size: false (default) recommended for most projects. |
| metadata | Metadata settings. If the option is omitted, the metadata hash appends by default: bytecodeHash. Can only be none. |
| dockerImage | (deprecated) option used to identify the name of the compiler docker image. |

Learn more about [compiling libraries here](https://era.zksync.io/docs/tools/hardhat/compiling-libraries.html)

Setting the forceEvmla field to true can have the following negative impacts:
Setting the forceEVMLA field to true can have the following negative impacts:

- No support for recursion.
- No support for internal function pointers.
Expand Down
20 changes: 16 additions & 4 deletions packages/hardhat-zksync-solc/src/compile/binary.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import { exec } from 'child_process';
import { ZkSolcConfig } from '../types';
import { isBreakableCompilerVersion } from '../utils';

export async function compileWithBinary(
input: any,
config: ZkSolcConfig,
solcPath: string,
detectMissingLibrariesMode: boolean = false,
): Promise<any> {
const { compilerPath, isSystem, forceEvmla } = config.settings;
const { compilerPath, debugOutputDir } = config.settings;

const processCommand = `${compilerPath} --standard-json ${isSystem ? '--system-mode' : ''} ${
forceEvmla ? '--force-evmla' : ''
} --solc ${solcPath} ${detectMissingLibrariesMode ? '--detect-missing-libraries' : ''}`;
let processCommand = `${compilerPath} --standard-json --solc ${solcPath} ${
debugOutputDir ? `--debug-output-dir ${debugOutputDir}` : ''
}`;

if (!isBreakableCompilerVersion(config.version)) {
const { enableEraVMExtensions, forceEVMLA } = config.settings;
processCommand += `${detectMissingLibrariesMode ? ' --detect-missing-libraries' : ''} ${
enableEraVMExtensions ? '--system-mode' : ''
} ${forceEVMLA ? '--force-evmla' : ''}`;
}

if (isBreakableCompilerVersion(config.version)) {
input.settings.detectMissingLibraries = detectMissingLibrariesMode;
}

const output: string = await new Promise((resolve, reject) => {
const process = exec(
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-zksync-solc/src/compile/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ export async function compileWithDocker(
zksolcConfig: ZkSolcConfig,
) {
const command = ['zksolc', '--standard-json'];
if (zksolcConfig.settings.isSystem) {
if (zksolcConfig.settings.enableEraVMExtensions) {
command.push('--system-mode');
}
if (zksolcConfig.settings.forceEvmla) {
if (zksolcConfig.settings.forceEVMLA) {
command.push('--force-evmla');
}

Expand Down
30 changes: 28 additions & 2 deletions packages/hardhat-zksync-solc/src/config-update.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { SolcConfig, SolcUserConfig } from 'hardhat/types';
import { COMPILERS_CONFLICT_ZKVM_SOLC } from './constants';
import { ZkSyncSolcPluginError } from './errors';
import { ZkSolcConfig } from './types';
import { isBreakableCompilerVersion } from './utils';

export interface SolcConfigData {
compiler: SolcConfig;
Expand All @@ -11,6 +13,8 @@ export interface SolcUserConfigUpdater {
suituble(_solcUserConfig: SolcUserConfig[] | Map<string, SolcUserConfig>, _file?: string): boolean;
update(
_compiler: SolcConfig,
_latestEraVersion: string,
_zksolc: ZkSolcConfig,
_solcUserConfig: SolcUserConfig[] | Map<string, SolcUserConfig>,
_file?: string,
): void;
Expand All @@ -21,11 +25,22 @@ export class OverrideCompilerSolcUserConfigUpdater implements SolcUserConfigUpda
return _solcUserConfig instanceof Map && _file !== undefined;
}

public update(_compiler: SolcConfig, _userConfigCompilers: Map<string, SolcUserConfig>, _file: string): void {
public update(
_compiler: SolcConfig,
_latestEraVersion: string,
_zksolc: ZkSolcConfig,
_userConfigCompilers: Map<string, SolcUserConfig>,
_file: string,
): void {
const compilerInfo = _userConfigCompilers.get(_file);

if (compilerInfo?.eraVersion) {
_compiler.eraVersion = compilerInfo.eraVersion;
return;
}

if (isBreakableCompilerVersion(_zksolc.version)) {
_compiler.eraVersion = _latestEraVersion;
}
}
}
Expand All @@ -35,7 +50,13 @@ export class CompilerSolcUserConfigUpdater implements SolcUserConfigUpdater {
return solcUserConfig instanceof Array && _file === undefined;
}

public update(_compiler: SolcConfig, _userConfigCompilers: SolcUserConfig[], _file?: string): void {
public update(
_compiler: SolcConfig,
_latestEraVersion: string,
_zksolc: ZkSolcConfig,
_userConfigCompilers: SolcUserConfig[],
_file?: string,
): void {
const compilerInfos = _userConfigCompilers.filter(
(userCompilerInfo) => userCompilerInfo.version === _compiler.version,
);
Expand All @@ -52,6 +73,11 @@ export class CompilerSolcUserConfigUpdater implements SolcUserConfigUpdater {

if (compilerInfo?.eraVersion) {
_compiler.eraVersion = compilerInfo.eraVersion;
return;
}

if (isBreakableCompilerVersion(_zksolc.version)) {
_compiler.eraVersion = _latestEraVersion;
}
}
}
16 changes: 14 additions & 2 deletions packages/hardhat-zksync-solc/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const DETECT_MISSING_LIBRARY_MODE_COMPILER_VERSION = '1.3.14';
// User agent of MacOSX Chrome 120.0.0.0
export const USER_AGENT =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
export const TASK_UPDATE_SOLIDITY_COMPILERS = 'compile:update-solidity-compilers';

export const ZKSOLC_COMPILER_PATH_VERSION = 'local_or_remote';

Expand Down Expand Up @@ -38,8 +39,12 @@ export const ZKSOLC_BIN_REPOSITORY_NAME = 'zksolc-bin';
export const ZKVM_SOLC_BIN_REPOSITORY_NAME = 'era-solidity';
export const ZKVM_SOLC_COMPILER_VERSION_MIN_VERSION = '1.0.0';

export const ZKSOLC_COMPILER_MIN_VERSION_BREAKABLE_CHANGE = '1.5.0';

export const ZKSOLC_COMPILER_VERSION_MIN_VERSION_WITH_ZKVM_COMPILER = '1.3.22';

export const ZKVM_SOLC_DEFAULT_COMPILER_VERSION = '1.0.0';

export const DEFAULT_COMPILER_VERSION_INFO_CACHE_PERIOD = 24 * 60 * 60 * 1000; // 24 hours

export const COMPILER_VERSION_INFO_FILE_NOT_FOUND_ERROR = 'Could not find zksolc compiler version info file.';
Expand All @@ -57,14 +62,21 @@ export const COMPILING_INFO_MESSAGE_ZKVM_SOLC = (zksolcVersion: string, zkvmSolc
`Compiling contracts for zkSync Era with zksolc v${zksolcVersion} and zkvm-solc v${zkvmSolcVersion}`;
export const COMPILER_BINARY_CORRUPTION_ERROR_ZKVM_SOLC = (compilerPath: string) =>
`The zkvm-solc binary at path ${compilerPath} is corrupted. Please delete it and try again.`;
export const COMPILER_ZKSOLC_VERSION_WITH_ZKVM_SOLC_ERROR = `zkVm (eraVersion) compiler is supported only with usage of zksolc version >= ${ZKSOLC_COMPILER_VERSION_MIN_VERSION_WITH_ZKVM_COMPILER}.`;
export const COMPILER_ZKSOLC_VERSION_WITH_ZKVM_SOLC_WARN = `zkVm (eraVersion) compiler is supported only with usage of zksolc version >= ${ZKSOLC_COMPILER_VERSION_MIN_VERSION_WITH_ZKVM_COMPILER}. Switching by default to the native solc compiler.`;

export const COMPILER_ZKSOLC_NEED_EVM_CODEGEN = `Yul codegen is only supported for solc >= 0.8. Flag forceEVMLA will automatically be set to true by default.`;

export const COMPILER_ZKSOLC_IS_SYSTEM_USE = `isSystem flag is deprecated. Please use enableEraVMExtensions instead. Automatically switched to the new naming with the provided value.`;
export const COMPILER_ZKSOLC_FORCE_EVMLA_USE = `forceEvmla flag is deprecated. Please use forceEVMLA instead. Automatically switched to the new naming with the provided value.`;

export const ZKVM_SOLC_COMPILER_NEEDS_ERA_VERSION = (eraVersion: string, solcVersion: string) =>
`Using zkSync edition of solc (zkSync Era Solidity Compiler) as default with version ${eraVersion} for solidity version ${solcVersion}.`;
export const COMPILERS_CONFLICT_ZKVM_SOLC = (version: string) =>
`Your Hardhat configuration has conflicting Solidity compiler versions for version ${version}. Specify either a compiler version with zkVm support (eraVersion) or one without it.`;
export const MISSING_LIBRARIES_NOTICE =
'zksolc compiler detected missing libraries! For more details, visit: https://era.zksync.io/docs/tools/hardhat/compiling-libraries.html.';
export const COMPILE_AND_DEPLOY_LIBRARIES_INSTRUCTIONS =
'To compile and deploy libraries, please run: `yarn hardhat deploy-zksync:libraries`';
'To compile and deploy libraries, please run: `hardhat deploy-zksync:libraries`';
export const MISSING_LIBRARY_LINK =
'For more details on how to use deploy-zksync:libraries task from hardhat-zksync-deploy plugin, visit: https://era.zksync.io/docs/tools/hardhat/hardhat-zksync-deploy.html.';

Expand Down
Loading

0 comments on commit 5ec997a

Please sign in to comment.