-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix collecting events in cairo0 (#1836)
<!-- Reference any GitHub issues resolved by this PR --> Closes #1633 ## Introduced changes <!-- A brief description of the changes --> - Fixes events collecting in cairo0 ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [x] Updated relevant documentation - [x] Added relevant tests - [x] Performed self-review of the code - [x] Added changes to `CHANGELOG.md`
- Loading branch information
Showing
9 changed files
with
304 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 39 additions & 14 deletions
53
...et/src/runtime_extensions/call_to_blockifier_runtime_extension/execution/syscall_hooks.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,50 @@ | ||
use blockifier::execution::syscalls::hint_processor::SyscallHintProcessor; | ||
|
||
use crate::{ | ||
runtime_extensions::forge_runtime_extension::cheatcodes::spy_events::Event, | ||
state::CheatnetState, | ||
}; | ||
use blockifier::execution::{ | ||
call_info::OrderedEvent, deprecated_syscalls::hint_processor::DeprecatedSyscallHintProcessor, | ||
syscalls::hint_processor::SyscallHintProcessor, | ||
}; | ||
use starknet_api::core::ContractAddress; | ||
|
||
pub trait SyscallHintProcessorExt { | ||
fn contract_address(&self) -> ContractAddress; | ||
fn last_event(&self) -> &OrderedEvent; | ||
} | ||
|
||
impl SyscallHintProcessorExt for SyscallHintProcessor<'_> { | ||
fn contract_address(&self) -> ContractAddress { | ||
self.call.code_address.unwrap_or(self.call.storage_address) | ||
} | ||
fn last_event(&self) -> &OrderedEvent { | ||
self.events.last().unwrap() | ||
} | ||
} | ||
|
||
impl SyscallHintProcessorExt for DeprecatedSyscallHintProcessor<'_> { | ||
fn contract_address(&self) -> ContractAddress { | ||
self.storage_address | ||
} | ||
fn last_event(&self) -> &OrderedEvent { | ||
self.events.last().unwrap() | ||
} | ||
} | ||
|
||
pub fn emit_event_hook( | ||
syscall_handler: &SyscallHintProcessor<'_>, | ||
syscall_handler: &impl SyscallHintProcessorExt, | ||
cheatnet_state: &mut CheatnetState, | ||
) { | ||
let contract_address = syscall_handler | ||
.call | ||
.code_address | ||
.unwrap_or(syscall_handler.call.storage_address); | ||
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)); | ||
|
||
for spy_on in &cheatnet_state.spies { | ||
if spy_on.does_spy(contract_address) { | ||
let event = | ||
Event::from_ordered_event(syscall_handler.events.last().unwrap(), contract_address); | ||
cheatnet_state.detected_events.push(event); | ||
break; | ||
} | ||
if is_spied_on { | ||
cheatnet_state | ||
.detected_events | ||
.push(Event::from_ordered_event(last_event, contract_address)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
crates/cheatnet/tests/contracts/src/events/spy_events_cairo0.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use starknet::ContractAddress; | ||
|
||
// https://testnet.starkscan.co/contract/0x1960625ba5c435bac113ecd15af3c60e327d550fc5dbb43f07cd0875ad2f54c | ||
#[starknet::interface] | ||
trait ICairo0Contract<TContractState> { | ||
// this function only job is to emit `my_event` with single felt252 value | ||
fn emit_one_cairo0_event(ref self: TContractState, contract_address: felt252); | ||
} | ||
|
||
#[starknet::interface] | ||
trait ISpyEventsCairo0<TContractState> { | ||
fn test_cairo0_event_collection(ref self: TContractState, cairo0_address: ContractAddress); | ||
} | ||
|
||
#[starknet::contract] | ||
mod SpyEventsCairo0 { | ||
use core::traits::Into; | ||
use starknet::{get_contract_address, get_caller_address, ContractAddress}; | ||
use super::ICairo0ContractDispatcherTrait; | ||
|
||
#[storage] | ||
struct Storage {} | ||
|
||
#[abi(embed_v0)] | ||
impl ISpyEventsCairo0 of super::ISpyEventsCairo0<ContractState> { | ||
fn test_cairo0_event_collection(ref self: ContractState, cairo0_address: ContractAddress) { | ||
let cairo0_contract = super::ICairo0ContractDispatcher { | ||
contract_address: cairo0_address | ||
}; | ||
|
||
cairo0_contract.emit_one_cairo0_event(123456789); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.