From 49c212e36fe9357487ff3d0a55169f3bf25afe31 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:07:19 -0400 Subject: [PATCH 1/7] Use a default account if the tx is only being simulated --- .../src/commands/contract/arg_parsing.rs | 3 +- .../src/commands/contract/invoke.rs | 43 ++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/cmd/soroban-cli/src/commands/contract/arg_parsing.rs b/cmd/soroban-cli/src/commands/contract/arg_parsing.rs index 2eb3c2696..f4e0b892c 100644 --- a/cmd/soroban-cli/src/commands/contract/arg_parsing.rs +++ b/cmd/soroban-cli/src/commands/contract/arg_parsing.rs @@ -63,8 +63,9 @@ pub fn build_host_function_parameters( cmd.build(); let long_help = cmd.render_long_help(); let mut matches_ = cmd.get_matches_from(slop); + println!("this doesn't print either"); let Some((function, matches_)) = &matches_.remove_subcommand() else { - println!("{long_help}"); + println!("LONG HELP: {long_help}"); std::process::exit(1); }; diff --git a/cmd/soroban-cli/src/commands/contract/invoke.rs b/cmd/soroban-cli/src/commands/contract/invoke.rs index 5d43f9bfa..e8a7bfd8f 100644 --- a/cmd/soroban-cli/src/commands/contract/invoke.rs +++ b/cmd/soroban-cli/src/commands/contract/invoke.rs @@ -7,7 +7,7 @@ use std::{fmt::Debug, fs, io}; use clap::{arg, command, Parser, ValueEnum}; -use soroban_rpc::{SimulateHostFunctionResult, SimulateTransactionResponse}; +use soroban_rpc::{Client, SimulateHostFunctionResult, SimulateTransactionResponse}; use soroban_spec::read::FromWasmError; use super::super::events; @@ -179,6 +179,27 @@ impl Cmd { Send::Yes => ShouldSend::Yes, }) } + + async fn should_send_tx( + &self, + host_function_params: InvokeContractArgs, + rpc_client: Client, + ) -> Result { + let account_details = default_account_entry(); + let sequence: i64 = account_details.seq_num.into(); + let AccountId(PublicKey::PublicKeyTypeEd25519(account_id)) = account_details.account_id; + + let tx = build_invoke_contract_tx( + host_function_params.clone(), + sequence + 1, + self.fee.fee, + account_id, + )?; + let txn = simulate_and_assemble_transaction(&rpc_client, &tx).await?; + let txn = self.fee.apply_to_assembled_txn(txn); // do we need this part? + let sim_res = txn.sim_response(); + self.send(sim_res) + } } #[async_trait::async_trait] @@ -205,13 +226,29 @@ impl NetworkRunnable for Cmd { let _ = build_host_function_parameters(&contract_id, &self.slop, spec_entries, config)?; } let client = network.rpc_client()?; - let account_details = if self.is_view { + + let spec_entries = get_remote_contract_spec( + &contract_id.0, + &config.locator, + &config.network, + global_args, + Some(config), + ) + .await + .map_err(Error::from)?; + let (function, spec, host_function_params, signers) = + build_host_function_parameters(&contract_id, &self.slop, &spec_entries, config)?; + + let should_send = self.should_send_tx(host_function_params, client.clone()).await?; + let account_details = if should_send != ShouldSend::Yes{ default_account_entry() } else { client .verify_network_passphrase(Some(&network.network_passphrase)) .await?; + println!("here, about to fail?"); + client .get_account(&config.source_account()?.to_string()) .await? @@ -232,6 +269,7 @@ impl NetworkRunnable for Cmd { // Get the ledger footprint let (function, spec, host_function_params, signers) = build_host_function_parameters(&contract_id, &self.slop, &spec_entries, config)?; + println!("this doesn't print"); let tx = build_invoke_contract_tx( host_function_params.clone(), sequence + 1, @@ -365,6 +403,7 @@ pub enum Send { Yes, } +#[derive(Debug, PartialEq)] enum ShouldSend { DefaultNo, No, From dc0f7709599e61262f4690fae686a92afbe62e44 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Fri, 1 Nov 2024 16:11:00 -0400 Subject: [PATCH 2/7] Refactor/cleanup --- .../src/commands/contract/arg_parsing.rs | 3 +- .../src/commands/contract/invoke.rs | 36 +++++++------------ 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/cmd/soroban-cli/src/commands/contract/arg_parsing.rs b/cmd/soroban-cli/src/commands/contract/arg_parsing.rs index f4e0b892c..2eb3c2696 100644 --- a/cmd/soroban-cli/src/commands/contract/arg_parsing.rs +++ b/cmd/soroban-cli/src/commands/contract/arg_parsing.rs @@ -63,9 +63,8 @@ pub fn build_host_function_parameters( cmd.build(); let long_help = cmd.render_long_help(); let mut matches_ = cmd.get_matches_from(slop); - println!("this doesn't print either"); let Some((function, matches_)) = &matches_.remove_subcommand() else { - println!("LONG HELP: {long_help}"); + println!("{long_help}"); std::process::exit(1); }; diff --git a/cmd/soroban-cli/src/commands/contract/invoke.rs b/cmd/soroban-cli/src/commands/contract/invoke.rs index e8a7bfd8f..06da3e250 100644 --- a/cmd/soroban-cli/src/commands/contract/invoke.rs +++ b/cmd/soroban-cli/src/commands/contract/invoke.rs @@ -163,7 +163,7 @@ impl Cmd { .transpose() } - fn send(&self, sim_res: &SimulateTransactionResponse) -> Result { + fn should_send_tx(&self, sim_res: &SimulateTransactionResponse) -> Result { Ok(match self.send { Send::Default => { if self.is_view { @@ -180,7 +180,7 @@ impl Cmd { }) } - async fn should_send_tx( + async fn check_should_send_tx_with_default_account( &self, host_function_params: InvokeContractArgs, rpc_client: Client, @@ -198,7 +198,7 @@ impl Cmd { let txn = simulate_and_assemble_transaction(&rpc_client, &tx).await?; let txn = self.fee.apply_to_assembled_txn(txn); // do we need this part? let sim_res = txn.sim_response(); - self.send(sim_res) + self.should_send_tx(sim_res) } } @@ -236,40 +236,28 @@ impl NetworkRunnable for Cmd { ) .await .map_err(Error::from)?; + let (function, spec, host_function_params, signers) = build_host_function_parameters(&contract_id, &self.slop, &spec_entries, config)?; - let should_send = self.should_send_tx(host_function_params, client.clone()).await?; - let account_details = if should_send != ShouldSend::Yes{ - default_account_entry() - } else { + let should_send_tx = self + .check_should_send_tx_with_default_account(host_function_params.clone(), client.clone()) + .await?; + + let account_details = if should_send_tx == ShouldSend::Yes { client .verify_network_passphrase(Some(&network.network_passphrase)) .await?; - println!("here, about to fail?"); - client .get_account(&config.source_account()?.to_string()) .await? + } else { + default_account_entry() }; let sequence: i64 = account_details.seq_num.into(); let AccountId(PublicKey::PublicKeyTypeEd25519(account_id)) = account_details.account_id; - let spec_entries = get_remote_contract_spec( - &contract_id.0, - &config.locator, - &config.network, - global_args, - Some(config), - ) - .await - .map_err(Error::from)?; - - // Get the ledger footprint - let (function, spec, host_function_params, signers) = - build_host_function_parameters(&contract_id, &self.slop, &spec_entries, config)?; - println!("this doesn't print"); let tx = build_invoke_contract_tx( host_function_params.clone(), sequence + 1, @@ -288,7 +276,7 @@ impl NetworkRunnable for Cmd { if global_args.map_or(true, |a| !a.no_cache) { data::write(sim_res.clone().into(), &network.rpc_uri()?)?; } - let should_send = self.send(sim_res)?; + let should_send = self.should_send_tx(sim_res)?; let (return_value, events) = match should_send { ShouldSend::Yes => { let global::Args { no_cache, .. } = global_args.cloned().unwrap_or_default(); From 32c9dd8098c02b01f4b402c56ec832c4e2ddd9ad Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Fri, 1 Nov 2024 17:03:43 -0400 Subject: [PATCH 3/7] WIP - troubleshooting get_matches_from for --help --- cmd/soroban-cli/src/commands/contract/arg_parsing.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cmd/soroban-cli/src/commands/contract/arg_parsing.rs b/cmd/soroban-cli/src/commands/contract/arg_parsing.rs index 2eb3c2696..ab61df1c1 100644 --- a/cmd/soroban-cli/src/commands/contract/arg_parsing.rs +++ b/cmd/soroban-cli/src/commands/contract/arg_parsing.rs @@ -62,6 +62,15 @@ pub fn build_host_function_parameters( } cmd.build(); let long_help = cmd.render_long_help(); + + match cmd.clone().try_get_matches_from(slop) { + Ok(m) => m, + Err(e) => { + println!("Failed to match subcommand: {}", e); + std::process::exit(1); + } + }; + let mut matches_ = cmd.get_matches_from(slop); let Some((function, matches_)) = &matches_.remove_subcommand() else { println!("{long_help}"); From 811d6fe502af0c196d03ff238a35d05a9142a2a7 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Mon, 4 Nov 2024 14:41:47 -0500 Subject: [PATCH 4/7] Add a comment to get_matches_from for --help --- cmd/soroban-cli/src/commands/contract/arg_parsing.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/cmd/soroban-cli/src/commands/contract/arg_parsing.rs b/cmd/soroban-cli/src/commands/contract/arg_parsing.rs index ab61df1c1..21fa2f383 100644 --- a/cmd/soroban-cli/src/commands/contract/arg_parsing.rs +++ b/cmd/soroban-cli/src/commands/contract/arg_parsing.rs @@ -63,14 +63,8 @@ pub fn build_host_function_parameters( cmd.build(); let long_help = cmd.render_long_help(); - match cmd.clone().try_get_matches_from(slop) { - Ok(m) => m, - Err(e) => { - println!("Failed to match subcommand: {}", e); - std::process::exit(1); - } - }; - + // get_matches_from exits the process 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}"); From fc950180c21755aefb7c15ef064b9b045b478199 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Thu, 7 Nov 2024 11:13:41 -0500 Subject: [PATCH 5/7] Update invoke_view_with_non_existent_source_account test --- cmd/crates/soroban-test/tests/it/integration/hello_world.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 272587c53..12963b1f4 100644 --- a/cmd/crates/soroban-test/tests/it/integration/hello_world.rs +++ b/cmd/crates/soroban-test/tests/it/integration/hello_world.rs @@ -20,9 +20,7 @@ async fn invoke_view_with_non_existent_source_account() { let id = deploy_hello(sandbox).await; let world = "world"; let mut cmd = hello_world_cmd(&id, world); - cmd.config.source_account = Address::default(); - cmd.is_view = true; - let res = sandbox.run_cmd_with(cmd, "test").await.unwrap(); + let res = sandbox.run_cmd_with(cmd, "").await.unwrap(); assert_eq!(res, TxnResult::Res(format!(r#"["Hello",{world:?}]"#))); } From 85cdc331c740a2ca9010eb20ea146e22198ff225 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Thu, 7 Nov 2024 12:26:37 -0500 Subject: [PATCH 6/7] Rename fn --- cmd/soroban-cli/src/commands/contract/invoke.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/soroban-cli/src/commands/contract/invoke.rs b/cmd/soroban-cli/src/commands/contract/invoke.rs index 06da3e250..343150c42 100644 --- a/cmd/soroban-cli/src/commands/contract/invoke.rs +++ b/cmd/soroban-cli/src/commands/contract/invoke.rs @@ -180,7 +180,7 @@ impl Cmd { }) } - async fn check_should_send_tx_with_default_account( + async fn should_send_tx_with_default_account( &self, host_function_params: InvokeContractArgs, rpc_client: Client, @@ -241,7 +241,7 @@ impl NetworkRunnable for Cmd { build_host_function_parameters(&contract_id, &self.slop, &spec_entries, config)?; let should_send_tx = self - .check_should_send_tx_with_default_account(host_function_params.clone(), client.clone()) + .should_send_tx_with_default_account(host_function_params.clone(), client.clone()) .await?; let account_details = if should_send_tx == ShouldSend::Yes { From 01585f4ccdf09a336e2bcf6b204d90e1b6beed12 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Tue, 12 Nov 2024 17:18:25 -0500 Subject: [PATCH 7/7] Update fn name --- cmd/soroban-cli/src/commands/contract/invoke.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/soroban-cli/src/commands/contract/invoke.rs b/cmd/soroban-cli/src/commands/contract/invoke.rs index 343150c42..47e2699fc 100644 --- a/cmd/soroban-cli/src/commands/contract/invoke.rs +++ b/cmd/soroban-cli/src/commands/contract/invoke.rs @@ -180,7 +180,8 @@ impl Cmd { }) } - async fn should_send_tx_with_default_account( + // uses a default account to check if the tx should be sent after the simulation + async fn should_send_after_sim( &self, host_function_params: InvokeContractArgs, rpc_client: Client, @@ -241,7 +242,7 @@ impl NetworkRunnable for Cmd { build_host_function_parameters(&contract_id, &self.slop, &spec_entries, config)?; let should_send_tx = self - .should_send_tx_with_default_account(host_function_params.clone(), client.clone()) + .should_send_after_sim(host_function_params.clone(), client.clone()) .await?; let account_details = if should_send_tx == ShouldSend::Yes {