From 74c1890c4ad125f2f2a8a67ade0497714bd4cf15 Mon Sep 17 00:00:00 2001 From: chen <23054115+cwkang1998@users.noreply.github.com> Date: Wed, 24 Jul 2024 13:24:54 +0200 Subject: [PATCH] chore: remove dynamic compiler (WIP) Dynamic compiler was initially introduced in order to support compiling multiple version of cairo with multiple version of scarb, and abstrating away the versions from the CLI. As we move to have a universal verifier, this should be removed and we should instead start have generic crates to handle aspects of the verification process. As of right now we also do not use this for its original purpose anymore, so it should be relatively safe to remove this. --- Cargo.lock | 10 --- Cargo.toml | 3 +- crates/cli/Cargo.toml | 2 - crates/cli/src/resolver.rs | 16 +--- crates/cli/src/utils.rs | 1 - crates/cli/src/verify.rs | 3 - crates/dyn-compiler/Cargo.toml | 8 -- crates/dyn-compiler/README.md | 1 - crates/dyn-compiler/src/dyn_compiler.rs | 77 ----------------- crates/dyn-compiler/src/lib.rs | 1 - crates/voyager-resolver-cairo/Cargo.toml | 1 - .../src/dyn_compiler.rs | 83 ------------------- crates/voyager-resolver-cairo/src/lib.rs | 1 - 13 files changed, 5 insertions(+), 202 deletions(-) delete mode 100644 crates/dyn-compiler/Cargo.toml delete mode 100644 crates/dyn-compiler/README.md delete mode 100644 crates/dyn-compiler/src/dyn_compiler.rs delete mode 100644 crates/dyn-compiler/src/lib.rs delete mode 100644 crates/voyager-resolver-cairo/src/dyn_compiler.rs diff --git a/Cargo.lock b/Cargo.lock index c87117a..d80465d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1069,7 +1069,6 @@ dependencies = [ "console", "dialoguer", "dirs", - "dyn-compiler", "indicatif", "regex", "reqwest", @@ -1599,14 +1598,6 @@ version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125" -[[package]] -name = "dyn-compiler" -version = "0.1.0" -dependencies = [ - "anyhow", - "camino", -] - [[package]] name = "either" version = "1.11.0" @@ -5903,7 +5894,6 @@ dependencies = [ "cairo-lang-test-utils", "cairo-lang-utils", "camino", - "dyn-compiler", "env_logger 0.9.3", "indoc 1.0.9", "itertools 0.10.5", diff --git a/Cargo.toml b/Cargo.toml index 4d1449a..2d98adf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,7 @@ [workspace] members = [ "crates/cli", - "crates/voyager-resolver-cairo", - "crates/dyn-compiler", + "crates/voyager-resolver-cairo" ] "resolver" = "2" diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 2786f3a..28ba17e 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -23,8 +23,6 @@ strum = "0.25.0" strum_macros = "0.25.0" dirs = "4.0" -dyn-compiler = { path = "../dyn-compiler" } - # All resolver versions, based on scarb + cairo voyager-resolver-cairo = { path = "../voyager-resolver-cairo" } walkdir = "2.3.3" diff --git a/crates/cli/src/resolver.rs b/crates/cli/src/resolver.rs index 0a0fc92..560b9c0 100644 --- a/crates/cli/src/resolver.rs +++ b/crates/cli/src/resolver.rs @@ -4,15 +4,7 @@ use std::fs; use walkdir::{DirEntry, WalkDir}; use crate::api::{FileInfo, ProjectMetadataInfo}; -use dyn_compiler::dyn_compiler::{DynamicCompiler, SupportedCairoVersions, SupportedScarbVersions}; -use voyager_resolver_cairo::compiler::scarb_utils::read_additional_scarb_manifest_metadata; -use voyager_resolver_cairo::dyn_compiler::VoyagerGeneratorWrapper as VoyagerGenerator; - -pub fn get_dynamic_compiler(cairo_version: SupportedCairoVersions) -> Box { - match cairo_version { - SupportedCairoVersions::V2_4_3 => Box::new(VoyagerGenerator), - } -} +use voyager_resolver_cairo::compiler::{scarb_utils::read_additional_scarb_manifest_metadata, VoyagerGenerator}; #[derive(Debug, Deserialize, Serialize)] struct ScarbTomlRawPackageData { @@ -27,8 +19,8 @@ struct ScarbTomlRawData { pub fn resolve_scarb( path: Utf8PathBuf, - cairo_version: SupportedCairoVersions, - scarb_version: SupportedScarbVersions, + cairo_version: &str, + scarb_version: &str, ) -> anyhow::Result<(Vec, ProjectMetadataInfo)> { // Extract necessary files from the Scarb project for the verified contract let source_dir = if path.is_absolute() { @@ -39,7 +31,7 @@ pub fn resolve_scarb( Utf8PathBuf::from_path_buf(current_path).unwrap() }; - let compiler = get_dynamic_compiler(cairo_version); + let compiler = Box::new(VoyagerGenerator); let contract_paths = compiler.get_contracts_to_verify_path(&source_dir)?; // TODO move the contract selection before the resolving step as a 'pre-resolving' step diff --git a/crates/cli/src/utils.rs b/crates/cli/src/utils.rs index 89855a6..ae713c4 100644 --- a/crates/cli/src/utils.rs +++ b/crates/cli/src/utils.rs @@ -1,4 +1,3 @@ -use dyn_compiler::dyn_compiler::{SupportedCairoVersions, SupportedScarbVersions}; use std::process::Command; pub fn detect_local_tools() -> (SupportedScarbVersions, SupportedCairoVersions) { diff --git a/crates/cli/src/verify.rs b/crates/cli/src/verify.rs index 4ad2373..4c8d16b 100644 --- a/crates/cli/src/verify.rs +++ b/crates/cli/src/verify.rs @@ -4,15 +4,12 @@ use anyhow::Result; use camino::Utf8PathBuf; use clap::{arg, Args}; -use dyn_compiler::dyn_compiler::SupportedCairoVersions; - use crate::{ api::{ dispatch_class_verification_job, poll_verification_status, FileInfo, Network, ProjectMetadataInfo, }, license::LicenseType, - resolver::get_dynamic_compiler, }; #[derive(Args, Debug)] diff --git a/crates/dyn-compiler/Cargo.toml b/crates/dyn-compiler/Cargo.toml deleted file mode 100644 index 8cd81e9..0000000 --- a/crates/dyn-compiler/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "dyn-compiler" -version = "0.1.0" -edition = "2021" - -[dependencies] -camino.workspace = true -anyhow.workspace = true diff --git a/crates/dyn-compiler/README.md b/crates/dyn-compiler/README.md deleted file mode 100644 index 1e27718..0000000 --- a/crates/dyn-compiler/README.md +++ /dev/null @@ -1 +0,0 @@ -# dyn-compiler crate diff --git a/crates/dyn-compiler/src/dyn_compiler.rs b/crates/dyn-compiler/src/dyn_compiler.rs deleted file mode 100644 index 241517e..0000000 --- a/crates/dyn-compiler/src/dyn_compiler.rs +++ /dev/null @@ -1,77 +0,0 @@ -use anyhow::Result; -use camino::Utf8PathBuf; - -#[derive(Debug, Clone, Copy)] -pub enum SupportedCairoVersions { - // V1_1_0, - // V1_1_1, - // V2_0_0, - // V2_0_1, - // V2_0_2, - // V2_1_0, - // V2_1_1, - // V2_2_0, - V2_4_3, -} - -impl ToString for SupportedCairoVersions { - fn to_string(&self) -> String { - match self { - // SupportedCairoVersions::V1_1_0 => "1.1.0".into(), - // SupportedCairoVersions::V1_1_1 => "1.1.1".into(), - // SupportedCairoVersions::V2_0_0 => "2.0.0".into(), - // SupportedCairoVersions::V2_0_1 => "2.0.1".into(), - // SupportedCairoVersions::V2_0_2 => "2.0.2".into(), - // SupportedCairoVersions::V2_1_0 => "2.1.0".into(), - // SupportedCairoVersions::V2_1_1 => "2.1.1".into(), - // SupportedCairoVersions::V2_2_0 => "2.2.0".into(), - SupportedCairoVersions::V2_4_3 => "2.4.3".into(), - } - } -} - -#[derive(Debug, Clone, Copy)] -pub enum SupportedScarbVersions { - // V0_4_0, - // V0_4_1, - // V0_5_0, - // V0_5_1, - // V0_5_2, - // V0_6_1, - // V0_6_2, - // V0_7_0, - V2_4_3, -} - -impl ToString for SupportedScarbVersions { - fn to_string(&self) -> String { - match self { - // SupportedScarbVersions::V0_4_0 => "0.4.0".into(), - // SupportedScarbVersions::V0_4_1 => "0.4.1".into(), - // SupportedScarbVersions::V0_5_0 => "0.5.0".into(), - // SupportedScarbVersions::V0_5_1 => "0.5.1".into(), - // SupportedScarbVersions::V0_5_2 => "0.5.2".into(), - // SupportedScarbVersions::V0_6_1 => "0.6.1".into(), - // SupportedScarbVersions::V0_6_2 => "0.6.2".into(), - // SupportedScarbVersions::V0_7_0 => "0.7.0".into(), - SupportedScarbVersions::V2_4_3 => "2.4.3".into(), - } - } -} - -/** - * This trait is required to be implemented by the voyager resolvers. - * This allows us to use multiple version of scarb + cairo in the same project, - * and compile scarb projects easily, - */ -pub trait DynamicCompiler { - fn get_supported_scarb_versions(&self) -> Vec; - - fn get_supported_cairo_versions(&self) -> Vec; - - fn get_contracts_to_verify_path(&self, project_path: &Utf8PathBuf) -> Result>; - - fn compile_project(&self, project_path: &Utf8PathBuf) -> Result<()>; - - fn compile_file(&self, file_path: &Utf8PathBuf) -> Result<()>; -} diff --git a/crates/dyn-compiler/src/lib.rs b/crates/dyn-compiler/src/lib.rs deleted file mode 100644 index b86113a..0000000 --- a/crates/dyn-compiler/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod dyn_compiler; diff --git a/crates/voyager-resolver-cairo/Cargo.toml b/crates/voyager-resolver-cairo/Cargo.toml index 2494bfc..5624665 100644 --- a/crates/voyager-resolver-cairo/Cargo.toml +++ b/crates/voyager-resolver-cairo/Cargo.toml @@ -24,7 +24,6 @@ toml.workspace = true toml_edit = "0.19.8" rstest = "0.17.0" camino.workspace = true -dyn-compiler = { path = "../dyn-compiler" } # All version dependent dependencies cairo-lang-casm = "=2.4.3" diff --git a/crates/voyager-resolver-cairo/src/dyn_compiler.rs b/crates/voyager-resolver-cairo/src/dyn_compiler.rs deleted file mode 100644 index ca9f25b..0000000 --- a/crates/voyager-resolver-cairo/src/dyn_compiler.rs +++ /dev/null @@ -1,83 +0,0 @@ -use std::env; - -use anyhow::Result; -use camino::Utf8PathBuf; -use dyn_compiler::dyn_compiler::{DynamicCompiler, SupportedCairoVersions, SupportedScarbVersions}; -use itertools::Itertools; -use scarb::{ - compiler::CompilerRepository, - core::{Config, TargetKind}, - ops, -}; - -use crate::{ - compiler::{scarb_utils::get_contracts_to_verify, VoyagerGenerator}, - utils::run_starknet_compile, -}; - -pub struct VoyagerGeneratorWrapper; - -impl DynamicCompiler for VoyagerGeneratorWrapper { - fn get_supported_scarb_versions(&self) -> Vec { - vec![SupportedScarbVersions::V2_4_3] - } - fn get_supported_cairo_versions(&self) -> Vec { - vec![SupportedCairoVersions::V2_4_3] - } - - fn get_contracts_to_verify_path(&self, project_path: &Utf8PathBuf) -> Result> { - let manifest_path = project_path.join("Scarb.toml"); - - let mut compilers = CompilerRepository::empty(); - compilers.add(Box::new(VoyagerGenerator)).unwrap(); - - let config = Config::builder(manifest_path) - // .ui_verbosity(Verbosity::Verbose) - .log_filter_directive(env::var_os("SCARB_LOG")) - .compilers(compilers) - .build() - .unwrap(); - - let ws = ops::read_workspace(config.manifest_path(), &config).unwrap_or_else(|err| { - eprintln!("error: {}", err); - std::process::exit(1); - }); - let package = ws.current_package().unwrap(); - let contracts_path = get_contracts_to_verify(package)?; - - Ok(contracts_path - .iter() - .map(|p| Utf8PathBuf::from_path_buf(p.to_path_buf()).unwrap()) - .collect_vec()) - } - - fn compile_project(&self, project_path: &Utf8PathBuf) -> Result<()> { - let manifest_path = project_path.join("Scarb.toml"); - - let mut compilers = CompilerRepository::empty(); - compilers.add(Box::new(VoyagerGenerator)).unwrap(); - - let config = Config::builder(manifest_path) - .ui_verbosity(scarb_ui::Verbosity::Verbose) - .log_filter_directive(env::var_os("SCARB_LOG")) - .compilers(compilers) - .build() - .unwrap(); - - let ws = ops::read_workspace(config.manifest_path(), &config).unwrap(); - let package_ids = ws.members().map(|p| p.id).collect(); - let compile_opts = ops::CompileOpts { - include_targets: vec![TargetKind::STARKNET_CONTRACT], - exclude_targets: vec![], - }; - - ops::compile(package_ids, compile_opts, &ws) - } - - fn compile_file(&self, file_path: &Utf8PathBuf) -> Result<()> { - //TODO detect_corelib will try to use the local corelib. - // Once cairo is released, it will probably be able to use - // the corelib from the release. - run_starknet_compile(file_path.as_str()) - } -} diff --git a/crates/voyager-resolver-cairo/src/lib.rs b/crates/voyager-resolver-cairo/src/lib.rs index 7f33954..39b3d2d 100644 --- a/crates/voyager-resolver-cairo/src/lib.rs +++ b/crates/voyager-resolver-cairo/src/lib.rs @@ -1,5 +1,4 @@ pub mod compiler; -pub mod dyn_compiler; mod graph; mod model; pub mod utils;