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 Nov 23, 2023
1 parent 587e3ac commit 1eabf5f
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 @@ -51,7 +51,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 @@ -21,7 +21,7 @@ use nimiq_network_libp2p::{
discovery::{behaviour::DiscoveryConfig, peer_contacts::PeerContact},
Config, Network, PeerId,
};
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 @@ -50,27 +50,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 1eabf5f

Please sign in to comment.