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

Unify deprecated and casm contract caches. #937

Merged
merged 5 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 5 additions & 5 deletions bench/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use starknet_in_rust::{
transaction::{declare::Declare, Deploy, DeployAccount, InvokeFunction},
utils::Address,
};
use std::{hint::black_box, sync::Arc};
use std::{collections::HashMap, hint::black_box, sync::Arc};

lazy_static! {
// include_str! doesn't seem to work in CI
Expand Down Expand Up @@ -61,7 +61,7 @@ fn deploy_account() {
const RUNS: usize = 500;

let state_reader = Arc::new(InMemoryStateReader::default());
let mut state = CachedState::new(state_reader, Some(Default::default()), None);
let mut state = CachedState::new(state_reader, HashMap::new());

state
.set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS)
Expand Down Expand Up @@ -97,7 +97,7 @@ fn declare() {
const RUNS: usize = 5;

let state_reader = Arc::new(InMemoryStateReader::default());
let state = CachedState::new(state_reader, Some(Default::default()), None);
let state = CachedState::new(state_reader, HashMap::new());

let block_context = &Default::default();

Expand Down Expand Up @@ -129,7 +129,7 @@ fn deploy() {
const RUNS: usize = 8;

let state_reader = Arc::new(InMemoryStateReader::default());
let mut state = CachedState::new(state_reader, Some(Default::default()), None);
let mut state = CachedState::new(state_reader, HashMap::new());

state
.set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS)
Expand Down Expand Up @@ -164,7 +164,7 @@ fn invoke() {
const RUNS: usize = 100;

let state_reader = Arc::new(InMemoryStateReader::default());
let mut state = CachedState::new(state_reader, Some(Default::default()), None);
let mut state = CachedState::new(state_reader, HashMap::new());

state
.set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS)
Expand Down
3 changes: 1 addition & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,7 @@ pub async fn start_devnet(port: u16) -> Result<(), std::io::Error> {
let cached_state = web::Data::new(AppState {
cached_state: Mutex::new(CachedState::<InMemoryStateReader>::new(
Arc::new(InMemoryStateReader::default()),
Some(HashMap::new()),
None,
HashMap::new(),
)),
});

Expand Down
13 changes: 8 additions & 5 deletions fuzzer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::{
path::PathBuf,
};

use starknet_in_rust::services::api::contract_classes::compiled_class::CompiledClass;
use std::fs;
use std::process::Command;
use std::thread;
Expand All @@ -44,11 +45,11 @@ fn main() {
let file_content1 = "
%lang starknet
from starkware.cairo.common.cairo_builtins import HashBuiltin

@storage_var
func _counter() -> (res: felt) {
}

@external
func write_and_read{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (res:felt) {
_counter.write('";
Expand Down Expand Up @@ -116,7 +117,10 @@ fn main() {
let address = Address(1111.into());
let class_hash = [1; 32];

contract_class_cache.insert(class_hash, contract_class);
contract_class_cache.insert(
class_hash,
CompiledClass::Deprecated(Arc::new(contract_class)),
);
let mut state_reader = InMemoryStateReader::default();
state_reader
.address_to_class_hash_mut()
Expand All @@ -126,8 +130,7 @@ fn main() {
//* Create state with previous data
//* ---------------------------------------

let mut state =
CachedState::new(Arc::new(state_reader), Some(contract_class_cache), None);
let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache);

//* ------------------------------------
//* Create execution entry point
Expand Down
4 changes: 2 additions & 2 deletions rpc_state_reader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ mod transaction_tests {
felt::felt_str,
state::cached_state::CachedState,
};
use std::sync::Arc;
use std::{collections::HashMap, sync::Arc};

fn test_tx(
tx_hash: &str,
Expand All @@ -754,7 +754,7 @@ mod transaction_tests {
// Instantiate the RPC StateReader and the CachedState
let block = BlockValue::Number(serde_json::to_value(block_number).unwrap());
let rpc_state = Arc::new(RpcState::new(network, block));
let mut state = CachedState::new(rpc_state.clone(), None, None);
let mut state = CachedState::new(rpc_state.clone(), HashMap::new());

let fee_token_address = Address(felt_str!(
"049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
Expand Down
20 changes: 12 additions & 8 deletions src/bin/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ use num_traits::Zero;

use lazy_static::lazy_static;
use starknet_in_rust::{
services::api::contract_classes::deprecated_contract_class::ContractClass,
state::cached_state::CachedState, state::in_memory_state_reader::InMemoryStateReader,
testing::state::StarknetState, utils::Address,
services::api::contract_classes::{
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
},
state::cached_state::CachedState,
state::in_memory_state_reader::InMemoryStateReader,
testing::state::StarknetState,
utils::Address,
};

#[cfg(feature = "with_mimalloc")]
Expand Down Expand Up @@ -78,17 +82,17 @@ fn create_initial_state() -> CachedState<InMemoryStateReader> {
state_reader
.address_to_nonce_mut()
.insert(CONTRACT_ADDRESS.clone(), Felt252::zero());
state_reader
.class_hash_to_contract_class_mut()
.insert(*CONTRACT_CLASS_HASH, CONTRACT_CLASS.clone());
state_reader.class_hash_to_compiled_class_mut().insert(
*CONTRACT_CLASS_HASH,
CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())),
);

state_reader
.address_to_storage_mut()
.insert((CONTRACT_ADDRESS.clone(), [0; 32]), Felt252::zero());
Arc::new(state_reader)
},
Some(HashMap::new()),
None,
HashMap::new(),
);

cached_state
Expand Down
20 changes: 12 additions & 8 deletions src/bin/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ use cairo_vm::felt::{felt_str, Felt252};
use num_traits::Zero;

use starknet_in_rust::{
services::api::contract_classes::deprecated_contract_class::ContractClass,
state::cached_state::CachedState, state::in_memory_state_reader::InMemoryStateReader,
testing::state::StarknetState, utils::Address,
services::api::contract_classes::{
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
},
state::cached_state::CachedState,
state::in_memory_state_reader::InMemoryStateReader,
testing::state::StarknetState,
utils::Address,
};

use lazy_static::lazy_static;
Expand Down Expand Up @@ -92,17 +96,17 @@ fn create_initial_state() -> CachedState<InMemoryStateReader> {
state_reader
.address_to_nonce_mut()
.insert(CONTRACT_ADDRESS.clone(), Felt252::zero());
state_reader
.class_hash_to_contract_class_mut()
.insert(*CONTRACT_CLASS_HASH, CONTRACT_CLASS.clone());
state_reader.class_hash_to_compiled_class_mut().insert(
*CONTRACT_CLASS_HASH,
CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())),
);

state_reader
.address_to_storage_mut()
.insert((CONTRACT_ADDRESS.clone(), [0; 32]), Felt252::zero());
Arc::new(state_reader)
},
Some(HashMap::new()),
None,
HashMap::new(),
);

cached_state
Expand Down
14 changes: 8 additions & 6 deletions src/bin/invoke_with_cachedstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use starknet_in_rust::{
block_context::{BlockContext, StarknetChainId, StarknetOsConfig},
constants::TRANSACTION_VERSION,
},
services::api::contract_classes::deprecated_contract_class::ContractClass,
services::api::contract_classes::{
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
},
state::in_memory_state_reader::InMemoryStateReader,
state::{cached_state::CachedState, BlockInfo},
transaction::InvokeFunction,
Expand Down Expand Up @@ -99,17 +101,17 @@ fn create_initial_state() -> CachedState<InMemoryStateReader> {
state_reader
.address_to_nonce_mut()
.insert(CONTRACT_ADDRESS.clone(), Felt252::zero());
state_reader
.class_hash_to_contract_class_mut()
.insert(*CONTRACT_CLASS_HASH, CONTRACT_CLASS.clone());
state_reader.class_hash_to_compiled_class_mut().insert(
*CONTRACT_CLASS_HASH,
CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())),
);

state_reader
.address_to_storage_mut()
.insert((CONTRACT_ADDRESS.clone(), [0; 32]), Felt252::zero());
Arc::new(state_reader)
},
Some(HashMap::new()),
None,
HashMap::new(),
);

cached_state
Expand Down
7 changes: 2 additions & 5 deletions src/execution/execution_entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,8 @@ impl ExecutionEntryPoint {
})
}
CompiledClass::Casm(contract_class) => {
let mut tmp_state = CachedState::new(
state.state_reader.clone(),
state.contract_classes.clone(),
state.casm_contract_classes.clone(),
);
let mut tmp_state =
CachedState::new(state.state_reader.clone(), state.contract_classes.clone());
tmp_state.cache = state.cache.clone();

match self._execute(
Expand Down
Loading
Loading