Skip to content

Commit

Permalink
Return another version of result
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 0f44ebd commit 8fa81cc
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 70 deletions.

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

4 changes: 2 additions & 2 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 as tx_hash,
transactions.index_in_block as tx_index_in_block,
transactions.hash AS tx_hash,
transactions.index_in_block AS tx_index_in_block,
call_trace
FROM
call_traces
Expand Down
5 changes: 3 additions & 2 deletions core/lib/dal/src/transactions_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ 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},
models::storage_transaction::{
parse_call_trace, serialize_call_into_bytes, StorageTransaction,
},
Core, CoreDal,
};

Expand Down
6 changes: 3 additions & 3 deletions core/lib/types/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,9 @@ pub enum BlockStatus {
/// structs 1 for blocks tracing and one for txs and call tracing
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum CallTracerResultWithNestedResult {
CallTrace(ResultDebugCall),
FlatCallTrace(Box<DebugCallFlat>),
pub enum CallTracerBlockResult {
CallTrace(Vec<ResultDebugCall>),
FlatCallTrace(Vec<DebugCallFlat>),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down
6 changes: 3 additions & 3 deletions core/lib/web3_decl/src/namespaces/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use jsonrpsee::core::RpcResult;
use jsonrpsee::proc_macros::rpc;
use zksync_types::{
api::{BlockId, BlockNumber, CallTracerResult, CallTracerResultWithNestedResult, TracerConfig},
api::{BlockId, BlockNumber, CallTracerBlockResult, CallTracerResult, TracerConfig},
transaction_request::CallRequest,
};

Expand All @@ -25,14 +25,14 @@ pub trait DebugNamespace {
&self,
block: BlockNumber,
options: Option<TracerConfig>,
) -> RpcResult<Vec<CallTracerResultWithNestedResult>>;
) -> RpcResult<CallTracerBlockResult>;

#[method(name = "traceBlockByHash")]
async fn trace_block_by_hash(
&self,
hash: H256,
options: Option<TracerConfig>,
) -> RpcResult<Vec<CallTracerResultWithNestedResult>>;
) -> RpcResult<CallTracerBlockResult>;

#[method(name = "traceCall")]
async fn trace_call(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use zksync_types::{
api::{BlockId, BlockNumber, CallTracerResult, CallTracerResultWithNestedResult, TracerConfig},
api::{BlockId, BlockNumber, CallTracerBlockResult, CallTracerResult, TracerConfig},
transaction_request::CallRequest,
H256,
};
Expand All @@ -16,7 +16,7 @@ impl DebugNamespaceServer for DebugNamespace {
&self,
block: BlockNumber,
options: Option<TracerConfig>,
) -> RpcResult<Vec<CallTracerResultWithNestedResult>> {
) -> RpcResult<CallTracerBlockResult> {
self.debug_trace_block_impl(BlockId::Number(block), options)
.await
.map_err(|err| self.current_method().map_err(err))
Expand All @@ -26,7 +26,7 @@ impl DebugNamespaceServer for DebugNamespace {
&self,
hash: H256,
options: Option<TracerConfig>,
) -> RpcResult<Vec<CallTracerResultWithNestedResult>> {
) -> RpcResult<CallTracerBlockResult> {
self.debug_trace_block_impl(BlockId::Hash(hash), options)
.await
.map_err(|err| self.current_method().map_err(err))
Expand Down
54 changes: 24 additions & 30 deletions core/node/api_server/src/web3/namespaces/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use zksync_multivm::interface::{Call, CallType, ExecutionResult, OneshotTracingP
use zksync_system_constants::MAX_ENCODED_TX_SIZE;
use zksync_types::{
api::{
BlockId, BlockNumber, CallTracerResult, CallTracerResultWithNestedResult, DebugCall,
DebugCallType, ResultDebugCall, SupportedTracers, TracerConfig,
BlockId, BlockNumber, CallTracerBlockResult, CallTracerResult, DebugCall, DebugCallType,
ResultDebugCall, SupportedTracers, TracerConfig,
},
debug_flat_call::{Action, CallResult, DebugCallFlat},
fee_model::BatchFeeInput,
Expand Down Expand Up @@ -146,21 +146,18 @@ impl DebugNamespace {
});

if !only_top_call {
call.calls
.into_iter()
.enumerate()
.for_each(|(number, call)| {
trace_address.push(number);
Self::map_flatten_call(
call,
calls,
trace_address,
false,
transaction_position,
transaction_hash,
);
trace_address.pop();
});
for (number, call) in call.calls.into_iter().enumerate() {
trace_address.push(number);
Self::map_flatten_call(
call,
calls,
trace_address,
false,
transaction_position,
transaction_hash,
);
trace_address.pop();
}
}
}

Expand All @@ -172,11 +169,11 @@ impl DebugNamespace {
&self,
block_id: BlockId,
options: Option<TracerConfig>,
) -> Result<Vec<CallTracerResultWithNestedResult>, Web3Error> {
) -> Result<CallTracerBlockResult, Web3Error> {
self.current_method().set_block_id(block_id);
if matches!(block_id, BlockId::Number(BlockNumber::Pending)) {
// See `EthNamespace::get_block_impl()` for an explanation why this check is needed.
return Ok(vec![]);
return Ok(CallTracerBlockResult::CallTrace(vec![]));
}

let mut connection = self.state.acquire_connection().await?;
Expand All @@ -191,22 +188,19 @@ impl DebugNamespace {
.map_err(DalError::generalize)?;

let mut calls = vec![];
let mut flat_calls = vec![];
for (call_trace, hash, index) in call_traces {
match Self::map_call(call_trace, index, hash, options) {
CallTracerResult::CallTrace(call) => calls.push(
CallTracerResultWithNestedResult::CallTrace(ResultDebugCall { result: call }),
),
CallTracerResult::FlattCallTrace(call) => calls.append(
&mut call
.into_iter()
.map(Box::new)
.map(CallTracerResultWithNestedResult::FlatCallTrace)
.collect(),
),
CallTracerResult::CallTrace(call) => calls.push(ResultDebugCall { result: call }),
CallTracerResult::FlattCallTrace(mut call) => flat_calls.append(&mut call),
}
}

Ok(calls)
if calls.is_empty() && !flat_calls.is_empty() {
Ok(CallTracerBlockResult::FlatCallTrace(flat_calls))
} else {
Ok(CallTracerBlockResult::CallTrace(calls))
}
}

pub async fn debug_trace_transaction_impl(
Expand Down
38 changes: 13 additions & 25 deletions core/node/api_server/src/web3/tests/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use zksync_multivm::interface::{Call, TransactionExecutionResult};
use zksync_types::{
api::{
CallTracerConfig, CallTracerResult, CallTracerResultWithNestedResult, ResultDebugCall,
SupportedTracers, TracerConfig,
CallTracerBlockResult, CallTracerConfig, CallTracerResult, SupportedTracers, TracerConfig,
},
BOOTLOADER_ADDRESS,
};
Expand Down Expand Up @@ -66,12 +65,12 @@ impl HttpTest for TraceBlockTest {
api::BlockId::Hash(hash) => client.trace_block_by_hash(hash, None).await?,
};

let CallTracerBlockResult::CallTrace(block_traces) = block_traces else {
unreachable!()
};
assert_eq!(block_traces.len(), tx_results.len()); // equals to the number of transactions in the block
for (trace, tx_result) in block_traces.iter().zip(&tx_results) {
let CallTracerResultWithNestedResult::CallTrace(ResultDebugCall { result }) = trace
else {
unreachable!()
};
let result = &trace.result;
assert_eq!(result.from, Address::zero());
assert_eq!(result.to, BOOTLOADER_ADDRESS);
assert_eq!(result.gas, tx_result.transaction.gas_limit());
Expand Down Expand Up @@ -143,41 +142,30 @@ impl HttpTest for TraceBlockFlatTest {
)
.await?;

let CallTracerBlockResult::FlatCallTrace(block_traces) = &block_traces else {
unreachable!()
};

// A transaction with 2 nested calls will convert into 3 Flattened calls.
// Also in this test, all tx have the same # of nested calls
assert_eq!(
block_traces.len(),
tx_results.len() * (tx_results[0].call_traces.len() + 1)
);

let CallTracerResultWithNestedResult::FlatCallTrace(block_traces_0) =
&block_traces[0]
else {
unreachable!()
};
let CallTracerResultWithNestedResult::FlatCallTrace(block_traces_1) =
&block_traces[1]
else {
unreachable!()
};
// First tx has 2 nested calls, thus 2 sub-traces
assert_eq!(block_traces_0.subtraces, 2);
assert_eq!(block_traces_0.traceaddress, [0]);
assert_eq!(block_traces[0].subtraces, 2);
assert_eq!(block_traces[0].traceaddress, [0]);
// Second flat-call (fist nested call) do not have nested calls
assert_eq!(block_traces_1.subtraces, 0);
assert_eq!(block_traces_1.traceaddress, [0, 0]);
assert_eq!(block_traces[1].subtraces, 0);
assert_eq!(block_traces[1].traceaddress, [0, 0]);

let top_level_call_indexes = [0, 3, 6];
let top_level_traces = top_level_call_indexes
.iter()
.map(|&i| block_traces[i].clone());

for (top_level_trace, tx_result) in top_level_traces.zip(&tx_results) {
let CallTracerResultWithNestedResult::FlatCallTrace(top_level_trace) =
top_level_trace
else {
unreachable!()
};
assert_eq!(top_level_trace.action.from, Address::zero());
assert_eq!(top_level_trace.action.to, BOOTLOADER_ADDRESS);
assert_eq!(
Expand Down

0 comments on commit 8fa81cc

Please sign in to comment.