Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mempool_infra): add serde utils #907

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
32 changes: 32 additions & 0 deletions crates/mempool_infra/src/serde_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::fmt::Debug;

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

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

// A generic wrapper struct for binary serialization and deserialization, used for remote component
// communication.
#[derive(Serialize, Deserialize, Debug)]
pub struct BincodeSerdeWrapper<T> {
data: T,
}

impl<T> BincodeSerdeWrapper<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::BincodeSerdeWrapper;

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 = BincodeSerdeWrapper::new(data).to_bincode().unwrap();
let decoded = BincodeSerdeWrapper::<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);
}
Loading