From 90287f80a2fdf9f3dbe3dc6f68b1b385c30bf01d Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Fri, 1 Nov 2024 09:15:20 -0400 Subject: [PATCH] refactor: Refactor contract ID handling in Args module. --- .../tests/it/integration/hello_world.rs | 2 +- .../src/commands/contract/alias/add.rs | 4 +- .../src/commands/contract/fetch.rs | 12 ++++-- .../src/commands/contract/info/shared.rs | 15 ++++--- .../src/commands/contract/invoke.rs | 10 +++-- cmd/soroban-cli/src/commands/events.rs | 14 +++--- cmd/soroban-cli/src/config/alias.rs | 43 ++++++++++++++++++- cmd/soroban-cli/src/config/locator.rs | 25 ++++++----- cmd/soroban-cli/src/config/mod.rs | 2 + cmd/soroban-cli/src/key.rs | 13 ++++-- cmd/soroban-cli/src/wasm.rs | 8 +--- 11 files changed, 104 insertions(+), 44 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..c4d6b80d4 100644 --- a/cmd/crates/soroban-test/tests/it/integration/hello_world.rs +++ b/cmd/crates/soroban-test/tests/it/integration/hello_world.rs @@ -161,7 +161,7 @@ pub(crate) fn invoke_hello_world(sandbox: &TestEnv, id: &str) { fn hello_world_cmd(id: &str, arg: &str) -> contract::invoke::Cmd { contract::invoke::Cmd { - contract_id: id.to_string(), + contract_id: id.parse().unwrap(), slop: vec!["hello".into(), format!("--world={arg}").into()], ..Default::default() } diff --git a/cmd/soroban-cli/src/commands/contract/alias/add.rs b/cmd/soroban-cli/src/commands/contract/alias/add.rs index 5a7a17ddb..c154567af 100644 --- a/cmd/soroban-cli/src/commands/contract/alias/add.rs +++ b/cmd/soroban-cli/src/commands/contract/alias/add.rs @@ -15,7 +15,7 @@ pub struct Cmd { #[command(flatten)] network: network::Args, - /// The contract alias that will be removed. + /// The contract alias that will be used. pub alias: String, /// Overwrite the contract alias if it already exists. @@ -41,7 +41,7 @@ pub enum Error { AlreadyExist { alias: String, network_passphrase: String, - contract: String, + contract: stellar_strkey::Contract, }, } diff --git a/cmd/soroban-cli/src/commands/contract/fetch.rs b/cmd/soroban-cli/src/commands/contract/fetch.rs index 2714b1f07..03f0650f2 100644 --- a/cmd/soroban-cli/src/commands/contract/fetch.rs +++ b/cmd/soroban-cli/src/commands/contract/fetch.rs @@ -10,7 +10,7 @@ use clap::{arg, command, Parser}; use crate::{ commands::{global, NetworkRunnable}, config::{ - self, locator, + self, alias, locator, network::{self, Network}, }, wasm, Pwd, @@ -22,7 +22,7 @@ use crate::{ pub struct Cmd { /// Contract ID to fetch #[arg(long = "id", env = "STELLAR_CONTRACT_ID")] - pub contract_id: String, + pub contract_id: config::ContractAddress, /// Where to write output otherwise stdout is used #[arg(long, short = 'o')] pub out_file: Option, @@ -111,6 +111,12 @@ impl NetworkRunnable for Cmd { config: Option<&config::Args>, ) -> Result, Error> { let network = config.map_or_else(|| self.network(), |c| Ok(c.get_network()?))?; - return Ok(wasm::fetch_from_contract(&self.contract_id, &network, &self.locator).await?); + Ok(wasm::fetch_from_contract( + &self + .contract_id + .resolve_contract_id(&self.locator, &network.network_passphrase)?, + &network, + ) + .await?) } } diff --git a/cmd/soroban-cli/src/commands/contract/info/shared.rs b/cmd/soroban-cli/src/commands/contract/info/shared.rs index 9c24d78b8..f0617ea5c 100644 --- a/cmd/soroban-cli/src/commands/contract/info/shared.rs +++ b/cmd/soroban-cli/src/commands/contract/info/shared.rs @@ -1,13 +1,13 @@ use std::path::PathBuf; -use crate::xdr; use clap::arg; use crate::{ commands::contract::info::shared::Error::InvalidWasmHash, - config::{locator, network}, + config::{self, locator, network}, utils::rpc::get_remote_wasm_from_hash, wasm::{self, Error::ContractIsStellarAsset}, + xdr, }; #[derive(Debug, clap::Args, Clone, Default)] @@ -26,7 +26,7 @@ pub struct Args { pub wasm_hash: Option, /// Contract id or contract alias to get the data for #[arg(long = "id", env = "STELLAR_CONTRACT_ID", group = "Source")] - pub contract_id: Option, + pub contract_id: Option, #[command(flatten)] pub network: network::Args, #[command(flatten)] @@ -56,6 +56,8 @@ pub enum Error { InvalidWasmHash(String), #[error(transparent)] Rpc(#[from] soroban_rpc::Error), + #[error(transparent)] + Locator(#[from] locator::Error), } pub async fn fetch_wasm(args: &Args) -> Result>, Error> { @@ -79,10 +81,9 @@ pub async fn fetch_wasm(args: &Args) -> Result>, Error> { get_remote_wasm_from_hash(&client, &hash).await? } else if let Some(contract_id) = &args.contract_id { - let contract_id = args - .locator - .resolve_contract_id(contract_id, &network.network_passphrase)?; - let res = wasm::fetch_from_contract(contract_id, network, &args.locator).await; + let contract_id = + contract_id.resolve_contract_id(&args.locator, &network.network_passphrase)?; + let res = wasm::fetch_from_contract(&contract_id, network).await; if let Some(ContractIsStellarAsset) = res.as_ref().err() { return Ok(None); } diff --git a/cmd/soroban-cli/src/commands/contract/invoke.rs b/cmd/soroban-cli/src/commands/contract/invoke.rs index 5d43f9bfa..5f4d49935 100644 --- a/cmd/soroban-cli/src/commands/contract/invoke.rs +++ b/cmd/soroban-cli/src/commands/contract/invoke.rs @@ -12,6 +12,7 @@ use soroban_spec::read::FromWasmError; use super::super::events; use super::arg_parsing; +use crate::config::alias; use crate::{ assembled::simulate_and_assemble_transaction, commands::{ @@ -40,7 +41,7 @@ use soroban_spec_tools::contract; pub struct Cmd { /// Contract ID to invoke #[arg(long = "id", env = "STELLAR_CONTRACT_ID")] - pub contract_id: String, + pub contract_id: config::ContractAddress, // For testing only #[arg(skip)] pub wasm: Option, @@ -123,6 +124,8 @@ pub enum Error { GetSpecError(#[from] get_spec::Error), #[error(transparent)] ArgParsing(#[from] arg_parsing::Error), + #[error(transparent)] + ContractAddress(#[from] alias::Error), } impl From for Error { @@ -195,9 +198,8 @@ impl NetworkRunnable for Cmd { let network = config.get_network()?; tracing::trace!(?network); let contract_id = self - .config - .locator - .resolve_contract_id(&self.contract_id, &network.network_passphrase)?; + .contract_id + .resolve_contract_id(&config.locator, &network.network_passphrase)?; let spec_entries = self.spec_entries()?; if let Some(spec_entries) = &spec_entries { diff --git a/cmd/soroban-cli/src/commands/events.rs b/cmd/soroban-cli/src/commands/events.rs index a1f5de921..d224bf6da 100644 --- a/cmd/soroban-cli/src/commands/events.rs +++ b/cmd/soroban-cli/src/commands/events.rs @@ -1,7 +1,10 @@ use clap::{arg, command, Parser}; use std::io; -use crate::xdr::{self, Limits, ReadXdr}; +use crate::{ + config::alias, + xdr::{self, Limits, ReadXdr}, +}; use super::{global, NetworkRunnable}; use crate::{ @@ -42,7 +45,7 @@ pub struct Cmd { num_args = 1..=6, help_heading = "FILTERS" )] - contract_ids: Vec, + contract_ids: Vec, /// A set of (up to 4) topic filters to filter event topics on. A single /// topic filter can contain 1-4 different segment filters, separated by /// commas, with an asterisk (`*` character) indicating a wildcard segment. @@ -121,6 +124,8 @@ pub enum Error { Locator(#[from] locator::Error), #[error(transparent)] Config(#[from] config::Error), + #[error(transparent)] + ContractId(#[from] alias::Error), } #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, clap::ValueEnum)] @@ -218,9 +223,8 @@ impl NetworkRunnable for Cmd { .contract_ids .iter() .map(|id| { - Ok(self - .locator - .resolve_contract_id(id, &network.network_passphrase)? + Ok(id + .resolve_contract_id(&self.locator, &network.network_passphrase)? .to_string()) }) .collect::, Error>>()?; diff --git a/cmd/soroban-cli/src/config/alias.rs b/cmd/soroban-cli/src/config/alias.rs index 68ad556d6..9d1d8c11b 100644 --- a/cmd/soroban-cli/src/config/alias.rs +++ b/cmd/soroban-cli/src/config/alias.rs @@ -1,8 +1,49 @@ -use std::collections::HashMap; +use std::{collections::HashMap, convert::Infallible, str::FromStr}; use serde::{Deserialize, Serialize}; +use super::locator; + #[derive(Serialize, Deserialize, Default)] pub struct Data { pub ids: HashMap, } + +/// Address can be either a contract address, C.. or eventually an alias of a contract address. +#[derive(Clone, Debug)] +pub enum ContractAddress { + ContractId(stellar_strkey::Contract), + Alias(String), +} + +impl Default for ContractAddress { + fn default() -> Self { + ContractAddress::Alias(String::default()) + } +} + +impl FromStr for ContractAddress { + type Err = Infallible; + + fn from_str(value: &str) -> Result { + Ok(stellar_strkey::Contract::from_str(value).map_or_else( + |_| ContractAddress::Alias(value.to_string()), + ContractAddress::ContractId, + )) + } +} + +impl ContractAddress { + pub fn resolve_contract_id( + &self, + locator: &locator::Args, + network_passphrase: &str, + ) -> Result { + match self { + ContractAddress::ContractId(muxed_account) => Ok(*muxed_account), + ContractAddress::Alias(alias) => locator + .get_contract_id(alias, network_passphrase)? + .ok_or_else(|| locator::Error::ContractNotFound(alias.to_owned())), + } + } +} diff --git a/cmd/soroban-cli/src/config/locator.rs b/cmd/soroban-cli/src/config/locator.rs index 70b4f75f8..909dbf16a 100644 --- a/cmd/soroban-cli/src/config/locator.rs +++ b/cmd/soroban-cli/src/config/locator.rs @@ -72,6 +72,8 @@ pub enum Error { CannotAccessAliasConfigFile, #[error("cannot parse contract ID {0}: {1}")] CannotParseContractId(String, DecodeError), + #[error("contract not found: {0}")] + ContractNotFound(String), #[error("Failed to read upgrade check file: {path}: {error}")] UpgradeCheckReadFailed { path: PathBuf, error: io::Error }, #[error("Failed to write upgrade check file: {path}: {error}")] @@ -320,12 +322,17 @@ impl Args { &self, alias: &str, network_passphrase: &str, - ) -> Result, Error> { + ) -> Result, Error> { let Some(alias_data) = self.load_contract_from_alias(alias)? else { return Ok(None); }; - Ok(alias_data.ids.get(network_passphrase).cloned()) + alias_data + .ids + .get(network_passphrase) + .map(|id| id.parse()) + .transpose() + .map_err(|e| Error::CannotParseContractId(alias.to_owned(), e)) } pub fn resolve_contract_id( @@ -333,14 +340,12 @@ impl Args { alias_or_contract_id: &str, network_passphrase: &str, ) -> Result { - let contract_id = self - .get_contract_id(alias_or_contract_id, network_passphrase)? - .unwrap_or_else(|| alias_or_contract_id.to_string()); - - Ok(Contract( - soroban_spec_tools::utils::contract_id_from_str(&contract_id) - .map_err(|e| Error::CannotParseContractId(contract_id.clone(), e))?, - )) + let Some(contract) = self.get_contract_id(alias_or_contract_id, network_passphrase)? else { + return alias_or_contract_id + .parse() + .map_err(|e| Error::CannotParseContractId(alias_or_contract_id.to_owned(), e)); + }; + Ok(contract) } } diff --git a/cmd/soroban-cli/src/config/mod.rs b/cmd/soroban-cli/src/config/mod.rs index b961f0f67..d7d3acdc8 100644 --- a/cmd/soroban-cli/src/config/mod.rs +++ b/cmd/soroban-cli/src/config/mod.rs @@ -20,6 +20,8 @@ pub mod secret; pub mod sign_with; pub mod upgrade_check; +pub use alias::ContractAddress; + #[derive(thiserror::Error, Debug)] pub enum Error { #[error(transparent)] diff --git a/cmd/soroban-cli/src/key.rs b/cmd/soroban-cli/src/key.rs index 3f4dcaf7c..37099bd34 100644 --- a/cmd/soroban-cli/src/key.rs +++ b/cmd/soroban-cli/src/key.rs @@ -4,7 +4,7 @@ use crate::xdr::{ }; use crate::{ commands::contract::Durability, - config::{locator, network::Network}, + config::{alias, locator, network::Network}, wasm, }; use clap::arg; @@ -22,6 +22,8 @@ pub enum Error { Wasm(#[from] wasm::Error), #[error(transparent)] Locator(#[from] locator::Error), + #[error(transparent)] + ContractId(#[from] alias::Error), } #[derive(Debug, clap::Args, Clone)] @@ -34,7 +36,7 @@ pub struct Args { required_unless_present = "wasm", required_unless_present = "wasm_hash" )] - pub contract_id: Option, + pub contract_id: Option, /// Storage key (symbols only) #[arg(long = "key", conflicts_with = "key_xdr")] pub key: Option>, @@ -97,8 +99,11 @@ impl Args { } else { vec![ScVal::LedgerKeyContractInstance] }; - let contract = - locator.resolve_contract_id(self.contract_id.as_ref().unwrap(), network_passphrase)?; + let contract = self + .contract_id + .as_ref() + .unwrap() + .resolve_contract_id(locator, network_passphrase)?; Ok(keys .into_iter() diff --git a/cmd/soroban-cli/src/wasm.rs b/cmd/soroban-cli/src/wasm.rs index 5f0ed3b5c..a907e021a 100644 --- a/cmd/soroban-cli/src/wasm.rs +++ b/cmd/soroban-cli/src/wasm.rs @@ -121,16 +121,10 @@ pub fn len(p: &Path) -> Result { } pub async fn fetch_from_contract( - contract_id: &str, + stellar_strkey::Contract(contract_id): &stellar_strkey::Contract, network: &Network, - locator: &locator::Args, ) -> Result, Error> { tracing::trace!(?network); - - let contract_id = &locator - .resolve_contract_id(contract_id, &network.network_passphrase)? - .0; - let client = network.rpc_client()?; client .verify_network_passphrase(Some(&network.network_passphrase))