Skip to content

Commit

Permalink
Small refactores
Browse files Browse the repository at this point in the history
Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo committed Sep 24, 2024
1 parent 0afecd0 commit 0f44ebd
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 106 deletions.

This file was deleted.

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.

8 changes: 4 additions & 4 deletions core/lib/dal/src/blocks_web3_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,8 @@ impl BlocksWeb3Dal<'_, '_> {
CallTrace,
r#"
SELECT
transactions.hash,
transactions.index_in_block,
transactions.hash as tx_hash,
transactions.index_in_block as tx_index_in_block,
call_trace
FROM
call_traces
Expand All @@ -573,8 +573,8 @@ impl BlocksWeb3Dal<'_, '_> {
.await?
.into_iter()
.map(|call_trace| {
let hash = H256::from_slice(&call_trace.hash);
let index = call_trace.index_in_block.unwrap_or_default() as usize;
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
27 changes: 15 additions & 12 deletions core/lib/dal/src/models/storage_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,24 +561,27 @@ 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>,
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()
}
parse_call_trace(&self.call_trace, protocol_version)
}
}

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::deserialize(&self.call_trace).unwrap()
let legacy_mixed_call_trace = bincode::deserialize::<LegacyMixedCall>(call_trace)
.expect("Failed to deserialize call trace");
legacy_mixed_call_trace.into()
}
} else {
bincode::deserialize(call_trace).unwrap()
}
}

Expand Down
21 changes: 6 additions & 15 deletions core/lib/dal/src/transactions_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use zksync_vm_interface::{
Call, TransactionExecutionMetrics, TransactionExecutionResult, TxExecutionStatus,
};

use crate::models::storage_transaction::parse_call_trace;
use crate::{
models::storage_transaction::{serialize_call_into_bytes, CallTrace, StorageTransaction},
Core, CoreDal,
Expand Down Expand Up @@ -2111,10 +2112,7 @@ impl TransactionsDal<'_, '_> {
Ok(data)
}

pub async fn get_call_trace(
&mut self,
tx_hash: H256,
) -> DalResult<Option<(Call, H256, usize)>> {
pub async fn get_call_trace(&mut self, tx_hash: H256) -> DalResult<Option<Call>> {
let row = sqlx::query!(
r#"
SELECT
Expand All @@ -2141,13 +2139,10 @@ 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,
tx_hash AS hash,
0 AS index_in_block
call_trace
FROM
call_traces
WHERE
Expand All @@ -2159,11 +2154,7 @@ impl TransactionsDal<'_, '_> {
.with_arg("tx_hash", &tx_hash)
.fetch_optional(self.storage)
.await?
.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)
}))
.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 Expand Up @@ -2235,7 +2226,7 @@ mod tests {
.await
.unwrap();

let (call_trace, _, _) = conn
let call_trace = conn
.transactions_dal()
.get_call_trace(tx_hash)
.await
Expand Down
2 changes: 1 addition & 1 deletion core/lib/types/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ pub enum BlockStatus {
#[serde(untagged)]
pub enum CallTracerResultWithNestedResult {
CallTrace(ResultDebugCall),
FlattCallTrace(Box<DebugCallFlat>),
FlatCallTrace(Box<DebugCallFlat>),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down
5 changes: 2 additions & 3 deletions core/node/api_server/src/web3/namespaces/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl DebugNamespace {
&mut call
.into_iter()
.map(Box::new)
.map(CallTracerResultWithNestedResult::FlattCallTrace)
.map(CallTracerResultWithNestedResult::FlatCallTrace)
.collect(),
),
}
Expand All @@ -220,8 +220,7 @@ impl DebugNamespace {
.get_call_trace(tx_hash)
.await
.map_err(DalError::generalize)?;
Ok(call_trace
.map(|(call_trace, hash, index)| Self::map_call(call_trace, index, hash, options)))
Ok(call_trace.map(|call_trace| Self::map_call(call_trace, 0, tx_hash, options)))
}

pub async fn debug_trace_call_impl(
Expand Down
6 changes: 3 additions & 3 deletions core/node/api_server/src/web3/tests/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ impl HttpTest for TraceBlockFlatTest {
tx_results.len() * (tx_results[0].call_traces.len() + 1)
);

let CallTracerResultWithNestedResult::FlattCallTrace(block_traces_0) =
let CallTracerResultWithNestedResult::FlatCallTrace(block_traces_0) =
&block_traces[0]
else {
unreachable!()
};
let CallTracerResultWithNestedResult::FlattCallTrace(block_traces_1) =
let CallTracerResultWithNestedResult::FlatCallTrace(block_traces_1) =
&block_traces[1]
else {
unreachable!()
Expand All @@ -173,7 +173,7 @@ impl HttpTest for TraceBlockFlatTest {
.map(|&i| block_traces[i].clone());

for (top_level_trace, tx_result) in top_level_traces.zip(&tx_results) {
let CallTracerResultWithNestedResult::FlattCallTrace(top_level_trace) =
let CallTracerResultWithNestedResult::FlatCallTrace(top_level_trace) =
top_level_trace
else {
unreachable!()
Expand Down

0 comments on commit 0f44ebd

Please sign in to comment.