Skip to content

Commit

Permalink
add failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Aug 20, 2024
1 parent 21abd64 commit 8e85c06
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 17 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ version = "0.1.0"
edition = "2021"

[dependencies]
cairo-lang-sierra = "2.7.0"
cairo-lang-utils = "2.7.0"
clap = { version = "4.5.11", features = ["derive"] }
cairo-lang-sierra = "2.7.1"
cairo-lang-utils = "2.7.1"
clap = { version = "4.5.16", features = ["derive"] }
k256 = "0.13.3"
keccak = "0.1.5"
num-bigint = "0.4.6"
p256 = "0.13.2"
sec1 = { version = "0.7.3", features = ["std"] }
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.121"
serde = { version = "1.0.208", features = ["derive"] }
serde_json = "1.0.125"
sha2 = { version = "0.10.8", features = ["compress"] }
smallvec = "1.13.2"
starknet-crypto = "0.7.1"
Expand Down
114 changes: 114 additions & 0 deletions programs/syscalls.cairo
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());
}
22 changes: 22 additions & 0 deletions src/lib.rs
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))
}
14 changes: 3 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ mod test {
use cairo_lang_compiler::CompilerConfig;
use cairo_lang_sierra::program::{GenFunction, Program, StatementIdx};
use cairo_lang_starknet::compile::compile_path;
use sierra_emu::{ContractExecutionResult, ProgramTrace, StateDump, VirtualMachine};
use sierra_emu::{
find_entry_point_by_idx, ContractExecutionResult, ProgramTrace, StateDump, VirtualMachine,
};

#[test]
fn test_contract() {
Expand Down Expand Up @@ -181,14 +183,4 @@ mod test {
// let trace_str = serde_json::to_string_pretty(&trace).unwrap();
// std::fs::write("contract_trace.json", trace_str).unwrap();
}

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)
}
}
6 changes: 5 additions & 1 deletion src/vm/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,11 @@ fn eval_get_execution_info_v2(
match result {
Ok(res) => EvalAction::NormalBranch(
0,
smallvec![Value::U128(gas), system, res.into_value(felt_ty, out_ty_id.clone())],
smallvec![
Value::U128(gas),
system,
res.into_value(felt_ty, out_ty_id.clone())
],
),
Err(e) => EvalAction::NormalBranch(
1,
Expand Down
62 changes: 62 additions & 0 deletions tests/syscalls.rs
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))
}

0 comments on commit 8e85c06

Please sign in to comment.