Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DNM] Add trace dump feature #8

Draft
wants to merge 20 commits into
base: native2.8.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,021 changes: 948 additions & 1,073 deletions Cargo.lock

Large diffs are not rendered by default.

96 changes: 48 additions & 48 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,44 @@
resolver = "2"

members = [
"crates/batcher",
"crates/batcher_types",
"crates/blockifier",
"crates/committer_cli",
"crates/consensus_manager",
"crates/consensus_manager_types",
"crates/gateway",
"crates/mempool",
"crates/mempool_infra",
"crates/mempool_node",
"crates/mempool_test_utils",
"crates/mempool_types",
"crates/native_blockifier",
"crates/papyrus_base_layer",
"crates/papyrus_common",
"crates/papyrus_config",
"crates/papyrus_execution",
"crates/papyrus_load_test",
"crates/papyrus_monitoring_gateway",
"crates/papyrus_network",
"crates/papyrus_node",
"crates/papyrus_p2p_sync",
"crates/papyrus_proc_macros",
"crates/papyrus_protobuf",
"crates/papyrus_rpc",
"crates/papyrus_storage",
"crates/papyrus_sync",
"crates/papyrus_test_utils",
"crates/sequencing/papyrus_block_builder",
"crates/sequencing/papyrus_consensus",
"crates/starknet_api",
"crates/starknet_client",
"crates/starknet_committer",
"crates/starknet_patricia",
"crates/starknet_sierra_compile",
"crates/task_executor",
"crates/tests-integration",
"workspace_tests",
"crates/batcher",
"crates/batcher_types",
"crates/blockifier",
"crates/committer_cli",
"crates/consensus_manager",
"crates/consensus_manager_types",
"crates/gateway",
"crates/mempool",
"crates/mempool_infra",
"crates/mempool_node",
"crates/mempool_test_utils",
"crates/mempool_types",
"crates/native_blockifier",
"crates/papyrus_base_layer",
"crates/papyrus_common",
"crates/papyrus_config",
"crates/papyrus_execution",
"crates/papyrus_load_test",
"crates/papyrus_monitoring_gateway",
"crates/papyrus_network",
"crates/papyrus_node",
"crates/papyrus_p2p_sync",
"crates/papyrus_proc_macros",
"crates/papyrus_protobuf",
"crates/papyrus_rpc",
"crates/papyrus_storage",
"crates/papyrus_sync",
"crates/papyrus_test_utils",
"crates/sequencing/papyrus_block_builder",
"crates/sequencing/papyrus_consensus",
"crates/starknet_api",
"crates/starknet_client",
"crates/starknet_committer",
"crates/starknet_patricia",
"crates/starknet_sierra_compile",
"crates/task_executor",
"crates/tests-integration",
"workspace_tests",
]

[workspace.package]
Expand Down Expand Up @@ -74,14 +74,14 @@ byteorder = "1.4.3"
bytes = "1"
cached = "0.44.0"
cairo-felt = "0.9.1"
cairo-lang-casm = "2.8.4"
cairo-lang-runner = "2.8.4"
cairo-lang-sierra = "2.8.4"
cairo-lang-sierra-to-casm = "2.8.4"
cairo-lang-starknet-classes = "2.8.4"
cairo-lang-utils = "2.8.4"
cairo-lang-casm = "2.9.0-dev.0"
cairo-lang-runner = "2.9.0-dev.0"
cairo-lang-sierra = "2.9.0-dev.0"
cairo-lang-sierra-to-casm = "2.9.0-dev.0"
cairo-lang-starknet-classes = "2.9.0-dev.0"
cairo-lang-utils = "2.9.0-dev.0"
# This is a temporary dependency, will be removed once the new version of cairo-native is released to main.
cairo-native = { git = "https://github.com/lambdaclass/cairo_native", rev = "ebfa9a116d03618e90fc52773e73958b179dd775" }
cairo-native = { git = "https://github.com/lambdaclass/cairo_native", rev = "76e83965d3bf1252eb6c68200a3accd5fd1ec004" }
cairo-vm = "1.0.1"
camelpaste = "0.1.0"
chrono = "0.4.26"
Expand Down Expand Up @@ -171,7 +171,7 @@ serde_repr = "0.1.19"
serde_yaml = "0.9.16"
sha2 = "0.10.8"
sha3 = "0.10.8"
sierra-emu = { git = "https://github.com/lambdaclass/sierra-emu.git", rev = "0b11466033d7f070fcdd13cb81681b0fd9aaaa00" }
sierra-emu = { git = "https://github.com/lambdaclass/sierra-emu.git" }
simple_logger = "4.0.0"
starknet-core = "0.6.0"
starknet-crypto = "0.5.1"
Expand All @@ -195,10 +195,10 @@ static_assertions = "1.1.0"
statistical = "1.0.0"
strum = "0.25.0"
strum_macros = "0.25.2"
tempfile = "3.7.0"
tempfile = "3.13.0"
test-case = "3.2.1"
test-log = "0.2.14"
thiserror = "1.0.37"
thiserror = "1.0.64"
tikv-jemallocator = "0.5.4"
tokio = { version = "1.37.0", features = ["full"] }
tokio-retry = "0.3"
Expand Down
1 change: 1 addition & 0 deletions crates/blockifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ concurrency = []
jemalloc = ["dep:tikv-jemallocator"]
testing = ["rand", "rstest"]
use-sierra-emu = []
with-trace-dump = ["cairo-native/with-trace-dump"]


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
22 changes: 12 additions & 10 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,11 +632,12 @@ impl NativeContractClassV1 {

pub struct NativeContractClassV1Inner {
pub executor: Arc<AotContractExecutor>,
entry_points_by_type: NativeContractEntryPoints,
pub entry_points_by_type: NativeContractEntryPoints,
pub contract: cairo_lang_starknet_classes::contract_class::ContractClass,
pub program: cairo_lang_sierra::program::Program, // for sierra emu
// Used for PartialEq
sierra_program_hash: starknet_api::hash::StarkHash,
sierra_raw_program: Vec<BigUintAsHex>
sierra_raw_program: Vec<BigUintAsHex>,
}

impl std::fmt::Debug for NativeContractClassV1Inner {
Expand Down Expand Up @@ -674,11 +675,12 @@ impl NativeContractClassV1Inner {
&lookup_fid,
&sierra_contract_class.entry_points_by_type,
)?,
contract: sierra_contract_class.clone(),
program: sierra_program.clone(),
sierra_program_hash: calculate_sierra_program_hash(
sierra_contract_class.sierra_program,
),
sierra_raw_program: bytecode
sierra_raw_program: bytecode,
})
}
}
Expand All @@ -702,10 +704,10 @@ impl PartialEq for NativeContractClassV1Inner {
/// Modelled after [SierraContractEntryPoints]
/// and enriched with information for the Cairo Native ABI.
/// See Note [Cairo Native ABI]
struct NativeContractEntryPoints {
constructor: Vec<NativeEntryPoint>,
external: Vec<NativeEntryPoint>,
l1_handler: Vec<NativeEntryPoint>,
pub struct NativeContractEntryPoints {
pub constructor: Vec<NativeEntryPoint>,
pub external: Vec<NativeEntryPoint>,
pub l1_handler: Vec<NativeEntryPoint>,
}

impl NativeContractEntryPoints {
Expand Down Expand Up @@ -751,11 +753,11 @@ impl Index<EntryPointType> for NativeContractEntryPoints {

#[derive(Debug, PartialEq)]
/// Provides a relation between a function in a contract and a compiled contract
struct NativeEntryPoint {
pub struct NativeEntryPoint {
/// The selector is the key to find the function in the contract
selector: EntryPointSelector,
pub selector: EntryPointSelector,
/// and the function_id is the key to find the function in the compiled contract
function_id: FunctionId,
pub function_id: FunctionId,
}

impl NativeEntryPoint {
Expand Down
84 changes: 83 additions & 1 deletion crates/blockifier/src/execution/native/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,93 @@ pub fn execute_entry_point_call(
let result = if cfg!(feature = "use-sierra-emu") {
let vm = sierra_emu::VirtualMachine::new_starknet(
Arc::new(contract_class.program.clone()),
&contract_class.contract.entry_points_by_type,
&mut syscall_handler,
);
run_sierra_emu_executor(vm, function_id, call.clone())
} else {
run_native_executor(&contract_class.executor, call.entry_point_selector.0, call, syscall_handler)
#[cfg(feature = "with-trace-dump")]
{
use std::collections::HashMap;
use std::sync::atomic::AtomicUsize;
use std::sync::Mutex;

use cairo_lang_sierra::program_registry::ProgramRegistry;
use cairo_native::runtime::trace_dump::TraceDump;
use cairo_native::types::TypeBuilder;

// Since the library is statically linked, then dynamically loaded, each instance of
// `TRACE_DUMP` for each contract is separate (probably). That's why we need this
// getter and cannot use `cairo_native::runtime::TRACE_DUMP` directly.
let trace_dump = unsafe {
let fn_ptr = contract_class
.executor
.library
.get::<extern "C" fn() -> &'static Mutex<HashMap<u64, TraceDump>>>(
b"get_trace_dump_ptr\0",
)
.unwrap();

fn_ptr()
};
let mut trace_dump = trace_dump.lock().unwrap();

static COUNTER: AtomicUsize = AtomicUsize::new(0);
let counter_value = COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
trace_dump.insert(
u64::try_from(counter_value).unwrap(),
TraceDump::new(
ProgramRegistry::new(&contract_class.program).unwrap(),
|x, registry| x.layout(registry).unwrap(),
),
);

// Set the active trace id.
let trace_id_ref = unsafe {
contract_class
.executor
.library
.get::<u64>(b"TRACE_DUMP__TRACE_ID\0")
.unwrap()
.try_as_raw_ptr()
.unwrap()
.cast::<u64>()
.as_mut()
.unwrap()
};
let old_trace_id = *trace_id_ref;

*trace_id_ref = u64::try_from(counter_value).unwrap();

println!("Execution started for trace #{counter_value}.");
dbg!(trace_dump.keys().collect::<Vec<_>>());

drop(trace_dump);

let x = run_native_executor(
&contract_class.executor,
call.entry_point_selector.0,
call,
syscall_handler,
counter_value,
);

println!("Execution finished for trace #{counter_value}.");

*trace_id_ref = old_trace_id;

println!("Resetting to trace #{old_trace_id}.");

x
}

#[cfg(not(feature = "with-trace-dump"))]
run_native_executor(
&contract_class.executor,
call.entry_point_selector.0,
call,
syscall_handler,
)
};
let execution_time = pre_execution_instant.elapsed().as_nanos();
tracing::info!(time = execution_time, "native contract execution finished");
Expand Down
27 changes: 17 additions & 10 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ impl<'state> NativeSyscallHandler<'state> {
}

pub fn update_remaining_gas(&mut self, remaining_gas: &mut u64, call_info: &CallInfo) {
// pass the reference to the function
update_remaining_gas(remaining_gas, call_info);
}

Expand Down Expand Up @@ -168,7 +167,8 @@ impl<'state> NativeSyscallHandler<'state> {
}

// Refund `SYSCALL_BASE_GAS_COST` as it was pre-charged.
let required_gas = syscall_gas_cost - self.execution_context.gas_costs().syscall_base_gas_cost;
let required_gas =
syscall_gas_cost - self.execution_context.gas_costs().syscall_base_gas_cost;

if *remaining_gas < required_gas {
// Out of gas failure.
Expand Down Expand Up @@ -231,6 +231,14 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {
}
}

fn get_class_hash_at(
&mut self,
_contract_address: Felt,
_remaining_gas: &mut u64,
) -> SyscallResult<Felt> {
todo!()
}

fn get_execution_info(&mut self, remaining_gas: &mut u64) -> SyscallResult<ExecutionInfo> {
self.pre_execute_syscall(
remaining_gas,
Expand Down Expand Up @@ -295,10 +303,7 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {
})
}

fn get_execution_info_v2(
&mut self,
remaining_gas: &mut u64,
) -> SyscallResult<ExecutionInfoV2> {
fn get_execution_info_v2(&mut self, remaining_gas: &mut u64) -> SyscallResult<ExecutionInfoV2> {
self.pre_execute_syscall(
remaining_gas,
SyscallSelector::GetExecutionInfo,
Expand Down Expand Up @@ -423,7 +428,7 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {
// Warning: converting of reference would create a new reference to different data,
// example:
// let mut a: u128 = 1;
// let a_ref: &mut u64 = &mut a;
// let a_ref: &mut u128 = &mut a;
//
// let mut b: u64 = u64::try_from(*a_ref).unwrap();
//
Expand Down Expand Up @@ -689,7 +694,8 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {
// TODO(Ori, 1/2/2024): Write an indicative expect message explaining why the conversion
// works.
let n_rounds_as_u64 = u64::try_from(n_rounds).expect("Failed to convert usize to u64.");
let gas_cost = n_rounds_as_u64 * self.execution_context.gas_costs().keccak_round_cost_gas_cost;
let gas_cost =
n_rounds_as_u64 * self.execution_context.gas_costs().keccak_round_cost_gas_cost;

if gas_cost > *remaining_gas {
// In VM this error is wrapped into `SyscallExecutionError::SyscallError`
Expand Down Expand Up @@ -1199,7 +1205,7 @@ pub mod sierra_emu_impl {
// Warning: converting of reference would create a new reference to different data,
// example:
// let mut a: u128 = 1;
// let a_ref: &mut u64 = &mut a;
// let a_ref: &mut u128 = &mut a;
//
// let mut b: u64 = u64::try_from(*a_ref).unwrap();
//
Expand Down Expand Up @@ -1469,7 +1475,8 @@ pub mod sierra_emu_impl {
// TODO(Ori, 1/2/2024): Write an indicative expect message explaining why the conversion
// works.
let n_rounds_as_u64 = u64::try_from(n_rounds).expect("Failed to convert usize to u64.");
let gas_cost = n_rounds_as_u64 * self.execution_context.gas_costs().keccak_round_cost_gas_cost;
let gas_cost =
n_rounds_as_u64 * self.execution_context.gas_costs().keccak_round_cost_gas_cost;

if gas_cost > *remaining_gas {
// In VM this error is wrapped into `SyscallExecutionError::SyscallError`
Expand Down
Loading
Loading