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

Remove StarknetState struct #1038

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
100 changes: 76 additions & 24 deletions examples/contract_execution/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@

use cairo_vm::felt::Felt252;
use starknet_in_rust::{
services::api::contract_classes::deprecated_contract_class::ContractClass,
testing::state::StarknetState,
definitions::{block_context::BlockContext, constants::TRANSACTION_VERSION},
services::api::contract_classes::{
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
},
state::{
cached_state::CachedState, in_memory_state_reader::InMemoryStateReader, state_api::State,
},
transaction::{Declare, Deploy, InvokeFunction, Transaction},
utils::{calculate_sn_keccak, Address},
};
use std::path::Path;
use std::{collections::HashMap, path::Path, sync::Arc};

fn main() {
// replace this with the path to your compiled contract
let contract_path = "starknet_programs/factorial.json";
let contract_path = "starknet_programs/fibonacci.json";

// replace this with the name of your entrypoint
let entry_point: &str = "factorial";
let entry_point: &str = "fib";

// replace this with the arguments for the entrypoint
let calldata: Vec<Felt252> = [1.into(), 1.into(), 10.into()].to_vec();
Expand All @@ -42,12 +48,21 @@ fn main() {
fn test_contract(
contract_path: impl AsRef<Path>,
entry_point: &str,
calldata: Vec<Felt252>,
call_data: Vec<Felt252>,
) -> Vec<Felt252> {
//* --------------------------------------------
//* Initialize needed variables
//* --------------------------------------------
let block_context = BlockContext::default();
let chain_id = block_context.starknet_os_config().chain_id().clone();
let sender_address = Address(1.into());
let signature = vec![];

//* --------------------------------------------
//* Initialize state
//* --------------------------------------------
let mut state = StarknetState::new(None);
let state_reader = Arc::new(InMemoryStateReader::default());
let mut state = CachedState::new(state_reader, HashMap::new());

//* --------------------------------------------
//* Read contract from file
Expand All @@ -58,37 +73,74 @@ fn test_contract(
//* --------------------------------------------
//* Declare new contract class
//* --------------------------------------------
state
.declare(contract_class.clone())
.expect("Could not declare the contract class");
let declare_tx = Declare::new(
contract_class.clone(),
chain_id.clone(),
sender_address,
0, // max fee
0.into(),
signature.clone(),
0.into(), // nonce
)
.expect("couldn't create declare transaction");

declare_tx
.execute(&mut state, &block_context)
.expect("could not declare the contract class");

//* --------------------------------------------
//* Deploy new contract class instance
//* --------------------------------------------
let (contract_address, _) = state
.deploy(contract_class, vec![], Default::default(), None, 0)
.expect("Could not deploy contract");

let deploy = Deploy::new(
Default::default(), // salt
contract_class.clone(),
vec![], // call data
block_context.starknet_os_config().chain_id().clone(),
TRANSACTION_VERSION.clone(),
)
.unwrap();

state
.set_contract_class(
&deploy.contract_hash,
&CompiledClass::Deprecated(Arc::new(contract_class)),
)
.unwrap();
let contract_address = deploy.contract_address.clone();

let tx = Transaction::Deploy(deploy);

tx.execute(&mut state, &block_context, 0)
.expect("could not deploy contract");

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

let caller_address = Address::default();

let callinfo = state
.execute_entry_point_raw(
contract_address,
entry_point_selector,
calldata,
caller_address,
)
.expect("Could not execute entry point");
let invoke_tx = InvokeFunction::new(
contract_address,
entry_point_selector,
0,
TRANSACTION_VERSION.clone(),
call_data,
signature,
chain_id,
Some(0.into()),
)
.unwrap();

let tx = Transaction::InvokeFunction(invoke_tx);
let tx_exec_info = tx.execute(&mut state, &block_context, 0).unwrap();

//* --------------------------------------------
//* Extract return values
//* --------------------------------------------
callinfo.retdata
tx_exec_info
.call_info
.expect("call info should exist")
.retdata
}

#[test]
Expand Down
42 changes: 32 additions & 10 deletions src/bin/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
use std::{collections::HashMap, sync::Arc};

use lazy_static::lazy_static;
use starknet_in_rust::{
services::api::contract_classes::deprecated_contract_class::ContractClass,
testing::state::StarknetState,
definitions::{block_context::BlockContext, constants::TRANSACTION_VERSION},
services::api::contract_classes::{
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
},
state::{
cached_state::CachedState, in_memory_state_reader::InMemoryStateReader, state_api::State,
},
transaction::{Deploy, Transaction},
};

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

fn main() {
const RUNS: usize = 100;
let mut starknet_state = StarknetState::new(None);

let block_context = BlockContext::default();
let state_reader = Arc::new(InMemoryStateReader::default());

let mut state = CachedState::new(state_reader, HashMap::new());
let call_data = vec![];

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

starknet_state
.deploy(
CONTRACT_CLASS.clone(),
vec![],
contract_address_salt,
None,
0,
let deploy = Deploy::new(
contract_address_salt,
CONTRACT_CLASS.clone(),
call_data.clone(),
block_context.starknet_os_config().chain_id().clone(),
TRANSACTION_VERSION.clone(),
)
.unwrap();

state
.set_contract_class(
&deploy.contract_hash,
&CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())),
)
.unwrap();
let tx = Transaction::Deploy(deploy);

tx.execute(&mut state, &block_context, 0).unwrap();
}
}
109 changes: 70 additions & 39 deletions src/bin/deploy_invoke.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use std::path::PathBuf;
use std::{collections::HashMap, path::PathBuf, sync::Arc};

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

use starknet_in_rust::{
services::api::contract_classes::deprecated_contract_class::ContractClass,
testing::state::StarknetState, utils::Address,
definitions::{block_context::BlockContext, constants::TRANSACTION_VERSION},
services::api::contract_classes::{
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
},
state::{
cached_state::CachedState, in_memory_state_reader::InMemoryStateReader, state_api::State,
},
transaction::{Deploy, InvokeFunction, Transaction},
utils::Address,
};

use lazy_static::lazy_static;
Expand Down Expand Up @@ -36,52 +43,76 @@ lazy_static! {

fn main() {
const RUNS: usize = 10000;
let mut starknet_state = StarknetState::new(None);
let contract_address_salt = 1.into();

let (contract_address, _exec_info) = starknet_state
.deploy(
CONTRACT_CLASS.to_owned(),
vec![],
contract_address_salt,
None,
0,
let block_context = BlockContext::default();
let state_reader = Arc::new(InMemoryStateReader::default());
let mut state = CachedState::new(state_reader, HashMap::new());

let call_data = vec![];
let contract_address_salt = 1.into();
let chain_id = block_context.starknet_os_config().chain_id().clone();

let deploy = Deploy::new(
contract_address_salt,
CONTRACT_CLASS.clone(),
call_data,
block_context.starknet_os_config().chain_id().clone(),
TRANSACTION_VERSION.clone(),
)
.unwrap();
Comment on lines +47 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely we can make a helper for this boilerplate.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see each binary as an independent item, but if you want i can change it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see what @juanbono thinks.


let contract_address = deploy.contract_address.clone();
state
.set_contract_class(
&deploy.contract_hash,
&CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())),
)
.unwrap();
let deploy_tx = Transaction::Deploy(deploy);

let _tx_exec_info = deploy_tx.execute(&mut state, &block_context, 0).unwrap();

let signature = Vec::new();

// Statement **not** in blockifier.
starknet_state
.state
state
.cache_mut()
.nonce_initial_values_mut()
.insert(contract_address.clone(), Felt252::zero());

for i in 0..RUNS {
starknet_state
.invoke_raw(
contract_address.clone(),
INCREASE_BALANCE_SELECTOR.clone(),
vec![1000.into()],
0,
Some(Vec::new()),
Some(Felt252::from(i * 2)),
None,
0,
)
.unwrap();

let tx_exec_info = starknet_state
.invoke_raw(
contract_address.clone(),
GET_BALANCE_SELECTOR.clone(),
vec![],
0,
Some(Vec::new()),
Some(Felt252::from((i * 2) + 1)),
None,
0,
)
.unwrap();
let nonce_first = Felt252::from(i * 2);
let nonce_second = Felt252::from((i * 2) + 1);

let invoke_first = InvokeFunction::new(
contract_address.clone(),
INCREASE_BALANCE_SELECTOR.clone(),
0,
TRANSACTION_VERSION.clone(),
vec![1000.into()],
signature.clone(),
chain_id.clone(),
Some(nonce_first),
)
.unwrap();

let tx = Transaction::InvokeFunction(invoke_first);
tx.execute(&mut state, &block_context, 0).unwrap();

let invoke_second = InvokeFunction::new(
contract_address.clone(),
GET_BALANCE_SELECTOR.clone(),
0,
TRANSACTION_VERSION.clone(),
vec![],
signature.clone(),
chain_id.clone(),
Some(nonce_second),
)
.unwrap();

let tx = Transaction::InvokeFunction(invoke_second);
let tx_exec_info = tx.execute(&mut state, &block_context, 0).unwrap();

assert_eq!(
tx_exec_info.call_info.unwrap().retdata,
Expand Down
Loading