Skip to content

Commit

Permalink
[rooch-networkgh-2385] add dry run check for run_function.rs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Feliciss committed Aug 18, 2024
1 parent 76ef961 commit c3c9a3c
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions crates/rooch/src/commands/move_cli/commands/run_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use move_core_types::language_storage::TypeTag;
use moveos_types::transaction::MoveAction;
use rooch_key::key_derive::verify_password;
use rooch_key::keystore::account_keystore::AccountKeystore;
use rooch_rpc_api::jsonrpc_types::{ExecuteTransactionResponseView, HumanReadableDisplay};
use rooch_rpc_api::jsonrpc_types::{ExecuteTransactionResponseView, HumanReadableDisplay, KeptVMStatusView};
use rooch_types::function_arg::parse_function_arg;
use rooch_types::{
address::RoochAddress,
Expand Down Expand Up @@ -81,15 +81,24 @@ impl CommandAction<ExecuteTransactionResponseView> for RunFunction {
})
.collect::<Result<Vec<_>>>()?;
let action = MoveAction::new_function_call(function_id, type_args, args);
match (self.tx_options.authenticator, self.tx_options.session_key) {

let dry_run_result = context
.dry_run(
context
.build_tx_data(sender, action.clone(), max_gas_amount)
.await?
)
.await;

let mut result = match (self.tx_options.authenticator, self.tx_options.session_key) {
(Some(authenticator), _) => {
let tx_data = context
.build_tx_data(sender, action, max_gas_amount)
.await?;
//TODO the authenticator usually is associated with the RoochTransactinData
//So we need to find a way to let user generate the authenticator based on the tx_data.
let tx = RoochTransaction::new(tx_data, authenticator.into());
context.execute(tx).await
context.execute(tx).await?
}
(_, Some(session_key)) => {
let tx_data = context
Expand Down Expand Up @@ -124,13 +133,13 @@ impl CommandAction<ExecuteTransactionResponseView> for RunFunction {
)
.map_err(|e| RoochError::SignMessageError(e.to_string()))?
};
context.execute(tx).await
context.execute(tx).await?
}
(None, None) => {
if context.keystore.get_if_password_is_empty() {
context
.sign_and_execute(sender, action, None, max_gas_amount)
.await
.await?
} else {
let password =
prompt_password("Enter the password to run functions:").unwrap_or_default();
Expand All @@ -147,10 +156,18 @@ impl CommandAction<ExecuteTransactionResponseView> for RunFunction {

context
.sign_and_execute(sender, action, Some(password), max_gas_amount)
.await
.await?
}
}
}
};

if let Ok(dry_run_resp) = dry_run_result {
if dry_run_resp.raw_output.status != KeptVMStatusView::Executed {
result.error_info = Some(dry_run_resp);
}
};

Ok(result)
}

/// Executes the command, and serializes it to the common JSON output type
Expand All @@ -171,6 +188,21 @@ impl CommandAction<ExecuteTransactionResponseView> for RunFunction {
output.push_str(&exe_info.to_human_readable_string(false, 0));

if let Some(txn_output) = &result.output {
// print error info
if let Some(error_info) = result.clone().error_info {
output.push_str(
format!(
"\n\n\nTransaction dry run failed:\n {:?}",
error_info.vm_error_info.error_message
)
.as_str(),
);
output.push_str("\nCallStack trace:\n".to_string().as_str());
for (idx, item) in error_info.vm_error_info.execution_state.iter().enumerate() {
output.push_str(format!("{} {}\n", idx, item).as_str());
}
};

// print objects changes
output.push_str("\n\n");
output.push_str(&txn_output.changeset.to_human_readable_string(false, 0));
Expand Down

0 comments on commit c3c9a3c

Please sign in to comment.