Skip to content

Commit

Permalink
fix(api): Order transaction traces in debug_traceBlock* methods (#924)
Browse files Browse the repository at this point in the history
## What ❔

Orders transaction traces returned by the `debug_traceBlock*` methods by
transaction execution order in the miniblock.

## Why ❔

Copies Ethereum behavior. Right now, the ordering of traces is not
guaranteed, which can be unexpected by the caller.

## Checklist

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [x] Tests for the changes have been added / updated.
- [x] Documentation comments have been added / updated.
- [x] Code has been formatted via `zk fmt` and `zk lint`.
- [x] Spellcheck has been run via `zk spellcheck`.
  • Loading branch information
slowli committed Jan 25, 2024
1 parent a9553b5 commit 5918ef9
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 112 deletions.

This file was deleted.

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.

70 changes: 55 additions & 15 deletions core/lib/dal/src/blocks_web3_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,26 +411,23 @@ impl BlocksWeb3Dal<'_, '_> {
Ok(result)
}

pub async fn get_trace_for_miniblock(
/// Returns call traces for all transactions in the specified miniblock in the order of their execution.
pub async fn get_traces_for_miniblock(
&mut self,
block_number: MiniblockNumber,
) -> sqlx::Result<Vec<Call>> {
Ok(sqlx::query_as!(
CallTrace,
r#"
SELECT
*
call_trace
FROM
call_traces
INNER JOIN transactions ON tx_hash = transactions.hash
WHERE
tx_hash IN (
SELECT
hash
FROM
transactions
WHERE
miniblock_number = $1
)
transactions.miniblock_number = $1
ORDER BY
transactions.index_in_block
"#,
block_number.0 as i64
)
Expand Down Expand Up @@ -599,12 +596,16 @@ impl BlocksWeb3Dal<'_, '_> {
mod tests {
use zksync_types::{
block::{MiniblockHasher, MiniblockHeader},
fee::TransactionExecutionMetrics,
snapshots::SnapshotRecoveryStatus,
MiniblockNumber, ProtocolVersion, ProtocolVersionId,
};

use super::*;
use crate::{tests::create_miniblock_header, ConnectionPool};
use crate::{
tests::{create_miniblock_header, mock_execution_result, mock_l2_transaction},
ConnectionPool,
};

#[tokio::test]
async fn getting_web3_block_and_tx_count() {
Expand Down Expand Up @@ -786,10 +787,6 @@ mod tests {
async fn resolving_block_by_hash() {
let connection_pool = ConnectionPool::test_pool().await;
let mut conn = connection_pool.access_storage().await.unwrap();
conn.blocks_dal()
.delete_miniblocks(MiniblockNumber(0))
.await
.unwrap();
conn.protocol_versions_dal()
.save_protocol_version_with_tx(ProtocolVersion::default())
.await;
Expand All @@ -814,4 +811,47 @@ mod tests {
.await;
assert_eq!(miniblock_number.unwrap(), None);
}

#[tokio::test]
async fn getting_traces_for_block() {
let connection_pool = ConnectionPool::test_pool().await;
let mut conn = connection_pool.access_storage().await.unwrap();
conn.protocol_versions_dal()
.save_protocol_version_with_tx(ProtocolVersion::default())
.await;
conn.blocks_dal()
.insert_miniblock(&create_miniblock_header(1))
.await
.unwrap();

let transactions = [mock_l2_transaction(), mock_l2_transaction()];
let mut tx_results = vec![];
for (i, tx) in transactions.into_iter().enumerate() {
conn.transactions_dal()
.insert_transaction_l2(tx.clone(), TransactionExecutionMetrics::default())
.await;
let mut tx_result = mock_execution_result(tx);
tx_result.call_traces.push(Call {
from: Address::from_low_u64_be(i as u64),
to: Address::from_low_u64_be(i as u64 + 1),
value: i.into(),
..Call::default()
});
tx_results.push(tx_result);
}
conn.transactions_dal()
.mark_txs_as_executed_in_miniblock(MiniblockNumber(1), &tx_results, 1.into())
.await;

let traces = conn
.blocks_web3_dal()
.get_traces_for_miniblock(MiniblockNumber(1))
.await
.unwrap();
assert_eq!(traces.len(), 2);
for (trace, tx_result) in traces.iter().zip(&tx_results) {
let expected_trace = tx_result.call_trace().unwrap();
assert_eq!(*trace, expected_trace);
}
}
}
3 changes: 1 addition & 2 deletions core/lib/dal/src/models/storage_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,7 @@ pub fn extract_web3_transaction(db_row: PgRow, chain_id: L2ChainId) -> api::Tran
}

#[derive(Debug, Clone, sqlx::FromRow)]
pub struct CallTrace {
pub tx_hash: Vec<u8>,
pub(crate) struct CallTrace {
pub call_trace: Vec<u8>,
}

Expand Down
84 changes: 65 additions & 19 deletions core/lib/dal/src/transactions_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,25 +1316,22 @@ impl TransactionsDal<'_, '_> {
}
}

pub async fn get_call_trace(&mut self, tx_hash: H256) -> Option<Call> {
{
sqlx::query_as!(
CallTrace,
r#"
SELECT
*
FROM
call_traces
WHERE
tx_hash = $1
"#,
tx_hash.as_bytes()
)
.fetch_optional(self.storage.conn())
.await
.unwrap()
.map(|trace| trace.into())
}
pub async fn get_call_trace(&mut self, tx_hash: H256) -> sqlx::Result<Option<Call>> {
Ok(sqlx::query_as!(
CallTrace,
r#"
SELECT
call_trace
FROM
call_traces
WHERE
tx_hash = $1
"#,
tx_hash.as_bytes()
)
.fetch_optional(self.storage.conn())
.await?
.map(Into::into))
}

pub(crate) async fn get_tx_by_hash(&mut self, hash: H256) -> Option<Transaction> {
Expand All @@ -1356,3 +1353,52 @@ impl TransactionsDal<'_, '_> {
.map(|tx| tx.into())
}
}

#[cfg(test)]
mod tests {
use zksync_types::ProtocolVersion;

use super::*;
use crate::{
tests::{create_miniblock_header, mock_execution_result, mock_l2_transaction},
ConnectionPool,
};

#[tokio::test]
async fn getting_call_trace_for_transaction() {
let connection_pool = ConnectionPool::test_pool().await;
let mut conn = connection_pool.access_storage().await.unwrap();
conn.protocol_versions_dal()
.save_protocol_version_with_tx(ProtocolVersion::default())
.await;
conn.blocks_dal()
.insert_miniblock(&create_miniblock_header(1))
.await
.unwrap();

let tx = mock_l2_transaction();
let tx_hash = tx.hash();
conn.transactions_dal()
.insert_transaction_l2(tx.clone(), TransactionExecutionMetrics::default())
.await;
let mut tx_result = mock_execution_result(tx);
tx_result.call_traces.push(Call {
from: Address::from_low_u64_be(1),
to: Address::from_low_u64_be(2),
value: 100.into(),
..Call::default()
});
let expected_call_trace = tx_result.call_trace().unwrap();
conn.transactions_dal()
.mark_txs_as_executed_in_miniblock(MiniblockNumber(1), &[tx_result], 1.into())
.await;

let call_trace = conn
.transactions_dal()
.get_call_trace(tx_hash)
.await
.unwrap()
.expect("no call trace");
assert_eq!(call_trace, expected_call_trace);
}
}
8 changes: 6 additions & 2 deletions core/lib/zksync_core/src/api_server/web3/namespaces/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl DebugNamespace {
.await?;
let call_traces = connection
.blocks_web3_dal()
.get_trace_for_miniblock(block_number) // FIXME: is some ordering among transactions expected?
.get_traces_for_miniblock(block_number)
.await
.map_err(|err| internal_error(METHOD_NAME, err))?;
let call_trace = call_traces
Expand Down Expand Up @@ -109,7 +109,11 @@ impl DebugNamespace {
.access_storage_tagged("api")
.await
.map_err(|err| internal_error(METHOD_NAME, err))?;
let call_trace = connection.transactions_dal().get_call_trace(tx_hash).await;
let call_trace = connection
.transactions_dal()
.get_call_trace(tx_hash)
.await
.map_err(|err| internal_error(METHOD_NAME, err))?;
Ok(call_trace.map(|call_trace| {
let mut result: DebugCall = call_trace.into();
if only_top_call {
Expand Down

0 comments on commit 5918ef9

Please sign in to comment.