Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Forge

#### Added

- Contract debug traces now include emitted Starknet events via the `events` trace component and `detailed` trace verbosity.

#### Changed

- `snforge_scarb_plugin` diagnostics for named-argument kind violations now include both possible values and invalid arguments found.
Expand Down
2 changes: 1 addition & 1 deletion crates/data-transformer/src/shared/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@ fn extract_generic_args(
return Err(PathSplitError::MoreThanOneGenericArg);
};

Ok(generic_arg.to_string())
Ok((*generic_arg).to_string())
}
23 changes: 21 additions & 2 deletions crates/debugging/src/trace/collect.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::contracts_data_store::ContractsDataStore;
use crate::trace::types::{
CallerAddress, ContractAddress, ContractName, ContractTrace, Gas, Selector, TestName,
TraceInfo, TransformedCallResult, TransformedCalldata,
CallerAddress, ContractAddress, ContractName, ContractTrace, Event, Events, Gas, Selector,
TestName, TraceInfo, TransformedCallResult, TransformedCalldata,
};
use crate::{Context, Trace};
use blockifier::execution::call_info::OrderedEvent;
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::{
CallFailure, CallSuccess,
};
Expand Down Expand Up @@ -53,6 +54,7 @@ impl<'a> Collector<'a> {
call_type: components.call_type(entry_point.call_type),
nested_calls,
call_result: components.call_result_lazy(|| self.collect_transformed_call_result(abi)),
events: components.events_lazy(|| self.collect_events()),
gas: components.gas_lazy(|| self.collect_gas()),
};

Expand Down Expand Up @@ -150,6 +152,23 @@ impl<'a> Collector<'a> {
.l2_gas)
}

fn collect_events(&self) -> Events {
Events(
self.call_trace
.events
.iter()
.map(Self::collect_event)
.collect(),
)
}

fn collect_event(event: &OrderedEvent) -> Event {
Event {
keys: event.event.keys.iter().map(|key| key.0).collect(),
data: event.event.data.0.clone(),
}
}

fn class_hash(&self) -> &ClassHash {
self.call_trace
.entry_point
Expand Down
6 changes: 5 additions & 1 deletion crates/debugging/src/trace/components.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::trace::types::{
CallerAddress, ContractAddress, ContractName, Gas, TransformedCallResult, TransformedCalldata,
CallerAddress, ContractAddress, ContractName, Events, Gas, TransformedCallResult,
TransformedCalldata,
};
use blockifier::execution::entry_point::CallType;
use paste::paste;
Expand Down Expand Up @@ -42,6 +43,8 @@ pub enum Component {
CallType,
/// The result of the call, transformed for display.
CallResult,
/// The raw events emitted by the call.
Events,
/// The L2 gas used by the call.
Gas,
}
Expand Down Expand Up @@ -125,3 +128,4 @@ impl_component_container!(CallerAddress);
impl_component_container!(CallType);
impl_component_container!(CallResult, TransformedCallResult);
impl_component_container!(Gas);
impl_component_container!(Events);
14 changes: 13 additions & 1 deletion crates/debugging/src/trace/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use crate::Context;
use crate::trace::collect::Collector;
use crate::trace::components::{
CallResultContainer, CallTypeContainer, CalldataContainer, CallerAddressContainer,
ContractAddressContainer, ContractNameContainer, EntryPointTypeContainer, GasContainer,
ContractAddressContainer, ContractNameContainer, EntryPointTypeContainer, EventsContainer,
GasContainer,
};
use crate::tree::TreeSerialize;
use cheatnet::trace_data::CallTrace;
use starknet_api::core::ContractAddress as ApiContractAddress;
use starknet_api::execution_resources::GasAmount as ApiGasAmount;
use starknet_types_core::felt::Felt;
use std::fmt;
use std::fmt::Display;

Expand All @@ -33,6 +35,7 @@ pub struct TraceInfo {
pub call_type: CallTypeContainer,
pub nested_calls: Vec<ContractTrace>,
pub call_result: CallResultContainer,
pub events: EventsContainer,
pub gas: GasContainer,
}

Expand Down Expand Up @@ -60,6 +63,15 @@ pub struct CallerAddress(pub ApiContractAddress);
#[derive(Debug, Clone)]
pub struct Gas(pub ApiGasAmount);

#[derive(Debug, Clone)]
pub struct Events(pub Vec<Event>);

#[derive(Debug, Clone)]
pub struct Event {
pub keys: Vec<Felt>,
pub data: Vec<Felt>,
}

impl Trace {
/// Creates a new [`Trace`] from a given [`Context`] and a test name.
#[must_use]
Expand Down
1 change: 1 addition & 0 deletions crates/debugging/src/tree/ui/as_tree_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl AsTreeNode for TraceInfo {
parent.leaf_optional(self.caller_address.as_option());
parent.leaf_optional(self.call_type.as_option());
parent.leaf_optional(self.call_result.as_option());
parent.leaf_optional(self.events.as_option());
parent.leaf_optional(self.gas.as_option());
for nested_call in &self.nested_calls {
parent.as_tree_node(nested_call);
Expand Down
36 changes: 34 additions & 2 deletions crates/debugging/src/tree/ui/display.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::trace::types::{
CallerAddress, ContractAddress, ContractName, Gas, Selector, TestName, TransformedCallResult,
TransformedCalldata,
CallerAddress, ContractAddress, ContractName, Event, Events, Gas, Selector, TestName,
TransformedCallResult, TransformedCalldata,
};
use blockifier::execution::entry_point::CallType;
use starknet_api::contract_class::EntryPointType;
Expand Down Expand Up @@ -84,6 +84,20 @@ impl NodeDisplay for TransformedCallResult {
}
}

impl NodeDisplay for Events {
const TAG: &'static str = "events";
fn string_pretty(&self) -> String {
format!(
"[{}]",
self.0
.iter()
.map(Event::string_pretty)
.collect::<Vec<_>>()
.join(", ")
)
}
}
Comment thread
franciszekjob marked this conversation as resolved.

impl NodeDisplay for Gas {
const TAG: &'static str = "L2 gas";
fn string_pretty(&self) -> String {
Expand All @@ -102,3 +116,21 @@ fn string_hex(data: impl Into<Felt>) -> String {
fn string_debug(data: impl Debug) -> String {
format!("{data:?}")
}

impl Event {
fn string_pretty(&self) -> String {
format!(
"Event {{ keys: [{}], data: [{}] }}",
self.keys
.iter()
.map(Felt::to_hex_string)
.collect::<Vec<_>>()
.join(", "),
self.data
.iter()
.map(Felt::to_hex_string)
.collect::<Vec<_>>()
.join(", "),
)
}
}
4 changes: 4 additions & 0 deletions crates/forge-runner/src/debugging/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub enum Component {
CallType,
/// The result of the call, transformed for display.
CallResult,
/// The raw events emitted by the call.
Events,
/// The L2 gas used by the call.
Gas,
}
Expand All @@ -33,6 +35,7 @@ impl Component {
| Component::CallerAddress
| Component::EntryPointType
| Component::CallType
| Component::Events
| Component::Gas => TraceVerbosity::Detailed,
}
}
Expand All @@ -48,6 +51,7 @@ impl From<&Component> for debugging::Component {
Component::CallerAddress => debugging::Component::CallerAddress,
Component::CallType => debugging::Component::CallType,
Component::CallResult => debugging::Component::CallResult,
Component::Events => debugging::Component::Events,
Component::Gas => debugging::Component::Gas,
}
}
Expand Down
18 changes: 15 additions & 3 deletions crates/forge/tests/data/debugging/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct RecursiveCall {

#[starknet::interface]
trait RecursiveCaller<T> {
fn execute_calls(self: @T, calls: Array<RecursiveCall>) -> Array<RecursiveCall>;
fn execute_calls(ref self: T, calls: Array<RecursiveCall>) -> Array<RecursiveCall>;
}

#[starknet::interface]
Expand All @@ -20,7 +20,7 @@ trait Failing<TContractState> {
mod SimpleContract {
use core::array::ArrayTrait;
use core::traits::Into;
use starknet::{ContractAddress, get_contract_address};
use starknet::ContractAddress;
use super::{
Failing, RecursiveCall, RecursiveCaller, RecursiveCallerDispatcher,
RecursiveCallerDispatcherTrait,
Expand All @@ -30,12 +30,24 @@ mod SimpleContract {
#[storage]
struct Storage {}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
CallsExecuted: CallsExecuted,
}

#[derive(Drop, starknet::Event)]
struct CallsExecuted {
calls_len: felt252,
}

#[abi(embed_v0)]
impl RecursiveCallerImpl of RecursiveCaller<ContractState> {
fn execute_calls(
self: @ContractState, calls: Array<RecursiveCall>,
ref self: ContractState, calls: Array<RecursiveCall>,
) -> Array<RecursiveCall> {
self.emit(Event::CallsExecuted(CallsExecuted { calls_len: calls.len().into() }));

let mut i = 0;
#[cairofmt::skip]
while i < calls.len() {
Expand Down
10 changes: 10 additions & 0 deletions crates/forge/tests/data/debugging_events/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "debugging_events"
version = "0.1.0"
edition = "2023_01"

[dependencies]
starknet = ">=2.8.0"

[dev-dependencies]
snforge_std = { path = "../../../../../snforge_std" }
37 changes: 37 additions & 0 deletions crates/forge/tests/data/debugging_events/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#[starknet::interface]
trait IEventsChecker<TContractState> {
fn emit_event(ref self: TContractState, value: felt252);
fn emit_two_events(ref self: TContractState, first_value: felt252, second_value: felt252);
fn do_not_emit(self: @TContractState);
}

#[starknet::contract]
mod EventsChecker {
#[storage]
struct Storage {}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
ValueEmitted: ValueEmitted,
}

#[derive(Drop, starknet::Event)]
struct ValueEmitted {
value: felt252,
}

#[abi(embed_v0)]
impl IEventsCheckerImpl of super::IEventsChecker<ContractState> {
fn emit_event(ref self: ContractState, value: felt252) {
self.emit(Event::ValueEmitted(ValueEmitted { value }));
}

fn emit_two_events(ref self: ContractState, first_value: felt252, second_value: felt252) {
self.emit(Event::ValueEmitted(ValueEmitted { value: first_value }));
self.emit(Event::ValueEmitted(ValueEmitted { value: second_value }));
}

fn do_not_emit(self: @ContractState) {}
}
}
27 changes: 27 additions & 0 deletions crates/forge/tests/data/debugging_events/tests/test_trace.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use debugging_events::{IEventsCheckerDispatcher, IEventsCheckerDispatcherTrait};
use snforge_std::cheatcodes::contract_class::DeclareResultTrait;
use snforge_std::{ContractClassTrait, declare};

#[test]
fn test_debugging_trace_events_component() {
let contract_class = declare("EventsChecker").unwrap().contract_class();
let (contract_address, _) = contract_class.deploy(@array![]).unwrap();

IEventsCheckerDispatcher { contract_address }.emit_event(42);
}

#[test]
fn test_debugging_trace_multiple_events() {
let contract_class = declare("EventsChecker").unwrap().contract_class();
let (contract_address, _) = contract_class.deploy(@array![]).unwrap();

IEventsCheckerDispatcher { contract_address }.emit_two_events(42, 43);
}

#[test]
fn test_debugging_trace_eventless_success() {
let contract_class = declare("EventsChecker").unwrap().contract_class();
let (contract_address, _) = contract_class.deploy(@array![]).unwrap();

IEventsCheckerDispatcher { contract_address }.do_not_emit();
}
Loading
Loading