From a6ee1d3693737b65334a7d00f0a123007ed38b40 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 22 Nov 2024 12:40:48 +0200 Subject: [PATCH] sc-meta template & install crash fix --- framework/meta/src/cli/cli_standalone_main.rs | 6 ++--- framework/meta/src/cmd/install.rs | 12 ++++++---- .../src/cmd/install/install_scenario_go.rs | 24 ++++++++++++------- .../meta/src/cmd/template/contract_creator.rs | 4 ++-- .../meta/src/cmd/template/repo_source.rs | 7 ++---- .../src/cmd/template/repo_temp_download.rs | 8 +++---- .../meta/src/cmd/template/template_list.rs | 4 ++-- framework/meta/tests/template_test.rs | 24 ++++++++++--------- 8 files changed, 48 insertions(+), 41 deletions(-) diff --git a/framework/meta/src/cli/cli_standalone_main.rs b/framework/meta/src/cli/cli_standalone_main.rs index 3d91747f45..bc83cc9714 100644 --- a/framework/meta/src/cli/cli_standalone_main.rs +++ b/framework/meta/src/cli/cli_standalone_main.rs @@ -21,16 +21,16 @@ pub async fn cli_main_standalone() { let cli_args = StandaloneCliArgs::parse(); match &cli_args.command { Some(StandaloneCliAction::Info(args)) => call_info(args), - Some(StandaloneCliAction::Install(args)) => install(args), + Some(StandaloneCliAction::Install(args)) => install(args).await, Some(StandaloneCliAction::All(args)) => call_all_meta(args), Some(StandaloneCliAction::Upgrade(args)) => { upgrade_sc(args); }, Some(StandaloneCliAction::Template(args)) => { - create_contract(args); + create_contract(args).await; }, Some(StandaloneCliAction::TemplateList(args)) => { - print_template_names(args); + print_template_names(args).await; }, Some(StandaloneCliAction::TestGen(args)) => { test_gen_tool(args); diff --git a/framework/meta/src/cmd/install.rs b/framework/meta/src/cmd/install.rs index 256f7640e7..dd337800df 100644 --- a/framework/meta/src/cmd/install.rs +++ b/framework/meta/src/cmd/install.rs @@ -8,7 +8,7 @@ use crate::cli::{ use self::install_scenario_go::ScenarioGoInstaller; -pub fn install(args: &InstallArgs) { +pub async fn install(args: &InstallArgs) { let command = args .command .as_ref() @@ -16,18 +16,20 @@ pub fn install(args: &InstallArgs) { match command { InstallCommand::All => { - install_scenario_go(&InstallMxScenarioGoArgs::default()); + install_scenario_go(&InstallMxScenarioGoArgs::default()).await; install_wasm32(&InstallWasm32Args::default()); install_wasm_opt(&InstallWasmOptArgs::default()); }, - InstallCommand::MxScenarioGo(sg_args) => install_scenario_go(sg_args), + InstallCommand::MxScenarioGo(sg_args) => install_scenario_go(sg_args).await, InstallCommand::Wasm32(wam32_args) => install_wasm32(wam32_args), InstallCommand::WasmOpt(wasm_opt_args) => install_wasm_opt(wasm_opt_args), } } -fn install_scenario_go(sg_args: &InstallMxScenarioGoArgs) { - ScenarioGoInstaller::new(sg_args.tag.clone()).install(); +async fn install_scenario_go(sg_args: &InstallMxScenarioGoArgs) { + ScenarioGoInstaller::new(sg_args.tag.clone()) + .install() + .await; } fn install_wasm32(_wasm32_args: &InstallWasm32Args) { diff --git a/framework/meta/src/cmd/install/install_scenario_go.rs b/framework/meta/src/cmd/install/install_scenario_go.rs index 2351ab2621..538da0e116 100644 --- a/framework/meta/src/cmd/install/install_scenario_go.rs +++ b/framework/meta/src/cmd/install/install_scenario_go.rs @@ -50,9 +50,10 @@ impl ScenarioGoInstaller { } } - pub fn install(&self) { + pub async fn install(&self) { let release_raw = self .get_scenario_go_release_json() + .await .expect("couldn't retrieve mx-chain-scenario-cli-go release"); assert!( @@ -62,6 +63,7 @@ impl ScenarioGoInstaller { let release = self.parse_scenario_go_release(&release_raw); self.download_zip(&release) + .await .expect("could not download artifact"); self.unzip_binaries(); @@ -76,16 +78,18 @@ impl ScenarioGoInstaller { } } - fn get_scenario_go_release_json(&self) -> Result { + async fn get_scenario_go_release_json(&self) -> Result { let release_url = self.release_url(); println_green(format!("Retrieving release info: {release_url}")); - let response = reqwest::blocking::Client::builder() + let response = reqwest::Client::builder() .user_agent(&self.user_agent) .build()? .get(release_url) - .send()? - .text()?; + .send() + .await? + .text() + .await?; Ok(response) } @@ -135,14 +139,16 @@ impl ScenarioGoInstaller { self.temp_dir_path.join(&self.zip_name) } - fn download_zip(&self, release: &ScenarioGoRelease) -> Result<(), reqwest::Error> { + async fn download_zip(&self, release: &ScenarioGoRelease) -> Result<(), reqwest::Error> { println_green(format!("Downloading binaries: {}", &release.download_url)); - let response = reqwest::blocking::Client::builder() + let response = reqwest::Client::builder() .user_agent(&self.user_agent) .build()? .get(&release.download_url) - .send()? - .bytes()?; + .send() + .await? + .bytes() + .await?; if response.len() < 10000 { panic!( "Could not download artifact: {}", diff --git a/framework/meta/src/cmd/template/contract_creator.rs b/framework/meta/src/cmd/template/contract_creator.rs index 8483f3c8f8..7e0cfb5ea1 100644 --- a/framework/meta/src/cmd/template/contract_creator.rs +++ b/framework/meta/src/cmd/template/contract_creator.rs @@ -12,10 +12,10 @@ use super::{ }; /// Creates a new contract on disk, from a template, given a name. -pub fn create_contract(args: &TemplateArgs) { +pub async fn create_contract(args: &TemplateArgs) { let version = get_repo_version(&args.tag); let version_tag: FrameworkVersion = version.get_tag(); - let repo_temp_download = RepoSource::download_from_github(version, std::env::temp_dir()); + let repo_temp_download = RepoSource::download_from_github(version, std::env::temp_dir()).await; let target = target_from_args(args); let creator = ContractCreator::new( diff --git a/framework/meta/src/cmd/template/repo_source.rs b/framework/meta/src/cmd/template/repo_source.rs index 2bb19e262f..b1e66063ee 100644 --- a/framework/meta/src/cmd/template/repo_source.rs +++ b/framework/meta/src/cmd/template/repo_source.rs @@ -11,12 +11,9 @@ pub enum RepoSource { } impl RepoSource { - pub fn download_from_github(version: RepoVersion, temp_dir_path: PathBuf) -> Self { + pub async fn download_from_github(version: RepoVersion, temp_dir_path: PathBuf) -> Self { fs::create_dir_all(&temp_dir_path).unwrap(); - RepoSource::Downloaded(RepoTempDownload::download_from_github( - version, - temp_dir_path, - )) + RepoSource::Downloaded(RepoTempDownload::download_from_github(version, temp_dir_path).await) } pub fn from_local_path(repo_local_path: impl AsRef) -> Self { diff --git a/framework/meta/src/cmd/template/repo_temp_download.rs b/framework/meta/src/cmd/template/repo_temp_download.rs index 374939eddd..242d21d0c6 100644 --- a/framework/meta/src/cmd/template/repo_temp_download.rs +++ b/framework/meta/src/cmd/template/repo_temp_download.rs @@ -14,12 +14,12 @@ pub struct RepoTempDownload { } impl RepoTempDownload { - pub fn download_from_github(version: RepoVersion, temp_dir_path: PathBuf) -> Self { + pub async fn download_from_github(version: RepoVersion, temp_dir_path: PathBuf) -> Self { let tt_download = RepoTempDownload { version, temp_dir_path, }; - tt_download.download_binaries().unwrap(); + tt_download.download_binaries().await.unwrap(); tt_download.delete_temp_folder(); tt_download.unzip_binaries(); tt_download.delete_zip(); @@ -34,8 +34,8 @@ impl RepoTempDownload { self.temp_dir_path.join(self.version.temp_dir_name()) } - fn download_binaries(&self) -> Result<(), reqwest::Error> { - let response = reqwest::blocking::get(self.version.url())?.bytes()?; + async fn download_binaries(&self) -> Result<(), reqwest::Error> { + let response = reqwest::get(self.version.url()).await?.bytes().await?; if response.len() < 10000 { panic!( "Could not download artifact: {}", diff --git a/framework/meta/src/cmd/template/template_list.rs b/framework/meta/src/cmd/template/template_list.rs index 1d297179b7..24170d95a5 100644 --- a/framework/meta/src/cmd/template/template_list.rs +++ b/framework/meta/src/cmd/template/template_list.rs @@ -2,9 +2,9 @@ use crate::cli::TemplateListArgs; use super::{contract_creator::get_repo_version, template_source::template_sources, RepoSource}; -pub fn print_template_names(args: &TemplateListArgs) { +pub async fn print_template_names(args: &TemplateListArgs) { let version = get_repo_version(&args.tag); - let repo_temp_download = RepoSource::download_from_github(version, std::env::temp_dir()); + let repo_temp_download = RepoSource::download_from_github(version, std::env::temp_dir()).await; let template_names = template_names_from_repo(&repo_temp_download); for template_name in template_names { println!("{template_name}"); diff --git a/framework/meta/tests/template_test.rs b/framework/meta/tests/template_test.rs index 7a52398ff8..695763095d 100644 --- a/framework/meta/tests/template_test.rs +++ b/framework/meta/tests/template_test.rs @@ -106,28 +106,29 @@ fn template_test_current(template_name: &str, sub_path: &str, new_name: &str, ne cargo_test(&target); } -#[test] +#[tokio::test] #[cfg_attr(not(feature = "template-test-released"), ignore)] -fn template_released_adder() { +async fn template_released_adder() { template_test_released( "adder", "released-adder", "Alin Cruceat ", - ); + ) + .await; cargo_check_interactor("", "released-adder"); } -#[test] +#[tokio::test] #[cfg_attr(not(feature = "template-test-released"), ignore)] -fn template_released_crypto_zombies() { - template_test_released("crypto-zombies", "released-crypto-zombies", ""); +async fn template_released_crypto_zombies() { + template_test_released("crypto-zombies", "released-crypto-zombies", "").await; } -#[test] +#[tokio::test] #[cfg_attr(not(feature = "template-test-released"), ignore)] -fn template_released_empty() { - template_test_released("empty", "released-empty", ""); +async fn template_released_empty() { + template_test_released("empty", "released-empty", "").await; } /// These tests fully replicate the templating process. They @@ -135,7 +136,7 @@ fn template_released_empty() { /// - create proper contracts, /// - build the newly created contracts (to wasm) /// - run all tests (including Go scenarios) on them. -fn template_test_released(template_name: &str, new_name: &str, new_author: &str) { +async fn template_test_released(template_name: &str, new_name: &str, new_author: &str) { let workspace_path = find_current_workspace().unwrap(); let target = ContractCreatorTarget { target_path: workspace_path.join(TEMPLATE_TEMP_DIR_NAME), @@ -149,7 +150,8 @@ fn template_test_released(template_name: &str, new_name: &str, new_author: &str) let repo_source = RepoSource::download_from_github( RepoVersion::Tag(version_history::LAST_TEMPLATE_VERSION.to_string()), temp_dir_path, - ); + ) + .await; prepare_target_dir(&target);