Skip to content

Commit 4d84969

Browse files
committed
starknet_committer,starknet_patricia: add serde errors
1 parent b7d1875 commit 4d84969

File tree

13 files changed

+69
-45
lines changed

13 files changed

+69
-45
lines changed

crates/starknet_committer/src/db/external_test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub async fn single_tree_flow_test<
9191
let json_hash = &json!(hash_result.0.to_hex_string());
9292
result_map.insert("root_hash", json_hash);
9393
// Serlialize the storage modifications.
94-
let json_storage = &json!(filled_tree.serialize(&EmptyKeyContext));
94+
let json_storage = &json!(filled_tree.serialize(&EmptyKeyContext).unwrap());
9595
result_map.insert("storage_changes", json_storage);
9696
serde_json::to_string(&result_map).expect("serialization failed")
9797
}

crates/starknet_committer/src/db/facts_db/create_facts_tree_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ async fn test_create_tree(
226226

227227
pub(crate) fn create_mock_leaf_entry(val: u128) -> (DbKey, DbValue) {
228228
let leaf = MockLeaf(Felt::from(val));
229-
(leaf.get_db_key(&EmptyKeyContext, &leaf.0.to_bytes_be()), leaf.serialize())
229+
(leaf.get_db_key(&EmptyKeyContext, &leaf.0.to_bytes_be()), leaf.serialize().unwrap())
230230
}
231231

232232
fn create_mock_leaf_modifications(

crates/starknet_committer/src/db/facts_db/db.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use starknet_patricia::patricia_merkle_tree::node_data::leaf::{Leaf, LeafModific
99
use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTreeImpl;
1010
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
1111
use starknet_patricia_storage::db_object::EmptyKeyContext;
12+
use starknet_patricia_storage::errors::SerializationError;
1213
use starknet_patricia_storage::map_storage::MapStorage;
1314
use starknet_patricia_storage::storage_trait::{
1415
create_db_key,
@@ -189,14 +190,19 @@ impl<S: Storage> ForestReader<FactsDbInitialRead> for FactsDb<S> {
189190

190191
#[async_trait]
191192
impl<S: Storage> ForestWriter for FactsDb<S> {
192-
fn serialize_forest(filled_forest: &FilledForest) -> DbHashMap {
193-
filled_forest
194-
.storage_tries
195-
.values()
196-
.flat_map(|tree| tree.serialize(&EmptyKeyContext).into_iter())
197-
.chain(filled_forest.contracts_trie.serialize(&EmptyKeyContext))
198-
.chain(filled_forest.classes_trie.serialize(&EmptyKeyContext))
199-
.collect()
193+
fn serialize_forest(filled_forest: &FilledForest) -> Result<DbHashMap, SerializationError> {
194+
let mut out = DbHashMap::new();
195+
196+
// storage tries
197+
for tree in filled_forest.storage_tries.values() {
198+
out.extend(tree.serialize(&EmptyKeyContext)?);
199+
}
200+
201+
// other tries
202+
out.extend(filled_forest.contracts_trie.serialize(&EmptyKeyContext)?);
203+
out.extend(filled_forest.classes_trie.serialize(&EmptyKeyContext)?);
204+
205+
Ok(out)
200206
}
201207

202208
async fn write_updates(&mut self, updates: DbHashMap) -> usize {

crates/starknet_committer/src/db/forest_trait.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use starknet_api::block::BlockNumber;
55
use starknet_api::core::ContractAddress;
66
use starknet_patricia::patricia_merkle_tree::node_data::leaf::LeafModifications;
77
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
8+
use starknet_patricia_storage::errors::SerializationError;
89
use starknet_patricia_storage::storage_trait::{DbHashMap, DbKey, DbValue, Storage};
910

1011
use crate::block_committer::input::{ConfigImpl, InputContext, StarknetStorageValue};
@@ -63,28 +64,28 @@ pub trait ForestReader<I: InputContext> {
6364
#[async_trait]
6465
pub trait ForestWriter: ForestMetadata + Send {
6566
/// Serializes a filled forest into a hash map.
66-
fn serialize_forest(filled_forest: &FilledForest) -> DbHashMap;
67+
fn serialize_forest(filled_forest: &FilledForest) -> Result<DbHashMap, SerializationError>;
6768

6869
/// Writes the updates map to storage. Returns the number of new updates written to storage.
6970
async fn write_updates(&mut self, updates: DbHashMap) -> usize;
7071

7172
/// Writes the serialized filled forest to storage. Returns the number of new updates written to
7273
/// storage.
73-
async fn write(&mut self, filled_forest: &FilledForest) -> usize {
74-
let updates = Self::serialize_forest(filled_forest);
75-
self.write_updates(updates).await
74+
async fn write(&mut self, filled_forest: &FilledForest) -> Result<usize, SerializationError> {
75+
let updates = Self::serialize_forest(filled_forest)?;
76+
Ok(self.write_updates(updates).await)
7677
}
7778

7879
async fn write_with_metadata(
7980
&mut self,
8081
filled_forest: &FilledForest,
8182
metadata: HashMap<ForestMetadataType, DbValue>,
82-
) -> usize {
83-
let mut updates = Self::serialize_forest(filled_forest);
83+
) -> Result<usize, SerializationError> {
84+
let mut updates = Self::serialize_forest(filled_forest)?;
8485
for (metadata_type, value) in metadata {
8586
Self::insert_metadata(&mut updates, metadata_type, value);
8687
}
87-
self.write_updates(updates).await
88+
Ok(self.write_updates(updates).await)
8889
}
8990
}
9091

crates/starknet_committer/src/forest/skeleton_forest_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ macro_rules! compare_skeleton_tree {
5151

5252
pub(crate) fn create_storage_leaf_entry(val: u128) -> (DbKey, DbValue) {
5353
let leaf = StarknetStorageValue(Felt::from(val));
54-
(leaf.get_db_key(&EmptyKeyContext, &leaf.0.to_bytes_be()), leaf.serialize())
54+
(leaf.get_db_key(&EmptyKeyContext, &leaf.0.to_bytes_be()), leaf.serialize().unwrap())
5555
}
5656

5757
pub(crate) fn create_compiled_class_leaf_entry(val: u128) -> (DbKey, DbValue) {
5858
let leaf = CompiledClassHash(Felt::from(val));
59-
(leaf.get_db_key(&EmptyKeyContext, &leaf.0.to_bytes_be()), leaf.serialize())
59+
(leaf.get_db_key(&EmptyKeyContext, &leaf.0.to_bytes_be()), leaf.serialize().unwrap())
6060
}
6161

6262
pub(crate) fn create_contract_state_leaf_entry(val: u128) -> (DbKey, DbValue) {
@@ -66,7 +66,7 @@ pub(crate) fn create_contract_state_leaf_entry(val: u128) -> (DbKey, DbValue) {
6666
storage_root_hash: HashOutput(felt),
6767
class_hash: ClassHash(felt),
6868
};
69-
(leaf.get_db_key(&EmptyKeyContext, &felt.to_bytes_be()), leaf.serialize())
69+
(leaf.get_db_key(&EmptyKeyContext, &felt.to_bytes_be()), leaf.serialize().unwrap())
7070
}
7171

7272
// This test uses addition hash for simplicity (i.e hash(a,b) = a + b).

crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_serde.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use starknet_api::core::{ClassHash, Nonce};
55
use starknet_api::hash::HashOutput;
66
use starknet_patricia::patricia_merkle_tree::types::SubTreeHeight;
77
use starknet_patricia_storage::db_object::{DBObject, EmptyDeserializationContext};
8-
use starknet_patricia_storage::errors::DeserializationError;
8+
use starknet_patricia_storage::errors::{DeserializationError, SerializationError};
99
use starknet_patricia_storage::storage_trait::{DbKeyPrefix, DbValue};
1010
use starknet_types_core::felt::Felt;
1111

@@ -34,8 +34,8 @@ impl DBObject for StarknetStorageValue {
3434
type DeserializeContext = EmptyDeserializationContext;
3535

3636
/// Serializes the value into a 32-byte vector.
37-
fn serialize(&self) -> DbValue {
38-
DbValue(self.0.to_bytes_be().to_vec())
37+
fn serialize(&self) -> Result<DbValue, SerializationError> {
38+
Ok(DbValue(self.0.to_bytes_be().to_vec()))
3939
}
4040

4141
fn deserialize(
@@ -50,9 +50,9 @@ impl DBObject for CompiledClassHash {
5050
type DeserializeContext = EmptyDeserializationContext;
5151

5252
/// Creates a json string describing the leaf and casts it into a byte vector.
53-
fn serialize(&self) -> DbValue {
53+
fn serialize(&self) -> Result<DbValue, SerializationError> {
5454
let json_string = format!(r#"{{"compiled_class_hash": "{}"}}"#, self.0.to_hex_string());
55-
DbValue(json_string.into_bytes())
55+
Ok(DbValue(json_string.into_bytes()))
5656
}
5757

5858
fn deserialize(
@@ -72,15 +72,15 @@ impl DBObject for ContractState {
7272
type DeserializeContext = EmptyDeserializationContext;
7373

7474
/// Creates a json string describing the leaf and casts it into a byte vector.
75-
fn serialize(&self) -> DbValue {
75+
fn serialize(&self) -> Result<DbValue, SerializationError> {
7676
let json_string = format!(
7777
r#"{{"contract_hash": "{}", "storage_commitment_tree": {{"root": "{}", "height": {}}}, "nonce": "{}"}}"#,
7878
fixed_hex_string_no_prefix(&self.class_hash.0),
7979
fixed_hex_string_no_prefix(&self.storage_root_hash.0),
8080
SubTreeHeight::ACTUAL_HEIGHT,
8181
self.nonce.0.to_hex_string(),
8282
);
83-
DbValue(json_string.into_bytes())
83+
Ok(DbValue(json_string.into_bytes()))
8484
}
8585

8686
fn deserialize(

crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_serde_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::patricia_merkle_tree::types::CompiledClassHash;
3131
})
3232
]
3333
fn test_leaf_serde<L: Leaf + Eq + Debug>(#[case] leaf: L) {
34-
let serialized = leaf.serialize();
34+
let serialized = leaf.serialize().unwrap();
3535
let deserialized = L::deserialize(&serialized, &EmptyDeserializationContext).unwrap();
3636
assert_eq!(deserialized, leaf);
3737
}

crates/starknet_committer_cli/src/commands.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ pub async fn run_storage_benchmark<S: Storage>(
360360
.await
361361
.expect("Failed to commit the given block.");
362362
time_measurement.start_measurement(Action::Write);
363-
let n_new_facts = facts_db.write(&filled_forest).await;
363+
let n_new_facts = facts_db.write(&filled_forest).await.unwrap();
364364
info!("Written {n_new_facts} new facts to storage");
365365
time_measurement.stop_measurement(None, Action::Write);
366366

crates/starknet_patricia/src/patricia_merkle_tree/external_test_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use starknet_patricia_storage::db_object::{
1010
EmptyKeyContext,
1111
HasStaticPrefix,
1212
};
13-
use starknet_patricia_storage::errors::DeserializationError;
13+
use starknet_patricia_storage::errors::{DeserializationError, SerializationError};
1414
use starknet_patricia_storage::storage_trait::{create_db_key, DbKey, DbKeyPrefix, DbValue};
1515
use starknet_types_core::felt::Felt;
1616
use starknet_types_core::hash::StarkHash;
@@ -39,8 +39,8 @@ impl HasStaticPrefix for MockLeaf {
3939
impl DBObject for MockLeaf {
4040
type DeserializeContext = EmptyDeserializationContext;
4141

42-
fn serialize(&self) -> DbValue {
43-
DbValue(self.0.to_bytes_be().to_vec())
42+
fn serialize(&self) -> Result<DbValue, SerializationError> {
43+
Ok(DbValue(self.0.to_bytes_be().to_vec()))
4444
}
4545

4646
fn deserialize(

crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/node_serde.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use starknet_patricia_storage::db_object::{
66
HasDynamicPrefix,
77
HasStaticPrefix,
88
};
9-
use starknet_patricia_storage::errors::DeserializationError;
9+
use starknet_patricia_storage::errors::{DeserializationError, SerializationError};
1010
use starknet_patricia_storage::storage_trait::{DbKey, DbKeyPrefix, DbValue};
1111
use starknet_types_core::felt::Felt;
1212

@@ -82,7 +82,7 @@ impl<L: Leaf> DBObject for FactDbFilledNode<L> {
8282
/// - For binary nodes: Concatenates left and right hashes.
8383
/// - For edge nodes: Concatenates bottom hash, path, and path length.
8484
/// - For leaf nodes: use leaf.serialize() method.
85-
fn serialize(&self) -> DbValue {
85+
fn serialize(&self) -> Result<DbValue, SerializationError> {
8686
match &self.data {
8787
NodeData::Binary(BinaryData { left_data: left_hash, right_data: right_hash }) => {
8888
// Serialize left and right hashes to byte arrays.
@@ -91,7 +91,7 @@ impl<L: Leaf> DBObject for FactDbFilledNode<L> {
9191

9292
// Concatenate left and right hashes.
9393
let serialized = [left, right].concat();
94-
DbValue(serialized)
94+
Ok(DbValue(serialized))
9595
}
9696

9797
NodeData::Edge(EdgeData { bottom_data: bottom_hash, path_to_bottom }) => {
@@ -103,7 +103,7 @@ impl<L: Leaf> DBObject for FactDbFilledNode<L> {
103103

104104
// Concatenate bottom hash, path, and path length.
105105
let serialized = [bottom.to_vec(), path.to_vec(), length.to_vec()].concat();
106-
DbValue(serialized)
106+
Ok(DbValue(serialized))
107107
}
108108

109109
NodeData::Leaf(leaf_data) => leaf_data.serialize(),

0 commit comments

Comments
 (0)