diff --git a/network-interface/src/request/mod.rs b/network-interface/src/request/mod.rs index 10a76d07c8..33c99e3312 100644 --- a/network-interface/src/request/mod.rs +++ b/network-interface/src/request/mod.rs @@ -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 @@ -177,6 +179,8 @@ pub trait RequestCommon: } } +impl RequestSerialize for T {} + pub trait Request: RequestCommon {} pub trait Message: RequestCommon {} diff --git a/network-libp2p/src/network.rs b/network-libp2p/src/network.rs index 1f7b3c50ea..2ef7360ee0 100644 --- a/network-libp2p/src/network.rs +++ b/network-libp2p/src/network.rs @@ -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; diff --git a/network-libp2p/tests/request_response.rs b/network-libp2p/tests/request_response.rs index cd3fb748c6..127441ee1e 100644 --- a/network-libp2p/tests/request_response.rs +++ b/network-libp2p/tests/request_response.rs @@ -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}; @@ -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 { - let mut data = Vec::with_capacity(self.serialized_request_size()); - nimiq_network_interface::request::RequestType::from_request::() - .serialize_to_writer(&mut data) - .unwrap(); - Serialize::serialize_to_writer(self, &mut data).unwrap(); - data - } - - fn deserialize_request(buffer: &[u8]) -> Result { - // Check for correct type. - let (ty, message_buf) = ::deserialize_take(buffer)?; - if ty != nimiq_network_interface::request::RequestType::from_request::().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 { diff --git a/network-mock/src/network.rs b/network-mock/src/network.rs index 5121771b0c..6ecf58206a 100644 --- a/network-mock/src/network.rs +++ b/network-mock/src/network.rs @@ -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};