Skip to content

Commit

Permalink
chore: rename BincodeSerdeWrapper to SerdeWrapper
Browse files Browse the repository at this point in the history
commit-id:6fdc6c31
  • Loading branch information
nadin-Starkware committed Dec 8, 2024
1 parent 0896088 commit 24690f4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use serde::Serialize;

use super::definitions::{ClientError, ClientResult};
use crate::component_definitions::{ComponentClient, RemoteClientConfig, APPLICATION_OCTET_STREAM};
use crate::serde_utils::BincodeSerdeWrapper;
use crate::serde_utils::SerdeWrapper;

/// The `RemoteComponentClient` struct is a generic client for sending component requests and
/// receiving responses asynchronously through HTTP connection.
Expand Down Expand Up @@ -143,8 +143,8 @@ where
{
async fn send(&self, component_request: Request) -> ClientResult<Response> {
// Serialize the request.
let serialized_request = BincodeSerdeWrapper::new(component_request)
.to_bincode()
let serialized_request = SerdeWrapper::new(component_request)
.wrapper_serialize()
.expect("Request serialization should succeed");

// Construct the request, and send it up to 'max_retries + 1' times. Return if received a
Expand Down Expand Up @@ -172,7 +172,7 @@ where
.await
.map_err(|e| ClientError::ResponseParsingFailure(Arc::new(e)))?;

BincodeSerdeWrapper::<Response>::from_bincode(&body_bytes)
SerdeWrapper::<Response>::wrapper_deserialize(&body_bytes)
.map_err(|e| ClientError::ResponseDeserializationFailure(Arc::new(e)))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::component_definitions::{
};
use crate::component_server::ComponentServerStarter;
use crate::errors::ComponentServerError;
use crate::serde_utils::BincodeSerdeWrapper;
use crate::serde_utils::SerdeWrapper;

/// The `RemoteComponentServer` struct is a generic server that handles requests and responses for a
/// specified component. It receives requests, processes them using the provided component, and
Expand Down Expand Up @@ -131,7 +131,7 @@ where
) -> Result<HyperResponse<Body>, hyper::Error> {
let body_bytes = to_bytes(http_request.into_body()).await?;

let http_response = match BincodeSerdeWrapper::<Request>::from_bincode(&body_bytes)
let http_response = match SerdeWrapper::<Request>::wrapper_deserialize(&body_bytes)
.map_err(|e| ClientError::ResponseDeserializationFailure(Arc::new(e)))
{
Ok(request) => {
Expand All @@ -141,8 +141,8 @@ where
.status(StatusCode::OK)
.header(CONTENT_TYPE, APPLICATION_OCTET_STREAM)
.body(Body::from(
BincodeSerdeWrapper::new(response)
.to_bincode()
SerdeWrapper::new(response)
.wrapper_serialize()
.expect("Response serialization should succeed"),
)),
Err(error) => {
Expand All @@ -156,8 +156,8 @@ where
Err(error) => {
let server_error = ServerError::RequestDeserializationFailure(error.to_string());
HyperResponse::builder().status(StatusCode::BAD_REQUEST).body(Body::from(
BincodeSerdeWrapper::new(server_error)
.to_bincode()
SerdeWrapper::new(server_error)
.wrapper_serialize()
.expect("Server error serialization should succeed"),
))
}
Expand Down
8 changes: 4 additions & 4 deletions crates/starknet_sequencer_infra/src/serde_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ 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> {
pub struct SerdeWrapper<T> {
data: T,
}

impl<T> BincodeSerdeWrapper<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> {
pub fn wrapper_serialize(&self) -> Result<Vec<u8>, bincode::Error> {
serialize(self)
}

pub fn from_bincode(bytes: &[u8]) -> Result<T, bincode::Error> {
pub fn wrapper_deserialize(bytes: &[u8]) -> Result<T, bincode::Error> {
deserialize(bytes).map(|serde_wrapper: Self| serde_wrapper.data)
}
}
6 changes: 3 additions & 3 deletions crates/starknet_sequencer_infra/src/serde_utils_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use std::fmt::Debug;
use serde::{Deserialize, Serialize};
use starknet_types_core::felt::Felt;

use crate::serde_utils::BincodeSerdeWrapper;
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 = BincodeSerdeWrapper::new(data).to_bincode().unwrap();
let decoded = BincodeSerdeWrapper::<T>::from_bincode(&encoded).unwrap();
let encoded = SerdeWrapper::new(data).wrapper_serialize().unwrap();
let decoded = SerdeWrapper::<T>::wrapper_deserialize(&encoded).unwrap();

// Assert that the data is the same after serialization and deserialization.
assert_eq!(data, decoded);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::component_server::{
LocalComponentServer,
RemoteComponentServer,
};
use crate::serde_utils::BincodeSerdeWrapper;
use crate::serde_utils::SerdeWrapper;
use crate::test_utils::get_available_socket;
use crate::tests::{
test_a_b_functionality,
Expand Down Expand Up @@ -128,7 +128,7 @@ where
{
Ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(BincodeSerdeWrapper::new(body).to_bincode().unwrap()))
.body(Body::from(SerdeWrapper::new(body).wrapper_serialize().unwrap()))
.unwrap())
}

Expand Down Expand Up @@ -227,12 +227,12 @@ async fn test_faulty_client_setup() {
format!("http://[{}]:{}/", self.socket.ip(), self.socket.port()).parse().unwrap();
let http_request = Request::post(uri)
.header(CONTENT_TYPE, APPLICATION_OCTET_STREAM)
.body(Body::from(BincodeSerdeWrapper::new(component_request).to_bincode().unwrap()))
.body(Body::from(SerdeWrapper::new(component_request).wrapper_serialize().unwrap()))
.unwrap();
let http_response = Client::new().request(http_request).await.unwrap();
let status_code = http_response.status();
let body_bytes = to_bytes(http_response.into_body()).await.unwrap();
let response = BincodeSerdeWrapper::<ServerError>::from_bincode(&body_bytes).unwrap();
let response = SerdeWrapper::<ServerError>::wrapper_deserialize(&body_bytes).unwrap();
Err(ClientError::ResponseError(status_code, response))
}
}
Expand Down Expand Up @@ -285,12 +285,12 @@ async fn test_retry_request() {
let ret = if *should_send_ok {
Response::builder()
.status(StatusCode::OK)
.body(Body::from(BincodeSerdeWrapper::new(body).to_bincode().unwrap()))
.body(Body::from(SerdeWrapper::new(body).wrapper_serialize().unwrap()))
.unwrap()
} else {
Response::builder()
.status(StatusCode::IM_A_TEAPOT)
.body(Body::from(BincodeSerdeWrapper::new(body).to_bincode().unwrap()))
.body(Body::from(SerdeWrapper::new(body).wrapper_serialize().unwrap()))
.unwrap()
};
*should_send_ok = !*should_send_ok;
Expand Down

0 comments on commit 24690f4

Please sign in to comment.