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(toolbox): add format and clippy to zk_toolbox ci #2100

Merged
merged 4 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 11 additions & 5 deletions infrastructure/zk/src/fmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,22 @@ async function prettierContracts(check: boolean) {
}

export async function rustfmt(check: boolean = false) {
process.chdir(process.env.ZKSYNC_HOME as string);

// We rely on a supposedly undocumented bug/feature of `rustfmt` that allows us to use unstable features on stable Rust.
// Please note that this only works with CLI flags, and if you happened to visit this place after things suddenly stopped working,
// it is certainly possible that the feature was deemed a bug and was fixed. Then welp.
const config = '--config imports_granularity=Crate --config group_imports=StdExternalCrate';
const command = check ? `cargo fmt -- --check ${config}` : `cargo fmt -- ${config}`;
await utils.spawn(command);
process.chdir('./prover');
await utils.spawn(command);

const dirs = [
process.env.ZKSYNC_HOME as string,
`${process.env.ZKSYNC_HOME}/prover`,
`${process.env.ZKSYNC_HOME}/zk_toolbox`
];

for (const dir of dirs) {
process.chdir(dir);
await utils.spawn(command);
}
}

export async function runAllRustFormatters(check: boolean = false) {
Expand Down
22 changes: 16 additions & 6 deletions infrastructure/zk/src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import * as utils from './utils';

// Note that `rust` is not noted here, as clippy isn't run via `yarn`.
// `rust` option is still supported though.
const LINT_COMMANDS: Record<string, string> = {
const LINT_COMMANDS = {
md: 'markdownlint',
sol: 'solhint',
js: 'eslint',
ts: 'eslint --ext ts'
};
const EXTENSIONS = Object.keys(LINT_COMMANDS);
const EXTENSIONS = Object.keys(LINT_COMMANDS) as (keyof typeof LINT_COMMANDS)[];
const CONFIG_PATH = 'etc/lint-config';

export async function lint(extension: string, check: boolean = false) {
export async function lint(extension: keyof typeof LINT_COMMANDS, check: boolean = false) {
if (!EXTENSIONS.includes(extension)) {
throw new Error('Unsupported extension');
}
Expand All @@ -34,17 +34,22 @@ async function clippy() {
}

async function proverClippy() {
process.chdir(process.env.ZKSYNC_HOME! + '/prover');
process.chdir(`${process.env.ZKSYNC_HOME}/prover`);
await utils.spawn('cargo clippy --tests --locked -- -D warnings -A incomplete_features');
}

const ARGS = [...EXTENSIONS, 'rust', 'prover', 'contracts'];
async function toolboxClippy() {
process.chdir(`${process.env.ZKSYNC_HOME}/zk_toolbox`);
await utils.spawn('cargo clippy --tests --locked -- -D warnings');
}

const ARGS = [...EXTENSIONS, 'rust', 'prover', 'contracts', 'toolbox'] as const;

export const command = new Command('lint')
.description('lint code')
.option('--check')
.arguments(`[extension] ${ARGS.join('|')}`)
.action(async (extension: string | null, cmd: Command) => {
.action(async (extension: (typeof ARGS)[number] | null, cmd: Command) => {
if (extension) {
switch (extension) {
case 'rust':
Expand All @@ -56,13 +61,18 @@ export const command = new Command('lint')
case 'contracts':
await lintContracts(cmd.check);
break;
case 'toolbox':
await toolboxClippy();
break;
default:
await lint(extension, cmd.check);
}
} else {
const promises = EXTENSIONS.map((ext) => lint(ext, cmd.check));
promises.push(lintContracts(cmd.check));
promises.push(clippy());
promises.push(proverClippy());
promises.push(toolboxClippy());
await Promise.all(promises);
}
});
3 changes: 2 additions & 1 deletion zk_toolbox/crates/common/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::{collections::HashMap, path::PathBuf};

use crate::{config::global_config, logger};
use sqlx::{
migrate::{Migrate, MigrateError, Migrator},
Connection, PgConnection,
};
use url::Url;
use xshell::Shell;

use crate::{config::global_config, logger};

pub async fn init_db(db_url: &Url, name: &str) -> anyhow::Result<()> {
// Connect to the database.
let mut connection = PgConnection::connect(db_url.as_ref()).await?;
Expand Down
3 changes: 2 additions & 1 deletion zk_toolbox/crates/common/src/docker.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::cmd::Cmd;
use xshell::{cmd, Shell};

use crate::cmd::Cmd;

pub fn up(shell: &Shell, docker_compose_file: &str) -> anyhow::Result<()> {
Cmd::new(cmd!(shell, "docker-compose -f {docker_compose_file} up -d")).run()
}
Expand Down
3 changes: 2 additions & 1 deletion zk_toolbox/crates/common/src/prerequisites.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{cmd::Cmd, logger};
use xshell::{cmd, Shell};

use crate::{cmd::Cmd, logger};

const PREREQUISITES: [Prerequisite; 6] = [
Prerequisite {
name: "git",
Expand Down
3 changes: 2 additions & 1 deletion zk_toolbox/crates/common/src/prompt/confirm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cliclack::Confirm;
use std::fmt::Display;

use cliclack::Confirm;

pub struct PromptConfirm {
inner: Confirm,
}
Expand Down
3 changes: 2 additions & 1 deletion zk_toolbox/crates/common/src/prompt/input.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cliclack::{Input, Validate};
use std::str::FromStr;

use cliclack::{Input, Validate};

pub struct Prompt {
inner: Input,
}
Expand Down
7 changes: 3 additions & 4 deletions zk_toolbox/crates/zk_inception/src/accept_ownership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ use common::{
forge::{Forge, ForgeScript, ForgeScriptArgs},
spinner::Spinner,
};
use xshell::Shell;

use crate::forge_utils::check_the_balance;
use crate::forge_utils::fill_forge_private_key;
use config::{
forge_interface::{
accept_ownership::AcceptOwnershipInput, script_params::ACCEPT_GOVERNANCE_SCRIPT_PARAMS,
Expand All @@ -14,6 +10,9 @@ use config::{
EcosystemConfig,
};
use ethers::types::{Address, H256};
use xshell::Shell;

use crate::forge_utils::{check_the_balance, fill_forge_private_key};

pub async fn accept_admin(
shell: &Shell,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use std::{path::PathBuf, str::FromStr};

use clap::Parser;
use common::{slugify, Prompt, PromptConfirm, PromptSelect};
use ethers::types::Address;
use serde::{Deserialize, Serialize};
use strum::IntoEnumIterator;
use strum_macros::{Display, EnumIter};
use types::{BaseToken, L1BatchCommitDataGeneratorMode, ProverMode, WalletCreation};

use crate::defaults::L2_CHAIN_ID;
use ethers::types::Address;
use types::{BaseToken, L1BatchCommitDataGeneratorMode, ProverMode, WalletCreation};

#[derive(Debug, Serialize, Deserialize, Parser)]
pub struct ChainCreateArgs {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use clap::Parser;
use common::{slugify, Prompt};
use config::{ChainConfig, DatabaseConfig, DatabasesConfig};
use serde::{Deserialize, Serialize};
use url::Url;

use crate::defaults::{generate_db_names, DBNames, DATABASE_PROVER_URL, DATABASE_SERVER_URL};
use config::{ChainConfig, DatabaseConfig, DatabasesConfig};

#[derive(Debug, Clone, Serialize, Deserialize, Parser, Default)]
pub struct GenesisArgs {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use clap::Parser;
use common::forge::ForgeScriptArgs;
use common::Prompt;
use common::{forge::ForgeScriptArgs, Prompt};
use config::ChainConfig;
use serde::{Deserialize, Serialize};
use types::L1Network;
use url::Url;

use super::genesis::GenesisArgsFinal;
use crate::commands::chain::args::genesis::GenesisArgs;
use crate::defaults::LOCAL_RPC_URL;
use crate::{commands::chain::args::genesis::GenesisArgs, defaults::LOCAL_RPC_URL};

#[derive(Debug, Clone, Serialize, Deserialize, Parser)]
pub struct InitArgs {
Expand Down
6 changes: 3 additions & 3 deletions zk_toolbox/crates/zk_inception/src/commands/chain/create.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::cell::OnceCell;

use common::{logger, spinner::Spinner};
use xshell::Shell;

use crate::commands::chain::args::create::{ChainCreateArgs, ChainCreateArgsFinal};
use config::{
create_local_configs_dir, create_wallets, traits::SaveConfigWithBasePath, ChainConfig,
EcosystemConfig,
};
use types::ChainId;
use xshell::Shell;

use crate::commands::chain::args::create::{ChainCreateArgs, ChainCreateArgsFinal};

pub fn run(args: ChainCreateArgs, shell: &Shell) -> anyhow::Result<()> {
let mut ecosystem_config = EcosystemConfig::from_file(shell)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ use common::{
forge::{Forge, ForgeScriptArgs},
spinner::Spinner,
};
use xshell::Shell;

use crate::forge_utils::fill_forge_private_key;
use crate::{config_manipulations::update_paymaster, forge_utils::check_the_balance};
use config::{
forge_interface::{
paymaster::{DeployPaymasterInput, DeployPaymasterOutput},
Expand All @@ -16,6 +12,12 @@ use config::{
traits::{ReadConfig, SaveConfig},
ChainConfig, EcosystemConfig,
};
use xshell::Shell;

use crate::{
config_manipulations::update_paymaster,
forge_utils::{check_the_balance, fill_forge_private_key},
};

pub async fn run(args: ForgeScriptArgs, shell: &Shell) -> anyhow::Result<()> {
let chain_name = global_config().chain_name.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use common::{
logger,
spinner::Spinner,
};
use config::{ChainConfig, DatabasesConfig, EcosystemConfig};
use xshell::Shell;

use super::args::genesis::GenesisArgsFinal;
Expand All @@ -15,7 +16,6 @@ use crate::{
config_manipulations::{update_database_secrets, update_general_config},
server::{RunServer, ServerMode},
};
use config::{ChainConfig, DatabasesConfig, EcosystemConfig};

const SERVER_MIGRATIONS: &str = "core/lib/dal/migrations";
const PROVER_MIGRATIONS: &str = "prover/prover_dal/migrations";
Expand Down
23 changes: 11 additions & 12 deletions zk_toolbox/crates/zk_inception/src/commands/chain/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@ use common::{
logger,
spinner::Spinner,
};
use xshell::Shell;

use super::args::init::InitArgsFinal;
use crate::{
accept_ownership::accept_admin,
commands::chain::{
args::init::InitArgs, deploy_paymaster, genesis::genesis, initialize_bridges,
},
config_manipulations::{update_l1_contracts, update_l1_rpc_url_secret},
forge_utils::fill_forge_private_key,
};
use crate::{config_manipulations::update_genesis, forge_utils::check_the_balance};
use config::{
copy_configs,
forge_interface::{
Expand All @@ -26,6 +14,17 @@ use config::{
traits::{ReadConfig, ReadConfigWithBasePath, SaveConfig, SaveConfigWithBasePath},
ChainConfig, ContractsConfig, EcosystemConfig,
};
use xshell::Shell;

use super::args::init::InitArgsFinal;
use crate::{
accept_ownership::accept_admin,
commands::chain::{
args::init::InitArgs, deploy_paymaster, genesis::genesis, initialize_bridges,
},
config_manipulations::{update_genesis, update_l1_contracts, update_l1_rpc_url_secret},
forge_utils::{check_the_balance, fill_forge_private_key},
};

pub(crate) async fn run(args: InitArgs, shell: &Shell) -> anyhow::Result<()> {
let chain_name = global_config().chain_name.clone();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use std::path::Path;

use anyhow::Context;
use common::{
cmd::Cmd,
config::global_config,
forge::{Forge, ForgeScriptArgs},
spinner::Spinner,
};
use xshell::{cmd, Shell};

use crate::forge_utils::fill_forge_private_key;
use crate::{config_manipulations::update_l2_shared_bridge, forge_utils::check_the_balance};
use anyhow::Context;
use config::{
forge_interface::{
initialize_bridges::{input::InitializeBridgeInput, output::InitializeBridgeOutput},
Expand All @@ -19,6 +15,12 @@ use config::{
traits::{ReadConfig, SaveConfig},
ChainConfig, EcosystemConfig,
};
use xshell::{cmd, Shell};

use crate::{
config_manipulations::update_l2_shared_bridge,
forge_utils::{check_the_balance, fill_forge_private_key},
};

pub async fn run(args: ForgeScriptArgs, shell: &Shell) -> anyhow::Result<()> {
let chain_name = global_config().chain_name.clone();
Expand Down
6 changes: 3 additions & 3 deletions zk_toolbox/crates/zk_inception/src/commands/containers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::{anyhow, Context};
use common::{docker, logger, spinner::Spinner};
use std::path::PathBuf;
use xshell::Shell;

use anyhow::{anyhow, Context};
use common::{docker, logger, spinner::Spinner};
use config::{EcosystemConfig, DOCKER_COMPOSE_FILE};
use xshell::Shell;

pub fn run(shell: &Shell) -> anyhow::Result<()> {
let ecosystem =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use serde::{Deserialize, Serialize};
use types::L1Network;
use url::Url;

use crate::commands::chain::args::genesis::GenesisArgs;
use crate::defaults::LOCAL_RPC_URL;
use crate::{commands::chain::args::genesis::GenesisArgs, defaults::LOCAL_RPC_URL};

#[derive(Debug, Clone, Serialize, Deserialize, Parser)]
pub struct EcosystemArgs {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use common::PromptSelect;
use config::{traits::SaveConfigWithBasePath, EcosystemConfig};
use xshell::Shell;

use crate::commands::ecosystem::args::change_default::ChangeDefaultChain;
use config::{traits::SaveConfigWithBasePath, EcosystemConfig};

pub fn run(args: ChangeDefaultChain, shell: &Shell) -> anyhow::Result<()> {
let mut ecosystem_config = EcosystemConfig::from_file(shell)?;
Expand Down
16 changes: 9 additions & 7 deletions zk_toolbox/crates/zk_inception/src/commands/ecosystem/create.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
use std::path::Path;
use std::{path::PathBuf, str::FromStr};
use std::{
path::{Path, PathBuf},
str::FromStr,
};

use anyhow::bail;
use common::{cmd::Cmd, logger, spinner::Spinner};
use config::{
create_local_configs_dir, create_wallets, get_default_era_chain_id,
traits::SaveConfigWithBasePath, EcosystemConfig, EcosystemConfigFromFileError,
ZKSYNC_ERA_GIT_REPO,
};
use xshell::{cmd, Shell};

use crate::commands::{
Expand All @@ -13,11 +20,6 @@ use crate::commands::{
create_configs::{create_erc20_deployment_config, create_initial_deployments_config},
},
};
use config::traits::SaveConfigWithBasePath;
use config::{
create_local_configs_dir, create_wallets, get_default_era_chain_id, EcosystemConfig,
EcosystemConfigFromFileError, ZKSYNC_ERA_GIT_REPO,
};

pub fn run(args: EcosystemCreateArgs, shell: &Shell) -> anyhow::Result<()> {
match EcosystemConfig::from_file(shell) {
Expand Down
Loading
Loading