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.

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,
transactions.index_in_block,
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
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;
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
(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
21 changes: 12 additions & 9 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 @@ -578,15 +580,16 @@ impl CallTrace {
bincode::deserialize(&self.call_trace).unwrap()
}
}
}

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())
} else {
bincode::serialize(&call)
}
.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()
}
22 changes: 15 additions & 7 deletions core/lib/dal/src/transactions_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use zksync_vm_interface::{
};

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

Expand Down Expand Up @@ -521,8 +521,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 @@ -2112,7 +2111,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)>> {
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
let row = sqlx::query!(
r#"
SELECT
Expand Down Expand Up @@ -2143,7 +2145,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 @@ -2155,7 +2159,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 @@ -2227,7 +2235,7 @@ mod tests {
.await
.unwrap();

let call_trace = conn
let (call_trace, _, _) = conn
.transactions_dal()
.get_call_trace(tx_hash)
.await
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 CallTracerResultWithNestedResult {
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
CallTrace(ResultDebugCall),
FlattCallTrace(Box<DebugCallFlat>),
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
}

#[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