Skip to content

Commit

Permalink
fix vm_latest calltracer bug
Browse files Browse the repository at this point in the history
  • Loading branch information
joonazan committed Sep 25, 2024
1 parent 947a21a commit 23b5343
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
3 changes: 3 additions & 0 deletions core/lib/multivm/src/tracers/call_tracer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub mod vm_virtual_blocks;
#[derive(Debug, Clone)]
pub struct CallTracer {
stack: Vec<FarcallAndNearCallCount>,
finished_calls: Vec<Call>,

result: Arc<OnceCell<Vec<Call>>>,

max_stack_depth: usize,
Expand All @@ -41,6 +43,7 @@ impl CallTracer {
pub fn new(result: Arc<OnceCell<Vec<Call>>>) -> Self {
Self {
stack: vec![],
finished_calls: vec![],
result,
max_stack_depth: 0,
max_near_calls: 0,
Expand Down
6 changes: 4 additions & 2 deletions core/lib/multivm/src/tracers/call_tracer/vm_latest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ impl<S: WriteStorage, H: HistoryMode> VmTracer<S, H> for CallTracer {
_bootloader_state: &BootloaderState,
_stop_reason: VmExecutionStopReason,
) {
self.store_result()
let result = std::mem::take(&mut self.finished_calls);
let cell = self.result.as_ref();
cell.set(result).unwrap();
}
}

Expand Down Expand Up @@ -199,7 +201,7 @@ impl CallTracer {
if let Some(parent_call) = self.stack.last_mut() {
parent_call.farcall.calls.push(current_call.farcall);
} else {
self.push_call_and_update_stats(current_call.farcall, current_call.near_calls_after);
self.finished_calls.push(current_call.farcall);
}
}
}
2 changes: 2 additions & 0 deletions core/lib/multivm/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::interface::L1BatchEnv;
pub(crate) mod bytecode;
mod deduplicator;
pub(crate) mod events;
#[cfg(test)]
pub(crate) mod testonly;

/// Calculates the base fee and gas per pubdata for the given L1 gas price.
pub fn derive_base_fee_and_gas_per_pubdata(
Expand Down
21 changes: 21 additions & 0 deletions core/lib/multivm/src/utils/testonly.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use zksync_types::zk_evm_types::FarCallOpcode;
use zksync_vm_interface::{Call, CallType};

pub(crate) fn check_call_tracer_test_result(call_tracer_result: &[Call]) {
assert!(call_tracer_result.len() > 10);

for call in call_tracer_result {
check_call(call);
}
}

fn check_call(call: &Call) {
assert!(call.gas_used > call.calls.iter().map(|call| call.gas_used).sum::<u64>());

for subcall in &call.calls {
if subcall.r#type != CallType::Call(FarCallOpcode::Mimic) {
assert_eq!(call.to, subcall.from);
}
check_call(subcall);
}
}
6 changes: 2 additions & 4 deletions core/lib/multivm/src/versions/vm_fast/tests/call_tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use zksync_types::{Address, Execute};

use crate::{
interface::{TxExecutionMode, VmExecutionMode, VmInterface},
utils::testonly::check_call_tracer_test_result,
versions::testonly::ContractToDeploy,
vm_fast::{
call_tracer::CallTracer,
Expand Down Expand Up @@ -79,9 +80,6 @@ fn test_basic_behavior() {

let call_tracer_result = call_tracer.result();

assert_eq!(call_tracer_result.len(), 1);
// Expect that there are a plenty of subcalls underneath.
let subcall = &call_tracer_result[0].calls;
assert!(subcall.len() > 10);
check_call_tracer_test_result(&call_tracer_result);
assert!(!res.result.is_failed());
}
6 changes: 2 additions & 4 deletions core/lib/multivm/src/versions/vm_latest/tests/call_tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use zksync_types::{Address, Execute};
use crate::{
interface::{TxExecutionMode, VmExecutionMode, VmInterface},
tracers::CallTracer,
utils::testonly::check_call_tracer_test_result,
vm_latest::{
constants::BATCH_COMPUTATIONAL_GAS_LIMIT,
tests::{
Expand Down Expand Up @@ -88,9 +89,6 @@ fn test_basic_behavior() {

let call_tracer_result = result.get().unwrap();

assert_eq!(call_tracer_result.len(), 1);
// Expect that there are a plenty of subcalls underneath.
let subcall = &call_tracer_result[0].calls;
assert!(subcall.len() > 10);
check_call_tracer_test_result(call_tracer_result);
assert!(!res.result.is_failed());
}

0 comments on commit 23b5343

Please sign in to comment.