From 0d4dff00ed92006e4d3a12072236235cefddb0b4 Mon Sep 17 00:00:00 2001 From: Poytr1 Date: Sat, 23 Sep 2023 23:09:09 +0800 Subject: [PATCH] make the call trace test work (#4) --- ...ace_test__test_get_call_trace_by_hash.json | 314 ++++++++++++++++++ api/src/tests/call_trace_test.rs | 32 ++ api/src/tests/call_trace_tests.rs | 30 -- api/src/tests/mod.rs | 2 +- api/src/transactions.rs | 17 +- aptos-move/aptos-vm/src/aptos_vm.rs | 11 +- .../move/move-vm/runtime/src/interpreter.rs | 4 +- 7 files changed, 369 insertions(+), 41 deletions(-) create mode 100644 api/goldens/aptos_api__tests__call_trace_test__test_get_call_trace_by_hash.json create mode 100644 api/src/tests/call_trace_test.rs delete mode 100644 api/src/tests/call_trace_tests.rs diff --git a/api/goldens/aptos_api__tests__call_trace_test__test_get_call_trace_by_hash.json b/api/goldens/aptos_api__tests__call_trace_test__test_get_call_trace_by_hash.json new file mode 100644 index 0000000000000..18a4d0ed4d168 --- /dev/null +++ b/api/goldens/aptos_api__tests__call_trace_test__test_get_call_trace_by_hash.json @@ -0,0 +1,314 @@ +{ + "pc": 0, + "module_id": "", + "func_name": "create_account", + "inputs": [ + "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" + ], + "outputs": [], + "type_args": [], + "sub_traces": [ + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::account", + "func_name": "create_account", + "inputs": [ + "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" + ], + "outputs": [ + "(container 600003fbec88: [Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])" + ], + "type_args": [], + "sub_traces": [ + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::account", + "func_name": "create_account_unchecked", + "inputs": [ + "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" + ], + "outputs": [ + "(container 600003fbec88: [Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])" + ], + "type_args": [], + "sub_traces": [ + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::guid", + "func_name": "create", + "inputs": [ + "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)", + "(&container 600003fd1bd8)[3]" + ], + "outputs": [ + "(container 600003fd1b78: [(container 600003fd1848: [U64(0), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + ], + "type_args": [], + "sub_traces": [] + }, + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::event", + "func_name": "new_event_handle", + "inputs": [ + "(container 600003fd1a28: [(container 600003fd1ab8: [U64(0), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + ], + "outputs": [ + "(container 600003fd1f68: [U64(0), (container 600003fd18d8: [(container 600003fd1848: [U64(0), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])])" + ], + "type_args": [], + "sub_traces": [] + }, + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::guid", + "func_name": "create", + "inputs": [ + "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)", + "(&container 600003fd1bd8)[3]" + ], + "outputs": [ + "(container 600003fd1e48: [(container 600003fd1ea8: [U64(1), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + ], + "type_args": [], + "sub_traces": [] + }, + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::event", + "func_name": "new_event_handle", + "inputs": [ + "(container 600003fd18d8: [(container 600003fd1ab8: [U64(1), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + ], + "outputs": [ + "(container 600003fd1d88: [U64(0), (container 600003fd1de8: [(container 600003fd1ea8: [U64(1), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])])" + ], + "type_args": [], + "sub_traces": [] + }, + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::option", + "func_name": "none", + "inputs": [], + "outputs": [ + "(container 600003fbf978: [(container 600003fbf6d8: [])])" + ], + "type_args": [], + "sub_traces": [] + }, + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::option", + "func_name": "none", + "inputs": [], + "outputs": [ + "(container 600003fbc978: [(container 600003fbc948: [])])" + ], + "type_args": [], + "sub_traces": [] + } + ] + } + ] + }, + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::coin", + "func_name": "register", + "inputs": [ + "(&container 600003fd1f98)" + ], + "outputs": [], + "type_args": [], + "sub_traces": [ + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::signer", + "func_name": "address_of", + "inputs": [ + "(&container 600003fd1f98)" + ], + "outputs": [ + "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" + ], + "type_args": [], + "sub_traces": [] + }, + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::coin", + "func_name": "is_account_registered", + "inputs": [ + "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" + ], + "outputs": [ + "false" + ], + "type_args": [], + "sub_traces": [] + }, + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::account", + "func_name": "register_coin", + "inputs": [ + "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" + ], + "outputs": [], + "type_args": [], + "sub_traces": [ + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::event", + "func_name": "emit_event", + "inputs": [ + "(&container 600003fd1b78)", + "(container 600003fbfb88: [(container 600003fbf588: [Address(1), (container 600003fbee38: [97, 112, 116, 111, 115, 95, 99, 111, 105, 110]), (container 600003fbe8f8: [65, 112, 116, 111, 115, 67, 111, 105, 110])])])" + ], + "outputs": [], + "type_args": [], + "sub_traces": [] + } + ] + }, + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::account", + "func_name": "new_event_handle", + "inputs": [ + "(&container 600003fd1f98)" + ], + "outputs": [ + "(container 600003fbe808: [U64(0), (container 600003fbeb68: [(container 600003fbeb38: [U64(2), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])])" + ], + "type_args": [], + "sub_traces": [ + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::account", + "func_name": "create_guid", + "inputs": [ + "(&container 600003fd1f98)" + ], + "outputs": [ + "(container 600003fbea48: [(container 600003fbee38: [U64(2), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + ], + "type_args": [], + "sub_traces": [ + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::signer", + "func_name": "address_of", + "inputs": [ + "(&container 600003fd1f98)" + ], + "outputs": [ + "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" + ], + "type_args": [], + "sub_traces": [] + }, + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::guid", + "func_name": "create", + "inputs": [ + "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)", + "(&container 600003fbc948)[2]" + ], + "outputs": [ + "(container 600003fbe808: [(container 600003fbeef8: [U64(2), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + ], + "type_args": [], + "sub_traces": [] + } + ] + }, + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::event", + "func_name": "new_event_handle", + "inputs": [ + "(container 600003fbeb68: [(container 600003fbeb38: [U64(2), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + ], + "outputs": [ + "(container 600003fbe808: [U64(0), (container 600003fbee38: [(container 600003fbea48: [U64(2), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])])" + ], + "type_args": [], + "sub_traces": [] + } + ] + }, + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::account", + "func_name": "new_event_handle", + "inputs": [ + "(&container 600003fd1f98)" + ], + "outputs": [ + "(container 600003fbec28: [U64(0), (container 600003fbeb68: [(container 600003fbea48: [U64(3), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])])" + ], + "type_args": [], + "sub_traces": [ + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::account", + "func_name": "create_guid", + "inputs": [ + "(&container 600003fd1f98)" + ], + "outputs": [ + "(container 600003fbeef8: [(container 600003fbe508: [U64(3), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + ], + "type_args": [], + "sub_traces": [ + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::signer", + "func_name": "address_of", + "inputs": [ + "(&container 600003fd1f98)" + ], + "outputs": [ + "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" + ], + "type_args": [], + "sub_traces": [] + }, + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::guid", + "func_name": "create", + "inputs": [ + "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)", + "(&container 600003fbc948)[2]" + ], + "outputs": [ + "(container 600003fbec28: [(container 600003fbeb98: [U64(3), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + ], + "type_args": [], + "sub_traces": [] + } + ] + }, + { + "pc": 0, + "module_id": "0000000000000000000000000000000000000000000000000000000000000001::event", + "func_name": "new_event_handle", + "inputs": [ + "(container 600003fbeb68: [(container 600003fbea48: [U64(3), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + ], + "outputs": [ + "(container 600003fbec28: [U64(0), (container 600003fbe508: [(container 600003fbeef8: [U64(3), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])])" + ], + "type_args": [], + "sub_traces": [] + } + ] + } + ] + } + ] +} diff --git a/api/src/tests/call_trace_test.rs b/api/src/tests/call_trace_test.rs new file mode 100644 index 0000000000000..3bf3583c0a3ab --- /dev/null +++ b/api/src/tests/call_trace_test.rs @@ -0,0 +1,32 @@ +// Copyright © Aptos Foundation +// SPDX-License-Identifier: Apache-2.0 + +use aptos_api_test_context::{assert_json, current_function_name, pretty, TestContext}; +use super::new_test_context; + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_get_call_trace_by_hash() { + let mut context = new_test_context(current_function_name!()); + let account = context.gen_account(); + let txn = context.create_user_account(&account).await; + context.commit_block(&vec![txn.clone()]).await; + + let txns = context.get("/transactions?start=2&limit=1").await; + assert_eq!(1, txns.as_array().unwrap().len()); + + let resp = context + .get(&format!( + "/transactions/by_hash/{}", + txns[0]["hash"].as_str().unwrap() + )) + .await; + assert_json(resp, txns[0].clone()); + + let call_trace_resp = context. + get(&format!( + "/call_trace/by_hash/{}", + txns[0]["hash"].as_str().unwrap() + )) + .await; + context.check_golden_output(call_trace_resp); +} diff --git a/api/src/tests/call_trace_tests.rs b/api/src/tests/call_trace_tests.rs deleted file mode 100644 index 17cad102a11b1..0000000000000 --- a/api/src/tests/call_trace_tests.rs +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright © Aptos Foundation -// SPDX-License-Identifier: Apache-2.0 - -use super::new_test_context; -use aptos_api_test_context::current_function_name; -use serde_json::json; - -#[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn test_simple_call_trace() { - let mut context = new_test_context(current_function_name!()); - let creator = &mut context.gen_account(); - let owner = &mut context.gen_account(); - let txn1 = context.mint_user_account(creator).await; - let txn2 = context.account_transfer(creator, owner, 100_000); - - context.commit_block(&vec![txn1, txn2]).await; - - let resp = context - .post( - "/view", - json!({ - "function":"0x1::coin::balance", - "arguments": vec![owner.address().to_string()], - "type_arguments": vec!["0x1::aptos_coin::AptosCoin"], - }), - ) - .await; - - context.check_golden_output_no_prune(resp); -} diff --git a/api/src/tests/mod.rs b/api/src/tests/mod.rs index 16345b7041eec..7b3236c5838d1 100644 --- a/api/src/tests/mod.rs +++ b/api/src/tests/mod.rs @@ -17,7 +17,7 @@ mod string_resource_test; mod transaction_vector_test; mod transactions_test; mod view_function; -mod call_trace_tests; +mod call_trace_test; use aptos_api_test_context::{new_test_context as super_new_test_context, TestContext}; use aptos_config::config::NodeConfig; diff --git a/api/src/transactions.rs b/api/src/transactions.rs index 7d02ef4e1461d..d01e61fa61bfd 100644 --- a/api/src/transactions.rs +++ b/api/src/transactions.rs @@ -68,8 +68,6 @@ type SubmitTransactionsBatchResult = type SimulateTransactionResult = poem::Result, SubmitTransactionError>; -type DebugCallTraceResult = poem::Result, SubmitTransactionError>; - // TODO: Consider making both content types accept either // SubmitTransactionRequest or SignedTransaction, the way // it is now is quite confusing. @@ -206,7 +204,7 @@ impl TransactionsApi { #[oai( path = "/call_trace/by_hash/:txn_hash", method = "get", - operation_id = "get_transaction_call_trace_by_hash", + operation_id = "get_call_trace_by_hash", tag = "ApiTags::Transactions" )] async fn get_transaction_call_trace_by_hash( @@ -270,14 +268,21 @@ impl TransactionsApi { Ok(CallTraces::new()) } Transaction::UserTransaction(user_transaction) => { + // use the previous ledger version where it doesn't take the effect + let previous_ledger_version = user_transaction.info.version.0 - 1; + let (requested_ledger_info, _) = self + .context + .get_latest_ledger_info_and_verify_lookup_version( + Some(Version::from(previous_ledger_version)), + )?; let state_view = self .context - .state_view_at_version(Version::from(user_transaction.info.version)) + .state_view_at_version(Version::from(previous_ledger_version)) .map_err(|err| { BasicErrorWith404::bad_request_with_code( err, AptosErrorCode::InternalError, - &ledger_info, + &requested_ledger_info, ) })?; let payload = user_transaction.request.payload; @@ -294,7 +299,7 @@ impl TransactionsApi { BasicErrorWith404::bad_request_with_code( err, AptosErrorCode::InvalidInput, - &ledger_info, + &requested_ledger_info, ) })?; AptosVM::get_call_trace( diff --git a/aptos-move/aptos-vm/src/aptos_vm.rs b/aptos-move/aptos-vm/src/aptos_vm.rs index 367698cb851f3..e231196b96f2b 100644 --- a/aptos-move/aptos-vm/src/aptos_vm.rs +++ b/aptos-move/aptos-vm/src/aptos_vm.rs @@ -79,7 +79,6 @@ use std::{ }, }; use move_core_types::call_trace::CallTraces; -// use move_vm_types::call_trace::CallTraces; static EXECUTION_CONCURRENCY_LEVEL: OnceCell = OnceCell::new(); static NUM_EXECUTION_SHARD: OnceCell = OnceCell::new(); @@ -1431,7 +1430,15 @@ impl AptosVM { ))); let resolver = vm.as_move_resolver(state_view); let mut session = vm.new_session(&resolver, SessionId::Void); - Ok(session.call_trace(&module_id, &func_name, type_args, arguments, &mut gas_meter).unwrap()) + let call_trace_res = session.call_trace(&module_id, &func_name, type_args, arguments, &mut gas_meter); + match call_trace_res { + Ok(call_trace) => { + Ok(call_trace) + } + Err(err) => { + Err(anyhow::Error::msg(err.into_vm_status())) + } + } } pub fn execute_view_function( diff --git a/third_party/move/move-vm/runtime/src/interpreter.rs b/third_party/move/move-vm/runtime/src/interpreter.rs index cbd7c7e73e3ea..c7187fea6ef2f 100644 --- a/third_party/move/move-vm/runtime/src/interpreter.rs +++ b/third_party/move/move-vm/runtime/src/interpreter.rs @@ -405,7 +405,7 @@ impl Interpreter { } call_traces.push(InternalCallTrace { pc: 0, - module_id: "".to_string(), + module_id: module_id.to_string(), func_name: func.name().to_string(), inputs: inputs.into_iter().enumerate().map(|(_, i)| i.to_string()).collect(), outputs: vec![], @@ -472,7 +472,7 @@ impl Interpreter { } call_traces.push(InternalCallTrace { pc: 0, - module_id: "".to_string(), + module_id: module_id.to_string(), func_name: func.name().to_string(), inputs: inputs.into_iter().enumerate().map(|(_, i)| i.to_string()).collect(), outputs: vec![],