Skip to content

Commit

Permalink
wip - tests running but still without cargo update
Browse files Browse the repository at this point in the history
commit-id:c0132bbb
  • Loading branch information
Itay-Tsabary-Starkware committed Sep 20, 2024
1 parent fc17d2d commit 9da1939
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ use crate::component_definitions::{BincodeSerializable, SerdeWrapper, APPLICATIO
/// use crate::starknet_mempool_infra::component_client::RemoteComponentClient;
///
/// // Define your request and response types
/// #[derive(Serialize, Deserialize, Clone)]
/// #[derive(Serialize, Deserialize, Debug, Clone)]
/// struct MyRequest {
/// pub content: String,
/// }
///
/// #[derive(Serialize, Deserialize)]
/// #[derive(Serialize, Deserialize, Debug)]
/// struct MyResponse {
/// content: String,
/// }
Expand Down Expand Up @@ -78,8 +78,8 @@ where

impl<Request, Response> RemoteComponentClient<Request, Response>
where
Request: Serialize + DeserializeOwned + std::fmt::Debug+ Clone,
Response: Serialize + DeserializeOwned+ std::fmt::Debug,
Request: Serialize + DeserializeOwned + std::fmt::Debug + Clone,
Response: Serialize + DeserializeOwned + std::fmt::Debug,
{
pub fn new(ip_address: IpAddr, port: u16, max_retries: usize) -> Self {
let uri = match ip_address {
Expand Down
13 changes: 7 additions & 6 deletions crates/mempool_infra/src/component_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::sync::mpsc::{Receiver, Sender};
use validator::Validate;
use tracing::{debug, error, info, instrument};
use validator::Validate;

const DEFAULT_CHANNEL_BUFFER_SIZE: usize = 32;
const DEFAULT_RETRIES: usize = 3;
Expand Down Expand Up @@ -122,8 +122,6 @@ impl Default for RemoteComponentCommunicationConfig {
}
}



// Generic wrapper struct
#[derive(Serialize, Deserialize, std::fmt::Debug)]
pub(crate) struct SerdeWrapper<T> {
Expand All @@ -137,13 +135,16 @@ pub(crate) trait BincodeSerializable: Sized {
}

// Implement the trait for our wrapper
impl<T: Serialize + for<'de> Deserialize<'de>> BincodeSerializable for SerdeWrapper<T> where T: std::fmt::Debug {
#[instrument]
impl<T: Serialize + for<'de> Deserialize<'de>> BincodeSerializable for SerdeWrapper<T>
where
T: std::fmt::Debug,
{
#[instrument(err, ret)]
fn to_bincode(&self) -> Result<Vec<u8>, bincode::Error> {
serialize(self)
}

#[instrument]
#[instrument(err, ret)]
fn from_bincode(bytes: &[u8]) -> Result<Self, bincode::Error> {
deserialize(bytes)
}
Expand Down
40 changes: 33 additions & 7 deletions crates/mempool_infra/src/component_definitions_test.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@

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

use crate::component_definitions::{BincodeSerializable, SerdeWrapper};
use crate::trace_util::configure_tracing;


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

let encoded =
SerdeWrapper { data }.to_bincode().expect("Server error serialization should succeed");
let decoded = SerdeWrapper::<u32>::from_bincode(&encoded).unwrap();

assert_eq!(data, decoded.data);
}

let data : u32 = 8;
#[test]
fn test_serde_struct_type() {
#[derive(Serialize, Deserialize, std::fmt::Debug, Clone, std::cmp::PartialEq, Copy)]
struct TestStruct {
a: u32,
b: u32,
}

let encoded = SerdeWrapper { data } .to_bincode() .expect("Server error serialization should succeed");
let decoded = SerdeWrapper::<u32>::from_bincode(&encoded).unwrap();
let data: TestStruct = TestStruct { a: 17, b: 8 };

let encoded =
SerdeWrapper { data }.to_bincode().expect("Server error serialization should succeed");
let decoded = SerdeWrapper::<TestStruct>::from_bincode(&encoded).unwrap();

assert_eq!(data, decoded.data);
}

#[test]
fn test_serde_felt() {
configure_tracing();

let data: Felt = Felt::ONE;

let encoded =
SerdeWrapper { data }.to_bincode().expect("Server error serialization should succeed");
let decoded = SerdeWrapper::<Felt>::from_bincode(&encoded).unwrap();

}
assert_eq!(data, decoded.data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ use crate::component_definitions::{
/// }
///
/// // Define your request and response types
/// #[derive(Serialize, Deserialize)]
/// #[derive(Serialize, Deserialize, Debug)]
/// struct MyRequest {
/// pub content: String,
/// }
///
/// #[derive(Serialize, Deserialize)]
/// #[derive(Serialize, Deserialize, Debug)]
/// struct MyResponse {
/// content: String,
/// }
Expand Down Expand Up @@ -112,8 +112,8 @@ where

impl<Request, Response> RemoteComponentServer<Request, Response>
where
Request: Serialize + DeserializeOwned+ std::fmt::Debug + Send + Sync + 'static,
Response: Serialize + DeserializeOwned + std::fmt::Debug+ Send + Sync + 'static,
Request: Serialize + DeserializeOwned + std::fmt::Debug + Send + Sync + 'static,
Response: Serialize + DeserializeOwned + std::fmt::Debug + Send + Sync + 'static,
{
pub fn new(
tx: Sender<ComponentRequestAndResponseSender<Request, Response>>,
Expand Down Expand Up @@ -162,8 +162,8 @@ where
#[async_trait]
impl<Request, Response> ComponentServerStarter for RemoteComponentServer<Request, Response>
where
Request: Serialize + DeserializeOwned + Send + Sync+ std::fmt::Debug + 'static,
Response: Serialize + DeserializeOwned + Send + Sync+ std::fmt::Debug + 'static,
Request: Serialize + DeserializeOwned + Send + Sync + std::fmt::Debug + 'static,
Response: Serialize + DeserializeOwned + Send + Sync + std::fmt::Debug + 'static,
{
async fn start(&mut self) {
let make_svc = make_service_fn(|_conn| {
Expand Down

0 comments on commit 9da1939

Please sign in to comment.