Skip to content

Commit

Permalink
feat(mempool_infra): add serde utils
Browse files Browse the repository at this point in the history
commit-id:93bc459d
  • Loading branch information
Itay-Tsabary-Starkware committed Sep 22, 2024
1 parent 169929f commit 74e4436
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/mempool_infra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ hyper = { workspace = true, features = ["client", "http2", "server", "tcp"] }
papyrus_config.workspace = true
rstest.workspace = true
serde = { workspace = true, features = ["derive"] }
starknet-types-core.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
tracing.workspace = true
Expand Down
1 change: 1 addition & 0 deletions crates/mempool_infra/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pub mod component_client;
pub mod component_definitions;
pub mod component_runner;
pub mod component_server;
pub mod serde_utils;
pub mod trace_util;
31 changes: 31 additions & 0 deletions crates/mempool_infra/src/serde_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::fmt::Debug;

use bincode::{deserialize, serialize};
use serde::{Deserialize, Serialize};

#[cfg(test)]
#[path = "serde_utils_test.rs"]
pub mod serde_utils_test;

// Generic wrapper struct
#[derive(Serialize, Deserialize, Debug)]
pub struct SerdeWrapper<T> {
data: T,
}

impl<T> SerdeWrapper<T>
where
T: Serialize + for<'de> Deserialize<'de> + Debug,
{
pub fn new(data: T) -> Self {
Self { data }
}

pub fn to_bincode(&self) -> Result<Vec<u8>, bincode::Error> {
serialize(self)
}

pub fn from_bincode(bytes: &[u8]) -> Result<T, bincode::Error> {
deserialize(bytes).map(|serde_wrapper: Self| serde_wrapper.data)
}
}
42 changes: 42 additions & 0 deletions crates/mempool_infra/src/serde_utils_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::fmt::Debug;

use serde::{Deserialize, Serialize};
use starknet_types_core::felt::Felt;

use crate::serde_utils::SerdeWrapper;

fn test_generic_data_serde<T>(data: T)
where
T: Serialize + for<'de> Deserialize<'de> + Debug + Clone + Copy + PartialEq,
{
// Serialize and deserialize the data.
let encoded = SerdeWrapper::new(data).to_bincode().unwrap();
let decoded = SerdeWrapper::<T>::from_bincode(&encoded).unwrap();

// Assert that the data is the same after serialization and deserialization.
assert_eq!(data, decoded);
}

#[test]
fn test_serde_native_type() {
let data: u32 = 8;
test_generic_data_serde(data);
}

#[test]
fn test_serde_struct_type() {
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
struct TestStruct {
a: u32,
b: u32,
}

let data: TestStruct = TestStruct { a: 17, b: 8 };
test_generic_data_serde(data);
}

#[test]
fn test_serde_felt() {
let data: Felt = Felt::ONE;
test_generic_data_serde(data);
}

0 comments on commit 74e4436

Please sign in to comment.