Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add deserialization types and basic casting implementation
- Loading branch information
1 parent
1d5f5e0
commit c61ab82
Showing
12 changed files
with
314 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod cast; | ||
pub mod errors; | ||
pub mod types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
use super::errors::DeserializationError; | ||
use super::types::{Input, RawInput, StarknetStorageKey, StarknetStorageValue, StateDiff}; | ||
use crate::deserialization::types::{ContractAddress, ContractState}; | ||
use crate::hash::types::HashOutput; | ||
use crate::patricia_merkle_tree::filled_node::{ClassHash, Nonce}; | ||
use crate::patricia_merkle_tree::types::TreeHeight; | ||
use crate::storage::storage_trait::{StorageKey, StorageValue}; | ||
use crate::types::Felt; | ||
use starknet_types_core::felt::Felt as StarknetTypesFelt; | ||
|
||
use std::collections::HashMap; | ||
|
||
impl TryFrom<RawInput> for Input { | ||
type Error = DeserializationError; | ||
fn try_from(raw_input: RawInput) -> Result<Self, Self::Error> { | ||
let mut storage = HashMap::new(); | ||
for entry in raw_input.storage { | ||
add_unique( | ||
&mut storage, | ||
"storage", | ||
StorageKey(entry.key), | ||
StorageValue(entry.value), | ||
)? | ||
} | ||
|
||
let mut address_to_class_hash = HashMap::new(); | ||
for entry in raw_input.state_diff.address_to_class_hash { | ||
add_unique( | ||
&mut address_to_class_hash, | ||
"address to class hash", | ||
ContractAddress(Felt::from(StarknetTypesFelt::from_bytes_be_slice( | ||
&entry.key, | ||
))), | ||
ClassHash(Felt::from(StarknetTypesFelt::from_bytes_be_slice( | ||
&entry.value, | ||
))), | ||
)? | ||
} | ||
|
||
let mut address_to_nonce = HashMap::new(); | ||
for entry in raw_input.state_diff.address_to_nonce { | ||
add_unique( | ||
&mut address_to_nonce, | ||
"address to nonce", | ||
ContractAddress(Felt::from(StarknetTypesFelt::from_bytes_be_slice( | ||
&entry.key, | ||
))), | ||
Nonce(Felt::from(StarknetTypesFelt::from_bytes_be_slice( | ||
&entry.value, | ||
))), | ||
)?; | ||
} | ||
|
||
let mut class_hash_to_compiled_class_hash = HashMap::new(); | ||
for entry in raw_input.state_diff.class_hash_to_compiled_class_hash { | ||
add_unique( | ||
&mut class_hash_to_compiled_class_hash, | ||
"class hash to compiled class hash", | ||
ClassHash(Felt::from(StarknetTypesFelt::from_bytes_be_slice( | ||
&entry.key, | ||
))), | ||
ClassHash(Felt::from(StarknetTypesFelt::from_bytes_be_slice( | ||
&entry.value, | ||
))), | ||
)?; | ||
} | ||
|
||
let mut current_contract_state_leaves = HashMap::new(); | ||
for entry in raw_input.state_diff.current_contract_state_leaves { | ||
add_unique( | ||
&mut current_contract_state_leaves, | ||
"current contract state leaves", | ||
ContractAddress(Felt::from(StarknetTypesFelt::from_bytes_be_slice( | ||
&entry.address, | ||
))), | ||
ContractState { | ||
nonce: Nonce(Felt::from(StarknetTypesFelt::from_bytes_be_slice( | ||
&entry.nonce, | ||
))), | ||
class_hash: ClassHash(Felt::from(StarknetTypesFelt::from_bytes_be_slice( | ||
&entry.class_hash, | ||
))), | ||
storage_root_hash: HashOutput(Felt::from( | ||
StarknetTypesFelt::from_bytes_be_slice(&entry.storage_root_hash), | ||
)), | ||
}, | ||
)?; | ||
} | ||
|
||
let mut storage_updates = HashMap::new(); | ||
for outer_entry in raw_input.state_diff.storage_updates { | ||
let inner_map = outer_entry | ||
.storage_updates | ||
.iter() | ||
.map(|inner_entry| { | ||
( | ||
StarknetStorageKey(Felt::from(StarknetTypesFelt::from_bytes_be_slice( | ||
&inner_entry.key, | ||
))), | ||
StarknetStorageValue(Felt::from(StarknetTypesFelt::from_bytes_be_slice( | ||
&inner_entry.value, | ||
))), | ||
) | ||
}) | ||
.collect(); | ||
add_unique( | ||
&mut storage_updates, | ||
"starknet storage updates", | ||
ContractAddress(Felt::from(StarknetTypesFelt::from_bytes_be_slice( | ||
&outer_entry.address, | ||
))), | ||
inner_map, | ||
)?; | ||
} | ||
|
||
Ok(Input { | ||
storage, | ||
state_diff: StateDiff { | ||
address_to_class_hash, | ||
address_to_nonce, | ||
class_hash_to_compiled_class_hash, | ||
current_contract_state_leaves, | ||
storage_updates, | ||
}, | ||
tree_height: TreeHeight(raw_input.tree_height), | ||
}) | ||
} | ||
} | ||
|
||
pub(crate) fn add_unique<K, V>( | ||
map: &mut HashMap<K, V>, | ||
map_name: &str, | ||
key: K, | ||
value: V, | ||
) -> Result<(), DeserializationError> | ||
where | ||
K: std::cmp::Eq + std::hash::Hash + std::fmt::Debug, | ||
{ | ||
if map.contains_key(&key) { | ||
return Err(DeserializationError::KeyDuplicate(format!( | ||
"{map_name}: {key:?}" | ||
))); | ||
} | ||
map.insert(key, value); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use std::fmt::Debug; | ||
|
||
use thiserror::Error; | ||
|
||
#[allow(dead_code)] | ||
#[derive(Debug, Error)] | ||
pub(crate) enum DeserializationError { | ||
#[error("There is a key duplicate at {0} mapping.")] | ||
KeyDuplicate(String), | ||
} |
Oops, something went wrong.