Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Oct 2, 2024
1 parent a373980 commit e06915b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 61 deletions.
50 changes: 22 additions & 28 deletions crates/evm/src/backend/starknet_backend.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,12 @@ mod tests {
};
use crate::test_utils::{evm_address};
use snforge_std::{
test_address, start_mock_call, get_class_hash, spy_events, EventSpyAssertionsTrait
test_address, start_mock_call, get_class_hash, spy_events, EventSpyTrait,
Event as StarknetEvent
};
use snforge_utils::snforge_utils::{
assert_not_called, assert_called, EventsFilterBuilderTrait, ContractEventsTrait
};
use snforge_utils::snforge_utils::{assert_not_called, assert_called};
use super::{commit_storage, emit_events};
use utils::helpers::compute_starknet_address;
use utils::traits::bytes::U8SpanExTrait;
Expand All @@ -276,24 +279,13 @@ mod tests {
}
}

// Helper function to serialize events into mock events
fn serialize_mock_events(
events: Array<Event>, ref mock_events: Array<(ContractAddress, MockEvent)>
) {
let contract_address = test_address();

// Serialize and append each event
for event in events {
let mut serialized_keys = Default::default();
let mut serialized_data = Default::default();

Serde::<Array<u256>>::serialize(@event.keys, ref serialized_keys);
Serde::<Array<u8>>::serialize(@event.data, ref serialized_data);

mock_events
.append(
(contract_address, MockEvent { keys: serialized_keys, data: serialized_data })
);
impl EventIntoStarknetEvent of Into<Event, StarknetEvent> {
fn into(self: Event) -> StarknetEvent {
let mut serialized_keys = array![];
let mut serialized_data = array![];
Serde::<Array<u256>>::serialize(@self.keys, ref serialized_keys);
Serde::<Array<u8>>::serialize(@self.data, ref serialized_data);
StarknetEvent { keys: serialized_keys, data: serialized_data }
}
}

Expand Down Expand Up @@ -506,10 +498,9 @@ mod tests {
#[test]
fn test_emit_events() {
let mut state: State = Default::default();
let mut mock_events = ArrayTrait::<(ContractAddress, MockEvent)>::new();

// Prepare events
let events = array![
let evm_events = array![
Event { keys: array![], data: array![] }, // Empty event
Event { keys: array![1.into()], data: array![2, 3] }, // Single key, multiple data
Event {
Expand All @@ -520,19 +511,23 @@ mod tests {
} // Multiple keys and data
];

// Serialize events and store in mock_events
serialize_mock_events(events.clone(), ref mock_events);

// Add events to state
for event in events {
for event in evm_events.clone() {
state.add_event(event);
};

// Emit and assert events
let mut spy = spy_events();
emit_events(ref state).expect('emit events failed');
assert!(state.events.is_empty());
spy.assert_emitted(@mock_events);
let contract_events = EventsFilterBuilderTrait::from_events(@spy.get_events())
.with_contract_address(test_address())
.build();

for event in evm_events {
let starknet_event = EventIntoStarknetEvent::into(event);
contract_events.assert_emitted(@starknet_event);
};
}
}
// #[test]
Expand Down Expand Up @@ -569,4 +564,3 @@ mod tests {
// }
// }


21 changes: 0 additions & 21 deletions crates/evm/src/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -223,27 +223,6 @@ pub struct Event {
pub data: Array<u8>,
}

/// Represents a mock event used for assertions in tests.
#[derive(Drop, Clone, Default, PartialEq)]
pub struct MockEvent {
/// Serialized keys associated with the event.
pub keys: Array<felt252>,
/// Serialized data associated with the event.
pub data: Array<felt252>,
}

/// Implementation of the EventTrait for testing purposes using MockEvent.
/// This is used to simulate event handling, specifically appending keys and data,
impl EventTraitImpl of starknet::Event<MockEvent> {
fn append_keys_and_data(self: @MockEvent, ref keys: Array<felt252>, ref data: Array<felt252>) {
keys.append_span(self.keys.span());
data.append_span(self.data.span());
}
fn deserialize(ref keys: Span<felt252>, ref data: Span<felt252>) -> Option<MockEvent> {
Option::None
}
}

/// Represents an address in both EVM and Starknet formats.
#[derive(Copy, Drop, PartialEq, Default, Debug)]
pub struct Address {
Expand Down
24 changes: 12 additions & 12 deletions crates/snforge_utils/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub mod snforge_utils {
/// A wrapper structure on an array of events emitted by a given contract.
#[derive(Drop, Clone)]
pub struct ContractEvents {
pub events: Array<Event>
pub events: Span<Event>
}

pub trait EventsFilterTrait {
Expand Down Expand Up @@ -246,33 +246,33 @@ pub mod snforge_utils {
}
};

ContractEvents { events: filtered_events }
ContractEvents { events: filtered_events.span() }
}
}

pub trait ContractEventsTrait {
fn assert_emitted<T, impl TEvent: starknet::Event<T>, impl TDrop: Drop<T>>(
self: ContractEvents, event: @T
self: @ContractEvents, event: @T
);
fn assert_not_emitted<T, impl TEvent: starknet::Event<T>, impl TDrop: Drop<T>>(
self: ContractEvents, event: @T
self: @ContractEvents, event: @T
);
}

impl ContractEventsTraitImpl of ContractEventsTrait {
fn assert_emitted<T, impl TEvent: starknet::Event<T>, impl TDrop: Drop<T>>(
self: ContractEvents, event: @T
self: @ContractEvents, event: @T
) {
let mut expected_keys = array![];
let mut expected_data = array![];
event.append_keys_and_data(ref expected_keys, ref expected_data);

let contract_events = (*self.events);
let mut found = false;
for i in 0
..self
.events
..contract_events
.len() {
let event = self.events.at(i);
let event = contract_events.at(i);
if event.keys == @expected_keys && event.data == @expected_data {
found = true;
break;
Expand All @@ -283,17 +283,17 @@ pub mod snforge_utils {
}

fn assert_not_emitted<T, impl TEvent: starknet::Event<T>, impl TDrop: Drop<T>>(
self: ContractEvents, event: @T
self: @ContractEvents, event: @T
) {
let mut expected_keys = array![];
let mut expected_data = array![];
event.append_keys_and_data(ref expected_keys, ref expected_data);

let contract_events = (*self.events);
for i in 0
..self
.events
..contract_events
.len() {
let event = self.events.at(i);
let event = contract_events.at(i);
assert(
event.keys != @expected_keys || event.data != @expected_data,
'Unexpected event was emitted'
Expand Down

0 comments on commit e06915b

Please sign in to comment.