Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Test multi syscall #687

Merged
merged 52 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
aabbb8f
create multy syscall
Jun 23, 2023
173f547
remove the replace syscall, it failed because the contract adress did…
Jun 23, 2023
52b7504
added library call_syscall
Jun 26, 2023
ed6908c
wip
Jun 26, 2023
feaf519
wip
Jun 26, 2023
0b5c1d9
wip
Jun 26, 2023
adf7cd8
wip
Jun 29, 2023
fd2ad5a
work in progress
Jun 30, 2023
faf96a5
remove .sjon files from starknet_programs
Jun 30, 2023
82956c1
finished implemented all the syscalls
Jun 30, 2023
ecdf72e
reorder code, create one call to syscall
Jul 6, 2023
73b58e2
fix pull bug
Jul 6, 2023
2fd09ad
Update tests/multi_syscall_test.rs
fguthmann Jul 7, 2023
137cb90
Update starknet_programs/cairo1/multi_syscall_test.cairo
fguthmann Jul 7, 2023
2004102
Update starknet_programs/cairo1/contract_a.cairo
fguthmann Jul 7, 2023
fe2542f
Update tests/multi_syscall_test.rs
fguthmann Jul 7, 2023
74d0ce4
added test syscall for deploy
Jul 7, 2023
4a160ed
make format changes
Jul 7, 2023
14907ee
corrected make clippy error
Jul 7, 2023
507ac7b
get_caller_address and get_contract_address return a adress
Jul 10, 2023
080986a
failed of get_contract_address
Jul 10, 2023
c21a8df
failed of get_contract_address
Jul 10, 2023
5c4d786
wip
Jul 10, 2023
5024398
Merge branch 'main' into test_multi_syscall
Jul 10, 2023
5c816f8
modify the selector entrypoint_selector to be function specific
Jul 10, 2023
07ce801
Merge branch 'main' into test_multi_syscall
fguthmann Jul 10, 2023
122318a
Merge branch 'main' into test_multi_syscall
Jul 14, 2023
a9eb94e
wip
Jul 14, 2023
23fd56d
wip
SantiagoPittella Jul 14, 2023
a67bc1f
Merge branch 'main' into test_multi_syscall
Jul 24, 2023
40cf362
wip
Jul 25, 2023
a9da0b6
Merge branch 'main' into test_multi_syscall
Jul 25, 2023
670d685
add input to cairo functions
Jul 25, 2023
fb6b605
coorect format problem
Jul 25, 2023
3f953cc
Merge branch 'main' into test_multi_syscall
Jul 31, 2023
7ed6258
wip
Jul 31, 2023
0ee4019
wip
Jul 31, 2023
d9222a8
Merge branch 'main' into test_multi_syscall
Jul 31, 2023
942b3fd
wip
Jul 31, 2023
cc5c244
remove format problem
Aug 3, 2023
50dc978
merge main
Aug 3, 2023
15861f7
Merge branch 'main' into test_multi_syscall
fguthmann Aug 4, 2023
3353b84
Fix sierra class hash calculation (#886)
juanbono Aug 4, 2023
42c1d95
Fail with an Err transactions whose calculated fee exceed `max_fee` (…
xqft Aug 7, 2023
84e3a9b
Fix test_get_nonce_at (#910)
xqft Aug 9, 2023
fc956ea
fix get_sorted_events bug (#912)
juanbono Aug 9, 2023
25f961f
Added documentations to syscalls/deprecated_syscall_handler module (#…
fguthmann Aug 9, 2023
ce01355
Merge branch 'main' into test_multi_syscall
Aug 10, 2023
d0cfacc
wip
Aug 11, 2023
4066c29
Merge branch 'main' into test_multi_syscall
Aug 11, 2023
5e5e9ee
Modify the tests
Aug 11, 2023
0b82485
fixed clippy errors
Aug 11, 2023
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
1 change: 0 additions & 1 deletion starknet_programs/cairo1/contract_a.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#[contract]
mod ContractA {
use traits::Into;
use starknet::info::get_contract_address;
struct Storage {
value: u128,
}
Expand Down
97 changes: 97 additions & 0 deletions starknet_programs/cairo1/multi_syscall_test.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#[contract]
mod multy_syscall {
use starknet::get_caller_address;
use starknet::get_contract_address;
use starknet::get_execution_info_syscall;
use starknet::replace_class_syscall;
use starknet::library_call_syscall;
use starknet::call_contract_syscall;
use starknet::send_message_to_l1_syscall;
use starknet::storage_read_syscall;
use starknet::storage_write_syscall;
use starknet::deploy_syscall;

use starknet::storage_access::{StorageAddress, storage_address_try_from_felt252};
use starknet::contract_address::ContractAddress;
use array::{Array, ArrayTrait, Span, SpanTrait};
use result::ResultTrait;
use starknet::info::ExecutionInfo;
use option::OptionTrait;
use starknet::class_hash::ClassHash;
use traits::TryInto;
use starknet::class_hash::Felt252TryIntoClassHash;
use box::Box;
use traits::Into;
use box::BoxTrait;

#[external]
fn caller_address() -> ContractAddress {
get_caller_address()
}

#[external]
fn contract_address() -> ContractAddress {
get_contract_address()
}

#[external]
fn execution_info_syscall() -> (ContractAddress, ContractAddress) {
let return_data = get_execution_info_syscall().unwrap().unbox();
(return_data.caller_address, return_data.contract_address)
}


#[external]
fn test_library_call_syscall(class_hash: ClassHash, function_selector: felt252, number: felt252) -> felt252 {
let mut calldata = ArrayTrait::new();
calldata.append(number);
let return_data = library_call_syscall(class_hash, function_selector, calldata.span()).unwrap();
*return_data.get(0_usize).unwrap().unbox()
}

#[external]
fn test_call_contract_syscall(function_selector: felt252, number: felt252) -> felt252 {
let mut calldata = ArrayTrait::new();
calldata.append(number);
let return_data = call_contract_syscall(get_contract_address(), function_selector, calldata.span()).unwrap();
*return_data.get(0_usize).unwrap().unbox()

}

#[external]
fn test_send_message_to_l1(to_address: felt252, payload_0: felt252, payload_1: felt252) -> () {
let mut calldata = ArrayTrait::new();
calldata.append(payload_0);
calldata.append(payload_1);
let return_data = send_message_to_l1_syscall(to_address, calldata.span()).unwrap();
return_data
}

#[external]
fn read()-> felt252{
//write to storage
let address = storage_address_try_from_felt252(3534535754756246375475423547453).unwrap();
storage_write_syscall(0, address, 'Hello');

//read from storage
match storage_read_syscall(0, address) {
Result::Ok(value) => value,
Result::Err(revert_reason) => *revert_reason.span().at(0),
}
}

#[event]
fn EmitEvent(n: felt252){}

#[external]
fn trigger_events() {
EmitEvent(1);
EmitEvent(2);
EmitEvent(3);
}

#[external]
fn get_number(number: felt252) -> felt252 {
number
}
}
262 changes: 262 additions & 0 deletions tests/multi_syscall_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
use cairo_lang_starknet::casm_contract_class::CasmContractClass;
use cairo_vm::felt::Felt252;
use num_traits::{Num, Zero};
use starknet_in_rust::utils::calculate_sn_keccak;
use starknet_in_rust::EntryPointType;
use starknet_in_rust::{
definitions::{block_context::BlockContext, constants::TRANSACTION_VERSION},
execution::{
execution_entry_point::ExecutionEntryPoint, CallInfo, CallType, OrderedEvent,
OrderedL2ToL1Message, TransactionExecutionContext,
},
state::cached_state::CachedState,
state::{in_memory_state_reader::InMemoryStateReader, ExecutionResourcesManager},
utils::{Address, ClassHash},
};
use std::{collections::HashMap, sync::Arc, vec};

#[test]
fn test_multiple_syscall() {
// Create program and entry point types for contract class
let program_data = include_bytes!("../starknet_programs/cairo1/multi_syscall_test.casm");
let contract_class: CasmContractClass = serde_json::from_slice(program_data).unwrap();

// Create state reader with class hash data
let mut contract_class_cache: HashMap<[u8; 32], _> = HashMap::new();

let address = Address(1111.into());
let class_hash: ClassHash = [1; 32];
let nonce = Felt252::zero();

contract_class_cache.insert(class_hash, contract_class);
let mut state_reader = InMemoryStateReader::default();
state_reader
.address_to_class_hash_mut()
.insert(address.clone(), class_hash);
state_reader
.address_to_nonce_mut()
.insert(address.clone(), nonce);

// Create state from the state_reader and contract cache.
let mut state = CachedState::new(
Arc::new(state_reader),
None,
Some(contract_class_cache.clone()),
);

// Create an execution entry point
let calldata = [].to_vec();
let caller_address = Address(0000.into());
let entry_point_type = EntryPointType::External;

// Block for get_caller_address.
{
let call_info = test_syscall(
"caller_address",
address.clone(),
vec![],
caller_address.clone(),
entry_point_type,
class_hash,
&mut state,
);
assert_eq!(call_info.retdata, vec![caller_address.clone().0])
}

// Block for get_contact_address.
{
let call_info = test_syscall(
"contract_address",
address.clone(),
vec![],
caller_address.clone(),
entry_point_type,
class_hash,
&mut state,
);
assert_eq!(call_info.retdata, vec![address.clone().0])
}
// Block for get_execution_info_syscall.
{
let call_info = test_syscall(
"execution_info_syscall",
address.clone(),
calldata.clone(),
caller_address.clone(),
entry_point_type,
class_hash,
&mut state,
);
assert_eq!(call_info.retdata, vec![0.into(), 1111.into()]);
}

// Block for library_call_syscall
{
let entrypoint_selector =
Felt252::from_bytes_be(&calculate_sn_keccak("get_number".as_bytes()));
let new_call_data = vec![
Felt252::from_bytes_be(&class_hash),
entrypoint_selector,
Felt252::from(25),
];
let call_info = test_syscall(
"test_library_call_syscall",
address.clone(),
new_call_data,
caller_address.clone(),
entry_point_type,
class_hash,
&mut state,
);
assert_eq!(call_info.retdata, vec![25.into()])
}

// Block for call_contract_syscall
{
let entrypoint_selector =
Felt252::from_bytes_be(&calculate_sn_keccak("get_number".as_bytes()));
let new_call_data = vec![entrypoint_selector, Felt252::from(25)];
let call_info = test_syscall(
"test_call_contract_syscall",
address.clone(),
new_call_data,
caller_address.clone(),
entry_point_type,
class_hash,
&mut state,
);
assert_eq!(call_info.retdata, vec![25.into()])
}

// Block for send_message_to_l1_syscall
{
let new_call_data = vec![2222.into(), Felt252::from(25), Felt252::from(30)];
let call_info = test_syscall(
"test_send_message_to_l1",
address.clone(),
new_call_data,
caller_address.clone(),
entry_point_type,
class_hash,
&mut state,
);
assert_eq!(
call_info.l2_to_l1_messages,
vec![OrderedL2ToL1Message {
order: 0,
to_address: Address(2222.into()),
payload: vec![Felt252::from(25), Felt252::from(30)],
},]
)
}

// Block for read write
{
let call_info = test_syscall(
"read",
address.clone(),
calldata.clone(),
caller_address.clone(),
entry_point_type,
class_hash,
&mut state,
);
assert_eq!(
call_info.retdata,
vec![Felt252::from_str_radix("310939249775", 10).unwrap()]
)
}

// Block for emit
{
let call_info = test_syscall(
"trigger_events",
address,
calldata,
caller_address,
entry_point_type,
class_hash,
&mut state,
);
assert_eq!(
call_info.events,
vec![
OrderedEvent {
order: 0,
keys: vec![Felt252::from_str_radix(
"1533133552972353850845856330693290141476612241335297758062928121906575244541",
10
)
.unwrap()],
data: vec![1.into()]
},
OrderedEvent {
order: 1,
keys: vec![Felt252::from_str_radix(
"1533133552972353850845856330693290141476612241335297758062928121906575244541",
10
)
.unwrap()],
data: vec![2.into()]
},
OrderedEvent {
order: 2,
keys: vec![Felt252::from_str_radix(
"1533133552972353850845856330693290141476612241335297758062928121906575244541",
10
)
.unwrap()],
data: vec![3.into()]
}
]
)
}
}

fn test_syscall(
entrypoint_selector: &str,
address: Address,
calldata: Vec<Felt252>,
caller_address: Address,
entry_point_type: EntryPointType,
class_hash: [u8; 32],
state: &mut CachedState<InMemoryStateReader>,
) -> CallInfo {
let entrypoint_selector =
Felt252::from_bytes_be(&calculate_sn_keccak(entrypoint_selector.as_bytes()));
let exec_entry_point = ExecutionEntryPoint::new(
address,
calldata,
Felt252::new(entrypoint_selector),
caller_address,
entry_point_type,
Some(CallType::Delegate),
Some(class_hash),
100000,
);

// Execute the entrypoint
let block_context = BlockContext::default();
let mut tx_execution_context = TransactionExecutionContext::new(
Address(0.into()),
Felt252::zero(),
Vec::new(),
0,
10.into(),
block_context.invoke_tx_max_n_steps(),
TRANSACTION_VERSION.clone(),
);
let mut resources_manager = ExecutionResourcesManager::default();
exec_entry_point
.execute(
state,
&block_context,
&mut resources_manager,
&mut tx_execution_context,
false,
block_context.invoke_tx_max_n_steps(),
)
.unwrap()
.call_info
.unwrap()
}