From e11c8f514b9e0ba24b107773f2381c4deea7f048 Mon Sep 17 00:00:00 2001 From: Danil Date: Tue, 15 Oct 2024 13:30:01 +0200 Subject: [PATCH] Include block hash and block number Signed-off-by: Danil --- ...9027b18d108a05f5855115ba36045e3b1850.json} | 12 +++- ...8bf345a8e043e87f137a3f309a378dbee8e5a.json | 40 +++++++++++++ ...c7d8bbdbee28d4ac2a2c0a331298c5e39b71d.json | 28 --------- core/lib/dal/src/blocks_web3_dal.rs | 32 +++++++--- core/lib/dal/src/transactions_dal.rs | 23 ++++++-- core/lib/types/src/debug_flat_call.rs | 10 ++++ .../api_server/src/web3/namespaces/debug.rs | 58 ++++++++----------- 7 files changed, 123 insertions(+), 80 deletions(-) rename core/lib/dal/.sqlx/{query-894665c2c467bd1aaeb331b112c567e2667c63a033baa6b427bd8a0898c08bf2.json => query-2076bee41f2db1534bb7e15043629027b18d108a05f5855115ba36045e3b1850.json} (50%) create mode 100644 core/lib/dal/.sqlx/query-5e7c7261b9a0a76b1530af0caa08bf345a8e043e87f137a3f309a378dbee8e5a.json delete mode 100644 core/lib/dal/.sqlx/query-96adbd0c9a5786a6cca74324353c7d8bbdbee28d4ac2a2c0a331298c5e39b71d.json diff --git a/core/lib/dal/.sqlx/query-894665c2c467bd1aaeb331b112c567e2667c63a033baa6b427bd8a0898c08bf2.json b/core/lib/dal/.sqlx/query-2076bee41f2db1534bb7e15043629027b18d108a05f5855115ba36045e3b1850.json similarity index 50% rename from core/lib/dal/.sqlx/query-894665c2c467bd1aaeb331b112c567e2667c63a033baa6b427bd8a0898c08bf2.json rename to core/lib/dal/.sqlx/query-2076bee41f2db1534bb7e15043629027b18d108a05f5855115ba36045e3b1850.json index 06d3461c3fa3..4f44879b6ec3 100644 --- a/core/lib/dal/.sqlx/query-894665c2c467bd1aaeb331b112c567e2667c63a033baa6b427bd8a0898c08bf2.json +++ b/core/lib/dal/.sqlx/query-2076bee41f2db1534bb7e15043629027b18d108a05f5855115ba36045e3b1850.json @@ -1,12 +1,17 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT\n protocol_version\n FROM\n miniblocks\n WHERE\n number = $1\n ", + "query": "\n SELECT\n protocol_version,\n hash\n FROM\n miniblocks\n WHERE\n number = $1\n ", "describe": { "columns": [ { "ordinal": 0, "name": "protocol_version", "type_info": "Int4" + }, + { + "ordinal": 1, + "name": "hash", + "type_info": "Bytea" } ], "parameters": { @@ -15,8 +20,9 @@ ] }, "nullable": [ - true + true, + false ] }, - "hash": "894665c2c467bd1aaeb331b112c567e2667c63a033baa6b427bd8a0898c08bf2" + "hash": "2076bee41f2db1534bb7e15043629027b18d108a05f5855115ba36045e3b1850" } diff --git a/core/lib/dal/.sqlx/query-5e7c7261b9a0a76b1530af0caa08bf345a8e043e87f137a3f309a378dbee8e5a.json b/core/lib/dal/.sqlx/query-5e7c7261b9a0a76b1530af0caa08bf345a8e043e87f137a3f309a378dbee8e5a.json new file mode 100644 index 000000000000..71801daafd6b --- /dev/null +++ b/core/lib/dal/.sqlx/query-5e7c7261b9a0a76b1530af0caa08bf345a8e043e87f137a3f309a378dbee8e5a.json @@ -0,0 +1,40 @@ +{ + "db_name": "PostgreSQL", + "query": "\n SELECT\n protocol_version,\n index_in_block,\n miniblocks.number as \"miniblock_number!\",\n miniblocks.hash as \"miniblocks_hash!\"\n FROM\n transactions\n INNER JOIN miniblocks ON transactions.miniblock_number = miniblocks.number\n WHERE\n transactions.hash = $1\n ", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "protocol_version", + "type_info": "Int4" + }, + { + "ordinal": 1, + "name": "index_in_block", + "type_info": "Int4" + }, + { + "ordinal": 2, + "name": "miniblock_number!", + "type_info": "Int8" + }, + { + "ordinal": 3, + "name": "miniblocks_hash!", + "type_info": "Bytea" + } + ], + "parameters": { + "Left": [ + "Bytea" + ] + }, + "nullable": [ + true, + true, + false, + false + ] + }, + "hash": "5e7c7261b9a0a76b1530af0caa08bf345a8e043e87f137a3f309a378dbee8e5a" +} diff --git a/core/lib/dal/.sqlx/query-96adbd0c9a5786a6cca74324353c7d8bbdbee28d4ac2a2c0a331298c5e39b71d.json b/core/lib/dal/.sqlx/query-96adbd0c9a5786a6cca74324353c7d8bbdbee28d4ac2a2c0a331298c5e39b71d.json deleted file mode 100644 index 3b8accb4fda2..000000000000 --- a/core/lib/dal/.sqlx/query-96adbd0c9a5786a6cca74324353c7d8bbdbee28d4ac2a2c0a331298c5e39b71d.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n SELECT\n protocol_version,\n index_in_block\n FROM\n transactions\n INNER JOIN miniblocks ON transactions.miniblock_number = miniblocks.number\n WHERE\n transactions.hash = $1\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "protocol_version", - "type_info": "Int4" - }, - { - "ordinal": 1, - "name": "index_in_block", - "type_info": "Int4" - } - ], - "parameters": { - "Left": [ - "Bytea" - ] - }, - "nullable": [ - true, - true - ] - }, - "hash": "96adbd0c9a5786a6cca74324353c7d8bbdbee28d4ac2a2c0a331298c5e39b71d" -} diff --git a/core/lib/dal/src/blocks_web3_dal.rs b/core/lib/dal/src/blocks_web3_dal.rs index 829e15b5710a..4cb577986380 100644 --- a/core/lib/dal/src/blocks_web3_dal.rs +++ b/core/lib/dal/src/blocks_web3_dal.rs @@ -5,6 +5,7 @@ use zksync_db_connection::{ use zksync_system_constants::EMPTY_UNCLES_HASH; use zksync_types::{ api, + debug_flat_call::CallTraceMeta, fee_model::BatchFeeInput, l2_to_l1_log::L2ToL1Log, web3::{BlockHeader, Bytes}, @@ -531,11 +532,12 @@ impl BlocksWeb3Dal<'_, '_> { pub async fn get_traces_for_l2_block( &mut self, block_number: L2BlockNumber, - ) -> DalResult> { - let protocol_version = sqlx::query!( + ) -> DalResult> { + let row = sqlx::query!( r#" SELECT - protocol_version + protocol_version, + hash FROM miniblocks WHERE @@ -543,14 +545,20 @@ impl BlocksWeb3Dal<'_, '_> { "#, i64::from(block_number.0) ) - .try_map(|row| row.protocol_version.map(parse_protocol_version).transpose()) + .try_map(|row| { + row.protocol_version + .map(parse_protocol_version) + .transpose() + .map(|val| (val, H256::from_slice(&row.hash))) + }) .instrument("get_traces_for_l2_block#get_l2_block_protocol_version_id") .with_arg("l2_block_number", &block_number) .fetch_optional(self.storage) .await?; - let Some(protocol_version) = protocol_version else { + let Some((protocol_version, block_hash)) = row else { return Ok(Vec::new()); }; + let protocol_version = protocol_version.unwrap_or_else(ProtocolVersionId::last_potentially_undefined); @@ -577,9 +585,15 @@ impl BlocksWeb3Dal<'_, '_> { .await? .into_iter() .map(|call_trace| { - let hash = H256::from_slice(&call_trace.tx_hash); + let tx_hash = H256::from_slice(&call_trace.tx_hash); let index = call_trace.tx_index_in_block.unwrap_or_default() as usize; - (call_trace.into_call(protocol_version), hash, index) + let meta = CallTraceMeta { + index_in_block: index, + tx_hash, + block_number: block_number.0, + block_hash, + }; + (call_trace.into_call(protocol_version), meta) }) .collect()) } @@ -1105,9 +1119,9 @@ mod tests { .await .unwrap(); assert_eq!(traces.len(), 2); - for ((trace, hash, _index), tx_result) in traces.iter().zip(&tx_results) { + for ((trace, meta), tx_result) in traces.iter().zip(&tx_results) { let expected_trace = tx_result.call_trace().unwrap(); - assert_eq!(&tx_result.hash, hash); + assert_eq!(tx_result.hash, meta.tx_hash); assert_eq!(*trace, expected_trace); } } diff --git a/core/lib/dal/src/transactions_dal.rs b/core/lib/dal/src/transactions_dal.rs index 67c965312bd4..5314e9799b33 100644 --- a/core/lib/dal/src/transactions_dal.rs +++ b/core/lib/dal/src/transactions_dal.rs @@ -10,9 +10,10 @@ use zksync_db_connection::{ utils::pg_interval_from_duration, }; use zksync_types::{ - block::L2BlockExecutionData, l1::L1Tx, l2::L2Tx, protocol_upgrade::ProtocolUpgradeTx, Address, - ExecuteTransactionCommon, L1BatchNumber, L1BlockNumber, L2BlockNumber, PriorityOpId, - ProtocolVersionId, Transaction, H256, PROTOCOL_UPGRADE_TX_TYPE, U256, + block::L2BlockExecutionData, debug_flat_call::CallTraceMeta, l1::L1Tx, l2::L2Tx, + protocol_upgrade::ProtocolUpgradeTx, Address, ExecuteTransactionCommon, L1BatchNumber, + L1BlockNumber, L2BlockNumber, PriorityOpId, ProtocolVersionId, Transaction, H256, + PROTOCOL_UPGRADE_TX_TYPE, U256, }; use zksync_utils::u256_to_big_decimal; use zksync_vm_interface::{ @@ -2131,12 +2132,17 @@ impl TransactionsDal<'_, '_> { Ok(data) } - pub async fn get_call_trace(&mut self, tx_hash: H256) -> DalResult> { + pub async fn get_call_trace( + &mut self, + tx_hash: H256, + ) -> DalResult> { let row = sqlx::query!( r#" SELECT protocol_version, - index_in_block + index_in_block, + miniblocks.number AS "miniblock_number!", + miniblocks.hash AS "miniblocks_hash!" FROM transactions INNER JOIN miniblocks ON transactions.miniblock_number = miniblocks.number @@ -2177,7 +2183,12 @@ impl TransactionsDal<'_, '_> { .map(|call_trace| { ( parse_call_trace(&call_trace.call_trace, protocol_version), - row.index_in_block.unwrap_or_default() as usize, + CallTraceMeta { + index_in_block: row.index_in_block.unwrap_or_default() as usize, + tx_hash, + block_number: row.miniblock_number as u32, + block_hash: H256::from_slice(&row.miniblocks_hash), + }, ) })) } diff --git a/core/lib/types/src/debug_flat_call.rs b/core/lib/types/src/debug_flat_call.rs index 95b7ab383d17..5809026e521c 100644 --- a/core/lib/types/src/debug_flat_call.rs +++ b/core/lib/types/src/debug_flat_call.rs @@ -19,6 +19,8 @@ pub struct DebugCallFlat { pub trace_address: Vec, pub transaction_position: usize, pub transaction_hash: H256, + pub block_number: u32, + pub block_hash: H256, pub r#type: DebugCallType, } @@ -39,3 +41,11 @@ pub struct CallResult { pub output: Bytes, pub gas_used: U256, } + +#[derive(Debug, Clone, PartialEq, Default)] +pub struct CallTraceMeta { + pub index_in_block: usize, + pub tx_hash: H256, + pub block_number: u32, + pub block_hash: H256, +} diff --git a/core/node/api_server/src/web3/namespaces/debug.rs b/core/node/api_server/src/web3/namespaces/debug.rs index 940b6ae87ca6..a6af7016c903 100644 --- a/core/node/api_server/src/web3/namespaces/debug.rs +++ b/core/node/api_server/src/web3/namespaces/debug.rs @@ -7,7 +7,7 @@ use zksync_types::{ BlockId, BlockNumber, CallTracerBlockResult, CallTracerResult, DebugCall, DebugCallType, ResultDebugCall, SupportedTracers, TracerConfig, }, - debug_flat_call::{Action, CallResult, DebugCallFlat, ResultDebugCallFlat}, + debug_flat_call::{Action, CallResult, CallTraceMeta, DebugCallFlat, ResultDebugCallFlat}, l2::L2Tx, transaction_request::CallRequest, web3, H256, U256, @@ -31,8 +31,7 @@ impl DebugNamespace { pub(crate) fn map_call( call: Call, - index: usize, - transaction_hash: H256, + meta: CallTraceMeta, tracer_option: TracerConfig, ) -> CallTracerResult { match tracer_option.tracer { @@ -42,14 +41,13 @@ impl DebugNamespace { )), SupportedTracers::FlatCallTracer => { let mut calls = vec![]; - let mut traces = vec![index]; + let mut traces = vec![meta.index_in_block]; Self::flatten_call( call, &mut calls, &mut traces, tracer_option.tracer_config.only_top_call, - index, - transaction_hash, + &meta, ); CallTracerResult::FlatCallTrace(calls) } @@ -89,8 +87,7 @@ impl DebugNamespace { calls: &mut Vec, trace_address: &mut Vec, only_top_call: bool, - transaction_position: usize, - transaction_hash: H256, + meta: &CallTraceMeta, ) { let subtraces = call.calls.len(); let debug_type = match call.r#type { @@ -120,22 +117,17 @@ impl DebugNamespace { result, subtraces, trace_address: trace_address.clone(), // Clone the current trace address - transaction_position, - transaction_hash, + transaction_position: meta.index_in_block, + transaction_hash: meta.tx_hash, + block_number: meta.block_number, + block_hash: meta.block_hash, r#type: DebugCallType::Call, }); if !only_top_call { for (number, call) in call.calls.into_iter().enumerate() { trace_address.push(number); - Self::flatten_call( - call, - calls, - trace_address, - false, - transaction_position, - transaction_hash, - ); + Self::flatten_call(call, calls, trace_address, false, meta); trace_address.pop(); } } @@ -173,7 +165,7 @@ impl DebugNamespace { SupportedTracers::CallTracer => CallTracerBlockResult::CallTrace( call_traces .into_iter() - .map(|(call, _, _)| ResultDebugCall { + .map(|(call, _)| ResultDebugCall { result: Self::map_default_call(call, options.tracer_config.only_top_call), }) .collect(), @@ -181,19 +173,18 @@ impl DebugNamespace { SupportedTracers::FlatCallTracer => { let res = call_traces .into_iter() - .map(|(call, tx_hash, tx_index)| { - let mut traces = vec![tx_index]; + .map(|(call, meta)| { + let mut traces = vec![meta.index_in_block]; let mut flat_calls = vec![]; Self::flatten_call( call, &mut flat_calls, &mut traces, options.tracer_config.only_top_call, - tx_index, - tx_hash, + &meta, ); ResultDebugCallFlat { - tx_hash, + tx_hash: meta.tx_hash, result: flat_calls, } }) @@ -215,13 +206,8 @@ impl DebugNamespace { .get_call_trace(tx_hash) .await .map_err(DalError::generalize)?; - Ok(call_trace.map(|(call_trace, index_in_block)| { - Self::map_call( - call_trace, - index_in_block, - tx_hash, - options.unwrap_or_default(), - ) + Ok(call_trace.map(|(call_trace, meta)| { + Self::map_call(call_trace, meta, options.unwrap_or_default()) })) } @@ -313,8 +299,6 @@ impl DebugNamespace { )) } }; - // It's a call request, it's safe to keep it zero - let hash = H256::zero(); let call = Call::new_high_level( call.common_data.fee.gas_limit.as_u64(), result.vm.statistics.gas_used, @@ -324,6 +308,12 @@ impl DebugNamespace { revert_reason, result.call_traces, ); - Ok(Self::map_call(call, 0, hash, options)) + let number = block_args.resolved_block_number(); + let meta = CallTraceMeta { + block_number: number.0, + // It's a call request, it's safe to everything as default + ..Default::default() + }; + Ok(Self::map_call(call, meta, options)) } }