Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
dev: test emit_events (#1003)
Browse files Browse the repository at this point in the history
* creating test case for emit events

* fixing format

* side stuff

* Update crates/evm/src/model.cairo

Co-authored-by: Mathieu <60658558+enitrat@users.noreply.github.com>

* Update crates/evm/src/backend/starknet_backend.cairo

Co-authored-by: Mathieu <60658558+enitrat@users.noreply.github.com>

* changes for implementation of  into trait

---------

Co-authored-by: enitrat <msaug@protonmail.com>
Co-authored-by: Mathieu <60658558+enitrat@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 3, 2024
1 parent fe6735d commit f0013f7
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 17 deletions.
68 changes: 63 additions & 5 deletions crates/evm/src/backend/starknet_backend.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,23 @@ fn commit_storage(ref self: State) -> Result<(), EVMError> {

#[cfg(test)]
mod tests {
use core::starknet::ClassHash;
use core::starknet::{ClassHash};
use crate::backend::starknet_backend;
use crate::model::Address;
use crate::model::account::Account;
use crate::model::{Address, Event};
use crate::state::{State, StateTrait};
use crate::test_utils::{
setup_test_environment, uninitialized_account, account_contract, register_account
};
use crate::test_utils::{evm_address};
use snforge_std::{test_address, start_mock_call, get_class_hash};
use snforge_utils::snforge_utils::{assert_not_called, assert_called};
use super::commit_storage;
use snforge_std::{
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 super::{commit_storage, emit_events};
use utils::helpers::compute_starknet_address;
use utils::traits::bytes::U8SpanExTrait;

Expand All @@ -274,6 +279,18 @@ mod tests {
}
}

// Implementation to convert an `Event` into a serialized `StarknetEvent`
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 }
}
}


mod test_commit_storage {
use snforge_std::start_mock_call;
use snforge_utils::snforge_utils::{assert_called_with, assert_not_called};
Expand Down Expand Up @@ -479,6 +496,47 @@ mod tests {
assert_called(starknet_address, selector!("set_code_hash"));
assert_called(starknet_address, selector!("set_nonce"));
}

#[test]
fn test_emit_events() {
// Initialize the state
let mut state: State = Default::default();

// Prepare a list of events with different combinations of keys and data
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 {
keys: array![4.into(), 5.into()], data: array![6]
}, // Multiple keys, single data
Event {
keys: array![7.into(), 8.into(), 9.into()], data: array![10, 11, 12, 13]
} // Multiple keys and data
];

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

// Emit the events and assert that no events are left in the state
let mut spy = spy_events();
emit_events(ref state).expect('emit events failed');
assert!(state.events.is_empty());

// Capture emitted events
let contract_events = EventsFilterBuilderTrait::from_events(@spy.get_events())
.with_contract_address(test_address())
.build();

// Assert that each original event was emitted as expected
for event in evm_events {
let starknet_event = EventIntoStarknetEvent::into(
event
); // Convert to StarkNet event format
contract_events.assert_emitted(@starknet_event);
};
}
}
// #[test]
// #[ignore]
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 f0013f7

Please sign in to comment.