Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
TransactionExecutionInfo: add serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadNassar1 committed Jan 7, 2024
1 parent b602b31 commit adb3ecc
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 34 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

55 changes: 49 additions & 6 deletions crates/blockifier/src/execution/call_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::HashSet;

use cairo_vm::vm::runners::cairo_runner::ExecutionResources as VmExecutionResources;
use serde::ser::SerializeStruct;
use serde::{Deserialize, Serialize, Serializer};
use starknet_api::core::{ClassHash, EthAddress};
use starknet_api::hash::StarkFelt;
use starknet_api::state::StorageKey;
Expand All @@ -11,7 +13,7 @@ use crate::state::cached_state::StorageEntry;
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::objects::TransactionExecutionResult;

#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct Retdata(pub Vec<StarkFelt>);

#[macro_export]
Expand All @@ -22,29 +24,29 @@ macro_rules! retdata {
}

#[cfg_attr(test, derive(Clone))]
#[derive(Debug, Default, Eq, PartialEq)]
#[derive(Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct OrderedEvent {
pub order: usize,
pub event: EventContent,
}

#[cfg_attr(test, derive(Clone))]
#[derive(Debug, Default, Eq, PartialEq)]
#[derive(Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct MessageToL1 {
pub to_address: EthAddress,
pub payload: L2ToL1Payload,
}

#[cfg_attr(test, derive(Clone))]
#[derive(Debug, Default, Eq, PartialEq)]
#[derive(Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct OrderedL2ToL1Message {
pub order: usize,
pub message: MessageToL1,
}

/// Represents the effects of executing a single entry point.
#[cfg_attr(test, derive(Clone))]
#[derive(Debug, Default, Eq, PartialEq)]
#[derive(Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct CallExecution {
pub retdata: Retdata,
pub events: Vec<OrderedEvent>,
Expand All @@ -53,19 +55,60 @@ pub struct CallExecution {
pub gas_consumed: u64,
}

#[derive(Debug, Default, Eq, PartialEq, Deserialize)]
pub struct VmExecutionResourcesWrapper(pub VmExecutionResources);

impl Serialize for VmExecutionResourcesWrapper {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
// Create a SerializeStruct with the appropriate number of fields
let mut state = serializer.serialize_struct("ExecutionResourcesWrapper", 3)?;

// Serialize each field individually
state.serialize_field("builtin_instance_counter", &self.0.builtin_instance_counter)?;
state.serialize_field("n_memory_holes", &self.0.n_memory_holes)?;
state.serialize_field("n_steps", &self.0.n_steps)?;

// Finish serializing the struct
state.end()
}
}

/// Represents the full effects of executing an entry point, including the inner calls it invoked.
#[derive(Debug, Default, Eq, PartialEq)]
pub struct CallInfo {
pub call: CallEntryPoint,
pub execution: CallExecution,
pub vm_resources: VmExecutionResources,
pub vm_resources: VmExecutionResourcesWrapper,
pub inner_calls: Vec<CallInfo>,

// Additional information gathered during execution.
pub storage_read_values: Vec<StarkFelt>,
pub accessed_storage_keys: HashSet<StorageKey>,
}

impl Serialize for CallInfo {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("CallInfo", 6)?;
state.serialize_field("call", &self.call)?;
state.serialize_field("execution", &self.execution)?;

// Manually serialize vm_resources here
state.serialize_field("vm_resources", &self.vm_resources)?;

state.serialize_field("inner_calls", &self.inner_calls)?;
state.serialize_field("storage_read_values", &self.storage_read_values)?;
state.serialize_field("accessed_storage_keys", &self.accessed_storage_keys)?;

state.end()
}
}

impl CallInfo {
/// Returns the set of class hashes that were executed during this call execution.
// TODO: Add unit test for this method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use starknet_api::core::EntryPointSelector;
use starknet_api::deprecated_contract_class::EntryPointType;
use starknet_api::hash::StarkHash;

use super::call_info::VmExecutionResourcesWrapper;
use crate::abi::abi_utils::selector_from_name;
use crate::abi::constants::{CONSTRUCTOR_ENTRY_POINT_NAME, DEFAULT_ENTRY_POINT_SELECTOR};
use crate::execution::call_info::{CallExecution, CallInfo};
Expand Down Expand Up @@ -253,7 +254,7 @@ pub fn finalize_execution(
failed: false,
gas_consumed: 0,
},
vm_resources: full_call_vm_resources.filter_unused_builtins(),
vm_resources: VmExecutionResourcesWrapper(full_call_vm_resources.filter_unused_builtins()),
inner_calls: syscall_handler.inner_calls,
storage_read_values: syscall_handler.read_values,
accessed_storage_keys: syscall_handler.accessed_keys,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use starknet_api::{calldata, class_hash, contract_address, patricia_key, stark_f
use test_case::test_case;

use crate::abi::abi_utils::selector_from_name;
use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
use crate::execution::call_info::{CallExecution, CallInfo, Retdata, VmExecutionResourcesWrapper};
use crate::execution::common_hints::ExecutionMode;
use crate::execution::entry_point::{CallEntryPoint, CallType};
use crate::execution::errors::{EntryPointExecutionError, VirtualMachineExecutionError};
Expand Down Expand Up @@ -135,7 +135,7 @@ fn test_nested_library_call() {
let nested_storage_call_info = CallInfo {
call: nested_storage_entry_point,
execution: CallExecution::from_retdata(retdata![stark_felt!(value + 1)]),
vm_resources: storage_entry_point_vm_resources.clone(),
vm_resources: VmExecutionResourcesWrapper(storage_entry_point_vm_resources.clone()),
storage_read_values: vec![stark_felt!(0_u8), stark_felt!(value + 1)],
accessed_storage_keys: HashSet::from([StorageKey(patricia_key!(key + 1))]),
..Default::default()
Expand All @@ -149,14 +149,14 @@ fn test_nested_library_call() {
let library_call_info = CallInfo {
call: library_entry_point,
execution: CallExecution::from_retdata(retdata![stark_felt!(value + 1)]),
vm_resources: library_call_vm_resources.clone(),
vm_resources: VmExecutionResourcesWrapper(library_call_vm_resources.clone()),
inner_calls: vec![nested_storage_call_info],
..Default::default()
};
let storage_call_info = CallInfo {
call: storage_entry_point,
execution: CallExecution::from_retdata(retdata![stark_felt!(value)]),
vm_resources: storage_entry_point_vm_resources.clone(),
vm_resources: VmExecutionResourcesWrapper(storage_entry_point_vm_resources.clone()),
storage_read_values: vec![stark_felt!(0_u8), stark_felt!(value)],
accessed_storage_keys: HashSet::from([StorageKey(patricia_key!(key))]),
..Default::default()
Expand All @@ -168,7 +168,7 @@ fn test_nested_library_call() {
let expected_call_info = CallInfo {
call: main_entry_point.clone(),
execution: CallExecution::from_retdata(retdata![stark_felt!(0_u8)]),
vm_resources: main_call_vm_resources,
vm_resources: VmExecutionResourcesWrapper(main_call_vm_resources),
inner_calls: vec![library_call_info, storage_call_info],
..Default::default()
};
Expand Down Expand Up @@ -209,7 +209,10 @@ fn test_call_contract() {
..trivial_external_entry_point()
},
execution: expected_execution.clone(),
vm_resources: VmExecutionResources { n_steps: 42, ..Default::default() },
vm_resources: VmExecutionResourcesWrapper(VmExecutionResources {
n_steps: 42,
..Default::default()
}),
storage_read_values: vec![StarkFelt::ZERO, stark_felt!(value)],
accessed_storage_keys: HashSet::from([StorageKey(patricia_key!(key))]),
..Default::default()
Expand All @@ -225,11 +228,11 @@ fn test_call_contract() {
..trivial_external_entry_point()
},
execution: expected_execution,
vm_resources: VmExecutionResources {
vm_resources: VmExecutionResourcesWrapper(VmExecutionResources {
n_steps: 81,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 1)]),
..Default::default()
},
}),
..Default::default()
};

Expand Down
7 changes: 4 additions & 3 deletions crates/blockifier/src/execution/entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::Arc;
use cairo_vm::vm::runners::cairo_runner::{
ExecutionResources as VmExecutionResources, ResourceTracker, RunResources,
};
use serde::Serialize;
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector};
use starknet_api::deprecated_contract_class::EntryPointType;
use starknet_api::hash::StarkFelt;
Expand Down Expand Up @@ -35,14 +36,14 @@ pub const FAULTY_CLASS_HASH: &str =
pub type EntryPointExecutionResult<T> = Result<T, EntryPointExecutionError>;

/// Represents a the type of the call (used for debugging).
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq, Serialize)]
pub enum CallType {
#[default]
Call = 0,
Delegate = 1,
}
/// Represents a call to an entry point of a Starknet contract.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize)]
pub struct CallEntryPoint {
// The class hash is not given if it can be deduced from the storage address.
pub class_hash: Option<ClassHash>,
Expand Down Expand Up @@ -281,7 +282,7 @@ impl EntryPointExecutionContext {
) -> usize {
let validate_steps = validate_call_info
.as_ref()
.map(|call_info| call_info.vm_resources.n_steps)
.map(|call_info| call_info.vm_resources.0.n_steps)
.unwrap_or_default();

let overhead_steps = OS_RESOURCES.resources_for_tx_type(tx_type).n_steps;
Expand Down
3 changes: 2 additions & 1 deletion crates/blockifier/src/execution/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use num_traits::ToPrimitive;
use starknet_api::hash::StarkFelt;
use starknet_api::stark_felt;

use super::call_info::VmExecutionResourcesWrapper;
use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
use crate::execution::contract_class::{ContractClassV1, EntryPointV1};
use crate::execution::entry_point::{
Expand Down Expand Up @@ -298,7 +299,7 @@ pub fn finalize_execution(
failed: call_result.failed,
gas_consumed: call_result.gas_consumed,
},
vm_resources: full_call_vm_resources.filter_unused_builtins(),
vm_resources: VmExecutionResourcesWrapper(full_call_vm_resources.filter_unused_builtins()),
inner_calls: syscall_handler.inner_calls,
storage_read_values: syscall_handler.read_values,
accessed_storage_keys: syscall_handler.accessed_keys,
Expand Down
1 change: 1 addition & 0 deletions crates/blockifier/src/execution/entry_point_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ fn test_cairo1_entry_point_segment_arena() {
.execute_directly(&mut state)
.unwrap()
.vm_resources
.0
.builtin_instance_counter
.contains_key(BuiltinName::segment_arena.name())
);
Expand Down
9 changes: 5 additions & 4 deletions crates/blockifier/src/execution/syscalls/syscalls_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::abi::constants;
use crate::block_context::BlockContext;
use crate::execution::call_info::{
CallExecution, CallInfo, MessageToL1, OrderedEvent, OrderedL2ToL1Message, Retdata,
VmExecutionResourcesWrapper,
};
use crate::execution::common_hints::ExecutionMode;
use crate::execution::contract_class::ContractClassV0;
Expand Down Expand Up @@ -554,7 +555,7 @@ fn test_nested_library_call() {
gas_consumed: REQUIRED_GAS_STORAGE_READ_WRITE_TEST,
..CallExecution::default()
},
vm_resources: storage_entry_point_vm_resources.clone(),
vm_resources: VmExecutionResourcesWrapper(storage_entry_point_vm_resources.clone()),
storage_read_values: vec![stark_felt!(value + 1)],
accessed_storage_keys: HashSet::from([StorageKey(patricia_key!(key + 1))]),
..Default::default()
Expand All @@ -571,7 +572,7 @@ fn test_nested_library_call() {
gas_consumed: REQUIRED_GAS_LIBRARY_CALL_TEST,
..CallExecution::default()
},
vm_resources: library_call_vm_resources,
vm_resources: VmExecutionResourcesWrapper(library_call_vm_resources),
inner_calls: vec![nested_storage_call_info],
..Default::default()
};
Expand All @@ -582,7 +583,7 @@ fn test_nested_library_call() {
gas_consumed: REQUIRED_GAS_STORAGE_READ_WRITE_TEST,
..CallExecution::default()
},
vm_resources: storage_entry_point_vm_resources,
vm_resources: VmExecutionResourcesWrapper(storage_entry_point_vm_resources),
storage_read_values: vec![stark_felt!(value)],
accessed_storage_keys: HashSet::from([StorageKey(patricia_key!(key))]),
..Default::default()
Expand All @@ -600,7 +601,7 @@ fn test_nested_library_call() {
gas_consumed: 316180,
..CallExecution::default()
},
vm_resources: main_call_vm_resources,
vm_resources: VmExecutionResourcesWrapper(main_call_vm_resources),
inner_calls: vec![library_call_info, storage_call_info],
..Default::default()
};
Expand Down
1 change: 1 addition & 0 deletions crates/blockifier/src/test_utils/prices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,5 @@ fn fee_transfer_resources(
)
.unwrap()
.vm_resources
.0
}
5 changes: 3 additions & 2 deletions crates/blockifier/src/transaction/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::{HashMap, HashSet};
use cairo_felt::Felt252;
use itertools::concat;
use num_traits::Pow;
use serde::Serialize;
use starknet_api::core::{ClassHash, ContractAddress, Nonce};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::transaction::{
Expand Down Expand Up @@ -139,7 +140,7 @@ pub struct CommonAccountFields {
}

/// Contains the information gathered by the execution of a transaction.
#[derive(Debug, Default, Eq, PartialEq)]
#[derive(Debug, Default, Eq, PartialEq, Serialize)]
pub struct TransactionExecutionInfo {
/// Transaction validation call info; [None] for `L1Handler`.
pub validate_call_info: Option<CallInfo>,
Expand Down Expand Up @@ -187,7 +188,7 @@ impl TransactionExecutionInfo {

/// A mapping from a transaction execution resource to its actual usage.
#[cfg_attr(test, derive(Clone))]
#[derive(Debug, Default, Eq, PartialEq)]
#[derive(Debug, Default, Eq, PartialEq, Serialize)]
pub struct ResourcesMapping(pub HashMap<String, usize>);

impl ResourcesMapping {
Expand Down
Loading

0 comments on commit adb3ecc

Please sign in to comment.