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

Commit 2d25af1

Browse files
edg-ljuanbono
andauthored
remove testing, move erc20 test, update fibonacci bin (#1038)
Co-authored-by: Juan Bono <juanbono94@gmail.com>
1 parent a7e2e56 commit 2d25af1

File tree

15 files changed

+498
-1253
lines changed

15 files changed

+498
-1253
lines changed

examples/contract_execution/main.rs

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,24 @@
1111
1212
use cairo_vm::felt::Felt252;
1313
use starknet_in_rust::{
14-
services::api::contract_classes::deprecated_contract_class::ContractClass,
15-
testing::state::StarknetState,
14+
definitions::{block_context::BlockContext, constants::TRANSACTION_VERSION},
15+
services::api::contract_classes::{
16+
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
17+
},
18+
state::{
19+
cached_state::CachedState, in_memory_state_reader::InMemoryStateReader, state_api::State,
20+
},
21+
transaction::{Declare, Deploy, InvokeFunction, Transaction},
1622
utils::{calculate_sn_keccak, Address},
1723
};
18-
use std::path::Path;
24+
use std::{collections::HashMap, path::Path, sync::Arc};
1925

2026
fn main() {
2127
// replace this with the path to your compiled contract
22-
let contract_path = "starknet_programs/factorial.json";
28+
let contract_path = "starknet_programs/fibonacci.json";
2329

2430
// replace this with the name of your entrypoint
25-
let entry_point: &str = "factorial";
31+
let entry_point: &str = "fib";
2632

2733
// replace this with the arguments for the entrypoint
2834
let calldata: Vec<Felt252> = [1.into(), 1.into(), 10.into()].to_vec();
@@ -42,12 +48,21 @@ fn main() {
4248
fn test_contract(
4349
contract_path: impl AsRef<Path>,
4450
entry_point: &str,
45-
calldata: Vec<Felt252>,
51+
call_data: Vec<Felt252>,
4652
) -> Vec<Felt252> {
53+
//* --------------------------------------------
54+
//* Initialize needed variables
55+
//* --------------------------------------------
56+
let block_context = BlockContext::default();
57+
let chain_id = block_context.starknet_os_config().chain_id().clone();
58+
let sender_address = Address(1.into());
59+
let signature = vec![];
60+
4761
//* --------------------------------------------
4862
//* Initialize state
4963
//* --------------------------------------------
50-
let mut state = StarknetState::new(None);
64+
let state_reader = Arc::new(InMemoryStateReader::default());
65+
let mut state = CachedState::new(state_reader, HashMap::new());
5166

5267
//* --------------------------------------------
5368
//* Read contract from file
@@ -58,37 +73,74 @@ fn test_contract(
5873
//* --------------------------------------------
5974
//* Declare new contract class
6075
//* --------------------------------------------
61-
state
62-
.declare(contract_class.clone())
63-
.expect("Could not declare the contract class");
76+
let declare_tx = Declare::new(
77+
contract_class.clone(),
78+
chain_id.clone(),
79+
sender_address,
80+
0, // max fee
81+
0.into(),
82+
signature.clone(),
83+
0.into(), // nonce
84+
)
85+
.expect("couldn't create declare transaction");
86+
87+
declare_tx
88+
.execute(&mut state, &block_context)
89+
.expect("could not declare the contract class");
6490

6591
//* --------------------------------------------
6692
//* Deploy new contract class instance
6793
//* --------------------------------------------
68-
let (contract_address, _) = state
69-
.deploy(contract_class, vec![], Default::default(), None, 0)
70-
.expect("Could not deploy contract");
94+
95+
let deploy = Deploy::new(
96+
Default::default(), // salt
97+
contract_class.clone(),
98+
vec![], // call data
99+
block_context.starknet_os_config().chain_id().clone(),
100+
TRANSACTION_VERSION.clone(),
101+
)
102+
.unwrap();
103+
104+
state
105+
.set_contract_class(
106+
&deploy.contract_hash,
107+
&CompiledClass::Deprecated(Arc::new(contract_class)),
108+
)
109+
.unwrap();
110+
let contract_address = deploy.contract_address.clone();
111+
112+
let tx = Transaction::Deploy(deploy);
113+
114+
tx.execute(&mut state, &block_context, 0)
115+
.expect("could not deploy contract");
71116

72117
//* --------------------------------------------
73118
//* Execute contract entrypoint
74119
//* --------------------------------------------
75120
let entry_point_selector = Felt252::from_bytes_be(&calculate_sn_keccak(entry_point.as_bytes()));
76121

77-
let caller_address = Address::default();
78-
79-
let callinfo = state
80-
.execute_entry_point_raw(
81-
contract_address,
82-
entry_point_selector,
83-
calldata,
84-
caller_address,
85-
)
86-
.expect("Could not execute entry point");
122+
let invoke_tx = InvokeFunction::new(
123+
contract_address,
124+
entry_point_selector,
125+
0,
126+
TRANSACTION_VERSION.clone(),
127+
call_data,
128+
signature,
129+
chain_id,
130+
Some(0.into()),
131+
)
132+
.unwrap();
133+
134+
let tx = Transaction::InvokeFunction(invoke_tx);
135+
let tx_exec_info = tx.execute(&mut state, &block_context, 0).unwrap();
87136

88137
//* --------------------------------------------
89138
//* Extract return values
90139
//* --------------------------------------------
91-
callinfo.retdata
140+
tx_exec_info
141+
.call_info
142+
.expect("call info should exist")
143+
.retdata
92144
}
93145

94146
#[test]

src/bin/deploy.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
use std::{collections::HashMap, sync::Arc};
2+
13
use lazy_static::lazy_static;
24
use starknet_in_rust::{
3-
services::api::contract_classes::deprecated_contract_class::ContractClass,
4-
testing::state::StarknetState,
5+
definitions::{block_context::BlockContext, constants::TRANSACTION_VERSION},
6+
services::api::contract_classes::{
7+
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
8+
},
9+
state::{
10+
cached_state::CachedState, in_memory_state_reader::InMemoryStateReader, state_api::State,
11+
},
12+
transaction::{Deploy, Transaction},
513
};
614

715
#[cfg(feature = "with_mimalloc")]
@@ -20,19 +28,33 @@ lazy_static! {
2028

2129
fn main() {
2230
const RUNS: usize = 100;
23-
let mut starknet_state = StarknetState::new(None);
31+
32+
let block_context = BlockContext::default();
33+
let state_reader = Arc::new(InMemoryStateReader::default());
34+
35+
let mut state = CachedState::new(state_reader, HashMap::new());
36+
let call_data = vec![];
2437

2538
for n in 0..RUNS {
2639
let contract_address_salt = n.into();
2740

28-
starknet_state
29-
.deploy(
30-
CONTRACT_CLASS.clone(),
31-
vec![],
32-
contract_address_salt,
33-
None,
34-
0,
41+
let deploy = Deploy::new(
42+
contract_address_salt,
43+
CONTRACT_CLASS.clone(),
44+
call_data.clone(),
45+
block_context.starknet_os_config().chain_id().clone(),
46+
TRANSACTION_VERSION.clone(),
47+
)
48+
.unwrap();
49+
50+
state
51+
.set_contract_class(
52+
&deploy.contract_hash,
53+
&CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())),
3554
)
3655
.unwrap();
56+
let tx = Transaction::Deploy(deploy);
57+
58+
tx.execute(&mut state, &block_context, 0).unwrap();
3759
}
3860
}

src/bin/deploy_invoke.rs

Lines changed: 70 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
use std::path::PathBuf;
1+
use std::{collections::HashMap, path::PathBuf, sync::Arc};
22

33
use cairo_vm::felt::{felt_str, Felt252};
44
use num_traits::Zero;
55

66
use starknet_in_rust::{
7-
services::api::contract_classes::deprecated_contract_class::ContractClass,
8-
testing::state::StarknetState, utils::Address,
7+
definitions::{block_context::BlockContext, constants::TRANSACTION_VERSION},
8+
services::api::contract_classes::{
9+
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
10+
},
11+
state::{
12+
cached_state::CachedState, in_memory_state_reader::InMemoryStateReader, state_api::State,
13+
},
14+
transaction::{Deploy, InvokeFunction, Transaction},
15+
utils::Address,
916
};
1017

1118
use lazy_static::lazy_static;
@@ -36,52 +43,76 @@ lazy_static! {
3643

3744
fn main() {
3845
const RUNS: usize = 10000;
39-
let mut starknet_state = StarknetState::new(None);
40-
let contract_address_salt = 1.into();
4146

42-
let (contract_address, _exec_info) = starknet_state
43-
.deploy(
44-
CONTRACT_CLASS.to_owned(),
45-
vec![],
46-
contract_address_salt,
47-
None,
48-
0,
47+
let block_context = BlockContext::default();
48+
let state_reader = Arc::new(InMemoryStateReader::default());
49+
let mut state = CachedState::new(state_reader, HashMap::new());
50+
51+
let call_data = vec![];
52+
let contract_address_salt = 1.into();
53+
let chain_id = block_context.starknet_os_config().chain_id().clone();
54+
55+
let deploy = Deploy::new(
56+
contract_address_salt,
57+
CONTRACT_CLASS.clone(),
58+
call_data,
59+
block_context.starknet_os_config().chain_id().clone(),
60+
TRANSACTION_VERSION.clone(),
61+
)
62+
.unwrap();
63+
64+
let contract_address = deploy.contract_address.clone();
65+
state
66+
.set_contract_class(
67+
&deploy.contract_hash,
68+
&CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())),
4969
)
5070
.unwrap();
71+
let deploy_tx = Transaction::Deploy(deploy);
72+
73+
let _tx_exec_info = deploy_tx.execute(&mut state, &block_context, 0).unwrap();
74+
75+
let signature = Vec::new();
5176

5277
// Statement **not** in blockifier.
53-
starknet_state
54-
.state
78+
state
5579
.cache_mut()
5680
.nonce_initial_values_mut()
5781
.insert(contract_address.clone(), Felt252::zero());
5882

5983
for i in 0..RUNS {
60-
starknet_state
61-
.invoke_raw(
62-
contract_address.clone(),
63-
INCREASE_BALANCE_SELECTOR.clone(),
64-
vec![1000.into()],
65-
0,
66-
Some(Vec::new()),
67-
Some(Felt252::from(i * 2)),
68-
None,
69-
0,
70-
)
71-
.unwrap();
72-
73-
let tx_exec_info = starknet_state
74-
.invoke_raw(
75-
contract_address.clone(),
76-
GET_BALANCE_SELECTOR.clone(),
77-
vec![],
78-
0,
79-
Some(Vec::new()),
80-
Some(Felt252::from((i * 2) + 1)),
81-
None,
82-
0,
83-
)
84-
.unwrap();
84+
let nonce_first = Felt252::from(i * 2);
85+
let nonce_second = Felt252::from((i * 2) + 1);
86+
87+
let invoke_first = InvokeFunction::new(
88+
contract_address.clone(),
89+
INCREASE_BALANCE_SELECTOR.clone(),
90+
0,
91+
TRANSACTION_VERSION.clone(),
92+
vec![1000.into()],
93+
signature.clone(),
94+
chain_id.clone(),
95+
Some(nonce_first),
96+
)
97+
.unwrap();
98+
99+
let tx = Transaction::InvokeFunction(invoke_first);
100+
tx.execute(&mut state, &block_context, 0).unwrap();
101+
102+
let invoke_second = InvokeFunction::new(
103+
contract_address.clone(),
104+
GET_BALANCE_SELECTOR.clone(),
105+
0,
106+
TRANSACTION_VERSION.clone(),
107+
vec![],
108+
signature.clone(),
109+
chain_id.clone(),
110+
Some(nonce_second),
111+
)
112+
.unwrap();
113+
114+
let tx = Transaction::InvokeFunction(invoke_second);
115+
let tx_exec_info = tx.execute(&mut state, &block_context, 0).unwrap();
85116

86117
assert_eq!(
87118
tx_exec_info.call_info.unwrap().retdata,

0 commit comments

Comments
 (0)