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

Commit 484acb3

Browse files
authored
remove transactionalstatereader as it is not needed as-is (#1054)
1 parent 4157830 commit 484acb3

File tree

7 files changed

+28
-140
lines changed

7 files changed

+28
-140
lines changed

cli/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ fn invoke_parser(
202202
)?;
203203
let mut transactional_state = cached_state.create_transactional();
204204
let _tx_info = internal_invoke.apply(&mut transactional_state, &BlockContext::default(), 0)?;
205-
cached_state.apply_state_update(&StateDiff::from_cached_state(transactional_state)?)?;
205+
cached_state.apply_state_update(&StateDiff::from_cached_state(transactional_state.cache())?)?;
206206

207207
let tx_hash = calculate_transaction_hash_common(
208208
TransactionHashPrefix::Invoke,

src/state/cached_state.rs

+2-111
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,9 @@ impl<T: StateReader> CachedState<T> {
102102

103103
/// Creates a copy of this state with an empty cache for saving changes and applying them
104104
/// later.
105-
pub fn create_transactional(&self) -> TransactionalCachedState<T> {
106-
let state_reader = Arc::new(TransactionalCachedStateReader::new(self));
105+
pub fn create_transactional(&self) -> CachedState<T> {
107106
CachedState {
108-
state_reader,
107+
state_reader: self.state_reader.clone(),
109108
cache: self.cache.clone(),
110109
contract_classes: self.contract_classes.clone(),
111110
cache_hits: 0,
@@ -445,114 +444,6 @@ impl<T: StateReader> State for CachedState<T> {
445444
}
446445
}
447446

448-
/// A CachedState which has access to another, "parent" state, used for executing transactions
449-
/// without commiting changes to the parent.
450-
pub type TransactionalCachedState<'a, T> = CachedState<TransactionalCachedStateReader<'a, T>>;
451-
452-
/// State reader used for transactional states which allows to check the parent state's cache and
453-
/// state reader if a transactional cache miss happens.
454-
///
455-
/// In practice this will act as a way to access the parent state's cache and other fields,
456-
/// without referencing the whole parent state, so there's no need to adapt state-modifying
457-
/// functions in the case that a transactional state is needed.
458-
#[derive(Debug, MutGetters, Getters, PartialEq, Clone)]
459-
pub struct TransactionalCachedStateReader<'a, T: StateReader> {
460-
/// The parent state's state_reader
461-
#[get(get = "pub")]
462-
pub(crate) state_reader: Arc<T>,
463-
/// The parent state's cache
464-
#[get(get = "pub")]
465-
pub(crate) cache: &'a StateCache,
466-
/// The parent state's contract_classes
467-
#[get(get = "pub")]
468-
pub(crate) contract_classes: ContractClassCache,
469-
}
470-
471-
impl<'a, T: StateReader> TransactionalCachedStateReader<'a, T> {
472-
fn new(state: &'a CachedState<T>) -> Self {
473-
Self {
474-
state_reader: state.state_reader.clone(),
475-
cache: &state.cache,
476-
contract_classes: state.contract_classes.clone(),
477-
}
478-
}
479-
}
480-
481-
impl<'a, T: StateReader> StateReader for TransactionalCachedStateReader<'a, T> {
482-
/// Returns the class hash for a given contract address.
483-
/// Returns zero as default value if missing
484-
fn get_class_hash_at(&self, contract_address: &Address) -> Result<ClassHash, StateError> {
485-
self.cache
486-
.get_class_hash(contract_address)
487-
.map(|a| Ok(*a))
488-
.unwrap_or_else(|| self.state_reader.get_class_hash_at(contract_address))
489-
}
490-
491-
/// Returns the nonce for a given contract address.
492-
fn get_nonce_at(&self, contract_address: &Address) -> Result<Felt252, StateError> {
493-
if self.cache.get_nonce(contract_address).is_none() {
494-
return self.state_reader.get_nonce_at(contract_address);
495-
}
496-
self.cache
497-
.get_nonce(contract_address)
498-
.ok_or_else(|| StateError::NoneNonce(contract_address.clone()))
499-
.cloned()
500-
}
501-
502-
/// Returns storage data for a given storage entry.
503-
/// Returns zero as default value if missing
504-
fn get_storage_at(&self, storage_entry: &StorageEntry) -> Result<Felt252, StateError> {
505-
self.cache
506-
.get_storage(storage_entry)
507-
.map(|v| Ok(v.clone()))
508-
.unwrap_or_else(|| self.state_reader.get_storage_at(storage_entry))
509-
}
510-
511-
// TODO: check if that the proper way to store it (converting hash to address)
512-
/// Returned the compiled class hash for a given class hash.
513-
fn get_compiled_class_hash(&self, class_hash: &ClassHash) -> Result<ClassHash, StateError> {
514-
if self
515-
.cache
516-
.class_hash_to_compiled_class_hash
517-
.get(class_hash)
518-
.is_none()
519-
{
520-
return self.state_reader.get_compiled_class_hash(class_hash);
521-
}
522-
self.cache
523-
.class_hash_to_compiled_class_hash
524-
.get(class_hash)
525-
.ok_or_else(|| StateError::NoneCompiledClass(*class_hash))
526-
.cloned()
527-
}
528-
529-
/// Returns the contract class for a given class hash.
530-
fn get_contract_class(&self, class_hash: &ClassHash) -> Result<CompiledClass, StateError> {
531-
// This method can receive both compiled_class_hash & class_hash and return both casm and deprecated contract classes
532-
//, which can be on the cache or on the state_reader, different cases will be described below:
533-
if class_hash == UNINITIALIZED_CLASS_HASH {
534-
return Err(StateError::UninitiaizedClassHash);
535-
}
536-
537-
// I: FETCHING FROM CACHE
538-
if let Some(compiled_class) = self.contract_classes.get(class_hash) {
539-
return Ok(compiled_class.clone());
540-
}
541-
542-
// I: CASM CONTRACT CLASS : CLASS_HASH
543-
if let Some(compiled_class_hash) =
544-
self.cache.class_hash_to_compiled_class_hash.get(class_hash)
545-
{
546-
if let Some(casm_class) = self.contract_classes.get(compiled_class_hash) {
547-
return Ok(casm_class.clone());
548-
}
549-
}
550-
551-
// II: FETCHING FROM STATE_READER
552-
self.state_reader.get_contract_class(class_hash)
553-
}
554-
}
555-
556447
impl<T: StateReader> CachedState<T> {
557448
// Updates the cache's storage_initial_values according to those in storage_writes
558449
// If a key is present in the storage_writes but not in storage_initial_values,

src/state/mod.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
utils::{Address, ClassHash},
1919
};
2020

21-
use self::{cached_state::CachedState, state_api::StateReader};
21+
use self::{cached_state::CachedState, state_api::StateReader, state_cache::StateCache};
2222

2323
#[derive(Clone, Debug, PartialEq, Eq)]
2424
pub struct BlockInfo {
@@ -125,18 +125,13 @@ impl StateDiff {
125125
}
126126
}
127127

128-
pub fn from_cached_state<T>(cached_state: CachedState<T>) -> Result<Self, StateError>
129-
where
130-
T: StateReader,
131-
{
132-
let state_cache = cached_state.cache().to_owned();
133-
134-
let substracted_maps = state_cache.storage_writes;
128+
pub fn from_cached_state(state_cache: &StateCache) -> Result<Self, StateError> {
129+
let substracted_maps = &state_cache.storage_writes;
135130
let storage_updates = to_state_diff_storage_mapping(substracted_maps);
136131

137-
let address_to_nonce = state_cache.nonce_writes;
138-
let class_hash_to_compiled_class = state_cache.compiled_class_hash_writes;
139-
let address_to_class_hash = state_cache.class_hash_writes;
132+
let address_to_nonce = state_cache.nonce_writes.clone();
133+
let class_hash_to_compiled_class = state_cache.compiled_class_hash_writes.clone();
134+
let address_to_class_hash = state_cache.class_hash_writes.clone();
140135

141136
Ok(StateDiff {
142137
address_to_class_hash,
@@ -248,7 +243,7 @@ mod test {
248243

249244
let cached_state = CachedState::new(Arc::new(state_reader), HashMap::new());
250245

251-
let diff = StateDiff::from_cached_state(cached_state).unwrap();
246+
let diff = StateDiff::from_cached_state(&cached_state.cache).unwrap();
252247

253248
assert_eq!(0, diff.storage_updates.len());
254249
}
@@ -319,7 +314,7 @@ mod test {
319314
let cached_state_original =
320315
CachedState::new(Arc::new(state_reader.clone()), HashMap::new());
321316

322-
let diff = StateDiff::from_cached_state(cached_state_original.clone()).unwrap();
317+
let diff = StateDiff::from_cached_state(cached_state_original.cache()).unwrap();
323318

324319
let cached_state = diff.to_cached_state(Arc::new(state_reader)).unwrap();
325320

@@ -367,7 +362,7 @@ mod test {
367362
let cached_state =
368363
CachedState::new_for_testing(Arc::new(state_reader), cache, HashMap::new());
369364

370-
let mut diff = StateDiff::from_cached_state(cached_state).unwrap();
365+
let mut diff = StateDiff::from_cached_state(cached_state.cache()).unwrap();
371366

372367
let diff_squashed = diff.squash(diff.clone());
373368

src/syscalls/deprecated_syscall_handler.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,7 @@ mod tests {
12031203
.unwrap();
12041204

12051205
state
1206-
.apply_state_update(&StateDiff::from_cached_state(transactional).unwrap())
1206+
.apply_state_update(&StateDiff::from_cached_state(transactional.cache()).unwrap())
12071207
.unwrap();
12081208

12091209
let result_call_info = result.call_info.unwrap();

src/transaction/deploy_account.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ impl DeployAccount {
190190
.as_str(),
191191
);
192192
} else {
193-
state.apply_state_update(&StateDiff::from_cached_state(transactional_state)?)?;
193+
state
194+
.apply_state_update(&StateDiff::from_cached_state(transactional_state.cache())?)?;
194195
}
195196

196197
let mut tx_execution_context =

src/transaction/invoke_function.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
},
1818
services::api::contract_classes::deprecated_contract_class::EntryPointType,
1919
state::{
20-
cached_state::{CachedState, TransactionalCachedState},
20+
cached_state::CachedState,
2121
state_api::{State, StateReader},
2222
ExecutionResourcesManager, StateDiff,
2323
},
@@ -239,7 +239,7 @@ impl InvokeFunction {
239239
/// - remaining_gas: The amount of gas that the transaction disposes.
240240
pub fn apply<S: StateReader>(
241241
&self,
242-
state: &mut TransactionalCachedState<S>,
242+
state: &mut CachedState<S>,
243243
block_context: &BlockContext,
244244
remaining_gas: u128,
245245
) -> Result<TransactionExecutionInfo, TransactionError> {
@@ -334,7 +334,8 @@ impl InvokeFunction {
334334
.as_str(),
335335
);
336336
} else {
337-
state.apply_state_update(&StateDiff::from_cached_state(transactional_state)?)?;
337+
state
338+
.apply_state_update(&StateDiff::from_cached_state(transactional_state.cache())?)?;
338339
}
339340

340341
let mut tx_execution_context =
@@ -674,7 +675,7 @@ mod tests {
674675
.apply(&mut transactional, &BlockContext::default(), 0)
675676
.unwrap();
676677
state
677-
.apply_state_update(&StateDiff::from_cached_state(transactional).unwrap())
678+
.apply_state_update(&StateDiff::from_cached_state(transactional.cache()).unwrap())
678679
.unwrap();
679680

680681
assert_eq!(result.tx_type, Some(TransactionType::InvokeFunction));
@@ -882,7 +883,7 @@ mod tests {
882883
.apply(&mut transactional, &BlockContext::default(), 0)
883884
.unwrap();
884885
state
885-
.apply_state_update(&StateDiff::from_cached_state(transactional).unwrap())
886+
.apply_state_update(&StateDiff::from_cached_state(transactional.cache()).unwrap())
886887
.unwrap();
887888

888889
assert_eq!(result.tx_type, Some(TransactionType::InvokeFunction));

src/utils.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,17 @@ pub fn string_to_hash(class_string: &String) -> ClassHash {
135135

136136
/// Converts CachedState storage mapping to StateDiff storage mapping.
137137
pub fn to_state_diff_storage_mapping(
138-
storage_writes: HashMap<StorageEntry, Felt252>,
138+
storage_writes: &HashMap<StorageEntry, Felt252>,
139139
) -> HashMap<Address, HashMap<Felt252, Felt252>> {
140140
let mut storage_updates: HashMap<Address, HashMap<Felt252, Felt252>> = HashMap::new();
141-
for ((address, key), value) in storage_writes.into_iter() {
141+
for ((address, key), value) in storage_writes.iter() {
142142
storage_updates
143-
.entry(address)
143+
.entry(address.clone())
144144
.and_modify(|updates_for_address: &mut HashMap<Felt252, Felt252>| {
145-
let key_fe = Felt252::from_bytes_be(&key);
145+
let key_fe = Felt252::from_bytes_be(key);
146146
updates_for_address.insert(key_fe, value.clone());
147147
})
148-
.or_insert_with(|| HashMap::from([(Felt252::from_bytes_be(&key), value)]));
148+
.or_insert_with(|| HashMap::from([(Felt252::from_bytes_be(key), value.clone())]));
149149
}
150150
storage_updates
151151
}
@@ -805,7 +805,7 @@ mod test {
805805
storage.insert((address1.clone(), key1), value1.clone());
806806
storage.insert((address2.clone(), key2), value2.clone());
807807

808-
let map = to_state_diff_storage_mapping(storage);
808+
let map = to_state_diff_storage_mapping(&storage);
809809

810810
let key1_fe = Felt252::from_bytes_be(key1.as_slice());
811811
let key2_fe = Felt252::from_bytes_be(key2.as_slice());
@@ -876,7 +876,7 @@ mod test {
876876
storage.insert((address1.clone(), key1), value1.clone());
877877
storage.insert((address2.clone(), key2), value2.clone());
878878

879-
let state_dff = to_state_diff_storage_mapping(storage);
879+
let state_dff = to_state_diff_storage_mapping(&storage);
880880
let cache_storage = to_cache_state_storage_mapping(&state_dff);
881881

882882
let mut expected_res = HashMap::new();

0 commit comments

Comments
 (0)