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

dev: test emit_events #1003

Merged
merged 7 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
63 changes: 59 additions & 4 deletions crates/evm/src/backend/starknet_backend.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,20 @@ fn commit_storage(ref self: State) -> Result<(), EVMError> {

#[cfg(test)]
mod tests {
use core::starknet::ClassHash;
use core::starknet::{ClassHash, ContractAddress};
use crate::backend::starknet_backend;
use crate::model::Address;
use crate::model::account::Account;
use crate::model::{Address, Event, MockEvent};
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_std::{
test_address, start_mock_call, get_class_hash, spy_events, EventSpyAssertionsTrait
};
use snforge_utils::snforge_utils::{assert_not_called, assert_called};
use super::commit_storage;
use super::{commit_storage, emit_events};
use utils::helpers::compute_starknet_address;
use utils::traits::bytes::U8SpanExTrait;

Expand All @@ -274,6 +276,27 @@ 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 })
);
}
}
DiegoB1911 marked this conversation as resolved.
Show resolved Hide resolved

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 +502,38 @@ mod tests {
assert_called(starknet_address, selector!("set_code_hash"));
assert_called(starknet_address, selector!("set_nonce"));
}

#[test]
fn test_emit_events() {
let mut state: State = Default::default();
let mut mock_events = ArrayTrait::<(ContractAddress, MockEvent)>::new();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut mock_events = ArrayTrait::<(ContractAddress, MockEvent)>::new();
let mut mock_events = ArrayTrait::<(ContractAddress, MockEvent)>::new();

no longer required


// Prepare events
let 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
];

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer the implementation of Into, see main message (but same underlying idea)


// Add events to state
for event in events {
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);
}
}
// #[test]
// #[ignore]
Expand Down
21 changes: 21 additions & 0 deletions crates/evm/src/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,27 @@ 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
}
}

DiegoB1911 marked this conversation as resolved.
Show resolved Hide resolved
/// Represents an address in both EVM and Starknet formats.
#[derive(Copy, Drop, PartialEq, Default, Debug)]
pub struct Address {
Expand Down
Loading