Skip to content

Commit

Permalink
Include block hash and block number
Browse files Browse the repository at this point in the history
Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo committed Oct 15, 2024
1 parent 4f5a883 commit 6b20afb
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 80 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

32 changes: 23 additions & 9 deletions core/lib/dal/src/blocks_web3_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -531,26 +532,33 @@ impl BlocksWeb3Dal<'_, '_> {
pub async fn get_traces_for_l2_block(
&mut self,
block_number: L2BlockNumber,
) -> DalResult<Vec<(Call, H256, usize)>> {
let protocol_version = sqlx::query!(
) -> DalResult<Vec<(Call, CallTraceMeta)>> {
let row = sqlx::query!(
r#"
SELECT
protocol_version
protocol_version,
hash
FROM
miniblocks
WHERE
number = $1
"#,
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);

Expand All @@ -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())
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
23 changes: 17 additions & 6 deletions core/lib/dal/src/transactions_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -2131,12 +2132,17 @@ impl TransactionsDal<'_, '_> {
Ok(data)
}

pub async fn get_call_trace(&mut self, tx_hash: H256) -> DalResult<Option<(Call, usize)>> {
pub async fn get_call_trace(
&mut self,
tx_hash: H256,
) -> DalResult<Option<(Call, CallTraceMeta)>> {
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
Expand Down Expand Up @@ -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),
},
)
}))
}
Expand Down
10 changes: 10 additions & 0 deletions core/lib/types/src/debug_flat_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub struct DebugCallFlat {
pub trace_address: Vec<usize>,
pub transaction_position: usize,
pub transaction_hash: H256,
pub block_number: u32,
pub block_hash: H256,
pub r#type: DebugCallType,
}

Expand All @@ -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,
}
58 changes: 24 additions & 34 deletions core/node/api_server/src/web3/namespaces/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -89,8 +87,7 @@ impl DebugNamespace {
calls: &mut Vec<DebugCallFlat>,
trace_address: &mut Vec<usize>,
only_top_call: bool,
transaction_position: usize,
transaction_hash: H256,
meta: &CallTraceMeta,
) {
let subtraces = call.calls.len();
let debug_type = match call.r#type {
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -173,27 +165,26 @@ 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(),
),
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,
}
})
Expand All @@ -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())
}))
}

Expand Down Expand Up @@ -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,
Expand All @@ -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))
}
}

0 comments on commit 6b20afb

Please sign in to comment.