Skip to content

Commit

Permalink
Unify deprecated and casm contract caches. (lambdaclass#937)
Browse files Browse the repository at this point in the history
* Unify deprecated and casm contract caches.

* Fix formatting and clippy.

* Remove unused code.

* Unify contract classes in the state traits too.

* Restore type alias.

---------

Co-authored-by: Esteve Soler Arderiu <esteve.soler@lambdaclass.com>
  • Loading branch information
azteca1998 and azteca1998 authored Aug 30, 2023
1 parent e87fe63 commit bfd588f
Show file tree
Hide file tree
Showing 41 changed files with 808 additions and 828 deletions.
29 changes: 20 additions & 9 deletions bench/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ use starknet_in_rust::{
constants::{TRANSACTION_VERSION, VALIDATE_ENTRY_POINT_SELECTOR},
},
hash_utils::calculate_contract_address,
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, state_api::State},
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,10 +63,13 @@ 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)
.set_contract_class(
&CLASS_HASH_BYTES,
&CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())),
)
.unwrap();

let block_context = &Default::default();
Expand Down Expand Up @@ -97,7 +102,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,10 +134,13 @@ 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)
.set_contract_class(
&CLASS_HASH_BYTES,
&CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())),
)
.unwrap();

let block_context = &Default::default();
Expand Down Expand Up @@ -164,10 +172,13 @@ 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)
.set_contract_class(
&CLASS_HASH_BYTES,
&CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())),
)
.unwrap();

let block_context = &Default::default();
Expand Down
12 changes: 8 additions & 4 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ use starknet_in_rust::{
hash_utils::calculate_contract_address,
parser_errors::ParserError,
serde_structs::read_abi,
services::api::contract_classes::deprecated_contract_class::ContractClass,
services::api::contract_classes::{
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
},
state::{cached_state::CachedState, state_api::State},
state::{in_memory_state_reader::InMemoryStateReader, ExecutionResourcesManager},
transaction::{error::TransactionError, InvokeFunction},
Expand Down Expand Up @@ -110,7 +112,10 @@ fn declare_parser(
let contract_class =
ContractClass::from_path(&args.contract).map_err(ContractAddressError::Program)?;
let class_hash = compute_deprecated_class_hash(&contract_class)?;
cached_state.set_contract_class(&felt_to_hash(&class_hash), &contract_class)?;
cached_state.set_contract_class(
&felt_to_hash(&class_hash),
&CompiledClass::Deprecated(Arc::new(contract_class.clone())),
)?;

let tx_hash = calculate_declare_transaction_hash(
&contract_class,
Expand Down Expand Up @@ -313,8 +318,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

0 comments on commit bfd588f

Please sign in to comment.