Skip to content

Commit bd18516

Browse files
authored
Remove network runnable abstraction. (#2364)
1 parent a64925e commit bd18516

19 files changed

Lines changed: 185 additions & 293 deletions

File tree

cmd/crates/soroban-test/src/lib.rs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use assert_fs::{fixture::FixtureError, prelude::PathChild, TempDir};
3434
use fs_extra::dir::CopyOptions;
3535

3636
use soroban_cli::{
37-
commands::{contract::invoke, global, keys, NetworkRunnable},
37+
commands::{contract::invoke, keys},
3838
config::{self, network},
3939
CommandParser,
4040
};
@@ -233,13 +233,14 @@ impl TestEnv {
233233
source: &str,
234234
) -> Result<String, invoke::Error> {
235235
let cmd = self.cmd_with_config::<I, invoke::Cmd>(command_str, None);
236-
self.run_cmd_with(cmd, source)
236+
let config = self.clone_config(source);
237+
cmd.execute(&config, false, false)
237238
.await
238239
.map(|tx| tx.into_result().unwrap())
239240
}
240241

241242
/// A convenience method for using the invoke command.
242-
pub fn cmd_with_config<I: AsRef<str>, T: CommandParser<T> + NetworkRunnable>(
243+
pub fn cmd_with_config<I: AsRef<str>, T: CommandParser<T>>(
243244
&self,
244245
command_str: &[I],
245246
source_account: Option<&str>,
@@ -286,29 +287,6 @@ impl TestEnv {
286287
}
287288
}
288289

289-
/// Invoke an already parsed invoke command
290-
pub async fn run_cmd_with<T: NetworkRunnable>(
291-
&self,
292-
cmd: T,
293-
account: &str,
294-
) -> Result<T::Result, T::Error> {
295-
let config = self.clone_config(account);
296-
297-
cmd.run_against_rpc_server(
298-
Some(&global::Args {
299-
locator: config.locator.clone(),
300-
filter_logs: Vec::default(),
301-
quiet: false,
302-
verbose: false,
303-
very_verbose: false,
304-
list: false,
305-
no_cache: false,
306-
}),
307-
Some(&config),
308-
)
309-
.await
310-
}
311-
312290
/// Reference to current directory of the `TestEnv`.
313291
pub fn dir(&self) -> &TempDir {
314292
&self.temp_dir

cmd/crates/soroban-test/tests/it/integration/bindings.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async fn invoke_test_generate_typescript_bindings() {
2121
&contract_id.to_string(),
2222
]);
2323

24-
let result = sandbox.run_cmd_with(cmd, "test").await;
24+
let result = cmd.execute(false).await;
2525

2626
assert!(result.is_ok(), "Failed to generate TypeScript bindings");
2727

@@ -51,7 +51,7 @@ async fn invoke_test_bindings_context_failure() {
5151
&contract_id.to_string(),
5252
]);
5353

54-
let result = sandbox.run_cmd_with(cmd, "test").await;
54+
let result = cmd.execute(false).await;
5555

5656
assert!(result.is_ok(), "Failed to generate TypeScript bindings");
5757

cmd/crates/soroban-test/tests/it/integration/hello_world.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ async fn invoke_view_with_non_existent_source_account() {
1717
let id = deploy_hello(sandbox).await;
1818
let world = "world";
1919
let cmd = hello_world_cmd(&id, world);
20-
let res = sandbox.run_cmd_with(cmd, "").await.unwrap();
20+
let config = sandbox.clone_config("");
21+
let res = cmd.execute(&config, false, false).await.unwrap();
2122
assert_eq!(res, TxnResult::Res(format!(r#"["Hello",{world:?}]"#)));
2223
}
2324

@@ -172,7 +173,8 @@ fn hello_world_cmd(id: &str, arg: &str) -> contract::invoke::Cmd {
172173

173174
async fn invoke_hello_world_with_lib(e: &TestEnv, id: &str) {
174175
let cmd = hello_world_cmd(id, "world");
175-
let res = e.run_cmd_with(cmd, "test").await.unwrap();
176+
let config = e.clone_config("test");
177+
let res = cmd.execute(&config, false, false).await.unwrap();
176178
assert_eq!(res, TxnResult::Res(r#"["Hello","world"]"#.to_string()));
177179
}
178180

cmd/crates/soroban-test/tests/it/integration/util.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,8 @@ pub async fn deploy_contract(
9090
);
9191
cmd.salt = salt;
9292

93-
let res = sandbox
94-
.run_cmd_with(cmd, deployer.as_deref().unwrap_or("test"))
95-
.await
96-
.unwrap();
93+
let config = sandbox.clone_config(deployer.as_deref().unwrap_or("test"));
94+
let res = cmd.execute(&config, false, false).await.unwrap();
9795

9896
match kind {
9997
DeployKind::Normal => (),

cmd/crates/soroban-test/tests/it/util.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ pub async fn invoke_custom(
5353
let mut i: contract::invoke::Cmd =
5454
sandbox.cmd_with_config(&["--id", id, "--", func, arg], None);
5555
i.wasm = Some(wasm.to_path_buf());
56-
let s = sandbox.run_cmd_with(i, TEST_ACCOUNT).await;
57-
s.map(|tx| tx.into_result().unwrap())
56+
let config = sandbox.clone_config(TEST_ACCOUNT);
57+
i.execute(&config, false, false)
58+
.await
59+
.map(|tx| tx.into_result().unwrap())
5860
}
5961

6062
pub const DEFAULT_CONTRACT_ID: &str = "CDR6QKTWZQYW6YUJ7UP7XXZRLWQPFRV6SWBLQS4ZQOSAF4BOUD77OO5Z";

cmd/soroban-cli/src/commands/container/start.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,20 +236,22 @@ impl Runner {
236236
}
237237

238238
fn get_protocol_version_arg(&self) -> String {
239-
if self.network == Network::Local && self.args.protocol_version.is_some() {
240-
let version = self.args.protocol_version.as_ref().unwrap();
241-
format!("--protocol-version {version}")
242-
} else {
243-
String::new()
239+
if self.network == Network::Local {
240+
if let Some(version) = self.args.protocol_version.as_ref() {
241+
return format!("--protocol-version {version}");
242+
}
244243
}
244+
245+
String::new()
245246
}
246247

247248
fn get_limits_arg(&self) -> String {
248-
if self.network == Network::Local && self.args.limits.is_some() {
249-
let limits = self.args.limits.as_ref().unwrap();
250-
format!("--limits {limits}")
251-
} else {
252-
String::new()
249+
if self.network == Network::Local {
250+
if let Some(limits) = self.args.limits.as_ref() {
251+
return format!("--limits {limits}");
252+
}
253253
}
254+
255+
String::new()
254256
}
255257
}

cmd/soroban-cli/src/commands/contract/bindings/typescript.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ use clap::Parser;
44
use soroban_spec_tools::contract as spec_tools;
55
use soroban_spec_typescript::boilerplate::Project;
66

7+
use crate::commands::contract::info::shared as contract_spec;
78
use crate::print::Print;
8-
use crate::{
9-
commands::{contract::info::shared as contract_spec, global, NetworkRunnable},
10-
config,
11-
};
129
use soroban_spec_tools::contract::Spec;
1310

1411
#[derive(Parser, Debug, Clone)]
@@ -48,17 +45,13 @@ pub enum Error {
4845
Xdr(#[from] crate::xdr::Error),
4946
}
5047

51-
#[async_trait::async_trait]
52-
impl NetworkRunnable for Cmd {
53-
type Error = Error;
54-
type Result = ();
48+
impl Cmd {
49+
pub async fn run(&self) -> Result<(), Error> {
50+
self.execute(false).await
51+
}
5552

56-
async fn run_against_rpc_server(
57-
&self,
58-
global_args: Option<&global::Args>,
59-
_config: Option<&config::Args>,
60-
) -> Result<(), Error> {
61-
let print = Print::new(global_args.is_some_and(|a| a.quiet));
53+
pub async fn execute(&self, quiet: bool) -> Result<(), Error> {
54+
let print = Print::new(quiet);
6255

6356
let contract_spec::Fetched { contract, source } =
6457
contract_spec::fetch(&self.wasm_or_hash_or_contract_id, &print).await?;
@@ -115,9 +108,3 @@ impl NetworkRunnable for Cmd {
115108
Ok(())
116109
}
117110
}
118-
119-
impl Cmd {
120-
pub async fn run(&self) -> Result<(), Error> {
121-
self.run_against_rpc_server(None, None).await
122-
}
123-
}

cmd/soroban-cli/src/commands/contract/deploy/asset.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use crate::{
1616
commands::{
1717
global,
1818
txn_result::{TxnEnvelopeResult, TxnResult},
19-
NetworkRunnable,
2019
},
2120
config::{self, data, network},
2221
rpc::Error as SorobanRpcError,
@@ -98,7 +97,7 @@ pub struct Cmd {
9897
impl Cmd {
9998
pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
10099
let res = self
101-
.run_against_rpc_server(Some(global_args), None)
100+
.execute(&self.config, global_args.quiet, global_args.no_cache)
102101
.await?
103102
.to_envelope();
104103
match res {
@@ -130,19 +129,13 @@ impl Cmd {
130129
}
131130
Ok(())
132131
}
133-
}
134-
135-
#[async_trait::async_trait]
136-
impl NetworkRunnable for Cmd {
137-
type Error = Error;
138-
type Result = TxnResult<stellar_strkey::Contract>;
139132

140-
async fn run_against_rpc_server(
133+
pub async fn execute(
141134
&self,
142-
args: Option<&global::Args>,
143-
config: Option<&config::Args>,
144-
) -> Result<Self::Result, Error> {
145-
let config = config.unwrap_or(&self.config);
135+
config: &config::Args,
136+
quiet: bool,
137+
no_cache: bool,
138+
) -> Result<TxnResult<stellar_strkey::Contract>, Error> {
146139
// Parse asset
147140
let asset = self.asset.resolve(&config.locator)?;
148141

@@ -185,12 +178,12 @@ impl NetworkRunnable for Cmd {
185178
let assembled = self.resources.apply_to_assembled_txn(assembled);
186179
let txn = assembled.transaction().clone();
187180
let get_txn_resp = client
188-
.send_transaction_polling(&self.config.sign(txn, args.is_some_and(|g| g.quiet)).await?)
181+
.send_transaction_polling(&self.config.sign(txn, quiet).await?)
189182
.await?;
190183

191184
self.resources.print_cost_info(&get_txn_resp)?;
192185

193-
if args.is_none_or(|a| !a.no_cache) {
186+
if !no_cache {
194187
data::write(get_txn_resp.clone().try_into()?, &network.rpc_uri()?)?;
195188
}
196189

cmd/soroban-cli/src/commands/contract/deploy/wasm.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use crate::{
2222
contract::{self, arg_parsing, id::wasm::get_contract_id, upload},
2323
global,
2424
txn_result::{TxnEnvelopeResult, TxnResult},
25-
NetworkRunnable,
2625
},
2726
config::{self, data, locator, network},
2827
print::Print,
@@ -152,7 +151,7 @@ pub enum Error {
152151
impl Cmd {
153152
pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
154153
let res = self
155-
.run_against_rpc_server(Some(global_args), None)
154+
.execute(&self.config, global_args.quiet, global_args.no_cache)
156155
.await?
157156
.to_envelope();
158157
match res {
@@ -184,23 +183,16 @@ impl Cmd {
184183
}
185184
Ok(())
186185
}
187-
}
188-
189-
#[async_trait::async_trait]
190-
impl NetworkRunnable for Cmd {
191-
type Error = Error;
192-
type Result = TxnResult<stellar_strkey::Contract>;
193186

194187
#[allow(clippy::too_many_lines)]
195188
#[allow(unused_variables)]
196-
async fn run_against_rpc_server(
189+
pub async fn execute(
197190
&self,
198-
global_args: Option<&global::Args>,
199-
config: Option<&config::Args>,
191+
config: &config::Args,
192+
quiet: bool,
193+
no_cache: bool,
200194
) -> Result<TxnResult<stellar_strkey::Contract>, Error> {
201-
let quiet = global_args.is_some_and(|a| a.quiet);
202195
let print = Print::new(quiet);
203-
let config = config.unwrap_or(&self.config);
204196
let wasm_hash = if let Some(wasm) = &self.wasm {
205197
let is_build = self.build_only;
206198
let hash = if is_build {
@@ -213,7 +205,7 @@ impl NetworkRunnable for Cmd {
213205
ignore_checks: self.ignore_checks,
214206
build_only: is_build,
215207
}
216-
.run_against_rpc_server(global_args, Some(config))
208+
.execute(config, quiet, no_cache)
217209
.await?
218210
.into_result()
219211
.expect("the value (hash) is expected because it should always be available since build-only is a shared parameter")
@@ -330,7 +322,7 @@ impl NetworkRunnable for Cmd {
330322

331323
self.resources.print_cost_info(&get_txn_resp)?;
332324

333-
if global_args.is_none_or(|a| !a.no_cache) {
325+
if !no_cache {
334326
data::write(get_txn_resp.clone().try_into()?, &network.rpc_uri()?)?;
335327
}
336328

cmd/soroban-cli/src/commands/contract/extend.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use crate::{
2121
commands::{
2222
global,
2323
txn_result::{TxnEnvelopeResult, TxnResult},
24-
NetworkRunnable,
2524
},
2625
config::{self, data, locator, network},
2726
key, rpc, wasm, Pwd,
@@ -131,7 +130,7 @@ impl Cmd {
131130
#[allow(clippy::too_many_lines)]
132131
pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
133132
let res = self
134-
.run_against_rpc_server(Some(global_args), None)
133+
.execute(&self.config, global_args.quiet, global_args.no_cache)
135134
.await?
136135
.to_envelope();
137136
match res {
@@ -184,21 +183,14 @@ impl Cmd {
184183

185184
Ok(self.ledgers_to_extend)
186185
}
187-
}
188-
189-
#[async_trait::async_trait]
190-
impl NetworkRunnable for Cmd {
191-
type Error = Error;
192-
type Result = TxnResult<u32>;
193186

194187
#[allow(clippy::too_many_lines)]
195-
async fn run_against_rpc_server(
188+
pub async fn execute(
196189
&self,
197-
args: Option<&global::Args>,
198-
config: Option<&config::Args>,
199-
) -> Result<TxnResult<u32>, Self::Error> {
200-
let config = config.unwrap_or(&self.config);
201-
let quiet = args.is_some_and(|a| a.quiet);
190+
config: &config::Args,
191+
quiet: bool,
192+
no_cache: bool,
193+
) -> Result<TxnResult<u32>, Error> {
202194
let print = Print::new(quiet);
203195
let network = config.get_network()?;
204196
tracing::trace!(?network);
@@ -258,7 +250,7 @@ impl NetworkRunnable for Cmd {
258250
.await?;
259251
self.resources.print_cost_info(&res)?;
260252

261-
if args.is_none_or(|a| !a.no_cache) {
253+
if !no_cache {
262254
data::write(res.clone().try_into()?, &network.rpc_uri()?)?;
263255
}
264256

0 commit comments

Comments
 (0)