Skip to content

Commit

Permalink
chore: add deserialization types and basic casting implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrod-starkware committed Apr 9, 2024
1 parent 707a58d commit 2ad6a38
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Expand Up @@ -14,6 +14,8 @@ license-file = "LICENSE"

[workspace.dependencies]
pretty_assertions = "1.2.1"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0"

[workspace.lints.rust]
warnings = "deny"
Expand Down
2 changes: 2 additions & 0 deletions crates/committer/Cargo.toml
Expand Up @@ -15,3 +15,5 @@ pretty_assertions.workspace = true
[dependencies]
starknet-types-core = "0.0.11"
starknet_api = "0.12.0-dev.0"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0"
87 changes: 87 additions & 0 deletions crates/committer/src/deserialization_impl.rs
@@ -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,
},
}
}
}
90 changes: 90 additions & 0 deletions crates/committer/src/deserialization_types.rs
@@ -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,
}
2 changes: 2 additions & 0 deletions crates/committer/src/lib.rs
@@ -1,3 +1,5 @@
pub mod deserialization_impl;
pub mod deserialization_types;
pub mod hash;
pub mod patricia_merkle_tree;
pub mod types;
1 change: 1 addition & 0 deletions crates/committer/src/patricia_merkle_tree/filled_node.rs
Expand Up @@ -2,6 +2,7 @@ use crate::patricia_merkle_tree::types::{EdgeData, LeafDataTrait};
use crate::{hash::types::HashOutput, types::Felt};
// TODO(Nimrod, 1/6/2024): Swap to starknet-types-core types once implemented.
#[allow(dead_code)]
#[derive(Eq, PartialEq, Hash)]
pub(crate) struct ClassHash(pub Felt);
#[allow(dead_code)]
pub(crate) struct Nonce(pub Felt);
Expand Down
1 change: 1 addition & 0 deletions crates/committer/src/patricia_merkle_tree/types.rs
Expand Up @@ -9,6 +9,7 @@ pub(crate) trait TreeHashFunction<L: LeafDataTrait, H: HashFunction> {

// TODO(Amos, 01/05/2024): Implement types for NodeIndex, EdgePath, EdgePathLength
#[allow(dead_code)]
#[derive(Eq, PartialEq, Hash)]
pub(crate) struct NodeIndex(pub Felt);

#[allow(dead_code)]
Expand Down

0 comments on commit 2ad6a38

Please sign in to comment.