This repository was archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Test multi syscall #687
Merged
Merged
Test multi syscall #687
Changes from all commits
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
aabbb8f
create multy syscall
173f547
remove the replace syscall, it failed because the contract adress did…
52b7504
added library call_syscall
ed6908c
wip
feaf519
wip
0b5c1d9
wip
adf7cd8
wip
fd2ad5a
work in progress
faf96a5
remove .sjon files from starknet_programs
82956c1
finished implemented all the syscalls
ecdf72e
reorder code, create one call to syscall
73b58e2
fix pull bug
2fd09ad
Update tests/multi_syscall_test.rs
fguthmann 137cb90
Update starknet_programs/cairo1/multi_syscall_test.cairo
fguthmann 2004102
Update starknet_programs/cairo1/contract_a.cairo
fguthmann fe2542f
Update tests/multi_syscall_test.rs
fguthmann 74d0ce4
added test syscall for deploy
4a160ed
make format changes
14907ee
corrected make clippy error
507ac7b
get_caller_address and get_contract_address return a adress
080986a
failed of get_contract_address
c21a8df
failed of get_contract_address
5c4d786
wip
5024398
Merge branch 'main' into test_multi_syscall
5c816f8
modify the selector entrypoint_selector to be function specific
07ce801
Merge branch 'main' into test_multi_syscall
fguthmann 122318a
Merge branch 'main' into test_multi_syscall
a9eb94e
wip
23fd56d
wip
SantiagoPittella a67bc1f
Merge branch 'main' into test_multi_syscall
40cf362
wip
a9da0b6
Merge branch 'main' into test_multi_syscall
670d685
add input to cairo functions
fb6b605
coorect format problem
3f953cc
Merge branch 'main' into test_multi_syscall
7ed6258
wip
0ee4019
wip
d9222a8
Merge branch 'main' into test_multi_syscall
942b3fd
wip
cc5c244
remove format problem
50dc978
merge main
15861f7
Merge branch 'main' into test_multi_syscall
fguthmann 3353b84
Fix sierra class hash calculation (#886)
juanbono 42c1d95
Fail with an Err transactions whose calculated fee exceed `max_fee` (…
xqft 84e3a9b
Fix test_get_nonce_at (#910)
xqft fc956ea
fix get_sorted_events bug (#912)
juanbono 25f961f
Added documentations to syscalls/deprecated_syscall_handler module (#…
fguthmann ce01355
Merge branch 'main' into test_multi_syscall
d0cfacc
wip
4066c29
Merge branch 'main' into test_multi_syscall
5e5e9ee
Modify the tests
0b82485
fixed clippy errors
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,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 | ||
} | ||
} |
This file contains hidden or 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,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", | ||
fguthmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.