Skip to content

Commit

Permalink
Move serialize methods out of the RequestCommon trait
Browse files Browse the repository at this point in the history
Implementers shouldn't override it.
  • Loading branch information
hrxi committed Dec 6, 2023
1 parent 6454581 commit 96cf992
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 24 deletions.
4 changes: 4 additions & 0 deletions network-interface/src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ pub trait RequestCommon:
type Response: Deserialize + Serialize + Send;
const MAX_REQUESTS: u32;
const TIME_WINDOW: Duration = DEFAULT_MAX_REQUEST_RESPONSE_TIME_WINDOW;
}

pub trait RequestSerialize: RequestCommon {
/// Serializes a request.
/// A serialized request is composed of:
/// - A varint for the Type ID of the request
Expand Down Expand Up @@ -177,6 +179,8 @@ pub trait RequestCommon:
}
}

impl<T: RequestCommon> RequestSerialize for T {}

pub trait Request: RequestCommon<Kind = RequestMarker> {}
pub trait Message: RequestCommon<Kind = MessageMarker, Response = ()> {}

Expand Down
2 changes: 1 addition & 1 deletion network-libp2p/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use nimiq_network_interface::{
peer_info::{PeerInfo, Services},
request::{
peek_type, InboundRequestError, Message, OutboundRequestError, Request, RequestCommon,
RequestError, RequestType,
RequestError, RequestSerialize, RequestType,
},
};
use nimiq_primitives::task_executor::TaskExecutor;
Expand Down
23 changes: 1 addition & 22 deletions network-libp2p/tests/request_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use nimiq_network_libp2p::{
discovery::{self, peer_contacts::PeerContact},
Config, Network,
};
use nimiq_serde::{Deserialize, DeserializeError, Serialize};
use nimiq_serde::{Deserialize, Serialize};
use nimiq_test_log::test;
use nimiq_utils::time::OffsetTime;
use rand::{thread_rng, Rng};
Expand Down Expand Up @@ -48,27 +48,6 @@ impl RequestCommon for TestRequest {
type Response = TestResponse;

const MAX_REQUESTS: u32 = MAX_REQUEST_RESPONSE_TEST_REQUEST;

fn serialize_request(&self) -> Vec<u8> {
let mut data = Vec::with_capacity(self.serialized_request_size());
nimiq_network_interface::request::RequestType::from_request::<Self>()
.serialize_to_writer(&mut data)
.unwrap();
Serialize::serialize_to_writer(self, &mut data).unwrap();
data
}

fn deserialize_request(buffer: &[u8]) -> Result<Self, DeserializeError> {
// Check for correct type.
let (ty, message_buf) = <u16>::deserialize_take(buffer)?;
if ty != nimiq_network_interface::request::RequestType::from_request::<Self>().0 {
return Err(DeserializeError::bad_encoding());
}

let message: Self = Deserialize::deserialize_from_vec(message_buf)?;

Ok(message)
}
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
struct TestResponse {
Expand Down
2 changes: 1 addition & 1 deletion network-mock/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use nimiq_network_interface::{
peer_info::{PeerInfo, Services},
request::{
InboundRequestError, Message, OutboundRequestError, Request, RequestCommon, RequestError,
RequestKind, RequestType,
RequestKind, RequestSerialize, RequestType,
},
};
use nimiq_serde::{Deserialize, DeserializeError, Serialize};
Expand Down

0 comments on commit 96cf992

Please sign in to comment.