Skip to content

Commit

Permalink
fix(api): Return correct flat call tracer
Browse files Browse the repository at this point in the history
Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo committed Sep 18, 2024
1 parent ba21c6e commit ccd265d
Show file tree
Hide file tree
Showing 15 changed files with 326 additions and 371 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.

This file was deleted.

13 changes: 10 additions & 3 deletions core/lib/dal/src/blocks_web3_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ impl BlocksWeb3Dal<'_, '_> {
pub async fn get_traces_for_l2_block(
&mut self,
block_number: L2BlockNumber,
) -> DalResult<Vec<Call>> {
) -> DalResult<Vec<(Call, H256, usize)>> {
let protocol_version = sqlx::query!(
r#"
SELECT
Expand All @@ -554,6 +554,8 @@ impl BlocksWeb3Dal<'_, '_> {
CallTrace,
r#"
SELECT
transactions.hash,
transactions.index_in_block,
call_trace
FROM
call_traces
Expand All @@ -570,7 +572,11 @@ impl BlocksWeb3Dal<'_, '_> {
.fetch_all(self.storage)
.await?
.into_iter()
.map(|call_trace| call_trace.into_call(protocol_version))
.map(|call_trace| {
let hash = H256::from_slice(&call_trace.hash);
let index = call_trace.index_in_block.unwrap_or_default() as usize;
(call_trace.into_call(protocol_version), hash, index)
})
.collect())
}

Expand Down Expand Up @@ -1084,8 +1090,9 @@ mod tests {
.await
.unwrap();
assert_eq!(traces.len(), 2);
for (trace, tx_result) in traces.iter().zip(&tx_results) {
for ((trace, hash, _index), tx_result) in traces.iter().zip(&tx_results) {
let expected_trace = tx_result.call_trace().unwrap();
assert_eq!(&tx_result.hash, hash);
assert_eq!(*trace, expected_trace);
}
}
Expand Down
10 changes: 5 additions & 5 deletions core/lib/dal/src/models/storage_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ impl StorageApiTransaction {
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct CallTrace {
pub call_trace: Vec<u8>,
pub hash: Vec<u8>,
pub index_in_block: Option<i32>,
}

impl CallTrace {
Expand All @@ -579,14 +581,12 @@ impl CallTrace {
}
}

pub(crate) fn from_call(call: Call, protocol_version: ProtocolVersionId) -> Self {
let call_trace = if protocol_version.is_pre_1_5_0() {
pub(crate) fn into_bytes(call: Call, protocol_version: ProtocolVersionId) -> Vec<u8> {
if protocol_version.is_pre_1_5_0() {
bincode::serialize(&LegacyCall::try_from(call).unwrap())
} else {
bincode::serialize(&call)
}
.unwrap();

Self { call_trace }
.unwrap()
}
}
20 changes: 14 additions & 6 deletions core/lib/dal/src/transactions_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,7 @@ impl TransactionsDal<'_, '_> {
let mut bytea_call_traces = Vec::with_capacity(transactions.len());
for tx_res in transactions {
if let Some(call_trace) = tx_res.call_trace() {
bytea_call_traces
.push(CallTrace::from_call(call_trace, protocol_version).call_trace);
bytea_call_traces.push(CallTrace::into_bytes(call_trace, protocol_version));
call_traces_tx_hashes.push(tx_res.hash.as_bytes());
}
}
Expand Down Expand Up @@ -2101,7 +2100,10 @@ impl TransactionsDal<'_, '_> {
Ok(data)
}

pub async fn get_call_trace(&mut self, tx_hash: H256) -> DalResult<Option<Call>> {
pub async fn get_call_trace(
&mut self,
tx_hash: H256,
) -> DalResult<Option<(Call, H256, usize)>> {
let row = sqlx::query!(
r#"
SELECT
Expand Down Expand Up @@ -2132,7 +2134,9 @@ impl TransactionsDal<'_, '_> {
CallTrace,
r#"
SELECT
call_trace
call_trace,
tx_hash AS hash,
0 AS index_in_block
FROM
call_traces
WHERE
Expand All @@ -2144,7 +2148,11 @@ impl TransactionsDal<'_, '_> {
.with_arg("tx_hash", &tx_hash)
.fetch_optional(self.storage)
.await?
.map(|call_trace| call_trace.into_call(protocol_version)))
.map(|call_trace| {
let hash = H256::from_slice(&call_trace.hash);
let index = call_trace.index_in_block.unwrap_or_default() as usize;
(call_trace.into_call(protocol_version), hash, index)
}))
}

pub(crate) async fn get_tx_by_hash(&mut self, hash: H256) -> DalResult<Option<Transaction>> {
Expand Down Expand Up @@ -2216,7 +2224,7 @@ mod tests {
.await
.unwrap();

let call_trace = conn
let (call_trace, _, _) = conn
.transactions_dal()
.get_call_trace(tx_hash)
.await
Expand Down
30 changes: 25 additions & 5 deletions core/lib/types/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use zksync_contracts::BaseSystemContractsHashes;
pub use crate::transaction_request::{
Eip712Meta, SerializationTransactionError, TransactionRequest,
};
use crate::{protocol_version::L1VerifierConfig, Address, L2BlockNumber, ProtocolVersionId};
use crate::{
debug_flat_call::DebugCallFlat, protocol_version::L1VerifierConfig, Address, L2BlockNumber,
ProtocolVersionId,
};

pub mod en;
pub mod state_override;
Expand Down Expand Up @@ -597,7 +600,7 @@ pub struct GetLogsFilter {
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ResultDebugCall {
pub result: DebugCall,
pub result: CallTracerResult,
}

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
Expand Down Expand Up @@ -700,19 +703,20 @@ impl ProtocolVersion {
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
#[serde(rename_all = "camelCase")]
pub enum SupportedTracers {
CallTracer,
FlatCallTracer,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[derive(Debug, Serialize, Deserialize, Clone, Default, Copy)]
#[serde(rename_all = "camelCase")]
pub struct CallTracerConfig {
pub only_top_call: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
#[serde(rename_all = "camelCase")]
pub struct TracerConfig {
pub tracer: SupportedTracers,
Expand All @@ -727,6 +731,22 @@ pub enum BlockStatus {
Verified,
}

/// FlatTracer is always more than one trace, so when we have to trace one transaction it also appeared as many traces
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase", untagged)]
pub enum CallTracerResult {
CallTrace(DebugCall),
FlattCallTrace(Box<Vec<DebugCallFlat>>),
}

/// For tracing blocks we need to have all traces being combined all together without separation.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase", untagged)]
pub enum CallTracerOption {
CallTrace(DebugCall),
FlattCallTrace(Box<DebugCallFlat>),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BlockDetailsBase {
Expand Down
Loading

0 comments on commit ccd265d

Please sign in to comment.