Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): Return correct flat call tracer #2917

Merged
merged 15 commits into from
Sep 25, 2024

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

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 AS tx_hash,
transactions.index_in_block AS tx_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.tx_hash);
let index = call_trace.tx_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
42 changes: 24 additions & 18 deletions core/lib/dal/src/models/storage_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,32 +561,38 @@ impl StorageApiTransaction {
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct CallTrace {
pub call_trace: Vec<u8>,
pub tx_hash: Vec<u8>,
pub tx_index_in_block: Option<i32>,
}

impl CallTrace {
pub(crate) fn into_call(self, protocol_version: ProtocolVersionId) -> Call {
if protocol_version.is_pre_1_5_0() {
if let Ok(legacy_call_trace) = bincode::deserialize::<LegacyCall>(&self.call_trace) {
legacy_call_trace.into()
} else {
let legacy_mixed_call_trace =
bincode::deserialize::<LegacyMixedCall>(&self.call_trace)
.expect("Failed to deserialize call trace");
legacy_mixed_call_trace.into()
}
} else {
bincode::deserialize(&self.call_trace).unwrap()
}
parse_call_trace(&self.call_trace, protocol_version)
}
}

pub(crate) fn from_call(call: Call, protocol_version: ProtocolVersionId) -> Self {
let call_trace = if protocol_version.is_pre_1_5_0() {
bincode::serialize(&LegacyCall::try_from(call).unwrap())
pub(crate) fn parse_call_trace(call_trace: &[u8], protocol_version: ProtocolVersionId) -> Call {
if protocol_version.is_pre_1_5_0() {
if let Ok(legacy_call_trace) = bincode::deserialize::<LegacyCall>(call_trace) {
legacy_call_trace.into()
} else {
bincode::serialize(&call)
let legacy_mixed_call_trace = bincode::deserialize::<LegacyMixedCall>(call_trace)
.expect("Failed to deserialize call trace");
legacy_mixed_call_trace.into()
}
.unwrap();
} else {
bincode::deserialize(call_trace).unwrap()
}
}

Self { call_trace }
pub(crate) fn serialize_call_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()
}
12 changes: 6 additions & 6 deletions core/lib/dal/src/transactions_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use zksync_vm_interface::{
};

use crate::{
models::storage_transaction::{CallTrace, StorageTransaction},
models::storage_transaction::{
parse_call_trace, serialize_call_into_bytes, StorageTransaction,
},
Core, CoreDal,
};

Expand Down Expand Up @@ -521,8 +523,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(serialize_call_into_bytes(call_trace, protocol_version));
call_traces_tx_hashes.push(tx_res.hash.as_bytes());
}
}
Expand Down Expand Up @@ -2139,8 +2140,7 @@ impl TransactionsDal<'_, '_> {
.map(|v| (v as u16).try_into().unwrap())
.unwrap_or_else(ProtocolVersionId::last_potentially_undefined);

Ok(sqlx::query_as!(
CallTrace,
Ok(sqlx::query!(
r#"
SELECT
call_trace
Expand All @@ -2155,7 +2155,7 @@ 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| parse_call_trace(&call_trace.call_trace, protocol_version)))
}

pub(crate) async fn get_tx_by_hash(&mut self, hash: H256) -> DalResult<Option<Transaction>> {
Expand Down
29 changes: 25 additions & 4 deletions core/lib/types/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,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 @@ -602,6 +605,7 @@ pub struct ResultDebugCall {
}

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum DebugCallType {
#[default]
Call,
Expand Down Expand Up @@ -701,19 +705,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 @@ -728,6 +733,22 @@ pub enum BlockStatus {
Verified,
}

/// Result tracers need to have a nested result field for compatibility. So we have two different
/// structs 1 for blocks tracing and one for txs and call tracing
joonazan marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum CallTracerBlockResult {
CallTrace(Vec<ResultDebugCall>),
FlatCallTrace(Vec<DebugCallFlat>),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum CallTracerResult {
CallTrace(DebugCall),
FlattCallTrace(Vec<DebugCallFlat>),
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
}

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