From 8fa81ccb8e2da0df3d493551d02f8c34c868b6bf Mon Sep 17 00:00:00 2001 From: Danil Date: Tue, 24 Sep 2024 13:27:34 +0200 Subject: [PATCH] Return another version of result Signed-off-by: Danil --- ...6bd1b7b9269375f11f050099cb6d3c1427aa.json} | 4 +- core/lib/dal/src/blocks_web3_dal.rs | 4 +- core/lib/dal/src/transactions_dal.rs | 5 +- core/lib/types/src/api/mod.rs | 6 +-- core/lib/web3_decl/src/namespaces/debug.rs | 6 +-- .../backend_jsonrpsee/namespaces/debug.rs | 6 +-- .../api_server/src/web3/namespaces/debug.rs | 54 +++++++++---------- core/node/api_server/src/web3/tests/debug.rs | 38 +++++-------- 8 files changed, 53 insertions(+), 70 deletions(-) rename core/lib/dal/.sqlx/{query-760649b92c10c9afdfaff9c3e61aa2c560d7be337118b3c1fc5ef21c1d9e142b.json => query-6171f2d631f69dba52cd913742a46bd1b7b9269375f11f050099cb6d3c1427aa.json} (86%) diff --git a/core/lib/dal/.sqlx/query-760649b92c10c9afdfaff9c3e61aa2c560d7be337118b3c1fc5ef21c1d9e142b.json b/core/lib/dal/.sqlx/query-6171f2d631f69dba52cd913742a46bd1b7b9269375f11f050099cb6d3c1427aa.json similarity index 86% rename from core/lib/dal/.sqlx/query-760649b92c10c9afdfaff9c3e61aa2c560d7be337118b3c1fc5ef21c1d9e142b.json rename to core/lib/dal/.sqlx/query-6171f2d631f69dba52cd913742a46bd1b7b9269375f11f050099cb6d3c1427aa.json index 893b06281569..84ff845b0d01 100644 --- a/core/lib/dal/.sqlx/query-760649b92c10c9afdfaff9c3e61aa2c560d7be337118b3c1fc5ef21c1d9e142b.json +++ b/core/lib/dal/.sqlx/query-6171f2d631f69dba52cd913742a46bd1b7b9269375f11f050099cb6d3c1427aa.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT\n transactions.hash as tx_hash,\n transactions.index_in_block as tx_index_in_block,\n call_trace\n FROM\n call_traces\n INNER JOIN transactions ON tx_hash = transactions.hash\n WHERE\n transactions.miniblock_number = $1\n ORDER BY\n transactions.index_in_block\n ", + "query": "\n SELECT\n transactions.hash AS tx_hash,\n transactions.index_in_block AS tx_index_in_block,\n call_trace\n FROM\n call_traces\n INNER JOIN transactions ON tx_hash = transactions.hash\n WHERE\n transactions.miniblock_number = $1\n ORDER BY\n transactions.index_in_block\n ", "describe": { "columns": [ { @@ -30,5 +30,5 @@ false ] }, - "hash": "760649b92c10c9afdfaff9c3e61aa2c560d7be337118b3c1fc5ef21c1d9e142b" + "hash": "6171f2d631f69dba52cd913742a46bd1b7b9269375f11f050099cb6d3c1427aa" } diff --git a/core/lib/dal/src/blocks_web3_dal.rs b/core/lib/dal/src/blocks_web3_dal.rs index 07fb7b86cd95..3d17a919a073 100644 --- a/core/lib/dal/src/blocks_web3_dal.rs +++ b/core/lib/dal/src/blocks_web3_dal.rs @@ -554,8 +554,8 @@ impl BlocksWeb3Dal<'_, '_> { CallTrace, r#" SELECT - transactions.hash as tx_hash, - transactions.index_in_block as tx_index_in_block, + transactions.hash AS tx_hash, + transactions.index_in_block AS tx_index_in_block, call_trace FROM call_traces diff --git a/core/lib/dal/src/transactions_dal.rs b/core/lib/dal/src/transactions_dal.rs index 40489eb3cf58..2ec35c89ab01 100644 --- a/core/lib/dal/src/transactions_dal.rs +++ b/core/lib/dal/src/transactions_dal.rs @@ -19,9 +19,10 @@ use zksync_vm_interface::{ Call, TransactionExecutionMetrics, TransactionExecutionResult, TxExecutionStatus, }; -use crate::models::storage_transaction::parse_call_trace; use crate::{ - models::storage_transaction::{serialize_call_into_bytes, CallTrace, StorageTransaction}, + models::storage_transaction::{ + parse_call_trace, serialize_call_into_bytes, StorageTransaction, + }, Core, CoreDal, }; diff --git a/core/lib/types/src/api/mod.rs b/core/lib/types/src/api/mod.rs index e05697af5253..f08d5852abbe 100644 --- a/core/lib/types/src/api/mod.rs +++ b/core/lib/types/src/api/mod.rs @@ -737,9 +737,9 @@ pub enum BlockStatus { /// structs 1 for blocks tracing and one for txs and call tracing #[derive(Debug, Serialize, Deserialize, Clone)] #[serde(untagged)] -pub enum CallTracerResultWithNestedResult { - CallTrace(ResultDebugCall), - FlatCallTrace(Box), +pub enum CallTracerBlockResult { + CallTrace(Vec), + FlatCallTrace(Vec), } #[derive(Debug, Serialize, Deserialize, Clone)] diff --git a/core/lib/web3_decl/src/namespaces/debug.rs b/core/lib/web3_decl/src/namespaces/debug.rs index 857102c688bc..8ca5622e95d4 100644 --- a/core/lib/web3_decl/src/namespaces/debug.rs +++ b/core/lib/web3_decl/src/namespaces/debug.rs @@ -2,7 +2,7 @@ use jsonrpsee::core::RpcResult; use jsonrpsee::proc_macros::rpc; use zksync_types::{ - api::{BlockId, BlockNumber, CallTracerResult, CallTracerResultWithNestedResult, TracerConfig}, + api::{BlockId, BlockNumber, CallTracerBlockResult, CallTracerResult, TracerConfig}, transaction_request::CallRequest, }; @@ -25,14 +25,14 @@ pub trait DebugNamespace { &self, block: BlockNumber, options: Option, - ) -> RpcResult>; + ) -> RpcResult; #[method(name = "traceBlockByHash")] async fn trace_block_by_hash( &self, hash: H256, options: Option, - ) -> RpcResult>; + ) -> RpcResult; #[method(name = "traceCall")] async fn trace_call( diff --git a/core/node/api_server/src/web3/backend_jsonrpsee/namespaces/debug.rs b/core/node/api_server/src/web3/backend_jsonrpsee/namespaces/debug.rs index d261b3961724..50981a2b284f 100644 --- a/core/node/api_server/src/web3/backend_jsonrpsee/namespaces/debug.rs +++ b/core/node/api_server/src/web3/backend_jsonrpsee/namespaces/debug.rs @@ -1,5 +1,5 @@ use zksync_types::{ - api::{BlockId, BlockNumber, CallTracerResult, CallTracerResultWithNestedResult, TracerConfig}, + api::{BlockId, BlockNumber, CallTracerBlockResult, CallTracerResult, TracerConfig}, transaction_request::CallRequest, H256, }; @@ -16,7 +16,7 @@ impl DebugNamespaceServer for DebugNamespace { &self, block: BlockNumber, options: Option, - ) -> RpcResult> { + ) -> RpcResult { self.debug_trace_block_impl(BlockId::Number(block), options) .await .map_err(|err| self.current_method().map_err(err)) @@ -26,7 +26,7 @@ impl DebugNamespaceServer for DebugNamespace { &self, hash: H256, options: Option, - ) -> RpcResult> { + ) -> RpcResult { self.debug_trace_block_impl(BlockId::Hash(hash), options) .await .map_err(|err| self.current_method().map_err(err)) diff --git a/core/node/api_server/src/web3/namespaces/debug.rs b/core/node/api_server/src/web3/namespaces/debug.rs index 40d21e697a94..5814a5ad8dae 100644 --- a/core/node/api_server/src/web3/namespaces/debug.rs +++ b/core/node/api_server/src/web3/namespaces/debug.rs @@ -4,8 +4,8 @@ use zksync_multivm::interface::{Call, CallType, ExecutionResult, OneshotTracingP use zksync_system_constants::MAX_ENCODED_TX_SIZE; use zksync_types::{ api::{ - BlockId, BlockNumber, CallTracerResult, CallTracerResultWithNestedResult, DebugCall, - DebugCallType, ResultDebugCall, SupportedTracers, TracerConfig, + BlockId, BlockNumber, CallTracerBlockResult, CallTracerResult, DebugCall, DebugCallType, + ResultDebugCall, SupportedTracers, TracerConfig, }, debug_flat_call::{Action, CallResult, DebugCallFlat}, fee_model::BatchFeeInput, @@ -146,21 +146,18 @@ impl DebugNamespace { }); if !only_top_call { - call.calls - .into_iter() - .enumerate() - .for_each(|(number, call)| { - trace_address.push(number); - Self::map_flatten_call( - call, - calls, - trace_address, - false, - transaction_position, - transaction_hash, - ); - trace_address.pop(); - }); + for (number, call) in call.calls.into_iter().enumerate() { + trace_address.push(number); + Self::map_flatten_call( + call, + calls, + trace_address, + false, + transaction_position, + transaction_hash, + ); + trace_address.pop(); + } } } @@ -172,11 +169,11 @@ impl DebugNamespace { &self, block_id: BlockId, options: Option, - ) -> Result, Web3Error> { + ) -> Result { self.current_method().set_block_id(block_id); if matches!(block_id, BlockId::Number(BlockNumber::Pending)) { // See `EthNamespace::get_block_impl()` for an explanation why this check is needed. - return Ok(vec![]); + return Ok(CallTracerBlockResult::CallTrace(vec![])); } let mut connection = self.state.acquire_connection().await?; @@ -191,22 +188,19 @@ impl DebugNamespace { .map_err(DalError::generalize)?; let mut calls = vec![]; + let mut flat_calls = vec![]; for (call_trace, hash, index) in call_traces { match Self::map_call(call_trace, index, hash, options) { - CallTracerResult::CallTrace(call) => calls.push( - CallTracerResultWithNestedResult::CallTrace(ResultDebugCall { result: call }), - ), - CallTracerResult::FlattCallTrace(call) => calls.append( - &mut call - .into_iter() - .map(Box::new) - .map(CallTracerResultWithNestedResult::FlatCallTrace) - .collect(), - ), + CallTracerResult::CallTrace(call) => calls.push(ResultDebugCall { result: call }), + CallTracerResult::FlattCallTrace(mut call) => flat_calls.append(&mut call), } } - Ok(calls) + if calls.is_empty() && !flat_calls.is_empty() { + Ok(CallTracerBlockResult::FlatCallTrace(flat_calls)) + } else { + Ok(CallTracerBlockResult::CallTrace(calls)) + } } pub async fn debug_trace_transaction_impl( diff --git a/core/node/api_server/src/web3/tests/debug.rs b/core/node/api_server/src/web3/tests/debug.rs index 661320d1d1ad..3350ec7a0894 100644 --- a/core/node/api_server/src/web3/tests/debug.rs +++ b/core/node/api_server/src/web3/tests/debug.rs @@ -3,8 +3,7 @@ use zksync_multivm::interface::{Call, TransactionExecutionResult}; use zksync_types::{ api::{ - CallTracerConfig, CallTracerResult, CallTracerResultWithNestedResult, ResultDebugCall, - SupportedTracers, TracerConfig, + CallTracerBlockResult, CallTracerConfig, CallTracerResult, SupportedTracers, TracerConfig, }, BOOTLOADER_ADDRESS, }; @@ -66,12 +65,12 @@ impl HttpTest for TraceBlockTest { api::BlockId::Hash(hash) => client.trace_block_by_hash(hash, None).await?, }; + let CallTracerBlockResult::CallTrace(block_traces) = block_traces else { + unreachable!() + }; assert_eq!(block_traces.len(), tx_results.len()); // equals to the number of transactions in the block for (trace, tx_result) in block_traces.iter().zip(&tx_results) { - let CallTracerResultWithNestedResult::CallTrace(ResultDebugCall { result }) = trace - else { - unreachable!() - }; + let result = &trace.result; assert_eq!(result.from, Address::zero()); assert_eq!(result.to, BOOTLOADER_ADDRESS); assert_eq!(result.gas, tx_result.transaction.gas_limit()); @@ -143,6 +142,10 @@ impl HttpTest for TraceBlockFlatTest { ) .await?; + let CallTracerBlockResult::FlatCallTrace(block_traces) = &block_traces else { + unreachable!() + }; + // A transaction with 2 nested calls will convert into 3 Flattened calls. // Also in this test, all tx have the same # of nested calls assert_eq!( @@ -150,22 +153,12 @@ impl HttpTest for TraceBlockFlatTest { tx_results.len() * (tx_results[0].call_traces.len() + 1) ); - let CallTracerResultWithNestedResult::FlatCallTrace(block_traces_0) = - &block_traces[0] - else { - unreachable!() - }; - let CallTracerResultWithNestedResult::FlatCallTrace(block_traces_1) = - &block_traces[1] - else { - unreachable!() - }; // First tx has 2 nested calls, thus 2 sub-traces - assert_eq!(block_traces_0.subtraces, 2); - assert_eq!(block_traces_0.traceaddress, [0]); + assert_eq!(block_traces[0].subtraces, 2); + assert_eq!(block_traces[0].traceaddress, [0]); // Second flat-call (fist nested call) do not have nested calls - assert_eq!(block_traces_1.subtraces, 0); - assert_eq!(block_traces_1.traceaddress, [0, 0]); + assert_eq!(block_traces[1].subtraces, 0); + assert_eq!(block_traces[1].traceaddress, [0, 0]); let top_level_call_indexes = [0, 3, 6]; let top_level_traces = top_level_call_indexes @@ -173,11 +166,6 @@ impl HttpTest for TraceBlockFlatTest { .map(|&i| block_traces[i].clone()); for (top_level_trace, tx_result) in top_level_traces.zip(&tx_results) { - let CallTracerResultWithNestedResult::FlatCallTrace(top_level_trace) = - top_level_trace - else { - unreachable!() - }; assert_eq!(top_level_trace.action.from, Address::zero()); assert_eq!(top_level_trace.action.to, BOOTLOADER_ADDRESS); assert_eq!(