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

Commit bfd588f

Browse files
authored
Unify deprecated and casm contract caches. (#937)
* 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>
1 parent e87fe63 commit bfd588f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+808
-828
lines changed

bench/internals.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ use starknet_in_rust::{
1111
constants::{TRANSACTION_VERSION, VALIDATE_ENTRY_POINT_SELECTOR},
1212
},
1313
hash_utils::calculate_contract_address,
14-
services::api::contract_classes::deprecated_contract_class::ContractClass,
14+
services::api::contract_classes::{
15+
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
16+
},
1517
state::in_memory_state_reader::InMemoryStateReader,
1618
state::{cached_state::CachedState, state_api::State},
1719
transaction::{declare::Declare, Deploy, DeployAccount, InvokeFunction},
1820
utils::Address,
1921
};
20-
use std::{hint::black_box, sync::Arc};
22+
use std::{collections::HashMap, hint::black_box, sync::Arc};
2123

2224
lazy_static! {
2325
// include_str! doesn't seem to work in CI
@@ -61,10 +63,13 @@ fn deploy_account() {
6163
const RUNS: usize = 500;
6264

6365
let state_reader = Arc::new(InMemoryStateReader::default());
64-
let mut state = CachedState::new(state_reader, Some(Default::default()), None);
66+
let mut state = CachedState::new(state_reader, HashMap::new());
6567

6668
state
67-
.set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS)
69+
.set_contract_class(
70+
&CLASS_HASH_BYTES,
71+
&CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())),
72+
)
6873
.unwrap();
6974

7075
let block_context = &Default::default();
@@ -97,7 +102,7 @@ fn declare() {
97102
const RUNS: usize = 5;
98103

99104
let state_reader = Arc::new(InMemoryStateReader::default());
100-
let state = CachedState::new(state_reader, Some(Default::default()), None);
105+
let state = CachedState::new(state_reader, HashMap::new());
101106

102107
let block_context = &Default::default();
103108

@@ -129,10 +134,13 @@ fn deploy() {
129134
const RUNS: usize = 8;
130135

131136
let state_reader = Arc::new(InMemoryStateReader::default());
132-
let mut state = CachedState::new(state_reader, Some(Default::default()), None);
137+
let mut state = CachedState::new(state_reader, HashMap::new());
133138

134139
state
135-
.set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS)
140+
.set_contract_class(
141+
&CLASS_HASH_BYTES,
142+
&CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())),
143+
)
136144
.unwrap();
137145

138146
let block_context = &Default::default();
@@ -164,10 +172,13 @@ fn invoke() {
164172
const RUNS: usize = 100;
165173

166174
let state_reader = Arc::new(InMemoryStateReader::default());
167-
let mut state = CachedState::new(state_reader, Some(Default::default()), None);
175+
let mut state = CachedState::new(state_reader, HashMap::new());
168176

169177
state
170-
.set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS)
178+
.set_contract_class(
179+
&CLASS_HASH_BYTES,
180+
&CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())),
181+
)
171182
.unwrap();
172183

173184
let block_context = &Default::default();

cli/src/main.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ use starknet_in_rust::{
2323
hash_utils::calculate_contract_address,
2424
parser_errors::ParserError,
2525
serde_structs::read_abi,
26-
services::api::contract_classes::deprecated_contract_class::ContractClass,
26+
services::api::contract_classes::{
27+
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
28+
},
2729
state::{cached_state::CachedState, state_api::State},
2830
state::{in_memory_state_reader::InMemoryStateReader, ExecutionResourcesManager},
2931
transaction::{error::TransactionError, InvokeFunction},
@@ -110,7 +112,10 @@ fn declare_parser(
110112
let contract_class =
111113
ContractClass::from_path(&args.contract).map_err(ContractAddressError::Program)?;
112114
let class_hash = compute_deprecated_class_hash(&contract_class)?;
113-
cached_state.set_contract_class(&felt_to_hash(&class_hash), &contract_class)?;
115+
cached_state.set_contract_class(
116+
&felt_to_hash(&class_hash),
117+
&CompiledClass::Deprecated(Arc::new(contract_class.clone())),
118+
)?;
114119

115120
let tx_hash = calculate_declare_transaction_hash(
116121
&contract_class,
@@ -313,8 +318,7 @@ pub async fn start_devnet(port: u16) -> Result<(), std::io::Error> {
313318
let cached_state = web::Data::new(AppState {
314319
cached_state: Mutex::new(CachedState::<InMemoryStateReader>::new(
315320
Arc::new(InMemoryStateReader::default()),
316-
Some(HashMap::new()),
317-
None,
321+
HashMap::new(),
318322
)),
319323
});
320324

fuzzer/src/main.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use std::{
2525
path::PathBuf,
2626
};
2727

28+
use starknet_in_rust::services::api::contract_classes::compiled_class::CompiledClass;
2829
use std::fs;
2930
use std::process::Command;
3031
use std::thread;
@@ -44,11 +45,11 @@ fn main() {
4445
let file_content1 = "
4546
%lang starknet
4647
from starkware.cairo.common.cairo_builtins import HashBuiltin
47-
48+
4849
@storage_var
4950
func _counter() -> (res: felt) {
5051
}
51-
52+
5253
@external
5354
func write_and_read{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (res:felt) {
5455
_counter.write('";
@@ -116,7 +117,10 @@ fn main() {
116117
let address = Address(1111.into());
117118
let class_hash = [1; 32];
118119

119-
contract_class_cache.insert(class_hash, contract_class);
120+
contract_class_cache.insert(
121+
class_hash,
122+
CompiledClass::Deprecated(Arc::new(contract_class)),
123+
);
120124
let mut state_reader = InMemoryStateReader::default();
121125
state_reader
122126
.address_to_class_hash_mut()
@@ -126,8 +130,7 @@ fn main() {
126130
//* Create state with previous data
127131
//* ---------------------------------------
128132

129-
let mut state =
130-
CachedState::new(Arc::new(state_reader), Some(contract_class_cache), None);
133+
let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache);
131134

132135
//* ------------------------------------
133136
//* Create execution entry point

rpc_state_reader/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ mod transaction_tests {
741741
felt::felt_str,
742742
state::cached_state::CachedState,
743743
};
744-
use std::sync::Arc;
744+
use std::{collections::HashMap, sync::Arc};
745745

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

759759
let fee_token_address = Address(felt_str!(
760760
"049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",

src/bin/fibonacci.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ use num_traits::Zero;
55

66
use lazy_static::lazy_static;
77
use starknet_in_rust::{
8-
services::api::contract_classes::deprecated_contract_class::ContractClass,
9-
state::cached_state::CachedState, state::in_memory_state_reader::InMemoryStateReader,
10-
testing::state::StarknetState, utils::Address,
8+
services::api::contract_classes::{
9+
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
10+
},
11+
state::cached_state::CachedState,
12+
state::in_memory_state_reader::InMemoryStateReader,
13+
testing::state::StarknetState,
14+
utils::Address,
1115
};
1216

1317
#[cfg(feature = "with_mimalloc")]
@@ -78,17 +82,17 @@ fn create_initial_state() -> CachedState<InMemoryStateReader> {
7882
state_reader
7983
.address_to_nonce_mut()
8084
.insert(CONTRACT_ADDRESS.clone(), Felt252::zero());
81-
state_reader
82-
.class_hash_to_contract_class_mut()
83-
.insert(*CONTRACT_CLASS_HASH, CONTRACT_CLASS.clone());
85+
state_reader.class_hash_to_compiled_class_mut().insert(
86+
*CONTRACT_CLASS_HASH,
87+
CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())),
88+
);
8489

8590
state_reader
8691
.address_to_storage_mut()
8792
.insert((CONTRACT_ADDRESS.clone(), [0; 32]), Felt252::zero());
8893
Arc::new(state_reader)
8994
},
90-
Some(HashMap::new()),
91-
None,
95+
HashMap::new(),
9296
);
9397

9498
cached_state

src/bin/invoke.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ 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-
state::cached_state::CachedState, state::in_memory_state_reader::InMemoryStateReader,
9-
testing::state::StarknetState, utils::Address,
7+
services::api::contract_classes::{
8+
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
9+
},
10+
state::cached_state::CachedState,
11+
state::in_memory_state_reader::InMemoryStateReader,
12+
testing::state::StarknetState,
13+
utils::Address,
1014
};
1115

1216
use lazy_static::lazy_static;
@@ -92,17 +96,17 @@ fn create_initial_state() -> CachedState<InMemoryStateReader> {
9296
state_reader
9397
.address_to_nonce_mut()
9498
.insert(CONTRACT_ADDRESS.clone(), Felt252::zero());
95-
state_reader
96-
.class_hash_to_contract_class_mut()
97-
.insert(*CONTRACT_CLASS_HASH, CONTRACT_CLASS.clone());
99+
state_reader.class_hash_to_compiled_class_mut().insert(
100+
*CONTRACT_CLASS_HASH,
101+
CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())),
102+
);
98103

99104
state_reader
100105
.address_to_storage_mut()
101106
.insert((CONTRACT_ADDRESS.clone(), [0; 32]), Felt252::zero());
102107
Arc::new(state_reader)
103108
},
104-
Some(HashMap::new()),
105-
None,
109+
HashMap::new(),
106110
);
107111

108112
cached_state

src/bin/invoke_with_cachedstate.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use starknet_in_rust::{
88
block_context::{BlockContext, StarknetChainId, StarknetOsConfig},
99
constants::TRANSACTION_VERSION,
1010
},
11-
services::api::contract_classes::deprecated_contract_class::ContractClass,
11+
services::api::contract_classes::{
12+
compiled_class::CompiledClass, deprecated_contract_class::ContractClass,
13+
},
1214
state::in_memory_state_reader::InMemoryStateReader,
1315
state::{cached_state::CachedState, BlockInfo},
1416
transaction::InvokeFunction,
@@ -99,17 +101,17 @@ fn create_initial_state() -> CachedState<InMemoryStateReader> {
99101
state_reader
100102
.address_to_nonce_mut()
101103
.insert(CONTRACT_ADDRESS.clone(), Felt252::zero());
102-
state_reader
103-
.class_hash_to_contract_class_mut()
104-
.insert(*CONTRACT_CLASS_HASH, CONTRACT_CLASS.clone());
104+
state_reader.class_hash_to_compiled_class_mut().insert(
105+
*CONTRACT_CLASS_HASH,
106+
CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())),
107+
);
105108

106109
state_reader
107110
.address_to_storage_mut()
108111
.insert((CONTRACT_ADDRESS.clone(), [0; 32]), Felt252::zero());
109112
Arc::new(state_reader)
110113
},
111-
Some(HashMap::new()),
112-
None,
114+
HashMap::new(),
113115
);
114116

115117
cached_state

src/execution/execution_entry_point.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,8 @@ impl ExecutionEntryPoint {
128128
})
129129
}
130130
CompiledClass::Casm(contract_class) => {
131-
let mut tmp_state = CachedState::new(
132-
state.state_reader.clone(),
133-
state.contract_classes.clone(),
134-
state.casm_contract_classes.clone(),
135-
);
131+
let mut tmp_state =
132+
CachedState::new(state.state_reader.clone(), state.contract_classes.clone());
136133
tmp_state.cache = state.cache.clone();
137134

138135
match self._execute(

0 commit comments

Comments
 (0)