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

Commit

Permalink
refactor(state,concurrency): remove state impl from ` VersionedStateP…
Browse files Browse the repository at this point in the history
…roxy ` and edit tests (#1846)
  • Loading branch information
noaov1 authored May 5, 2024
1 parent 5f6a087 commit 605118e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 77 deletions.
69 changes: 2 additions & 67 deletions crates/blockifier/src/concurrency/versioned_state_proxy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use std::sync::{Arc, Mutex, MutexGuard};

use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
Expand All @@ -9,7 +9,7 @@ use crate::concurrency::versioned_storage::VersionedStorage;
use crate::concurrency::TxIndex;
use crate::execution::contract_class::ContractClass;
use crate::state::cached_state::{CachedState, ContractClassMapping, StateMaps};
use crate::state::state_api::{State, StateReader, StateResult};
use crate::state::state_api::{StateReader, StateResult};

#[cfg(test)]
#[path = "versioned_state_proxy_test.rs"]
Expand Down Expand Up @@ -159,71 +159,6 @@ impl<S: StateReader> VersionedStateProxy<S> {
}
}

// TODO: Remove.
impl<S: StateReader> State for VersionedStateProxy<S> {
fn set_storage_at(
&mut self,
contract_address: ContractAddress,
key: StorageKey,
value: StarkFelt,
) -> StateResult<()> {
let mut state = self.state();
state.storage.write(self.tx_index, (contract_address, key), value);

Ok(())
}

fn set_class_hash_at(
&mut self,
contract_address: ContractAddress,
class_hash: ClassHash,
) -> StateResult<()> {
let mut state = self.state();
state.class_hashes.write(self.tx_index, contract_address, class_hash);

Ok(())
}

fn increment_nonce(&mut self, contract_address: ContractAddress) -> StateResult<()> {
let mut state = self.state();
let current_nonce = state.nonces.read(self.tx_index, contract_address).unwrap();

let current_nonce_as_u64: u64 =
usize::try_from(current_nonce.0)?.try_into().expect("Failed to convert usize to u64.");
let next_nonce_val = 1_u64 + current_nonce_as_u64;
let next_nonce = Nonce(StarkFelt::from(next_nonce_val));
state.nonces.write(self.tx_index, contract_address, next_nonce);

Ok(())
}

fn set_compiled_class_hash(
&mut self,
class_hash: ClassHash,
compiled_class_hash: CompiledClassHash,
) -> StateResult<()> {
let mut state = self.state();
state.compiled_class_hashes.write(self.tx_index, class_hash, compiled_class_hash);

Ok(())
}

fn set_contract_class(
&mut self,
class_hash: ClassHash,
contract_class: ContractClass,
) -> StateResult<()> {
let mut state = self.state();
state.compiled_contract_classes.write(self.tx_index, class_hash, contract_class);

Ok(())
}

fn add_visited_pcs(&mut self, _class_hash: ClassHash, _pcs: &HashSet<usize>) {
todo!()
}
}

impl<S: StateReader> StateReader for VersionedStateProxy<S> {
fn get_storage_at(
&self,
Expand Down
54 changes: 44 additions & 10 deletions crates/blockifier/src/concurrency/versioned_state_proxy_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::concurrency::versioned_state_proxy::{
ThreadSafeVersionedState, VersionedState, VersionedStateProxy,
};
use crate::context::BlockContext;
use crate::state::cached_state::CachedState;
use crate::state::cached_state::{CachedState, StateMaps};
use crate::state::state_api::{State, StateReader};
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::deploy_account::deploy_account_tx;
Expand Down Expand Up @@ -49,7 +49,7 @@ fn test_versioned_state_proxy() {
let versioned_state = Arc::new(Mutex::new(VersionedState::new(cached_state)));

let safe_versioned_state = ThreadSafeVersionedState(Arc::clone(&versioned_state));
let mut versioned_state_proxys: Vec<VersionedStateProxy<CachedState<DictStateReader>>> =
let versioned_state_proxys: Vec<VersionedStateProxy<CachedState<DictStateReader>>> =
(0..20).map(|i| safe_versioned_state.pin_version(i)).collect();

// Read initial data
Expand Down Expand Up @@ -78,14 +78,48 @@ fn test_versioned_state_proxy() {
let compiled_class_hash_v18 = compiled_class_hash!(30_u8);
let contract_class_v11 = FeatureContract::TestContract(CairoVersion::Cairo1).get_class();

versioned_state_proxys[3].set_storage_at(contract_address, new_key, stark_felt_v3).unwrap();
versioned_state_proxys[4].increment_nonce(contract_address).unwrap();
versioned_state_proxys[7].set_class_hash_at(contract_address, class_hash_v7).unwrap();
versioned_state_proxys[10].set_class_hash_at(contract_address, class_hash_v10).unwrap();
versioned_state_proxys[18]
.set_compiled_class_hash(class_hash, compiled_class_hash_v18)
.unwrap();
versioned_state_proxys[11].set_contract_class(class_hash, contract_class_v11.clone()).unwrap();
versioned_state_proxys[3].state().apply_writes(
3,
&StateMaps {
storage: HashMap::from([((contract_address, new_key), stark_felt_v3)]),
..Default::default()
},
&HashMap::default(),
);
versioned_state_proxys[4].state().apply_writes(
4,
&StateMaps { nonces: HashMap::from([(contract_address, nonce_v4)]), ..Default::default() },
&HashMap::default(),
);
versioned_state_proxys[7].state().apply_writes(
7,
&StateMaps {
class_hashes: HashMap::from([(contract_address, class_hash_v7)]),
..Default::default()
},
&HashMap::default(),
);
versioned_state_proxys[10].state().apply_writes(
10,
&StateMaps {
class_hashes: HashMap::from([(contract_address, class_hash_v10)]),
..Default::default()
},
&HashMap::default(),
);
versioned_state_proxys[18].state().apply_writes(
18,
&StateMaps {
compiled_class_hashes: HashMap::from([(class_hash, compiled_class_hash_v18)]),
..Default::default()
},
&HashMap::default(),
);
versioned_state_proxys[11].state().apply_writes(
11,
&StateMaps::default(),
&HashMap::from([(class_hash, contract_class_v11.clone())]),
);

// Read the data
assert_eq!(versioned_state_proxys[2].get_nonce_at(contract_address).unwrap(), nonce);
Expand Down

0 comments on commit 605118e

Please sign in to comment.