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
Remove StarknetState struct #1038
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
@@ -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; | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Surely we can make a helper for this boilerplate. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
Oops, something went wrong.
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.