Skip to content

Commit

Permalink
Add trace dump feature when building native
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianGCalderon committed Dec 30, 2024
1 parent d95e520 commit 91d16fa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
1 change: 1 addition & 0 deletions crates/blockifier/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fn compile_cairo_native_aot_runtime() {
"--release",
"-p",
"cairo-native-runtime",
"--features=with-trace-dump",
"--message-format=json",
"--target-dir",
runtime_target_dir.to_str().unwrap(),
Expand Down
21 changes: 11 additions & 10 deletions crates/blockifier/src/execution/native/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::collections::HashMap;
use std::fs::{self, File};
use std::path::PathBuf;
use std::sync::atomic::AtomicU64;
use std::sync::Mutex;
use std::sync::{Arc, Mutex};

use cairo_lang_sierra::program::Program;
use cairo_lang_sierra::program_registry::ProgramRegistry;
use cairo_lang_starknet_classes::contract_class::ContractEntryPoints;
use cairo_native::execution_result::ContractExecutionResult;
use cairo_native::executor::AotContractExecutor;
use cairo_native::runtime::trace_dump::TraceDump;
Expand All @@ -21,8 +22,7 @@ use super::syscall_handler::NativeSyscallHandler;
#[derive(Debug)]
pub enum ContractExecutor {
Aot(AotContractExecutor),
// must use mutex, as emu executor has state, therefore it must me mutable.
Emu(Mutex<VirtualMachine>),
Emu((Arc<Program>, ContractEntryPoints)),
// must use a differnt variant as we need `Program` for trace feature
AotTrace((AotContractExecutor, Program)),
}
Expand All @@ -32,9 +32,9 @@ impl From<AotContractExecutor> for ContractExecutor {
Self::Aot(value)
}
}
impl From<VirtualMachine> for ContractExecutor {
fn from(value: VirtualMachine) -> Self {
Self::Emu(Mutex::new(value))
impl From<(Arc<Program>, ContractEntryPoints)> for ContractExecutor {
fn from(value: (Arc<Program>, ContractEntryPoints)) -> Self {
Self::Emu(value)
}
}
impl From<(AotContractExecutor, Program)> for ContractExecutor {
Expand All @@ -56,17 +56,18 @@ impl ContractExecutor {
ContractExecutor::Aot(aot_contract_executor) => {
aot_contract_executor.run(selector, args, gas, builtin_costs, syscall_handler)
}
ContractExecutor::Emu(virtual_machine) => {
let mut virtual_machine = virtual_machine.lock().unwrap();
ContractExecutor::Emu((program, entrypoints)) => {
let mut virtual_machine =
VirtualMachine::new_starknet(program.to_owned(), entrypoints);

let args = args.to_owned();
virtual_machine.call_contract(selector, gas, args);

let trace = virtual_machine.run_with_trace(&mut syscall_handler);

static COUNTER: AtomicU64 = AtomicU64::new(0);
let counter = COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);

let trace = virtual_machine.run_with_trace(&mut syscall_handler);

let trace_path = PathBuf::from(format!("traces/emu/{counter}.json"));
let trace_parent_path = trace_path.parent().unwrap();
fs::create_dir_all(trace_parent_path).unwrap();
Expand Down

0 comments on commit 91d16fa

Please sign in to comment.