Skip to content

Commit

Permalink
[api] Add contextual information to VMStatus::Error
Browse files Browse the repository at this point in the history
  • Loading branch information
runtian-zhou authored and davidiw committed Oct 31, 2023
1 parent 54ceaac commit 06367ba
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
26 changes: 26 additions & 0 deletions api/src/tests/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,32 @@ async fn test_gas_estimation_static_override() {
context.check_golden_output(resp);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn simulation_failure_error_message() {
let mut context = new_test_context(current_function_name!());
let admin0 = context.root_account().await;

// script {
// fun main() {
// 1/0;
// }
// }

let output = context.simulate_transaction(&admin0, json!({
"type": "script_payload",
"code": {
"bytecode": "a11ceb0b030000000105000100000000050601000000000000000600000000000000001a0102",
},
"type_arguments": [],
"arguments": [],
}), 200).await;

assert!(output.as_array().unwrap()[0]["vm_status"]
.as_str()
.unwrap()
.contains("Division by zero"));
}

fn gen_string(len: u64) -> String {
let mut rng = thread_rng();
std::iter::repeat(())
Expand Down
20 changes: 18 additions & 2 deletions api/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use aptos_types::{
vm_status::StatusCode,
};
use aptos_vm::{data_cache::AsMoveResolver, AptosVM};
use move_core_types::vm_status::VMStatus;
use poem_openapi::{
param::{Path, Query},
payload::Json,
Expand Down Expand Up @@ -1188,7 +1189,7 @@ impl TransactionsApi {
// Simulate transaction
let state_view = self.context.latest_state_view_poem(&ledger_info)?;
let move_resolver = state_view.as_move_resolver();
let (_, output) = AptosVM::simulate_signed_transaction(&txn, &move_resolver);
let (vm_status, output) = AptosVM::simulate_signed_transaction(&txn, &move_resolver);
let version = ledger_info.version();

// Ensure that all known statuses return their values in the output (even if they aren't supposed to)
Expand Down Expand Up @@ -1230,7 +1231,22 @@ impl TransactionsApi {
let mut user_transactions = Vec::new();
for transaction in transactions.into_iter() {
match transaction {
Transaction::UserTransaction(user_txn) => user_transactions.push(*user_txn),
Transaction::UserTransaction(user_txn) => {
let mut txn = *user_txn;
match &vm_status {
VMStatus::Error {
message: Some(msg), ..
}
| VMStatus::ExecutionFailure {
message: Some(msg), ..
} => {
txn.info.vm_status +=
format!("\nExecution failed with status: {}", msg).as_str();
},
_ => (),
}
user_transactions.push(txn);
},
_ => {
return Err(SubmitTransactionError::internal_with_code(
"Simulation transaction resulted in a non-UserTransaction",
Expand Down
5 changes: 4 additions & 1 deletion third_party/move/move-vm/types/src/values/values_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,10 @@ impl IntegerValue {
return Err(PartialVMError::new(StatusCode::INTERNAL_TYPE_ERROR).with_message(msg));
},
};
res.ok_or_else(|| PartialVMError::new(StatusCode::ARITHMETIC_ERROR))
res.ok_or_else(|| {
PartialVMError::new(StatusCode::ARITHMETIC_ERROR)
.with_message("Division by zero error".to_string())
})
}

pub fn rem_checked(self, other: Self) -> PartialVMResult<Self> {
Expand Down

0 comments on commit 06367ba

Please sign in to comment.