Skip to content

Commit

Permalink
Refactor event logic (foundry-rs#2251)
Browse files Browse the repository at this point in the history
<!-- Reference any GitHub issues resolved by this PR -->

Closes foundry-rs#1889 

## Introduced changes

<!-- A brief description of the changes -->

- revamp event logic

Original stack here:
- foundry-rs#2193 
- foundry-rs#2194 
- foundry-rs#2195
- foundry-rs#2224 

## Checklist

<!-- Make sure all of these are complete -->

- [ ] Linked relevant issue
- [ ] Updated relevant documentation
- [ ] Added relevant tests
- [ ] Performed self-review of the code
- [ ] Added changes to `CHANGELOG.md`
  • Loading branch information
tomek0123456789 authored Jun 24, 2024
1 parent 2200ebd commit 18f32ec
Show file tree
Hide file tree
Showing 14 changed files with 360 additions and 415 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
#### Changed

- `SyscallResultStringErrorTrait::map_error_to_string` removed in favor of utility function (`snforge_std::byte_array::try_deserialize_bytearray_error`)
- Updated event testing - read more [here](./docs/src/testing/testing-events.md) on how it now works and [here](./docs/src/appendix/cheatcodes/spy_events.md)
about updated `spy_events` cheatcode

### Cast

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@ pub fn emit_event_hook(
) {
let contract_address = syscall_handler.contract_address();
let last_event = syscall_handler.last_event();
let is_spied_on = cheatnet_state
.spies
.iter()
.any(|spy_on| spy_on.does_spy(contract_address));

if is_spied_on {
cheatnet_state
.detected_events
.push(Event::from_ordered_event(last_event, contract_address));
}
cheatnet_state
.detected_events
.push(Event::from_ordered_event(last_event, contract_address));
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::CheatnetState;
use blockifier::execution::call_info::OrderedEvent;
use cairo_felt::Felt252;
use cairo_vm::hint_processor::hint_processor_utils::felt_to_usize;
use conversions::{
serde::{deserialize::CairoDeserialize, serialize::CairoSerialize},
FromConv,
};
use starknet_api::core::ContractAddress;
use std::mem::take;

/// Represents an emitted event. It is used in the `CheatnetState` to keep track of events
/// emitted in the `cheatnet::src::rpc::call_contract`
Expand Down Expand Up @@ -63,29 +61,7 @@ impl SpyTarget {
}

impl CheatnetState {
pub fn spy_events(&mut self, spy_on: SpyTarget) -> usize {
self.spies.push(spy_on);
self.spies.len() - 1
}

pub fn fetch_events(&mut self, id: &Felt252) -> Vec<Event> {
let spy_on = &self.spies[felt_to_usize(id).unwrap()];

// replace with empty to get ownership
let emitted_events = take(&mut self.detected_events);

emitted_events
.into_iter()
.filter_map(|event| {
if spy_on.does_spy(event.from) {
Some(event)
} else {
// push back unconsumed ones
self.detected_events.push(event);

None
}
})
.collect()
pub fn get_events(&mut self, event_offset: usize) -> Vec<Event> {
self.detected_events[event_offset..].to_vec()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,24 +273,23 @@ impl<'a> ExtensionLogic for ForgeExtension<'a> {
Ok(CheatcodeHandlingResult::Handled(parsed_content))
}
"spy_events" => {
let spy_on = input_reader.read()?;

let id = extended_runtime
let events_offset = extended_runtime
.extended_runtime
.extension
.cheatnet_state
.spy_events(spy_on);
.detected_events
.len();

Ok(CheatcodeHandlingResult::from_serializable(id))
Ok(CheatcodeHandlingResult::from_serializable(events_offset))
}
"fetch_events" => {
let id = input_reader.read()?;
"get_events" => {
let events_offset = input_reader.read()?;

let events = extended_runtime
.extended_runtime
.extension
.cheatnet_state
.fetch_events(&id);
.get_events(events_offset);

Ok(CheatcodeHandlingResult::from_serializable(events))
}
Expand Down
6 changes: 1 addition & 5 deletions crates/cheatnet/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use crate::runtime_extensions::call_to_blockifier_runtime_extension::rpc::CallRe
use crate::runtime_extensions::forge_runtime_extension::cheatcodes::cheat_execution_info::{
ExecutionInfoMock, ResourceBounds,
};
use crate::runtime_extensions::forge_runtime_extension::cheatcodes::spy_events::{
Event, SpyTarget,
};
use crate::runtime_extensions::forge_runtime_extension::cheatcodes::spy_events::Event;
use blockifier::blockifier::block::BlockInfo;
use blockifier::execution::call_info::OrderedL2ToL1Message;
use blockifier::execution::entry_point::CallEntryPoint;
Expand Down Expand Up @@ -320,7 +318,6 @@ pub struct CheatnetState {
pub mocked_functions:
HashMap<ContractAddress, HashMap<EntryPointSelector, CheatStatus<Vec<StarkFelt>>>>,
pub replaced_bytecode_contracts: HashMap<ContractAddress, ClassHash>,
pub spies: Vec<SpyTarget>,
pub detected_events: Vec<Event>,
pub deploy_salt_base: u32,
pub block_info: BlockInfo,
Expand All @@ -341,7 +338,6 @@ impl Default for CheatnetState {
global_cheated_execution_info: Default::default(),
mocked_functions: Default::default(),
replaced_bytecode_contracts: Default::default(),
spies: vec![],
detected_events: vec![],
deploy_salt_base: 0,
block_info: SerializableBlockInfo::default().into(),
Expand Down
7 changes: 2 additions & 5 deletions crates/cheatnet/tests/cheatcodes/cheat_caller_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use crate::common::get_contracts;
use cairo_felt::Felt252;
use cairo_lang_starknet_classes::keccak::starknet_keccak;
use cheatnet::constants::TEST_ADDRESS;
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::spy_events::{
Event, SpyTarget,
};
use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::spy_events::Event;
use cheatnet::state::CheatSpan;
use conversions::IntoConv;
use starknet_api::core::{ContractAddress, PatriciaKey};
Expand Down Expand Up @@ -306,7 +304,6 @@ fn cheat_caller_address_cairo0_callback() {
let contract_address = test_env.deploy("Cairo1Contract_v1", &[]);

test_env.start_cheat_caller_address(contract_address, 123);
let id = test_env.cheatnet_state.spy_events(SpyTarget::All);

let expected_caller_address = Felt252::from(123);

Expand All @@ -326,7 +323,7 @@ fn cheat_caller_address_cairo0_callback() {
&[],
);

let events = test_env.cheatnet_state.fetch_events(&Felt252::from(id));
let events = test_env.cheatnet_state.get_events(0);

// make sure end() was called by cairo0 contract
assert_eq!(
Expand Down
Loading

0 comments on commit 18f32ec

Please sign in to comment.