Skip to content

Commit

Permalink
fix(api): Return correct flat call tracer (#2917)
Browse files Browse the repository at this point in the history
## What ❔

It was an inconsistency between call tracer results

## Why ❔

<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [x] Code has been formatted via `zk fmt` and `zk lint`.

---------

Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo authored Sep 25, 2024
1 parent 1fdbb30 commit 218646a
Show file tree
Hide file tree
Showing 38 changed files with 576 additions and 559 deletions.

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 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()
}
24 changes: 15 additions & 9 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 @@ -2112,11 +2113,12 @@ 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, usize)>> {
let row = sqlx::query!(
r#"
SELECT
protocol_version
protocol_version,
index_in_block
FROM
transactions
INNER JOIN miniblocks ON transactions.miniblock_number = miniblocks.number
Expand All @@ -2139,8 +2141,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 +2156,12 @@ 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),
row.index_in_block.unwrap_or_default() as usize,
)
}))
}

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

let call_trace = conn
let (call_trace, _) = conn
.transactions_dal()
.get_call_trace(tx_hash)
.await
Expand Down
Loading

0 comments on commit 218646a

Please sign in to comment.