Skip to content

Commit

Permalink
[gh-2385] add dry run check for run_function.rs. (#2458)
Browse files Browse the repository at this point in the history
* [gh-2385] add dry run check for run_function.rs.

* [gh-2385] fmt code.

* [gh-2385] return errors if dry_run fails.

---------

Co-authored-by: Feliciss <10203-feliciss@users.noreply.0xacab.org>
  • Loading branch information
feliciss and Feliciss authored Aug 23, 2024
1 parent 75f0be4 commit 782cabe
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 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,9 @@ 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 +83,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 +135,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 +158,16 @@ impl CommandAction<ExecuteTransactionResponseView> for RunFunction {

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

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

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 782cabe

Please sign in to comment.