From 2b58f20296be7831bf7f0bf85f71a6ce859d0c63 Mon Sep 17 00:00:00 2001 From: Gleb Date: Thu, 14 Nov 2024 14:18:01 -0800 Subject: [PATCH 01/14] fix help tests --- Cargo.lock | 1 + cmd/crates/soroban-test/Cargo.toml | 1 + cmd/crates/soroban-test/src/lib.rs | 8 +- .../tests/fixtures/workspace/Cargo.lock | 89 ++++++++++++------- cmd/crates/soroban-test/tests/it/help.rs | 1 + .../tests/it/integration/hello_world.rs | 12 +-- .../soroban-test/tests/it/integration/util.rs | 3 +- cmd/crates/soroban-test/tests/it/util.rs | 13 +-- .../src/commands/contract/arg_parsing.rs | 45 ++++++---- .../src/commands/contract/deploy/wasm.rs | 83 +++++++++-------- .../src/commands/contract/invoke.rs | 48 +++++++--- 11 files changed, 190 insertions(+), 114 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e043b4f8e..9ffab2eb4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4599,6 +4599,7 @@ dependencies = [ "assert_cmd", "assert_fs", "ed25519-dalek", + "either", "fs_extra", "hex", "predicates", diff --git a/cmd/crates/soroban-test/Cargo.toml b/cmd/crates/soroban-test/Cargo.toml index f694ec533..3d137d158 100644 --- a/cmd/crates/soroban-test/Cargo.toml +++ b/cmd/crates/soroban-test/Cargo.toml @@ -25,6 +25,7 @@ sep5 = { workspace = true } soroban-cli = { workspace = true } soroban-rpc = { workspace = true } +either = "1.13.0" thiserror = "1.0.31" sha2 = "0.10.6" assert_cmd = "2.0.4" diff --git a/cmd/crates/soroban-test/src/lib.rs b/cmd/crates/soroban-test/src/lib.rs index 2c62578ef..3a16b63ca 100644 --- a/cmd/crates/soroban-test/src/lib.rs +++ b/cmd/crates/soroban-test/src/lib.rs @@ -27,6 +27,7 @@ use std::{ffi::OsString, fmt::Display, path::Path}; use assert_cmd::{assert::Assert, Command}; use assert_fs::{fixture::FixtureError, prelude::PathChild, TempDir}; +use either::Either; use fs_extra::dir::CopyOptions; use soroban_cli::{ @@ -202,9 +203,10 @@ impl TestEnv { source: &str, ) -> Result { let cmd = self.cmd_with_config::(command_str, None); - self.run_cmd_with(cmd, source) - .await - .map(|r| r.into_result().unwrap()) + self.run_cmd_with(cmd, source).await.map(|r| match r { + Either::Left(help) => help, + Either::Right(tx) => tx.into_result().unwrap(), + }) } /// A convenience method for using the invoke command. diff --git a/cmd/crates/soroban-test/tests/fixtures/workspace/Cargo.lock b/cmd/crates/soroban-test/tests/fixtures/workspace/Cargo.lock index cc4bfbb58..fb7a3d15c 100644 --- a/cmd/crates/soroban-test/tests/fixtures/workspace/Cargo.lock +++ b/cmd/crates/soroban-test/tests/fixtures/workspace/Cargo.lock @@ -355,7 +355,6 @@ dependencies = [ "elliptic-curve", "rfc6979", "signature", - "spki", ] [[package]] @@ -370,15 +369,16 @@ dependencies = [ [[package]] name = "ed25519-dalek" -version = "2.0.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7277392b266383ef8396db7fdeb1e77b6c52fed775f5df15bb24f35b72156980" +checksum = "4a3daa8e81a3963a60642bcc1f90a670680bd4a77535faa384e9d1c79d620871" dependencies = [ "curve25519-dalek", "ed25519", "rand_core", "serde", "sha2", + "subtle", "zeroize", ] @@ -400,7 +400,6 @@ dependencies = [ "ff", "generic-array", "group", - "pkcs8", "rand_core", "sec1", "subtle", @@ -614,9 +613,7 @@ dependencies = [ "cfg-if", "ecdsa", "elliptic-curve", - "once_cell", "sha2", - "signature", ] [[package]] @@ -717,6 +714,18 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "p256" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9863ad85fa8f4460f9c48cb909d38a0d689dba1f6f6988a5e3e0d31071bcd4b" +dependencies = [ + "ecdsa", + "elliptic-curve", + "primeorder", + "sha2", +] + [[package]] name = "paste" version = "1.0.14" @@ -761,6 +770,15 @@ dependencies = [ "syn", ] +[[package]] +name = "primeorder" +version = "0.13.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "353e1ca18966c16d9deb1c69278edbc5f194139612772bd9537af60ac231e1e6" +dependencies = [ + "elliptic-curve", +] + [[package]] name = "proc-macro2" version = "1.0.69" @@ -849,7 +867,6 @@ dependencies = [ "base16ct", "der", "generic-array", - "pkcs8", "subtle", "zeroize", ] @@ -959,9 +976,9 @@ checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970" [[package]] name = "soroban-builtin-sdk-macros" -version = "20.3.0" +version = "21.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cc32c6e817f3ca269764ec0d7d14da6210b74a5bf14d4e745aa3ee860558900" +checksum = "2f57a68ef8777e28e274de0f3a88ad9a5a41d9a2eb461b4dd800b086f0e83b80" dependencies = [ "itertools", "proc-macro2", @@ -971,9 +988,9 @@ dependencies = [ [[package]] name = "soroban-env-common" -version = "20.3.0" +version = "21.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c14e18d879c520ff82612eaae0590acaf6a7f3b977407e1abb1c9e31f94c7814" +checksum = "2fd1c89463835fe6da996318156d39f424b4f167c725ec692e5a7a2d4e694b3d" dependencies = [ "arbitrary", "crate-git-revision", @@ -985,13 +1002,14 @@ dependencies = [ "soroban-wasmi", "static_assertions", "stellar-xdr", + "wasmparser", ] [[package]] name = "soroban-env-guest" -version = "20.3.0" +version = "21.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5122ca2abd5ebcc1e876a96b9b44f87ce0a0e06df8f7c09772ddb58b159b7454" +checksum = "6bfb2536811045d5cd0c656a324cbe9ce4467eb734c7946b74410d90dea5d0ce" dependencies = [ "soroban-env-common", "static_assertions", @@ -999,13 +1017,16 @@ dependencies = [ [[package]] name = "soroban-env-host" -version = "20.3.0" +version = "21.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "114a0fa0d0cc39d0be16b1ee35b6e5f4ee0592ddcf459bde69391c02b03cf520" +checksum = "2b7a32c28f281c423189f1298960194f0e0fc4eeb72378028171e556d8cd6160" dependencies = [ "backtrace", "curve25519-dalek", + "ecdsa", "ed25519-dalek", + "elliptic-curve", + "generic-array", "getrandom", "hex-literal", "hmac", @@ -1013,8 +1034,10 @@ dependencies = [ "num-derive", "num-integer", "num-traits", + "p256", "rand", "rand_chacha", + "sec1", "sha2", "sha3", "soroban-builtin-sdk-macros", @@ -1022,13 +1045,14 @@ dependencies = [ "soroban-wasmi", "static_assertions", "stellar-strkey", + "wasmparser", ] [[package]] name = "soroban-env-macros" -version = "20.3.0" +version = "21.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b13e3f8c86f812e0669e78fcb3eae40c385c6a9dd1a4886a1de733230b4fcf27" +checksum = "242926fe5e0d922f12d3796cd7cd02dd824e5ef1caa088f45fce20b618309f64" dependencies = [ "itertools", "proc-macro2", @@ -1041,9 +1065,9 @@ dependencies = [ [[package]] name = "soroban-ledger-snapshot" -version = "20.5.0" +version = "21.7.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61a54708f44890e0546180db6b4f530e2a88d83b05a9b38a131caa21d005e25a" +checksum = "e6edf92749fd8399b417192d301c11f710b9cdce15789a3d157785ea971576fa" dependencies = [ "serde", "serde_json", @@ -1055,9 +1079,9 @@ dependencies = [ [[package]] name = "soroban-sdk" -version = "20.5.0" +version = "21.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84fc8be9068dd4e0212d8b13ad61089ea87e69ac212c262914503a961c8dc3a3" +checksum = "69e39bf9e8ab05579c836e8e5be5f2f4c5ba75e7337ece20e975e82fc3a9d41e" dependencies = [ "arbitrary", "bytes-lit", @@ -1075,9 +1099,9 @@ dependencies = [ [[package]] name = "soroban-sdk-macros" -version = "20.5.0" +version = "21.7.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db20def4ead836663633f58d817d0ed8e1af052c9650a04adf730525af85b964" +checksum = "0974e413731aeff2443f2305b344578b3f1ffd18335a7ba0f0b5d2eb4e94c9ce" dependencies = [ "crate-git-revision", "darling", @@ -1095,9 +1119,9 @@ dependencies = [ [[package]] name = "soroban-spec" -version = "20.5.0" +version = "21.7.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3eefeb5d373b43f6828145d00f0c5cc35e96db56a6671ae9614f84beb2711cab" +checksum = "c2c70b20e68cae3ef700b8fa3ae29db1c6a294b311fba66918f90cb8f9fd0a1a" dependencies = [ "base64 0.13.1", "stellar-xdr", @@ -1107,9 +1131,9 @@ dependencies = [ [[package]] name = "soroban-spec-rust" -version = "20.5.0" +version = "21.7.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3152bca4737ef734ac37fe47b225ee58765c9095970c481a18516a2b287c7a33" +checksum = "a2dafbde981b141b191c6c036abc86097070ddd6eaaa33b273701449501e43d3" dependencies = [ "prettyplease", "proc-macro2", @@ -1169,9 +1193,9 @@ dependencies = [ [[package]] name = "stellar-xdr" -version = "20.1.0" +version = "21.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e59cdf3eb4467fb5a4b00b52e7de6dca72f67fac6f9b700f55c95a5d86f09c9d" +checksum = "2675a71212ed39a806e415b0dbf4702879ff288ec7f5ee996dda42a135512b50" dependencies = [ "arbitrary", "base64 0.13.1", @@ -1353,11 +1377,12 @@ dependencies = [ [[package]] name = "wasmparser" -version = "0.88.0" +version = "0.116.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb8cf7dd82407fe68161bedcd57fde15596f32ebf6e9b3bdbf3ae1da20e38e5e" +checksum = "a58e28b80dd8340cb07b8242ae654756161f6fc8d0038123d679b7b99964fa50" dependencies = [ - "indexmap 1.9.3", + "indexmap 2.1.0", + "semver", ] [[package]] diff --git a/cmd/crates/soroban-test/tests/it/help.rs b/cmd/crates/soroban-test/tests/it/help.rs index ef84a361b..12db8b9bd 100644 --- a/cmd/crates/soroban-test/tests/it/help.rs +++ b/cmd/crates/soroban-test/tests/it/help.rs @@ -35,6 +35,7 @@ async fn tuple_help() { #[tokio::test] async fn strukt_help() { let output = invoke_custom("strukt", "--help").await.unwrap(); + println!("{output}"); assert!(output.contains("--strukt '{ \"a\": 1, \"b\": true, \"c\": \"hello\" }'",)); assert!(output.contains("This is from the rust doc above the struct Test",)); } diff --git a/cmd/crates/soroban-test/tests/it/integration/hello_world.rs b/cmd/crates/soroban-test/tests/it/integration/hello_world.rs index fd38c2012..f77bcb3b1 100644 --- a/cmd/crates/soroban-test/tests/it/integration/hello_world.rs +++ b/cmd/crates/soroban-test/tests/it/integration/hello_world.rs @@ -1,12 +1,12 @@ -use predicates::boolean::PredicateBooleanExt; +use soroban_rpc::GetLatestLedgerResponse; + use soroban_cli::{ commands::{ contract::{self, fetch}, txn_result::TxnResult, }, - config::{address::Address, locator, secret}, + config::{locator, secret}, }; -use soroban_rpc::GetLatestLedgerResponse; use soroban_test::{AssertExt, TestEnv, LOCAL_NETWORK_PASSPHRASE}; use crate::integration::util::extend_contract; @@ -19,8 +19,8 @@ async fn invoke_view_with_non_existent_source_account() { let sandbox = &TestEnv::new(); let id = deploy_hello(sandbox).await; let world = "world"; - let mut cmd = hello_world_cmd(&id, world); - let res = sandbox.run_cmd_with(cmd, "").await.unwrap(); + let cmd = hello_world_cmd(&id, world); + let res = sandbox.run_cmd_with(cmd, "").await.unwrap().unwrap_right(); assert_eq!(res, TxnResult::Res(format!(r#"["Hello",{world:?}]"#))); } @@ -167,7 +167,7 @@ fn hello_world_cmd(id: &str, arg: &str) -> contract::invoke::Cmd { async fn invoke_hello_world_with_lib(e: &TestEnv, id: &str) { let cmd = hello_world_cmd(id, "world"); - let res = e.run_cmd_with(cmd, "test").await.unwrap(); + let res = e.run_cmd_with(cmd, "test").await.unwrap().unwrap_right(); assert_eq!(res, TxnResult::Res(r#"["Hello","world"]"#.to_string())); } diff --git a/cmd/crates/soroban-test/tests/it/integration/util.rs b/cmd/crates/soroban-test/tests/it/integration/util.rs index 486b00a1b..08cf19e65 100644 --- a/cmd/crates/soroban-test/tests/it/integration/util.rs +++ b/cmd/crates/soroban-test/tests/it/integration/util.rs @@ -80,7 +80,8 @@ pub async fn deploy_contract( let res = sandbox .run_cmd_with(cmd, deployer.unwrap_or("test")) .await - .unwrap(); + .unwrap() + .unwrap_right(); match deploy { DeployKind::BuildOnly | DeployKind::SimOnly => match res.to_envelope() { commands::txn_result::TxnEnvelopeResult::TxnEnvelope(e) => { diff --git a/cmd/crates/soroban-test/tests/it/util.rs b/cmd/crates/soroban-test/tests/it/util.rs index a74428666..7923f7429 100644 --- a/cmd/crates/soroban-test/tests/it/util.rs +++ b/cmd/crates/soroban-test/tests/it/util.rs @@ -1,10 +1,10 @@ -use std::path::Path; - +use either::Either; use soroban_cli::{ commands::contract, config::{locator::KeyType, secret::Secret}, }; use soroban_test::{TestEnv, Wasm, TEST_ACCOUNT}; +use std::path::Path; pub const CUSTOM_TYPES: &Wasm = &Wasm::Custom("test-wasms", "test_custom_types"); @@ -54,10 +54,11 @@ pub async fn invoke_custom( let mut i: contract::invoke::Cmd = sandbox.cmd_with_config(&["--id", id, "--", func, arg], None); i.wasm = Some(wasm.to_path_buf()); - sandbox - .run_cmd_with(i, TEST_ACCOUNT) - .await - .map(|r| r.into_result().unwrap()) + let s = sandbox.run_cmd_with(i, TEST_ACCOUNT).await; + s.map(|r| match r { + Either::Left(help) => help, + Either::Right(tx) => tx.into_result().unwrap(), + }) } pub const DEFAULT_CONTRACT_ID: &str = "CDR6QKTWZQYW6YUJ7UP7XXZRLWQPFRV6SWBLQS4ZQOSAF4BOUD77OO5Z"; diff --git a/cmd/soroban-cli/src/commands/contract/arg_parsing.rs b/cmd/soroban-cli/src/commands/contract/arg_parsing.rs index 21fa2f383..9f04e4427 100644 --- a/cmd/soroban-cli/src/commands/contract/arg_parsing.rs +++ b/cmd/soroban-cli/src/commands/contract/arg_parsing.rs @@ -1,18 +1,18 @@ +use crate::xdr::{ + self, Hash, InvokeContractArgs, ScAddress, ScSpecEntry, ScSpecFunctionV0, ScSpecTypeDef, ScVal, + ScVec, +}; +use clap::error::ErrorKind::DisplayHelp; +use clap::value_parser; +use ed25519_dalek::SigningKey; +use heck::ToKebabCase; use std::collections::HashMap; use std::convert::TryInto; use std::ffi::OsString; use std::fmt::Debug; use std::path::PathBuf; -use clap::value_parser; -use ed25519_dalek::SigningKey; -use heck::ToKebabCase; - -use crate::xdr::{ - self, Hash, InvokeContractArgs, ScAddress, ScSpecEntry, ScSpecFunctionV0, ScSpecTypeDef, ScVal, - ScVec, -}; - +use crate::commands::contract::arg_parsing::HostFunctionParameters::{HelpMessage, Params}; use crate::commands::txn_result::TxnResult; use crate::config::{self}; use soroban_spec_tools::Spec; @@ -45,12 +45,17 @@ pub enum Error { MissingFileArg(PathBuf), } +pub enum HostFunctionParameters { + Params((String, Spec, InvokeContractArgs, Vec)), + HelpMessage(String), +} + pub fn build_host_function_parameters( contract_id: &stellar_strkey::Contract, slop: &[OsString], spec_entries: &[ScSpecEntry], config: &config::Args, -) -> Result<(String, Spec, InvokeContractArgs, Vec), Error> { +) -> Result { let spec = Spec(Some(spec_entries.to_vec())); let mut cmd = clap::Command::new(contract_id.to_string()) .no_binary_name(true) @@ -63,12 +68,20 @@ pub fn build_host_function_parameters( cmd.build(); let long_help = cmd.render_long_help(); - // get_matches_from exits the process if `help`, `--help` or `-h`are passed in the slop + // try_get_matches_from returns an error if `help`, `--help` or `-h`are passed in the slop // see clap documentation for more info: https://github.com/clap-rs/clap/blob/v4.1.8/src/builder/command.rs#L631 - let mut matches_ = cmd.get_matches_from(slop); - let Some((function, matches_)) = &matches_.remove_subcommand() else { - println!("{long_help}"); - std::process::exit(1); + let maybe_matches = cmd.try_get_matches_from(slop); + let Some((function, matches_)) = (match maybe_matches { + Ok(mut matches) => &matches.remove_subcommand(), + Err(e) => { + // to not exit immediately (to be able to fetch help message in tests), check for an error + if e.kind() == DisplayHelp { + return Ok(HelpMessage(e.to_string())); + } + e.exit(); + } + }) else { + return Ok(HelpMessage(format!("{long_help}"))); }; let func = spec.find_function(function)?; @@ -145,7 +158,7 @@ pub fn build_host_function_parameters( args: final_args, }; - Ok((function.clone(), spec, invoke_args, signers)) + Ok(Params((function.clone(), spec, invoke_args, signers))) } fn build_custom_cmd(name: &str, spec: &Spec) -> Result { diff --git a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs index 9bf63802c..4c22df616 100644 --- a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs +++ b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs @@ -1,8 +1,3 @@ -use std::array::TryFromSliceError; -use std::ffi::OsString; -use std::fmt::Debug; -use std::num::ParseIntError; - use crate::xdr::{ AccountId, ContractExecutable, ContractIdPreimage, ContractIdPreimageFromAddress, CreateContractArgs, CreateContractArgsV2, Error as XdrError, Hash, HostFunction, @@ -11,11 +6,17 @@ use crate::xdr::{ VecM, WriteXdr, }; use clap::{arg, command, Parser}; +use itertools::Either; +use itertools::Either::{Left, Right}; use rand::Rng; use regex::Regex; - use soroban_spec_tools::contract as contract_spec; +use std::array::TryFromSliceError; +use std::ffi::OsString; +use std::fmt::Debug; +use std::num::ParseIntError; +use crate::commands::contract::arg_parsing::HostFunctionParameters; use crate::{ assembled::simulate_and_assemble_transaction, commands::{ @@ -128,26 +129,31 @@ pub enum Error { impl Cmd { pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> { - let res = self - .run_against_rpc_server(Some(global_args), None) - .await? - .to_envelope(); + let res = self.run_against_rpc_server(Some(global_args), None).await?; match res { - TxnEnvelopeResult::TxnEnvelope(tx) => println!("{}", tx.to_xdr_base64(Limits::none())?), - TxnEnvelopeResult::Res(contract) => { - let network = self.config.get_network()?; - - if let Some(alias) = self.alias.clone() { - self.config.locator.save_contract_id( - &network.network_passphrase, - &contract, - &alias, - )?; - } - - println!("{contract}"); + Left(help) => { + println!("{help}"); } + Right(res) => match res.to_envelope() { + TxnEnvelopeResult::TxnEnvelope(tx) => { + println!("{}", tx.to_xdr_base64(Limits::none())?); + } + TxnEnvelopeResult::Res(contract) => { + let network = self.config.get_network()?; + + if let Some(alias) = self.alias.clone() { + self.config.locator.save_contract_id( + &network.network_passphrase, + &contract, + &alias, + )?; + } + + println!("{contract}"); + } + }, } + Ok(()) } } @@ -167,14 +173,14 @@ fn alias_validator(alias: &str) -> Result { #[async_trait::async_trait] impl NetworkRunnable for Cmd { type Error = Error; - type Result = TxnResult; + type Result = Either>; #[allow(clippy::too_many_lines)] async fn run_against_rpc_server( &self, global_args: Option<&global::Args>, config: Option<&config::Args>, - ) -> Result, Error> { + ) -> Result>, Error> { let print = Print::new(global_args.map_or(false, |a| a.quiet)); let config = config.unwrap_or(&self.config); let wasm_hash = if let Some(wasm) = &self.wasm { @@ -248,15 +254,18 @@ impl NetworkRunnable for Cmd { } else { let mut slop = vec![OsString::from(CONSTRUCTOR_FUNCTION_NAME)]; slop.extend_from_slice(&self.slop); - Some( - arg_parsing::build_host_function_parameters( - &stellar_strkey::Contract(contract_id.0), - &slop, - &entries, - config, - )? - .2, - ) + let params = arg_parsing::build_host_function_parameters( + &stellar_strkey::Contract(contract_id.0), + &slop, + &entries, + config, + )?; + match params { + HostFunctionParameters::Params(p) => Some(p.2), + HostFunctionParameters::HelpMessage(h) => { + return Ok(Left(h)); + } + } } } else { None @@ -276,7 +285,7 @@ impl NetworkRunnable for Cmd { if self.fee.build_only { print.checkln("Transaction built!"); - return Ok(TxnResult::Txn(txn)); + return Ok(Right(TxnResult::Txn(txn))); } print.infoln("Simulating deploy transaction…"); @@ -286,7 +295,7 @@ impl NetworkRunnable for Cmd { if self.fee.sim_only { print.checkln("Done!"); - return Ok(TxnResult::Txn(txn)); + return Ok(Right(TxnResult::Txn(txn))); } print.globeln("Submitting deploy transaction…"); @@ -307,7 +316,7 @@ impl NetworkRunnable for Cmd { print.checkln("Deployed!"); - Ok(TxnResult::Res(contract_id)) + Ok(Right(TxnResult::Res(contract_id))) } } diff --git a/cmd/soroban-cli/src/commands/contract/invoke.rs b/cmd/soroban-cli/src/commands/contract/invoke.rs index 04eeaebd6..d29e6738d 100644 --- a/cmd/soroban-cli/src/commands/contract/invoke.rs +++ b/cmd/soroban-cli/src/commands/contract/invoke.rs @@ -6,12 +6,15 @@ use std::str::FromStr; use std::{fmt::Debug, fs, io}; use clap::{arg, command, Parser, ValueEnum}; - +use itertools::Either; +use itertools::Either::{Left, Right}; use soroban_rpc::{Client, SimulateHostFunctionResult, SimulateTransactionResponse}; use soroban_spec::read::FromWasmError; use super::super::events; use super::arg_parsing; +use crate::commands::contract::arg_parsing::HostFunctionParameters; +use crate::commands::contract::arg_parsing::HostFunctionParameters::HelpMessage; use crate::{ assembled::simulate_and_assemble_transaction, commands::{ @@ -133,17 +136,27 @@ impl From for Error { impl Cmd { pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> { - let res = self.invoke(global_args).await?.to_envelope(); + let res = self.invoke(global_args).await?; match res { - TxnEnvelopeResult::TxnEnvelope(tx) => println!("{}", tx.to_xdr_base64(Limits::none())?), - TxnEnvelopeResult::Res(output) => { - println!("{output}"); + Right(res) => match res.to_envelope() { + TxnEnvelopeResult::TxnEnvelope(tx) => { + println!("{}", tx.to_xdr_base64(Limits::none())?); + } + TxnEnvelopeResult::Res(output) => { + println!("{output}"); + } + }, + Left(help) => { + println!("{help}"); } } Ok(()) } - pub async fn invoke(&self, global_args: &global::Args) -> Result, Error> { + pub async fn invoke( + &self, + global_args: &global::Args, + ) -> Result>, Error> { self.run_against_rpc_server(Some(global_args), None).await } @@ -206,13 +219,13 @@ impl Cmd { #[async_trait::async_trait] impl NetworkRunnable for Cmd { type Error = Error; - type Result = TxnResult; + type Result = Either>; async fn run_against_rpc_server( &self, global_args: Option<&global::Args>, config: Option<&config::Args>, - ) -> Result, Error> { + ) -> Result>, Error> { let config = config.unwrap_or(&self.config); let network = config.get_network()?; tracing::trace!(?network); @@ -223,7 +236,11 @@ impl NetworkRunnable for Cmd { let spec_entries = self.spec_entries()?; if let Some(spec_entries) = &spec_entries { // For testing wasm arg parsing - let _ = build_host_function_parameters(&contract_id, &self.slop, spec_entries, config)?; + let params = + build_host_function_parameters(&contract_id, &self.slop, spec_entries, config)?; + if let HelpMessage(s) = params { + return Ok(Left(s)); + } } let client = network.rpc_client()?; @@ -237,9 +254,14 @@ impl NetworkRunnable for Cmd { .await .map_err(Error::from)?; - let (function, spec, host_function_params, signers) = + let params = build_host_function_parameters(&contract_id, &self.slop, &spec_entries, config)?; + let (function, spec, host_function_params, signers) = match params { + HostFunctionParameters::Params(x) => x, + HelpMessage(s) => return Ok(Left(s)), + }; + let should_send_tx = self .should_send_after_sim(host_function_params.clone(), client.clone()) .await?; @@ -265,12 +287,12 @@ impl NetworkRunnable for Cmd { account_id, )?; if self.fee.build_only { - return Ok(TxnResult::Txn(tx)); + return Ok(Right(TxnResult::Txn(tx))); } let txn = simulate_and_assemble_transaction(&client, &tx).await?; let txn = self.fee.apply_to_assembled_txn(txn); if self.fee.sim_only { - return Ok(TxnResult::Txn(txn.transaction().clone())); + return Ok(Right(TxnResult::Txn(txn.transaction().clone()))); } let sim_res = txn.sim_response(); if global_args.map_or(true, |a| !a.no_cache) { @@ -306,7 +328,7 @@ impl NetworkRunnable for Cmd { } }; crate::log::events(&events); - Ok(output_to_string(&spec, &return_value, &function)?) + Ok(Right(output_to_string(&spec, &return_value, &function)?)) } } From 19ae5ba8101566888de1f5393748d4981470ec4b Mon Sep 17 00:00:00 2001 From: Gleb Date: Tue, 26 Nov 2024 10:40:25 -0800 Subject: [PATCH 02/14] Update cmd/soroban-cli/src/commands/contract/arg_parsing.rs Co-authored-by: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> --- cmd/soroban-cli/src/commands/contract/arg_parsing.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/soroban-cli/src/commands/contract/arg_parsing.rs b/cmd/soroban-cli/src/commands/contract/arg_parsing.rs index 9f04e4427..c038520b8 100644 --- a/cmd/soroban-cli/src/commands/contract/arg_parsing.rs +++ b/cmd/soroban-cli/src/commands/contract/arg_parsing.rs @@ -69,7 +69,7 @@ pub fn build_host_function_parameters( let long_help = cmd.render_long_help(); // try_get_matches_from returns an error if `help`, `--help` or `-h`are passed in the slop - // see clap documentation for more info: https://github.com/clap-rs/clap/blob/v4.1.8/src/builder/command.rs#L631 + // see clap documentation for more info: https://github.com/clap-rs/clap/blob/v4.1.8/src/builder/command.rs#L586 let maybe_matches = cmd.try_get_matches_from(slop); let Some((function, matches_)) = (match maybe_matches { Ok(mut matches) => &matches.remove_subcommand(), From 2523975988434535bf549c4bd1796f3e91ecd1e8 Mon Sep 17 00:00:00 2001 From: Gleb Date: Mon, 9 Dec 2024 16:16:51 -0800 Subject: [PATCH 03/14] Get rid of either --- Cargo.lock | 1 - cmd/crates/soroban-test/Cargo.toml | 1 - cmd/crates/soroban-test/src/lib.rs | 8 ++-- cmd/crates/soroban-test/tests/it/help.rs | 8 +++- cmd/crates/soroban-test/tests/it/util.rs | 6 +-- .../src/commands/contract/arg_parsing.rs | 22 +++++------ .../src/commands/contract/deploy/wasm.rs | 33 +++++++--------- .../src/commands/contract/invoke.rs | 39 +++++++------------ 8 files changed, 49 insertions(+), 69 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8dd9cbdb0..d5ca20513 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4599,7 +4599,6 @@ dependencies = [ "assert_cmd", "assert_fs", "ed25519-dalek", - "either", "fs_extra", "hex", "predicates", diff --git a/cmd/crates/soroban-test/Cargo.toml b/cmd/crates/soroban-test/Cargo.toml index d704b6fbd..d035652d1 100644 --- a/cmd/crates/soroban-test/Cargo.toml +++ b/cmd/crates/soroban-test/Cargo.toml @@ -25,7 +25,6 @@ sep5 = { workspace = true } soroban-cli = { workspace = true } soroban-rpc = { workspace = true } -either = "1.13.0" thiserror = "1.0.31" sha2 = "0.10.6" assert_cmd = "2.0.4" diff --git a/cmd/crates/soroban-test/src/lib.rs b/cmd/crates/soroban-test/src/lib.rs index 3a16b63ca..96074e23d 100644 --- a/cmd/crates/soroban-test/src/lib.rs +++ b/cmd/crates/soroban-test/src/lib.rs @@ -27,7 +27,6 @@ use std::{ffi::OsString, fmt::Display, path::Path}; use assert_cmd::{assert::Assert, Command}; use assert_fs::{fixture::FixtureError, prelude::PathChild, TempDir}; -use either::Either; use fs_extra::dir::CopyOptions; use soroban_cli::{ @@ -203,10 +202,9 @@ impl TestEnv { source: &str, ) -> Result { let cmd = self.cmd_with_config::(command_str, None); - self.run_cmd_with(cmd, source).await.map(|r| match r { - Either::Left(help) => help, - Either::Right(tx) => tx.into_result().unwrap(), - }) + self.run_cmd_with(cmd, source) + .await + .map(|tx| tx.into_result().unwrap()) } /// A convenience method for using the invoke command. diff --git a/cmd/crates/soroban-test/tests/it/help.rs b/cmd/crates/soroban-test/tests/it/help.rs index 12db8b9bd..7ddaf3afc 100644 --- a/cmd/crates/soroban-test/tests/it/help.rs +++ b/cmd/crates/soroban-test/tests/it/help.rs @@ -1,3 +1,5 @@ +use soroban_cli::commands::contract::arg_parsing::Error::HelpMessage; +use soroban_cli::commands::contract::invoke::Error::ArgParsing; use soroban_cli::commands::contract::{self, arg_parsing}; use soroban_test::TestEnv; @@ -5,7 +7,11 @@ use crate::util::{invoke_custom as invoke, CUSTOM_TYPES, DEFAULT_CONTRACT_ID}; async fn invoke_custom(func: &str, args: &str) -> Result { let e = &TestEnv::default(); - invoke(e, DEFAULT_CONTRACT_ID, func, args, &CUSTOM_TYPES.path()).await + let r = invoke(e, DEFAULT_CONTRACT_ID, func, args, &CUSTOM_TYPES.path()).await; + if let Err(ArgParsing(HelpMessage(e))) = r { + return Ok(e); + } + r } #[tokio::test] diff --git a/cmd/crates/soroban-test/tests/it/util.rs b/cmd/crates/soroban-test/tests/it/util.rs index 7923f7429..7f652bb68 100644 --- a/cmd/crates/soroban-test/tests/it/util.rs +++ b/cmd/crates/soroban-test/tests/it/util.rs @@ -1,4 +1,3 @@ -use either::Either; use soroban_cli::{ commands::contract, config::{locator::KeyType, secret::Secret}, @@ -55,10 +54,7 @@ pub async fn invoke_custom( sandbox.cmd_with_config(&["--id", id, "--", func, arg], None); i.wasm = Some(wasm.to_path_buf()); let s = sandbox.run_cmd_with(i, TEST_ACCOUNT).await; - s.map(|r| match r { - Either::Left(help) => help, - Either::Right(tx) => tx.into_result().unwrap(), - }) + s.map(|tx| tx.into_result().unwrap()) } pub const DEFAULT_CONTRACT_ID: &str = "CDR6QKTWZQYW6YUJ7UP7XXZRLWQPFRV6SWBLQS4ZQOSAF4BOUD77OO5Z"; diff --git a/cmd/soroban-cli/src/commands/contract/arg_parsing.rs b/cmd/soroban-cli/src/commands/contract/arg_parsing.rs index c038520b8..27350bb74 100644 --- a/cmd/soroban-cli/src/commands/contract/arg_parsing.rs +++ b/cmd/soroban-cli/src/commands/contract/arg_parsing.rs @@ -1,3 +1,6 @@ +use crate::commands::contract::arg_parsing::Error::HelpMessage; +use crate::commands::txn_result::TxnResult; +use crate::config::{self}; use crate::xdr::{ self, Hash, InvokeContractArgs, ScAddress, ScSpecEntry, ScSpecFunctionV0, ScSpecTypeDef, ScVal, ScVec, @@ -6,17 +9,13 @@ use clap::error::ErrorKind::DisplayHelp; use clap::value_parser; use ed25519_dalek::SigningKey; use heck::ToKebabCase; +use soroban_spec_tools::Spec; use std::collections::HashMap; use std::convert::TryInto; use std::ffi::OsString; use std::fmt::Debug; use std::path::PathBuf; -use crate::commands::contract::arg_parsing::HostFunctionParameters::{HelpMessage, Params}; -use crate::commands::txn_result::TxnResult; -use crate::config::{self}; -use soroban_spec_tools::Spec; - #[derive(thiserror::Error, Debug)] pub enum Error { #[error("parsing argument {arg}: {error}")] @@ -43,13 +42,12 @@ pub enum Error { MissingArgument(String), #[error("")] MissingFileArg(PathBuf), -} - -pub enum HostFunctionParameters { - Params((String, Spec, InvokeContractArgs, Vec)), + #[error("")] HelpMessage(String), } +pub type HostFunctionParameters = (String, Spec, InvokeContractArgs, Vec); + pub fn build_host_function_parameters( contract_id: &stellar_strkey::Contract, slop: &[OsString], @@ -76,12 +74,12 @@ pub fn build_host_function_parameters( Err(e) => { // to not exit immediately (to be able to fetch help message in tests), check for an error if e.kind() == DisplayHelp { - return Ok(HelpMessage(e.to_string())); + return Err(HelpMessage(e.to_string())); } e.exit(); } }) else { - return Ok(HelpMessage(format!("{long_help}"))); + return Err(HelpMessage(format!("{long_help}"))); }; let func = spec.find_function(function)?; @@ -158,7 +156,7 @@ pub fn build_host_function_parameters( args: final_args, }; - Ok(Params((function.clone(), spec, invoke_args, signers))) + Ok((function.clone(), spec, invoke_args, signers)) } fn build_custom_cmd(name: &str, spec: &Spec) -> Result { diff --git a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs index 8d6d9d253..bc4548b11 100644 --- a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs +++ b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs @@ -6,8 +6,6 @@ use crate::xdr::{ VecM, WriteXdr, }; use clap::{arg, command, Parser}; -use itertools::Either; -use itertools::Either::{Left, Right}; use rand::Rng; use regex::Regex; use soroban_spec_tools::contract as contract_spec; @@ -16,7 +14,8 @@ use std::ffi::OsString; use std::fmt::Debug; use std::num::ParseIntError; -use crate::commands::contract::arg_parsing::HostFunctionParameters; +use crate::commands::contract::arg_parsing::Error::HelpMessage; +use crate::commands::contract::deploy::wasm::Error::ArgParse; use crate::{ assembled::simulate_and_assemble_transaction, commands::{ @@ -129,12 +128,9 @@ pub enum Error { impl Cmd { pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> { - let res = self.run_against_rpc_server(Some(global_args), None).await?; + let res = self.run_against_rpc_server(Some(global_args), None).await; match res { - Left(help) => { - println!("{help}"); - } - Right(res) => match res.to_envelope() { + Ok(res) => match res.to_envelope() { TxnEnvelopeResult::TxnEnvelope(tx) => { println!("{}", tx.to_xdr_base64(Limits::none())?); } @@ -152,6 +148,10 @@ impl Cmd { println!("{contract}"); } }, + Err(ArgParse(HelpMessage(help))) => { + println!("{help}"); + } + Err(e) => return Err(e), } Ok(()) @@ -173,14 +173,14 @@ fn alias_validator(alias: &str) -> Result { #[async_trait::async_trait] impl NetworkRunnable for Cmd { type Error = Error; - type Result = Either>; + type Result = TxnResult; #[allow(clippy::too_many_lines)] async fn run_against_rpc_server( &self, global_args: Option<&global::Args>, config: Option<&config::Args>, - ) -> Result>, Error> { + ) -> Result, Error> { let print = Print::new(global_args.map_or(false, |a| a.quiet)); let config = config.unwrap_or(&self.config); let wasm_hash = if let Some(wasm) = &self.wasm { @@ -260,12 +260,7 @@ impl NetworkRunnable for Cmd { &entries, config, )?; - match params { - HostFunctionParameters::Params(p) => Some(p.2), - HostFunctionParameters::HelpMessage(h) => { - return Ok(Left(h)); - } - } + Some(params.2) } } else { None @@ -285,7 +280,7 @@ impl NetworkRunnable for Cmd { if self.fee.build_only { print.checkln("Transaction built!"); - return Ok(Right(TxnResult::Txn(txn))); + return Ok(TxnResult::Txn(txn)); } print.infoln("Simulating deploy transaction…"); @@ -295,7 +290,7 @@ impl NetworkRunnable for Cmd { if self.fee.sim_only { print.checkln("Done!"); - return Ok(Right(TxnResult::Txn(txn))); + return Ok(TxnResult::Txn(txn)); } print.globeln("Submitting deploy transaction…"); @@ -316,7 +311,7 @@ impl NetworkRunnable for Cmd { print.checkln("Deployed!"); - Ok(Right(TxnResult::Res(contract_id))) + Ok(TxnResult::Res(contract_id)) } } diff --git a/cmd/soroban-cli/src/commands/contract/invoke.rs b/cmd/soroban-cli/src/commands/contract/invoke.rs index d56778451..a42f0a926 100644 --- a/cmd/soroban-cli/src/commands/contract/invoke.rs +++ b/cmd/soroban-cli/src/commands/contract/invoke.rs @@ -6,15 +6,13 @@ use std::str::FromStr; use std::{fmt::Debug, fs, io}; use clap::{arg, command, Parser, ValueEnum}; -use itertools::Either; -use itertools::Either::{Left, Right}; use soroban_rpc::{Client, SimulateHostFunctionResult, SimulateTransactionResponse}; use soroban_spec::read::FromWasmError; use super::super::events; use super::arg_parsing; -use crate::commands::contract::arg_parsing::HostFunctionParameters; -use crate::commands::contract::arg_parsing::HostFunctionParameters::HelpMessage; +use crate::commands::contract::arg_parsing::Error::HelpMessage; +use crate::commands::contract::invoke::Error::ArgParsing; use crate::{ assembled::simulate_and_assemble_transaction, commands::{ @@ -134,9 +132,9 @@ impl From for Error { impl Cmd { pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> { - let res = self.invoke(global_args).await?; + let res = self.invoke(global_args).await; match res { - Right(res) => match res.to_envelope() { + Ok(res) => match res.to_envelope() { TxnEnvelopeResult::TxnEnvelope(tx) => { println!("{}", tx.to_xdr_base64(Limits::none())?); } @@ -144,17 +142,15 @@ impl Cmd { println!("{output}"); } }, - Left(help) => { + Err(ArgParsing(HelpMessage(help))) => { println!("{help}"); } + Err(e) => return Err(e), } Ok(()) } - pub async fn invoke( - &self, - global_args: &global::Args, - ) -> Result>, Error> { + pub async fn invoke(&self, global_args: &global::Args) -> Result, Error> { self.run_against_rpc_server(Some(global_args), None).await } @@ -217,13 +213,13 @@ impl Cmd { #[async_trait::async_trait] impl NetworkRunnable for Cmd { type Error = Error; - type Result = Either>; + type Result = TxnResult; async fn run_against_rpc_server( &self, global_args: Option<&global::Args>, config: Option<&config::Args>, - ) -> Result>, Error> { + ) -> Result, Error> { let config = config.unwrap_or(&self.config); let network = config.get_network()?; tracing::trace!(?network); @@ -234,11 +230,7 @@ impl NetworkRunnable for Cmd { let spec_entries = self.spec_entries()?; if let Some(spec_entries) = &spec_entries { // For testing wasm arg parsing - let params = - build_host_function_parameters(&contract_id, &self.slop, spec_entries, config)?; - if let HelpMessage(s) = params { - return Ok(Left(s)); - } + build_host_function_parameters(&contract_id, &self.slop, spec_entries, config)?; } let client = network.rpc_client()?; @@ -255,10 +247,7 @@ impl NetworkRunnable for Cmd { let params = build_host_function_parameters(&contract_id, &self.slop, &spec_entries, config)?; - let (function, spec, host_function_params, signers) = match params { - HostFunctionParameters::Params(x) => x, - HelpMessage(s) => return Ok(Left(s)), - }; + let (function, spec, host_function_params, signers) = params; let should_send_tx = self .should_send_after_sim(host_function_params.clone(), client.clone()) @@ -285,13 +274,13 @@ impl NetworkRunnable for Cmd { account_id, )?); if self.fee.build_only { - return Ok(Right(TxnResult::Txn(tx))); + return Ok(TxnResult::Txn(tx)); } let txn = simulate_and_assemble_transaction(&client, &tx).await?; let assembled = self.fee.apply_to_assembled_txn(txn); let mut txn = Box::new(assembled.transaction().clone()); if self.fee.sim_only { - return Ok(Right(TxnResult::Txn(txn))); + return Ok(TxnResult::Txn(txn)); } let sim_res = assembled.sim_response(); if global_args.map_or(true, |a| !a.no_cache) { @@ -326,7 +315,7 @@ impl NetworkRunnable for Cmd { } }; crate::log::events(&events); - Ok(Right(output_to_string(&spec, &return_value, &function)?)) + Ok(output_to_string(&spec, &return_value, &function)?) } } From a6640255ea7f7bc41118a04bc2f3765da4ce173f Mon Sep 17 00:00:00 2001 From: Gleb Date: Fri, 20 Dec 2024 11:19:59 -0800 Subject: [PATCH 04/14] 1682 fix init test (#1738) * Fix init test * Update sdk version --- cmd/crates/soroban-test/tests/it/init.rs | 79 +++---------------- .../soroban-test/tests/it/integration.rs | 1 + .../soroban-test/tests/it/integration/init.rs | 62 +++++++++++++++ .../Cargo.toml.removeextension | 2 +- 4 files changed, 75 insertions(+), 69 deletions(-) create mode 100644 cmd/crates/soroban-test/tests/it/integration/init.rs diff --git a/cmd/crates/soroban-test/tests/it/init.rs b/cmd/crates/soroban-test/tests/it/init.rs index c3cc9b694..f21fe042e 100644 --- a/cmd/crates/soroban-test/tests/it/init.rs +++ b/cmd/crates/soroban-test/tests/it/init.rs @@ -1,14 +1,13 @@ use assert_fs::prelude::*; use predicates::prelude::predicate; -use soroban_test::{AssertExt, TestEnv}; +use soroban_test::TestEnv; #[test] fn init() { let sandbox = TestEnv::default(); - let major = soroban_cli::commands::version::pkg() - .split('.') - .next() - .unwrap(); + let cli_version = soroban_cli::commands::version::pkg(); + let major = cli_version.split('.').next().unwrap(); + let is_rc = cli_version.contains("rc"); sandbox .new_assert_cmd("contract") .arg("init") @@ -20,68 +19,12 @@ fn init() { .child("Cargo.toml") .assert(predicate::function(|c: &str| { let table = toml::from_str::(c).unwrap(); - table["workspace"]["dependencies"]["soroban-sdk"].as_str() - == Some(&format!("{major}.0.0")) + let sdk_version = table["workspace"]["dependencies"]["soroban-sdk"].as_str(); + println!("Check expected version {major}.0.0 matches template's {sdk_version:?}"); + if is_rc { + sdk_version.and_then(|x| x.split('-').next()) == Some(&format!("{major}.0.0")) + } else { + sdk_version == Some(&format!("{major}.0.0")) + } })); } - -#[test] -fn init_and_deploy() { - let name = "hello_world"; - let sandbox = TestEnv::default(); - - sandbox - .new_assert_cmd("contract") - .arg("init") - .arg("--name") - .arg(name) - .arg("project") - .assert() - .success(); - - let manifest_path = sandbox - .dir() - .join(format!("project/contracts/{name}/Cargo.toml")); - assert!(manifest_path.exists()); - - sandbox - .new_assert_cmd("contract") - .arg("build") - .arg("--manifest-path") - .arg(manifest_path) - .assert() - .success(); - - let target_dir = sandbox - .dir() - .join("project/target/wasm32-unknown-unknown/release"); - assert!(target_dir.exists()); - - let assert = sandbox - .new_assert_cmd("contract") - .arg("deploy") - .arg("--wasm") - .arg(target_dir.join(format!("{name}.wasm"))) - .assert(); - - let contract = assert.stdout_as_str(); - - assert.success(); - - let assert = sandbox - .new_assert_cmd("contract") - .arg("invoke") - .arg("--id") - .arg(contract) - .arg("--") - .arg("hello") - .arg("--to") - .arg("bar") - .assert(); - - let output = assert.stdout_as_str(); - - assert_eq!(output, r#"["Hello","bar"]"#); - - assert.success(); -} diff --git a/cmd/crates/soroban-test/tests/it/integration.rs b/cmd/crates/soroban-test/tests/it/integration.rs index 3ec0d61ed..c7a3fbec5 100644 --- a/cmd/crates/soroban-test/tests/it/integration.rs +++ b/cmd/crates/soroban-test/tests/it/integration.rs @@ -4,6 +4,7 @@ mod cookbook; mod custom_types; mod dotenv; mod hello_world; +mod init; mod keys; mod snapshot; mod tx; diff --git a/cmd/crates/soroban-test/tests/it/integration/init.rs b/cmd/crates/soroban-test/tests/it/integration/init.rs new file mode 100644 index 000000000..e47d44c81 --- /dev/null +++ b/cmd/crates/soroban-test/tests/it/integration/init.rs @@ -0,0 +1,62 @@ +use soroban_test::{AssertExt, TestEnv}; + +#[test] +fn init_and_deploy() { + let name = "hello_world"; + let sandbox = TestEnv::default(); + + sandbox + .new_assert_cmd("contract") + .arg("init") + .arg("--name") + .arg(name) + .arg("project") + .assert() + .success(); + + let manifest_path = sandbox + .dir() + .join(format!("project/contracts/{name}/Cargo.toml")); + assert!(manifest_path.exists()); + + sandbox + .new_assert_cmd("contract") + .arg("build") + .arg("--manifest-path") + .arg(manifest_path) + .assert() + .success(); + + let target_dir = sandbox + .dir() + .join("project/target/wasm32-unknown-unknown/release"); + assert!(target_dir.exists()); + + let assert = sandbox + .new_assert_cmd("contract") + .arg("deploy") + .arg("--wasm") + .arg(target_dir.join(format!("{name}.wasm"))) + .assert(); + + let contract = assert.stdout_as_str(); + + assert.success(); + + let assert = sandbox + .new_assert_cmd("contract") + .arg("invoke") + .arg("--id") + .arg(contract) + .arg("--") + .arg("hello") + .arg("--to") + .arg("bar") + .assert(); + + let output = assert.stdout_as_str(); + + assert_eq!(output, r#"["Hello","bar"]"#); + + assert.success(); +} diff --git a/cmd/soroban-cli/src/utils/contract-workspace-template/Cargo.toml.removeextension b/cmd/soroban-cli/src/utils/contract-workspace-template/Cargo.toml.removeextension index c0d6366e0..a04ea4fb3 100644 --- a/cmd/soroban-cli/src/utils/contract-workspace-template/Cargo.toml.removeextension +++ b/cmd/soroban-cli/src/utils/contract-workspace-template/Cargo.toml.removeextension @@ -5,7 +5,7 @@ members = [ ] [workspace.dependencies] -soroban-sdk = "21.0.0" +soroban-sdk = "22.0.0" [profile.release] opt-level = "z" From b63a40ba46f3490a0b36817989399c92623bf5e1 Mon Sep 17 00:00:00 2001 From: Gleb Date: Fri, 20 Dec 2024 11:20:12 -0800 Subject: [PATCH 05/14] 1682 fix config/version tests (#1737) * fix config tests * Fix version test * fix: clippy --------- Co-authored-by: Willem Wyndham --- cmd/crates/soroban-test/tests/it/config.rs | 96 +++++++++++---------- cmd/crates/soroban-test/tests/it/version.rs | 2 +- cmd/soroban-cli/src/config/mod.rs | 8 +- 3 files changed, 60 insertions(+), 46 deletions(-) diff --git a/cmd/crates/soroban-test/tests/it/config.rs b/cmd/crates/soroban-test/tests/it/config.rs index b796910a8..fb751ecf1 100644 --- a/cmd/crates/soroban-test/tests/it/config.rs +++ b/cmd/crates/soroban-test/tests/it/config.rs @@ -19,20 +19,27 @@ fn ls(sandbox: &TestEnv) -> Vec { .collect::>() } +pub const NETWORKS: &str = "\ +local +futurenet +mainnet +testnet +"; + #[test] fn set_and_remove_network() { TestEnv::with_default(|sandbox| { - add_network(sandbox, "local"); - let dir = sandbox.dir().join(".soroban").join("network"); + add_network(sandbox, "custom"); + let dir = sandbox.dir().join(".stellar").join("network"); let mut read_dir = std::fs::read_dir(dir).unwrap(); let file = read_dir.next().unwrap().unwrap(); - assert_eq!(file.file_name().to_str().unwrap(), "local.toml"); + assert_eq!(file.file_name().to_str().unwrap(), "custom.toml"); let res = ls(sandbox); - assert_eq!(res[0], "local"); + assert_eq!(res[0], "custom"); sandbox .new_assert_cmd("network") .arg("rm") - .arg("local") + .arg("custom") .assert() .success(); @@ -40,37 +47,7 @@ fn set_and_remove_network() { .new_assert_cmd("network") .arg("ls") .assert() - .stdout("\n"); - }); -} - -#[test] -fn use_default_futurenet() { - TestEnv::with_default(|sandbox| { - sandbox - .new_assert_cmd("keys") - .args(["generate", "alice", "--network", "futurenet"]) - .assert() - .success(); - let dir = sandbox.dir().join(".soroban").join("network"); - let mut read_dir = std::fs::read_dir(dir).unwrap(); - let file = read_dir.next().unwrap().unwrap(); - assert_eq!(file.file_name().to_str().unwrap(), "futurenet.toml"); - }); -} - -#[test] -fn use_default_testnet() { - TestEnv::with_default(|sandbox| { - sandbox - .new_assert_cmd("keys") - .args(["generate", "alice", "--network", "testnet"]) - .assert() - .success(); - let dir = sandbox.dir().join(".soroban").join("network"); - let mut read_dir = std::fs::read_dir(dir).unwrap(); - let file = read_dir.next().unwrap().unwrap(); - assert_eq!(file.file_name().to_str().unwrap(), "testnet.toml"); + .stdout(NETWORKS); }); } @@ -118,7 +95,7 @@ fn set_and_remove_global_network() { .arg("ls") .arg("--global") .assert() - .stdout("global\n"); + .stdout(format!("global\n{NETWORKS}")); sandbox .new_assert_cmd("network") @@ -134,7 +111,7 @@ fn set_and_remove_global_network() { .env("XDG_CONFIG_HOME", dir.to_str().unwrap()) .arg("ls") .assert() - .stdout("\n"); + .stdout(NETWORKS); } #[test] @@ -142,15 +119,35 @@ fn multiple_networks() { let sandbox = TestEnv::default(); let ls = || -> Vec { ls(&sandbox) }; - add_network(&sandbox, "local"); + println!("{:#?}", ls()); + add_network(&sandbox, "custom"); println!("{:#?}", ls()); add_network(&sandbox, "local2"); - assert_eq!(ls().as_slice(), ["local".to_owned(), "local2".to_owned()]); + assert_eq!( + ls().as_slice(), + [ + "custom".to_owned(), + "local2".to_owned(), + "local".to_owned(), + "futurenet".to_owned(), + "mainnet".to_owned(), + "testnet".to_owned() + ] + ); - sandbox.cmd::("local").run().unwrap(); + sandbox.cmd::("custom").run().unwrap(); - assert_eq!(ls().as_slice(), ["local2".to_owned()]); + assert_eq!( + ls().as_slice(), + [ + "local2".to_owned(), + "local".to_owned(), + "futurenet".to_owned(), + "mainnet".to_owned(), + "testnet".to_owned() + ] + ); let sub_dir = sandbox.dir().join("sub_directory"); fs::create_dir(&sub_dir).unwrap(); @@ -168,7 +165,17 @@ fn multiple_networks() { .run() .unwrap(); - assert_eq!(ls().as_slice(), ["local2".to_owned(), "local3".to_owned()]); + assert_eq!( + ls().as_slice(), + [ + "local2".to_owned(), + "local3".to_owned(), + "local".to_owned(), + "futurenet".to_owned(), + "mainnet".to_owned(), + "testnet".to_owned() + ] + ); } #[test] @@ -205,7 +212,7 @@ fn generate_key() { .assert() .stdout(predicates::str::contains("test_2\n")); let file_contents = - fs::read_to_string(sandbox.dir().join(".soroban/identity/test_2.toml")).unwrap(); + fs::read_to_string(sandbox.dir().join(".stellar/identity/test_2.toml")).unwrap(); assert_eq!( file_contents, format!("seed_phrase = \"{DEFAULT_SEED_PHRASE}\"\n") @@ -368,6 +375,7 @@ fn set_default_identity() { sandbox .new_assert_cmd("env") + .env_remove("SOROBAN_ACCOUNT") .assert() .stdout(predicate::str::contains("STELLAR_ACCOUNT=alice")) .success(); diff --git a/cmd/crates/soroban-test/tests/it/version.rs b/cmd/crates/soroban-test/tests/it/version.rs index cb7826fa4..878dbd758 100644 --- a/cmd/crates/soroban-test/tests/it/version.rs +++ b/cmd/crates/soroban-test/tests/it/version.rs @@ -8,5 +8,5 @@ fn version() { .new_assert_cmd("version") .assert() .success() - .stdout(format!("soroban {}\n", long())); + .stdout(format!("stellar {}\n", long())); } diff --git a/cmd/soroban-cli/src/config/mod.rs b/cmd/soroban-cli/src/config/mod.rs index a429ff434..af374b634 100644 --- a/cmd/soroban-cli/src/config/mod.rs +++ b/cmd/soroban-cli/src/config/mod.rs @@ -192,7 +192,13 @@ impl Config { pub fn save(&self) -> Result<(), locator::Error> { let toml_string = toml::to_string(&self)?; - let mut file = File::create(locator::config_file()?)?; + let path = locator::config_file()?; + let parent = path.parent(); + if let Some(parent) = parent { + fs::create_dir_all(parent)?; + } + // Depending on the platform, this function may fail if the full directory path does not exist + let mut file = File::create(path)?; file.write_all(toml_string.as_bytes())?; Ok(()) From 2c215cdf30dfda2d0de0fc582cfecd124c3818b2 Mon Sep 17 00:00:00 2001 From: Gleb Date: Fri, 20 Dec 2024 11:20:23 -0800 Subject: [PATCH 06/14] #1682: fix build tests (#1736) * Fix build tests * revert implementation --- Cargo.lock | 1 + cmd/crates/soroban-test/Cargo.toml | 1 + cmd/crates/soroban-test/tests/it/build.rs | 59 ++++++++++++++--------- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6789ea504..bf7893a4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4601,6 +4601,7 @@ dependencies = [ "ed25519-dalek", "fs_extra", "hex", + "home", "predicates", "sep5", "serde_json", diff --git a/cmd/crates/soroban-test/Cargo.toml b/cmd/crates/soroban-test/Cargo.toml index d035652d1..61fcc0bd8 100644 --- a/cmd/crates/soroban-test/Cargo.toml +++ b/cmd/crates/soroban-test/Cargo.toml @@ -32,6 +32,7 @@ assert_fs = "1.0.7" predicates = { workspace = true } fs_extra = "1.3.0" toml = { workspace = true } +home = "0.5.9" [dev-dependencies] diff --git a/cmd/crates/soroban-test/tests/it/build.rs b/cmd/crates/soroban-test/tests/it/build.rs index cb63ae907..a2b771cce 100644 --- a/cmd/crates/soroban-test/tests/it/build.rs +++ b/cmd/crates/soroban-test/tests/it/build.rs @@ -2,6 +2,7 @@ use predicates::prelude::predicate; use soroban_cli::xdr::{Limited, Limits, ReadXdr, ScMetaEntry, ScMetaV0}; use soroban_spec_tools::contract::Spec; use soroban_test::TestEnv; +use std::env; use std::io::Cursor; #[test] @@ -16,11 +17,9 @@ fn build_all() { .arg("--print-commands-only") .assert() .success() - .stdout(predicate::eq("\ -cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release + .stdout(predicate::eq(with_flags("cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release cargo rustc --manifest-path=contracts/call/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release -cargo rustc --manifest-path=contracts/add/add2/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release -")); +cargo rustc --manifest-path=contracts/add/add2/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release"))); } #[test] @@ -36,9 +35,7 @@ fn build_package_by_name() { .arg("--package=add") .assert() .success() - .stdout(predicate::eq("\ -cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release -")); + .stdout(predicate::eq(with_flags("cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release"))); } #[test] @@ -54,9 +51,7 @@ fn build_package_by_current_dir() { .assert() .success() .stdout(predicate::eq( - "\ -cargo rustc --manifest-path=Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release -", + with_flags("cargo rustc --manifest-path=Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release"), )); } @@ -85,6 +80,7 @@ fn build_all_when_in_non_package_directory() { let sandbox = TestEnv::default(); let cargo_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); let fixture_path = cargo_dir.join("tests/fixtures/workspace/contracts/add/src/"); + sandbox .new_assert_cmd("contract") .current_dir(fixture_path) @@ -92,13 +88,9 @@ fn build_all_when_in_non_package_directory() { .arg("--print-commands-only") .assert() .success() - .stdout(predicate::eq( - "\ -cargo rustc --manifest-path=../Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release -cargo rustc --manifest-path=../../call/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release -cargo rustc --manifest-path=../add2/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release -", - )); + .stdout(predicate::eq(with_flags( + "cargo rustc --manifest-path=../Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release" + ))); } #[test] @@ -113,11 +105,7 @@ fn build_default_members() { .arg("--print-commands-only") .assert() .success() - .stdout(predicate::eq( - "\ -cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release -", - )); + .stdout(predicate::eq(with_flags("cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release"))); } #[test] @@ -185,3 +173,30 @@ fn build_with_metadata() { assert_eq!(entries, expected_entries); } + +fn with_flags(expected: &str) -> String { + let cargo_home = home::cargo_home().unwrap(); + let cargo_home = format!("{}", cargo_home.display()); + let registry_prefix = format!("{cargo_home}/registry/src/"); + + let vec: Vec<_> = if env::var("RUSTFLAGS").is_ok() { + expected.split("\n").map(|x| x.to_string()).collect() + } else { + expected + .split("\n") + .map(|x| { + format!( + "CARGO_BUILD_RUSTFLAGS=--remap-path-prefix={}= {}", + registry_prefix, x + ) + }) + .collect() + }; + + return format!( + "\ +{} +", + vec.join("\n") + ); +} From ed53892f23f2b863d2842bab4736c6411e982975 Mon Sep 17 00:00:00 2001 From: Gleb Date: Fri, 20 Dec 2024 12:16:13 -0800 Subject: [PATCH 07/14] clippy --- cmd/crates/soroban-test/tests/it/build.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/cmd/crates/soroban-test/tests/it/build.rs b/cmd/crates/soroban-test/tests/it/build.rs index a2b771cce..8ad6a7968 100644 --- a/cmd/crates/soroban-test/tests/it/build.rs +++ b/cmd/crates/soroban-test/tests/it/build.rs @@ -180,23 +180,18 @@ fn with_flags(expected: &str) -> String { let registry_prefix = format!("{cargo_home}/registry/src/"); let vec: Vec<_> = if env::var("RUSTFLAGS").is_ok() { - expected.split("\n").map(|x| x.to_string()).collect() + expected.split('\n').map(ToString::to_string).collect() } else { expected - .split("\n") - .map(|x| { - format!( - "CARGO_BUILD_RUSTFLAGS=--remap-path-prefix={}= {}", - registry_prefix, x - ) - }) + .split('\n') + .map(|x| format!("CARGO_BUILD_RUSTFLAGS=--remap-path-prefix={registry_prefix}= {x}",)) .collect() }; - return format!( + format!( "\ {} ", vec.join("\n") - ); + ) } From 461e828cf4d9159a72f97d41f511300b2e19baf2 Mon Sep 17 00:00:00 2001 From: Gleb Date: Fri, 20 Dec 2024 13:39:41 -0800 Subject: [PATCH 08/14] Exclude integration tests --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 7aa1832ba..258fd3419 100644 --- a/Makefile +++ b/Makefile @@ -48,7 +48,8 @@ generate-full-help-doc: cargo run --bin doc-gen --features clap-markdown test: build-test - cargo test --workspace + cargo test --workspace --exclude soroban-test + cargo test -p soroban-test -- --skip integration:: e2e-test: cargo test --features it --test it -- integration From fc109c04ca3b9e1bc93621da7c068f7572a47c6e Mon Sep 17 00:00:00 2001 From: Gleb Date: Fri, 20 Dec 2024 13:52:49 -0800 Subject: [PATCH 09/14] ignore test and update version --- cmd/crates/soroban-test/tests/it/build.rs | 1 + cmd/crates/soroban-test/tests/it/rpc_provider.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/crates/soroban-test/tests/it/build.rs b/cmd/crates/soroban-test/tests/it/build.rs index 8ad6a7968..e64b7fdab 100644 --- a/cmd/crates/soroban-test/tests/it/build.rs +++ b/cmd/crates/soroban-test/tests/it/build.rs @@ -109,6 +109,7 @@ fn build_default_members() { } #[test] +#[ignore] // TODO: unignore -- reproduces unfixed bug https://github.com/stellar/stellar-cli/issues/1694 fn build_with_metadata() { let sandbox = TestEnv::default(); let cargo_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); diff --git a/cmd/crates/soroban-test/tests/it/rpc_provider.rs b/cmd/crates/soroban-test/tests/it/rpc_provider.rs index 04c4c1bdc..899f11d00 100644 --- a/cmd/crates/soroban-test/tests/it/rpc_provider.rs +++ b/cmd/crates/soroban-test/tests/it/rpc_provider.rs @@ -35,7 +35,7 @@ fn mock_generate_account(server: &MockServer) -> Mock { when.method(GET) .path("/friendbot") .header("accept", "*/*") - .header("user-agent", "soroban-cli/22.0.1"); //update this to be future proof + .header("user-agent", "soroban-cli/22.1.0"); // TODO: update this to be future-proof then.status(200); }) } From 0fdb76c27160370a9a27f8e8e91b48044796c959 Mon Sep 17 00:00:00 2001 From: Gleb Date: Fri, 20 Dec 2024 16:51:04 -0800 Subject: [PATCH 10/14] PR comments --- cmd/crates/soroban-test/tests/it/config.rs | 5 ++--- cmd/soroban-cli/src/config/mod.rs | 6 +----- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/cmd/crates/soroban-test/tests/it/config.rs b/cmd/crates/soroban-test/tests/it/config.rs index ea925ca59..acd04c062 100644 --- a/cmd/crates/soroban-test/tests/it/config.rs +++ b/cmd/crates/soroban-test/tests/it/config.rs @@ -19,12 +19,11 @@ fn ls(sandbox: &TestEnv) -> Vec { .collect::>() } -pub const NETWORKS: &str = "\ -local +pub const NETWORKS: &str = r#"local futurenet mainnet testnet -"; +"#; #[test] fn set_and_remove_network() { diff --git a/cmd/soroban-cli/src/config/mod.rs b/cmd/soroban-cli/src/config/mod.rs index 1465ed4b4..499896437 100644 --- a/cmd/soroban-cli/src/config/mod.rs +++ b/cmd/soroban-cli/src/config/mod.rs @@ -195,12 +195,8 @@ impl Config { pub fn save(&self) -> Result<(), locator::Error> { let toml_string = toml::to_string(&self)?; let path = locator::config_file()?; - let parent = path.parent(); - if let Some(parent) = parent { - fs::create_dir_all(parent)?; - } // Depending on the platform, this function may fail if the full directory path does not exist - let mut file = File::create(path)?; + let mut file = File::create(locator::ensure_directory(path)?)?; file.write_all(toml_string.as_bytes())?; Ok(()) From 6e487498bf184306cda46930c47ae38e5fbf66ca Mon Sep 17 00:00:00 2001 From: Gleb Date: Tue, 4 Feb 2025 16:01:46 -0800 Subject: [PATCH 11/14] Refactoring --- cmd/crates/soroban-test/tests/it/config.rs | 4 +- cmd/soroban-cli/src/cli.rs | 17 ++++ .../src/commands/contract/deploy/wasm.rs | 78 +++++++++---------- .../src/commands/contract/invoke.rs | 18 +---- 4 files changed, 60 insertions(+), 57 deletions(-) diff --git a/cmd/crates/soroban-test/tests/it/config.rs b/cmd/crates/soroban-test/tests/it/config.rs index acd04c062..719f6b325 100644 --- a/cmd/crates/soroban-test/tests/it/config.rs +++ b/cmd/crates/soroban-test/tests/it/config.rs @@ -19,11 +19,11 @@ fn ls(sandbox: &TestEnv) -> Vec { .collect::>() } -pub const NETWORKS: &str = r#"local +pub const NETWORKS: &str = r"local futurenet mainnet testnet -"#; +"; #[test] fn set_and_remove_network() { diff --git a/cmd/soroban-cli/src/cli.rs b/cmd/soroban-cli/src/cli.rs index 3baa54ecf..616a81b29 100644 --- a/cmd/soroban-cli/src/cli.rs +++ b/cmd/soroban-cli/src/cli.rs @@ -2,10 +2,16 @@ use clap::CommandFactory; use dotenvy::dotenv; use tracing_subscriber::{fmt, EnvFilter}; +use crate::commands::contract::arg_parsing::Error::HelpMessage; +use crate::commands::contract::deploy::wasm::Error::ArgParse; +use crate::commands::contract::invoke::Error::ArgParsing; +use crate::commands::contract::Error::{Deploy, Invoke}; +use crate::commands::Error::Contract; use crate::config::Config; use crate::print::Print; use crate::upgrade_check::upgrade_check; use crate::{commands, Root}; +use std::error::Error; #[tokio::main] pub async fn main() { @@ -86,6 +92,17 @@ pub async fn main() { let printer = Print::new(root.global_args.quiet); if let Err(e) = root.run().await { + // TODO: source is None (should be HelpMessage) + let _source = commands::Error::source(&e); + // TODO use source instead + if let Contract(Invoke(ArgParsing(HelpMessage(help)))) = e { + println!("{help}"); + std::process::exit(1); + } + if let Contract(Deploy(ArgParse(HelpMessage(help)))) = e { + println!("{help}"); + std::process::exit(1); + } printer.errorln(format!("error: {e}")); std::process::exit(1); } diff --git a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs index 581d203c1..f793d61bd 100644 --- a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs +++ b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs @@ -13,10 +13,9 @@ use crate::xdr::{ }; use clap::{arg, command, Parser}; use rand::Rng; + use soroban_spec_tools::contract as contract_spec; -use crate::commands::contract::arg_parsing::Error::HelpMessage; -use crate::commands::contract::deploy::wasm::Error::ArgParse; use crate::{ assembled::simulate_and_assemble_transaction, commands::{ @@ -129,42 +128,37 @@ pub enum Error { impl Cmd { pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> { - let res = self.run_against_rpc_server(Some(global_args), None).await; + let res = self + .run_against_rpc_server(Some(global_args), None) + .await? + .to_envelope(); match res { - Ok(res) => match res.to_envelope() { - TxnEnvelopeResult::TxnEnvelope(tx) => { - println!("{}", tx.to_xdr_base64(Limits::none())?); - } - TxnEnvelopeResult::Res(contract) => { - let network = self.config.get_network()?; - - if let Some(alias) = self.alias.clone() { - if let Some(existing_contract) = self - .config - .locator - .get_contract_id(&alias, &network.network_passphrase)? - { - let print = Print::new(global_args.quiet); - print.warnln(format!( - "Overwriting existing contract id: {existing_contract}" - )); - }; - self.config.locator.save_contract_id( - &network.network_passphrase, - &contract, - &alias, - )?; - } - - println!("{contract}"); + TxnEnvelopeResult::TxnEnvelope(tx) => println!("{}", tx.to_xdr_base64(Limits::none())?), + TxnEnvelopeResult::Res(contract) => { + let network = self.config.get_network()?; + + if let Some(alias) = self.alias.clone() { + if let Some(existing_contract) = self + .config + .locator + .get_contract_id(&alias, &network.network_passphrase)? + { + let print = Print::new(global_args.quiet); + print.warnln(format!( + "Overwriting existing contract id: {existing_contract}" + )); + }; + + self.config.locator.save_contract_id( + &network.network_passphrase, + &contract, + &alias, + )?; } - }, - Err(ArgParse(HelpMessage(help))) => { - println!("{help}"); + + println!("{contract}"); } - Err(e) => return Err(e), } - Ok(()) } } @@ -253,13 +247,15 @@ impl NetworkRunnable for Cmd { } else { let mut slop = vec![OsString::from(CONSTRUCTOR_FUNCTION_NAME)]; slop.extend_from_slice(&self.slop); - let params = arg_parsing::build_host_function_parameters( - &stellar_strkey::Contract(contract_id.0), - &slop, - &entries, - config, - )?; - Some(params.2) + Some( + arg_parsing::build_host_function_parameters( + &stellar_strkey::Contract(contract_id.0), + &slop, + &entries, + config, + )? + .2, + ) } } else { None diff --git a/cmd/soroban-cli/src/commands/contract/invoke.rs b/cmd/soroban-cli/src/commands/contract/invoke.rs index 1baef8270..092e1d188 100644 --- a/cmd/soroban-cli/src/commands/contract/invoke.rs +++ b/cmd/soroban-cli/src/commands/contract/invoke.rs @@ -12,8 +12,6 @@ use soroban_spec::read::FromWasmError; use super::super::events; use super::arg_parsing; use crate::assembled::Assembled; -use crate::commands::contract::arg_parsing::Error::HelpMessage; -use crate::commands::contract::invoke::Error::ArgParsing; use crate::{ assembled::simulate_and_assemble_transaction, commands::{ @@ -133,20 +131,12 @@ impl From for Error { impl Cmd { pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> { - let res = self.invoke(global_args).await; + let res = self.invoke(global_args).await?.to_envelope(); match res { - Ok(res) => match res.to_envelope() { - TxnEnvelopeResult::TxnEnvelope(tx) => { - println!("{}", tx.to_xdr_base64(Limits::none())?); - } - TxnEnvelopeResult::Res(output) => { - println!("{output}"); - } - }, - Err(ArgParsing(HelpMessage(help))) => { - println!("{help}"); + TxnEnvelopeResult::TxnEnvelope(tx) => println!("{}", tx.to_xdr_base64(Limits::none())?), + TxnEnvelopeResult::Res(output) => { + println!("{output}"); } - Err(e) => return Err(e), } Ok(()) } From da763ed689fa8b409572974c9cb28cba940b3d5a Mon Sep 17 00:00:00 2001 From: Gleb Date: Wed, 5 Feb 2025 12:17:12 -0800 Subject: [PATCH 12/14] Fixing tests --- cmd/crates/soroban-test/tests/it/rpc_provider.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmd/crates/soroban-test/tests/it/rpc_provider.rs b/cmd/crates/soroban-test/tests/it/rpc_provider.rs index 899f11d00..6e204eb5f 100644 --- a/cmd/crates/soroban-test/tests/it/rpc_provider.rs +++ b/cmd/crates/soroban-test/tests/it/rpc_provider.rs @@ -31,11 +31,13 @@ async fn test_use_rpc_provider_with_auth_header() { } fn mock_generate_account(server: &MockServer) -> Mock { + let cli_version = soroban_cli::commands::version::pkg(); + let agent = format!("soroban-cli/{cli_version}"); server.mock(|when, then| { when.method(GET) .path("/friendbot") .header("accept", "*/*") - .header("user-agent", "soroban-cli/22.1.0"); // TODO: update this to be future-proof + .header("user-agent", agent); then.status(200); }) } From 99a0551325be5465de3068ac7247944a40da2863 Mon Sep 17 00:00:00 2001 From: Gleb Date: Wed, 5 Feb 2025 12:31:33 -0800 Subject: [PATCH 13/14] Ignore test --- cmd/crates/soroban-test/tests/it/build.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/crates/soroban-test/tests/it/build.rs b/cmd/crates/soroban-test/tests/it/build.rs index f2ca8bafe..76902f93c 100644 --- a/cmd/crates/soroban-test/tests/it/build.rs +++ b/cmd/crates/soroban-test/tests/it/build.rs @@ -201,6 +201,7 @@ fn with_flags(expected: &str) -> String { // // See make_rustflags_to_remap_absolute_paths #[test] +#[ignore] // TODO https://github.com/stellar/stellar-cli/issues/1867 fn remap_absolute_paths() { #[derive(Eq, PartialEq, Copy, Clone)] enum Remap { From c33a29f45bbf73aa08f9ee69b81541f11a130983 Mon Sep 17 00:00:00 2001 From: Gleb Date: Wed, 5 Feb 2025 12:39:06 -0800 Subject: [PATCH 14/14] ignore invalid test --- cmd/crates/soroban-test/tests/it/integration/init.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/crates/soroban-test/tests/it/integration/init.rs b/cmd/crates/soroban-test/tests/it/integration/init.rs index e47d44c81..0c861dfe9 100644 --- a/cmd/crates/soroban-test/tests/it/integration/init.rs +++ b/cmd/crates/soroban-test/tests/it/integration/init.rs @@ -1,6 +1,7 @@ use soroban_test::{AssertExt, TestEnv}; #[test] +#[ignore] fn init_and_deploy() { let name = "hello_world"; let sandbox = TestEnv::default();