Skip to content

Commit

Permalink
fix: ensure --is-view doesn't fetch account info
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal authored and gitbutler-client committed Apr 2, 2024
1 parent ab17b86 commit 3f87119
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 31 deletions.
24 changes: 20 additions & 4 deletions cmd/crates/soroban-test/tests/it/integration/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ use crate::integration::util::extend_contract;

use super::util::{deploy_hello, extend, HELLO_WORLD};

#[tokio::test]
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);
cmd.config.source_account = String::new();
cmd.is_view = true;
let res = sandbox.run_cmd_with(cmd, "test").await.unwrap();
assert_eq!(res, format!(r#"["Hello",{world:?}]"#));
}

#[tokio::test]
async fn invoke() {
let sandbox = &TestEnv::new();
Expand Down Expand Up @@ -132,12 +144,16 @@ fn invoke_hello_world(sandbox: &TestEnv, id: &str) {
.success();
}

async fn invoke_hello_world_with_lib(e: &TestEnv, id: &str) {
let cmd = contract::invoke::Cmd {
fn hello_world_cmd(id: &str, arg: &str) -> contract::invoke::Cmd {
contract::invoke::Cmd {
contract_id: id.to_string(),
slop: vec!["hello".into(), "--world=world".into()],
slop: vec!["hello".into(), format!("--world={arg}").into()],
..Default::default()
};
}
}

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();
assert_eq!(res, r#"["Hello","world"]"#);
}
Expand Down
76 changes: 49 additions & 27 deletions cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ use heck::ToKebabCase;

use soroban_env_host::{
xdr::{
self, Error as XdrError, Hash, HostFunction, InvokeContractArgs, InvokeHostFunctionOp,
LedgerEntryData, LedgerFootprint, Memo, MuxedAccount, Operation, OperationBody,
Preconditions, ScAddress, ScSpecEntry, ScSpecFunctionV0, ScSpecTypeDef, ScVal, ScVec,
SequenceNumber, SorobanAuthorizationEntry, SorobanResources, Transaction, TransactionExt,
Uint256, VecM,
self, Hash, HostFunction, InvokeContractArgs, InvokeHostFunctionOp, LedgerEntryData,
LedgerFootprint, Memo, MuxedAccount, Operation, OperationBody, Preconditions, PublicKey,
ScAddress, ScSpecEntry, ScSpecFunctionV0, ScSpecTypeDef, ScVal, ScVec, SequenceNumber,
SorobanAuthorizationEntry, SorobanResources, String32, StringM, Transaction,
TransactionExt, Uint256, VecM,
},
HostError,
};

use soroban_sdk::xdr::{AccountEntry, AccountEntryExt, AccountId, DiagnosticEvent, Thresholds};
use soroban_spec::read::FromWasmError;
use stellar_strkey::DecodeError;

Expand Down Expand Up @@ -78,7 +79,7 @@ pub enum Error {
error: soroban_spec_tools::Error,
},
#[error("cannot add contract to ledger entries: {0}")]
CannotAddContractToLedgerEntries(XdrError),
CannotAddContractToLedgerEntries(xdr::Error),
#[error(transparent)]
// TODO: the Display impl of host errors is pretty user-unfriendly
// (it just calls Debug). I think we can do better than that
Expand Down Expand Up @@ -107,7 +108,7 @@ pub enum Error {
error: soroban_spec_tools::Error,
},
#[error(transparent)]
Xdr(#[from] XdrError),
Xdr(#[from] xdr::Error),
#[error("error parsing int: {0}")]
ParseIntError(#[from] ParseIntError),
#[error(transparent)]
Expand Down Expand Up @@ -312,16 +313,21 @@ impl NetworkRunnable for Cmd {
let _ = self.build_host_function_parameters(contract_id, spec_entries)?;
}
let client = rpc::Client::new(&network.rpc_url)?;
client
.verify_network_passphrase(Some(&network.network_passphrase))
.await?;
let key = config.key_pair()?;

// Get the account sequence number
let public_strkey =
stellar_strkey::ed25519::PublicKey(key.verifying_key().to_bytes()).to_string();
let account_details = client.get_account(&public_strkey).await?;
let account_details = if self.is_view {
default_account_entry()
} else {
client
.verify_network_passphrase(Some(&network.network_passphrase))
.await?;
let key = config.key_pair()?;

// Get the account sequence number
let public_strkey =
stellar_strkey::ed25519::PublicKey(key.verifying_key().to_bytes()).to_string();
client.get_account(&public_strkey).await?
};
let sequence: i64 = account_details.seq_num.into();
let AccountId(PublicKey::PublicKeyTypeEd25519(account_id)) = account_details.account_id;

// Get the contract
let spec_entries = client.get_remote_contract_spec(&contract_id).await?;
Expand All @@ -333,7 +339,7 @@ impl NetworkRunnable for Cmd {
host_function_params.clone(),
sequence + 1,
self.fee.fee,
&key,
account_id,
)?;
if self.fee.build_only {
return Ok(TxnResult::from_xdr(&tx)?);
Expand All @@ -354,7 +360,7 @@ impl NetworkRunnable for Cmd {
let res = client
.send_assembled_transaction(
txn,
&key,
&self.config.key_pair()?,
&signers,
&network.network_passphrase,
Some(log_events),
Expand All @@ -369,10 +375,27 @@ impl NetworkRunnable for Cmd {
}
}

const DEFAULT_ACCOUNT_ID: AccountId = AccountId(PublicKey::PublicKeyTypeEd25519(Uint256([0; 32])));

fn default_account_entry() -> AccountEntry {
AccountEntry {
account_id: DEFAULT_ACCOUNT_ID,
balance: 0,
seq_num: SequenceNumber(0),
num_sub_entries: 0,
inflation_dest: None,
flags: 0,
home_domain: String32::from(unsafe { StringM::<32>::from_str("TEST").unwrap_unchecked() }),
thresholds: Thresholds([0; 4]),
signers: unsafe { [].try_into().unwrap_unchecked() },
ext: AccountEntryExt::V0,
}
}

fn log_events(
footprint: &LedgerFootprint,
auth: &[VecM<SorobanAuthorizationEntry>],
events: &[xdr::DiagnosticEvent],
events: &[DiagnosticEvent],
) {
crate::log::auth(auth);
crate::log::diagnostic_events(events, tracing::Level::TRACE);
Expand Down Expand Up @@ -405,7 +428,7 @@ fn build_invoke_contract_tx(
parameters: InvokeContractArgs,
sequence: i64,
fee: u32,
key: &SigningKey,
source_account_id: Uint256,
) -> Result<Transaction, Error> {
let op = Operation {
source_account: None,
Expand All @@ -415,7 +438,7 @@ fn build_invoke_contract_tx(
}),
};
Ok(Transaction {
source_account: MuxedAccount::Ed25519(Uint256(key.verifying_key().to_bytes())),
source_account: MuxedAccount::Ed25519(source_account_id),
fee,
seq_num: SequenceNumber(sequence),
cond: Preconditions::None,
Expand Down Expand Up @@ -475,16 +498,15 @@ fn build_custom_cmd(name: &str, spec: &Spec) -> Result<clap::Command, Error> {

// Set up special-case arg rules
arg = match type_ {
xdr::ScSpecTypeDef::Bool => arg
ScSpecTypeDef::Bool => arg
.num_args(0..1)
.default_missing_value("true")
.default_value("false")
.num_args(0..=1),
xdr::ScSpecTypeDef::Option(_val) => arg.required(false),
xdr::ScSpecTypeDef::I256
| xdr::ScSpecTypeDef::I128
| xdr::ScSpecTypeDef::I64
| xdr::ScSpecTypeDef::I32 => arg.allow_hyphen_values(true),
ScSpecTypeDef::Option(_val) => arg.required(false),
ScSpecTypeDef::I256 | ScSpecTypeDef::I128 | ScSpecTypeDef::I64 | ScSpecTypeDef::I32 => {
arg.allow_hyphen_values(true)
}
_ => arg,
};

Expand Down

0 comments on commit 3f87119

Please sign in to comment.