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
707a58d
commit 2ad6a38
Showing
8 changed files
with
187 additions
and
0 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,87 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::deserialization_types::{ | ||
ActualInput, ActualStateDiff, ContractState, DeserializedInput, | ||
}; | ||
use crate::patricia_merkle_tree::filled_node::{ClassHash, Nonce}; | ||
use starknet_types_core::felt::Felt; | ||
|
||
#[allow(dead_code)] | ||
impl DeserializedInput { | ||
pub fn actual_input(self) -> ActualInput { | ||
let mut storage = HashMap::new(); | ||
for entry in self.storage.into_iter() { | ||
assert!(storage.insert(entry.key, entry.value).is_none()) | ||
} | ||
|
||
let mut address_to_class_hash = HashMap::new(); | ||
for entry in self.state_diff.address_to_class_hash { | ||
assert!(address_to_class_hash | ||
.insert( | ||
Felt::from_bytes_be_slice(&entry.key), | ||
ClassHash(Felt::from_bytes_be_slice(&entry.value)) | ||
) | ||
.is_none()); | ||
} | ||
|
||
let mut address_to_nonce = HashMap::new(); | ||
for entry in self.state_diff.address_to_nonce { | ||
assert!(address_to_nonce | ||
.insert( | ||
Felt::from_bytes_be_slice(&entry.key), | ||
Nonce(Felt::from_bytes_be_slice(&entry.value)) | ||
) | ||
.is_none()); | ||
} | ||
|
||
let mut class_hash_to_compiled_class_hash = HashMap::new(); | ||
for entry in self.state_diff.class_hash_to_compiled_class_hash { | ||
assert!(class_hash_to_compiled_class_hash | ||
.insert( | ||
ClassHash(Felt::from_bytes_be_slice(&entry.key)), | ||
ClassHash(Felt::from_bytes_be_slice(&entry.value)) | ||
) | ||
.is_none()); | ||
} | ||
|
||
let mut current_contract_state_leaves = HashMap::new(); | ||
for entry in self.state_diff.current_contract_state_leaves { | ||
assert!(current_contract_state_leaves | ||
.insert( | ||
Felt::from_bytes_be_slice(&entry.address), | ||
ContractState { | ||
nonce: Nonce(Felt::from_bytes_be_slice(&entry.nonce)), | ||
class_hash: ClassHash(Felt::from_bytes_be_slice(&entry.class_hash)), | ||
storage_root_hash: Felt::from_bytes_be_slice(&entry.storage_root_hash) | ||
} | ||
) | ||
.is_none()); | ||
} | ||
|
||
let mut storage_updates: HashMap<Felt, HashMap<Felt, Felt>> = HashMap::new(); | ||
for outer_entry in self.state_diff.storage_updates { | ||
let tmp_map: HashMap<Felt, Felt> = outer_entry | ||
.storage_updates | ||
.iter() | ||
.map(|inner_entry| { | ||
( | ||
Felt::from_bytes_be_slice(&inner_entry.key), | ||
Felt::from_bytes_be_slice(&inner_entry.value), | ||
) | ||
}) | ||
.collect(); | ||
storage_updates.insert(Felt::from_bytes_be_slice(&outer_entry.address), tmp_map); | ||
} | ||
|
||
ActualInput { | ||
storage, | ||
state_diff: ActualStateDiff { | ||
address_to_class_hash, | ||
address_to_nonce, | ||
class_hash_to_compiled_class_hash, | ||
current_contract_state_leaves, | ||
storage_updates, | ||
}, | ||
} | ||
} | ||
} |
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,90 @@ | ||
use crate::patricia_merkle_tree::filled_node::{ClassHash, Nonce}; | ||
use serde::Deserialize; | ||
use starknet_types_core::felt::Felt; | ||
use std::collections::HashMap; | ||
|
||
type DeserializedFelt = Vec<u8>; | ||
type Bytes = Vec<u8>; | ||
type Address = Felt; | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[allow(dead_code)] | ||
/// Input to the committer. | ||
pub(crate) struct DeserializedInput { | ||
/// Storage. Will be casted to HashMap<Bytes, Bytes> to simulate DB access. | ||
pub storage: Vec<DeserializedStorageEntry>, | ||
/// All relevant information for the state diff commitment. | ||
pub state_diff: DeserializedStateDiff, | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub(crate) struct ActualInput { | ||
pub storage: HashMap<Bytes, Bytes>, | ||
pub state_diff: ActualStateDiff, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[allow(dead_code)] | ||
/// Fact storage entry. | ||
pub(crate) struct DeserializedStorageEntry { | ||
pub key: Bytes, | ||
pub value: Bytes, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[allow(dead_code)] | ||
pub(crate) struct DeserializedFeltMapEntry { | ||
pub key: DeserializedFelt, | ||
pub value: DeserializedFelt, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[allow(dead_code)] | ||
/// Represents storage updates. Later will be casted to HashMap<Felt, HashMap<Felt,Felt>> entry. | ||
pub(crate) struct DeserializedStorageUpdates { | ||
pub address: DeserializedFelt, | ||
pub storage_updates: Vec<DeserializedFeltMapEntry>, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[allow(dead_code)] | ||
/// Represents current state leaf at the contract state tree. Later will be casted to | ||
/// HashMap<Felt, (nonce, class_hash, storage_root_hash)> entry. | ||
pub(crate) struct DeserializedContractStateLeaf { | ||
pub address: DeserializedFelt, | ||
pub nonce: DeserializedFelt, | ||
pub storage_root_hash: DeserializedFelt, | ||
pub class_hash: DeserializedFelt, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[allow(dead_code)] | ||
/// Represents state diff. | ||
pub(crate) struct DeserializedStateDiff { | ||
/// Will be casted to HashMap<Felt,Felt>. | ||
pub address_to_class_hash: Vec<DeserializedFeltMapEntry>, | ||
/// Will be casted to HashMap<Felt,Felt>. | ||
pub address_to_nonce: Vec<DeserializedFeltMapEntry>, | ||
/// Will be casted to HashMap<Felt,Felt>. | ||
pub class_hash_to_compiled_class_hash: Vec<DeserializedFeltMapEntry>, | ||
/// Will be casted to HashMap<Felt,HashMap<Felt,Felt>>. | ||
pub storage_updates: Vec<DeserializedStorageUpdates>, | ||
/// Will be casted to HashMap<Felt,ContractState>. | ||
pub current_contract_state_leaves: Vec<DeserializedContractStateLeaf>, | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub(crate) struct ActualStateDiff { | ||
pub address_to_class_hash: HashMap<Address, ClassHash>, | ||
pub address_to_nonce: HashMap<Address, Nonce>, | ||
pub class_hash_to_compiled_class_hash: HashMap<ClassHash, ClassHash>, | ||
pub current_contract_state_leaves: HashMap<Address, ContractState>, | ||
pub storage_updates: HashMap<Address, HashMap<Address, Felt>>, | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub(crate) struct ContractState { | ||
pub nonce: Nonce, | ||
pub class_hash: ClassHash, | ||
pub storage_root_hash: Felt, | ||
} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod deserialization_impl; | ||
pub mod deserialization_types; | ||
pub mod hash; | ||
pub mod patricia_merkle_tree; | ||
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
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