Skip to content

Commit

Permalink
dev: refactor custom serde for storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh Bajpai authored and Harsh Bajpai committed Nov 1, 2023
1 parent 8fbb186 commit 3d27d5b
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions crates/sequencer/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod serialize_contract_storage {
use blockifier::state::cached_state::ContractStorageKey;
use rustc_hash::FxHashMap;
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer};
use serde::ser::{Serialize, SerializeMap, Serializer};
use starknet_api::hash::StarkFelt;
use std::collections::HashMap;

Expand All @@ -66,13 +66,18 @@ mod serialize_contract_storage {
where
S: Serializer,
{
let mut transformed: HashMap<String, String> = HashMap::new();
for (contract_storage_key, storage_value) in map.iter() {
let key_str = serde_json::to_string(&contract_storage_key).unwrap();
let value_str = serde_json::to_string(&storage_value).unwrap();
transformed.insert(key_str, value_str);
let mut serialized_map = serializer.serialize_map(Some(map.len()))?;
for (k, v) in map {
let key = serde_json::to_string(k).map_err(|error| {
serde::ser::Error::custom(format!(
"failed to deserialize contract_storage_key {:?},\n error {}",
k, error
))
})?;

serialized_map.serialize_entry(&key, &v)?;
}
transformed.serialize(serializer)
serialized_map.end()
}

pub fn deserialize<'de, D>(
Expand All @@ -81,8 +86,8 @@ mod serialize_contract_storage {
where
D: Deserializer<'de>,
{
let transformed = HashMap::<String, String>::deserialize(deserializer)?;
let mut map = FxHashMap::default();
let transformed = HashMap::<String, StarkFelt>::deserialize(deserializer)?;
let mut map: FxHashMap<ContractStorageKey, StarkFelt> = FxHashMap::default();
for (key_str, value_str) in transformed.iter() {
let contract_storage_key: ContractStorageKey =
serde_json::from_str(key_str).map_err(|error| {
Expand All @@ -92,14 +97,7 @@ mod serialize_contract_storage {
))
})?;

let storage_value: StarkFelt = serde_json::from_str(value_str).map_err(|error| {
serde::de::Error::custom(format!(
"failed to deserialize storage_value {},\n error {}",
value_str, error
))
})?;

map.insert(contract_storage_key, storage_value);
map.insert(contract_storage_key, value_str.to_owned());
}
Ok(map)
}
Expand Down

0 comments on commit 3d27d5b

Please sign in to comment.