Skip to content

Commit

Permalink
fix: address PR comments and skip install step for build only deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed May 7, 2024
1 parent 9f57428 commit 83157b3
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 42 deletions.
2 changes: 1 addition & 1 deletion cmd/crates/soroban-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl TestEnv {
Some(&config),
)
.await?
.res()
.into_res()
.unwrap())
}

Expand Down
12 changes: 7 additions & 5 deletions cmd/soroban-cli/src/commands/contract/deploy/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ impl Cmd {
#[async_trait::async_trait]
impl NetworkRunnable for Cmd {
type Error = Error;
type Result = String;
type Result = stellar_strkey::Contract;

async fn run_against_rpc_server(
&self,
args: Option<&global::Args>,
config: Option<&config::Args>,
) -> Result<TxnResult<String>, Error> {
) -> Result<TxnResult<Self::Result>, Error> {
let config = config.unwrap_or(&self.config);
// Parse asset
let asset = parse_asset(&self.asset)?;
Expand Down Expand Up @@ -115,6 +115,10 @@ impl NetworkRunnable for Cmd {
}
let txn = client.create_assembled_transaction(&tx).await?;
let txn = self.fee.apply_to_assembled_txn(txn)?;
let txn = match txn {
TxnResult::Xdr(raw) => return Ok(TxnResult::Xdr(raw)),
TxnResult::Res(txn) => txn,
};
let get_txn_resp = client
.send_assembled_transaction(txn, &key, &[], network_passphrase, None, None)
.await?
Expand All @@ -123,9 +127,7 @@ impl NetworkRunnable for Cmd {
data::write(get_txn_resp, &network.rpc_uri()?)?;
}

Ok(TxnResult::Xdr(
stellar_strkey::Contract(contract_id.0).to_string(),
))
Ok(TxnResult::Res(stellar_strkey::Contract(contract_id.0)))
}
}

Expand Down
33 changes: 22 additions & 11 deletions cmd/soroban-cli/src/commands/contract/deploy/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ pub enum Error {
Data(#[from] data::Error),
#[error(transparent)]
Network(#[from] network::Error),
#[error(transparent)]
Wasm(#[from] wasm::Error),
}

impl Cmd {
Expand All @@ -122,17 +124,22 @@ impl NetworkRunnable for Cmd {
) -> Result<TxnResult<String>, Error> {
let config = config.unwrap_or(&self.config);
let wasm_hash = if let Some(wasm) = &self.wasm {
let mut fee = self.fee.clone();
fee.build_only = false;
let hash = install::Cmd {
wasm: wasm::Args { wasm: wasm.clone() },
config: config.clone(),
fee,
ignore_checks: self.ignore_checks,
}
.run_against_rpc_server(global_args, Some(config))
.await?;
hex::encode(hash.try_res()?)
let hash = if self.fee.build_only {
wasm::Args { wasm: wasm.clone() }.hash()?
} else {
let mut fee = self.fee.clone();
fee.build_only = false;
install::Cmd {
wasm: wasm::Args { wasm: wasm.clone() },
config: config.clone(),
fee,
ignore_checks: self.ignore_checks,
}
.run_against_rpc_server(global_args, Some(config))
.await?
.try_into_res()?
};
hex::encode(hash)
} else {
self.wasm_hash
.as_ref()
Expand Down Expand Up @@ -181,6 +188,10 @@ impl NetworkRunnable for Cmd {

let txn = client.create_assembled_transaction(&txn).await?;
let txn = self.fee.apply_to_assembled_txn(txn)?;
let txn = match txn {
TxnResult::Xdr(raw) => return Ok(TxnResult::Xdr(raw)),
TxnResult::Res(txn) => txn,
};
let get_txn_resp = client
.send_assembled_transaction(txn, &key, &[], &network.network_passphrase, None, None)
.await?
Expand Down
13 changes: 5 additions & 8 deletions cmd/soroban-cli/src/commands/contract/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ pub enum Error {
Io(#[from] std::io::Error),
#[error("missing result")]
MissingResult,
#[error("Unexpected XDR")]
UnexpectedXdr,
#[error("unexpected contract code data type: {0:?}")]
UnexpectedContractCodeDataType(LedgerEntryData),
#[error("reading file {0:?}: {1}")]
Expand Down Expand Up @@ -117,13 +119,9 @@ impl Cmd {
}

pub async fn get_bytes(&self) -> Result<Vec<u8>, Error> {
// This is safe because fetch doesn't create a transaction
unsafe {
Ok(self
.run_against_rpc_server(None, None)
.await?
.res()
.unwrap_unchecked())
match self.run_against_rpc_server(None, None).await? {
TxnResult::Xdr(_) => Err(Error::UnexpectedXdr),
TxnResult::Res(v) => Ok(v),
}
}

Expand Down Expand Up @@ -153,7 +151,6 @@ impl NetworkRunnable for Cmd {
client
.verify_network_passphrase(Some(&network.network_passphrase))
.await?;
// async closures are not yet stable
Ok(TxnResult::Res(client.get_remote_wasm(&contract_id).await?))
}
}
Expand Down
9 changes: 6 additions & 3 deletions cmd/soroban-cli/src/commands/contract/id/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ pub enum Error {
}
impl Cmd {
pub fn run(&self) -> Result<(), Error> {
println!("{}", self.contract_address()?);
Ok(())
}

pub fn contract_address(&self) -> Result<stellar_strkey::Contract, Error> {
let asset = parse_asset(&self.asset)?;
let network = self.config.get_network()?;
let contract_id = contract_id_hash_from_asset(&asset, &network.network_passphrase)?;
let strkey_contract_id = stellar_strkey::Contract(contract_id.0).to_string();
println!("{strkey_contract_id}");
Ok(())
Ok(stellar_strkey::Contract(contract_id.0))
}
}
4 changes: 4 additions & 0 deletions cmd/soroban-cli/src/commands/contract/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ impl NetworkRunnable for Cmd {
.create_assembled_transaction(&tx_without_preflight)
.await?;
let txn = self.fee.apply_to_assembled_txn(txn)?;
let txn = match txn {
TxnResult::Xdr(raw) => return Ok(TxnResult::Xdr(raw)),
TxnResult::Res(txn) => txn,
};
let txn_resp = client
.send_assembled_transaction(txn, &key, &[], &network.network_passphrase, None, None)
.await?;
Expand Down
4 changes: 4 additions & 0 deletions cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ impl NetworkRunnable for Cmd {
}
let txn = client.create_assembled_transaction(&tx).await?;
let txn = self.fee.apply_to_assembled_txn(txn)?;
let txn = match txn {
TxnResult::Xdr(raw) => return Ok(TxnResult::Xdr(raw)),
TxnResult::Res(txn) => txn,
};
let sim_res = txn.sim_response();
if global_args.map_or(true, |a| !a.no_cache) {
data::write(sim_res.clone().into(), &network.rpc_uri()?)?;
Expand Down
17 changes: 15 additions & 2 deletions cmd/soroban-cli/src/commands/txn_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ impl<T> TxnResult<T> {
}
}

pub fn res(self) -> Option<T> {
pub fn res(&self) -> Option<&T> {
match self {
TxnResult::Res(res) => Some(res),
TxnResult::Xdr(_) => None,
}
}

pub fn into_res(self) -> Option<T> {
match self {
TxnResult::Res(res) => Some(res),
TxnResult::Xdr(_) => None,
Expand All @@ -38,9 +45,15 @@ impl<T> TxnResult<T> {
self.xdr().ok_or(Error::XdrStringExpected)
}

pub fn try_res(self) -> Result<T, Error> {
pub fn try_res(&self) -> Result<&T, Error> {
self.res().ok_or(Error::ResultExpected)
}
pub fn try_into_res(self) -> Result<T, Error> {
match self {
TxnResult::Res(res) => Ok(res),
TxnResult::Xdr(_) => Err(Error::XdrStringExpected),
}
}
}

impl<T> Display for TxnResult<T>
Expand Down
20 changes: 9 additions & 11 deletions cmd/soroban-cli/src/fee.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use clap::arg;

use soroban_env_host::xdr::{self, WriteXdr};
use soroban_env_host::xdr;
use soroban_rpc::Assembled;

use crate::commands::HEADING_RPC;
use crate::commands::{txn_result::TxnResult, HEADING_RPC};

#[derive(Debug, clap::Args, Clone)]
#[group(skip)]
Expand All @@ -26,22 +26,20 @@ pub struct Args {
}

impl Args {
pub fn apply_to_assembled_txn(&self, txn: Assembled) -> Result<Assembled, xdr::Error> {
pub fn apply_to_assembled_txn(
&self,
txn: Assembled,
) -> Result<TxnResult<Assembled>, xdr::Error> {
let simulated_txn = if let Some(instructions) = self.instructions {
txn.set_max_instructions(instructions)
} else {
add_padding_to_instructions(txn)
};
if self.sim_only {
println!(
"{}",
simulated_txn
.transaction()
.to_xdr_base64(xdr::Limits::none())?
);
std::process::exit(0);
TxnResult::from_xdr(simulated_txn.transaction())
} else {
Ok(TxnResult::Res(simulated_txn))
}
Ok(simulated_txn)
}
}

Expand Down
7 changes: 6 additions & 1 deletion cmd/soroban-cli/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::arg;
use soroban_env_host::xdr::{self, LedgerKey, LedgerKeyContractCode};
use sha2::{Digest, Sha256};
use soroban_env_host::xdr::{self, Hash, LedgerKey, LedgerKeyContractCode};
use soroban_spec_tools::contract::{self, Spec};
use std::{
fs, io,
Expand Down Expand Up @@ -65,6 +66,10 @@ impl Args {
let contents = self.read()?;
Ok(Spec::new(&contents)?)
}

pub fn hash(&self) -> Result<Hash, Error> {
Ok(Hash(Sha256::digest(self.read()?).into()))
}
}

impl From<&PathBuf> for Args {
Expand Down

0 comments on commit 83157b3

Please sign in to comment.