-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
211 additions
and
17 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
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,114 @@ | ||
use core::starknet::{ | ||
call_contract_syscall, class_hash_const, contract_address_const, ContractAddress, | ||
deploy_syscall, emit_event_syscall, ExecutionInfo, get_block_hash_syscall, | ||
keccak_syscall, | ||
library_call_syscall, replace_class_syscall, send_message_to_l1_syscall, | ||
storage_address_try_from_felt252, storage_read_syscall, storage_write_syscall, SyscallResult, | ||
testing::cheatcode, | ||
}; | ||
use core::starknet::syscalls::get_execution_info_syscall; | ||
use core::starknet::syscalls::get_execution_info_v2_syscall; | ||
|
||
fn get_block_hash() -> SyscallResult<felt252> { | ||
get_block_hash_syscall(0) | ||
} | ||
|
||
fn get_execution_info() -> SyscallResult<Box<core::starknet::info::ExecutionInfo>> { | ||
get_execution_info_syscall() | ||
} | ||
|
||
fn get_execution_info_v2() -> SyscallResult<Box<core::starknet::info::v2::ExecutionInfo>> { | ||
get_execution_info_v2_syscall() | ||
} | ||
|
||
fn deploy() -> SyscallResult<(ContractAddress, Span<felt252>)> { | ||
deploy_syscall(class_hash_const::<0>(), 0, array![].span(), false) | ||
} | ||
|
||
fn replace_class() -> SyscallResult<()> { | ||
replace_class_syscall(class_hash_const::<0>()) | ||
} | ||
|
||
fn library_call() -> SyscallResult<Span<felt252>> { | ||
library_call_syscall(class_hash_const::<0>(), 0, array![].span()) | ||
} | ||
|
||
fn call_contract() -> SyscallResult<Span<felt252>> { | ||
call_contract_syscall(contract_address_const::<0>(), 0, array![].span()) | ||
} | ||
|
||
fn storage_read() -> felt252 { | ||
storage_read_syscall(0, storage_address_try_from_felt252(0).unwrap()).unwrap() | ||
} | ||
|
||
fn storage_write() { | ||
storage_write_syscall(0, storage_address_try_from_felt252(0).unwrap(), 0).unwrap() | ||
} | ||
|
||
fn emit_event() -> SyscallResult<()> { | ||
emit_event_syscall(array![].span(), array![].span()) | ||
} | ||
|
||
fn send_message_to_l1() -> SyscallResult<()> { | ||
send_message_to_l1_syscall(3, array![2].span()) | ||
} | ||
|
||
fn keccak() -> SyscallResult<u256> { | ||
keccak_syscall(array![].span()) | ||
} | ||
|
||
fn set_sequencer_address(address: felt252) -> Span<felt252> { | ||
return cheatcode::<'set_sequencer_address'>(array![address].span()); | ||
} | ||
|
||
fn set_account_contract_address(address: felt252) -> Span<felt252> { | ||
return cheatcode::<'set_account_contract_address'>(array![address].span()); | ||
} | ||
|
||
fn set_block_number(number: felt252) -> Span<felt252> { | ||
return cheatcode::<'set_block_number'>(array![number].span()); | ||
} | ||
|
||
fn set_block_timestamp(timestamp: felt252) -> Span<felt252> { | ||
return cheatcode::<'set_block_timestamp'>(array![timestamp].span()); | ||
} | ||
|
||
fn set_caller_address(address: felt252) -> Span<felt252> { | ||
return cheatcode::<'set_caller_address'>(array![address].span()); | ||
} | ||
|
||
fn set_chain_id(id: felt252) -> Span<felt252> { | ||
return cheatcode::<'set_chain_id'>(array![id].span()); | ||
} | ||
|
||
fn set_contract_address(address: felt252) -> Span<felt252> { | ||
return cheatcode::<'set_contract_address'>(array![address].span()); | ||
} | ||
|
||
fn set_max_fee(fee: felt252) -> Span<felt252> { | ||
return cheatcode::<'set_max_fee'>(array![fee].span()); | ||
} | ||
|
||
fn set_nonce(nonce: felt252) -> Span<felt252> { | ||
return cheatcode::<'set_nonce'>(array![nonce].span()); | ||
} | ||
|
||
fn set_signature(signature: Array<felt252>) -> Span<felt252> { | ||
return cheatcode::<'set_signature'>(signature.span()); | ||
} | ||
|
||
fn set_transaction_hash(hash: felt252) -> Span<felt252> { | ||
return cheatcode::<'set_transaction_hash'>(array![hash].span()); | ||
} | ||
|
||
fn set_version(version: felt252) -> Span<felt252> { | ||
return cheatcode::<'set_version'>(array![version].span()); | ||
} | ||
|
||
fn pop_log(log: felt252) -> Span<felt252> { | ||
return cheatcode::<'pop_log'>(array![log].span()); | ||
} | ||
|
||
fn pop_l2_to_l1_message(message: felt252) -> Span<felt252> { | ||
return cheatcode::<'pop_l2_to_l1_message'>(array![message].span()); | ||
} |
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,6 +1,28 @@ | ||
use cairo_lang_sierra::program::{GenFunction, Program, StatementIdx}; | ||
|
||
pub use self::{dump::*, value::*, vm::VirtualMachine}; | ||
|
||
mod dump; | ||
pub mod starknet; | ||
mod value; | ||
mod vm; | ||
|
||
pub fn find_entry_point_by_idx( | ||
program: &Program, | ||
entry_point_idx: usize, | ||
) -> Option<&GenFunction<StatementIdx>> { | ||
program | ||
.funcs | ||
.iter() | ||
.find(|x| x.id.id == entry_point_idx as u64) | ||
} | ||
|
||
pub fn find_entry_point_by_name<'a>( | ||
program: &'a Program, | ||
name: &str, | ||
) -> Option<&'a GenFunction<StatementIdx>> { | ||
program | ||
.funcs | ||
.iter() | ||
.find(|x| x.id.debug_name.as_ref().map(|x| x.as_str()) == Some(name)) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::{path::Path, sync::Arc}; | ||
|
||
use cairo_lang_compiler::{compile_cairo_project_at_path, CompilerConfig}; | ||
use cairo_lang_sierra::program::{GenFunction, Program, StatementIdx}; | ||
use sierra_emu::{ProgramTrace, StateDump, VirtualMachine}; | ||
|
||
fn run_syscall(func_name: &str) -> ProgramTrace { | ||
let path = Path::new("programs/syscalls.cairo"); | ||
|
||
let sierra_program = Arc::new( | ||
compile_cairo_project_at_path( | ||
path, | ||
CompilerConfig { | ||
replace_ids: true, | ||
..Default::default() | ||
}, | ||
) | ||
.unwrap(), | ||
); | ||
|
||
let function = find_entry_point_by_name(&sierra_program, func_name).unwrap(); | ||
|
||
let mut vm = VirtualMachine::new(sierra_program.clone()); | ||
|
||
let calldata = []; | ||
let initial_gas = 1000000; | ||
|
||
vm.call_contract(function, initial_gas, calldata); | ||
|
||
let mut trace = ProgramTrace::new(); | ||
|
||
while let Some((statement_idx, state)) = vm.step() { | ||
trace.push(StateDump::new(statement_idx, state)); | ||
} | ||
|
||
trace | ||
} | ||
|
||
#[test] | ||
fn test_contract_constructor() { | ||
let mut trace = run_syscall("syscalls::syscalls::get_execution_info_v2"); | ||
} | ||
|
||
pub fn find_entry_point_by_idx( | ||
program: &Program, | ||
entry_point_idx: usize, | ||
) -> Option<&GenFunction<StatementIdx>> { | ||
program | ||
.funcs | ||
.iter() | ||
.find(|x| x.id.id == entry_point_idx as u64) | ||
} | ||
|
||
pub fn find_entry_point_by_name<'a>( | ||
program: &'a Program, | ||
name: &str, | ||
) -> Option<&'a GenFunction<StatementIdx>> { | ||
program | ||
.funcs | ||
.iter() | ||
.find(|x| x.id.debug_name.as_ref().map(|x| x.as_str()) == Some(name)) | ||
} |