Skip to content

Commit

Permalink
Other StateReader impl, nearly done
Browse files Browse the repository at this point in the history
  • Loading branch information
notlesh committed Apr 26, 2024
1 parent 35aac51 commit a96cb8a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::storage::storage::{DbObject, Fact, FactFetchingContext, HashFunctionT
/// Represents a leaf in the Starknet contract class tree.
#[derive(Deserialize, Clone, Debug, Serialize, PartialEq)]
pub struct ContractClassLeaf {
compiled_class_hash: Felt252,
pub compiled_class_hash: Felt252,
}

impl ContractClassLeaf {
Expand Down
41 changes: 39 additions & 2 deletions src/starknet/business_logic/fact_state/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ impl SharedState {

/// helper to get contract_state
/// TODO: move? make async? (it helps to not be async...)
/// also doesn't take FFC like others here
pub fn get_contract_state(
&self,
contract_address: ContractAddress,
Expand All @@ -351,11 +352,40 @@ impl SharedState {

Ok(contract_state)
}

/// helper to get contract_state
/// TODO: as above, this doesn't exactly fit well here
pub fn get_contract_class(
&self,
contract_address: ContractAddress,
) -> StateResult<ContractClassLeaf> {
let contract_address: TreeIndex = felt_api2vm(*contract_address.0.key()).to_biguint();

if self.contract_classes.is_none() {
return Err(StateError::StateReadError(format!("{:?}", contract_address.clone())));
}

// TODO: FFC makes no sense here
let mut ffc = FactFetchingContext::<DictStorage, PedersenHash>::new(Default::default());

let contract_class = execute_coroutine_threadsafe(async {
let contract_classes: HashMap<TreeIndex, ContractClassLeaf> = self.contract_classes.as_ref().unwrap()
.get_leaves(&mut ffc, &[contract_address.clone()], &mut None)
.await
.unwrap(); // TODO: error
let contract_class = contract_classes
.get(&contract_address.clone())
.ok_or(StateError::StateReadError(format!("{:?}", contract_address.clone())))?
.clone();
StateResult::Ok(contract_class)
})?;

Ok(contract_class)
}
}

impl StateReader for SharedState {


/// Returns the storage value under the given key in the given contract instance (represented by
/// its address).
/// Default: 0 for an uninitialized contract address.
Expand Down Expand Up @@ -402,12 +432,19 @@ impl StateReader for SharedState {

/// Returns the contract class of the given class hash.
fn get_compiled_contract_class(&mut self, class_hash: ClassHash) -> StateResult<ContractClass> {
let contract_address = ContractAddress(PatriciaKey::try_from(class_hash.0).unwrap());
let leaf = self.get_contract_class(contract_address)?;
// TODO: convert ContractClassLeaf -> ContractClass
// TODO: how can we know whether v0 or v1 here?
unimplemented!();
}

/// Returns the compiled class hash of the given class hash.
fn get_compiled_class_hash(&mut self, class_hash: ClassHash) -> StateResult<CompiledClassHash> {
unimplemented!();
let contract_address = ContractAddress(PatriciaKey::try_from(class_hash.0).unwrap());
let leaf = self.get_contract_class(contract_address)?;
let hash = felt_vm2api(leaf.compiled_class_hash);
Ok(CompiledClassHash(hash))
}

// TODO: do we care about `fn get_free_token_balance()`?
Expand Down

0 comments on commit a96cb8a

Please sign in to comment.