Skip to content

Commit

Permalink
Merge pull request #1871 from multiversx/sc-meta-tokio-fix
Browse files Browse the repository at this point in the history
sc-meta template & install crash fix
  • Loading branch information
andrei-marinica authored Nov 22, 2024
2 parents a7dd92b + a6ee1d3 commit 3d5d3bc
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 41 deletions.
6 changes: 3 additions & 3 deletions framework/meta/src/cli/cli_standalone_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 7 additions & 5 deletions framework/meta/src/cmd/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,28 @@ 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()
.expect("command expected after `install`");

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) {
Expand Down
24 changes: 15 additions & 9 deletions framework/meta/src/cmd/install/install_scenario_go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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();
Expand All @@ -76,16 +78,18 @@ impl ScenarioGoInstaller {
}
}

fn get_scenario_go_release_json(&self) -> Result<String, reqwest::Error> {
async fn get_scenario_go_release_json(&self) -> Result<String, reqwest::Error> {
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)
}
Expand Down Expand Up @@ -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: {}",
Expand Down
4 changes: 2 additions & 2 deletions framework/meta/src/cmd/template/contract_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 2 additions & 5 deletions framework/meta/src/cmd/template/repo_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path>) -> Self {
Expand Down
8 changes: 4 additions & 4 deletions framework/meta/src/cmd/template/repo_temp_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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: {}",
Expand Down
4 changes: 2 additions & 2 deletions framework/meta/src/cmd/template/template_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down
24 changes: 13 additions & 11 deletions framework/meta/tests/template_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,36 +106,37 @@ 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 <[email protected]>",
);
)
.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
/// - download the last released version of the repo,
/// - 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),
Expand All @@ -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);

Expand Down

0 comments on commit 3d5d3bc

Please sign in to comment.