From c564941d769d1524d04dfec9675b6f7da7d5b5f6 Mon Sep 17 00:00:00 2001 From: acerone85 Date: Mon, 28 Oct 2024 22:12:19 +0000 Subject: [PATCH] Decode trait function takes &self as argument --- crates/fuel-core/src/p2p_test_helpers.rs | 2 +- crates/services/p2p/src/codecs.rs | 2 +- crates/services/p2p/src/codecs/gossipsub.rs | 2 +- crates/services/p2p/src/codecs/postcard.rs | 6 +++--- crates/services/p2p/src/codecs/request_response.rs | 10 ++++------ 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/crates/fuel-core/src/p2p_test_helpers.rs b/crates/fuel-core/src/p2p_test_helpers.rs index 55b95df03d..2513efdea4 100644 --- a/crates/fuel-core/src/p2p_test_helpers.rs +++ b/crates/fuel-core/src/p2p_test_helpers.rs @@ -25,7 +25,7 @@ use fuel_core_chain_config::{ use fuel_core_p2p::{ codecs::{ request_response::RequestResponseMessageHandler, - unbounded::GossipsubMessageHandler, + gossipsub::GossipsubMessageHandler, }, network_service::FuelP2PService, p2p_service::FuelP2PEvent, diff --git a/crates/services/p2p/src/codecs.rs b/crates/services/p2p/src/codecs.rs index 2b99724e91..0d00cbb178 100644 --- a/crates/services/p2p/src/codecs.rs +++ b/crates/services/p2p/src/codecs.rs @@ -37,7 +37,7 @@ pub trait Encode { pub trait Decode { type Error; /// Decodes the type `T` from the bytes. - fn decode(bytes: &[u8]) -> Result; + fn decode(&self, bytes: &[u8]) -> Result; } impl<'a> Encoder for Cow<'a, [u8]> { diff --git a/crates/services/p2p/src/codecs/gossipsub.rs b/crates/services/p2p/src/codecs/gossipsub.rs index 2fe6b7f504..f225de81f2 100644 --- a/crates/services/p2p/src/codecs/gossipsub.rs +++ b/crates/services/p2p/src/codecs/gossipsub.rs @@ -44,7 +44,7 @@ where ) -> Result { let decoded_response = match gossipsub_tag { GossipTopicTag::NewTx => { - GossipsubMessage::NewTx(Format::decode(encoded_data)?) + GossipsubMessage::NewTx(self.data_format.decode(encoded_data)?) } }; diff --git a/crates/services/p2p/src/codecs/postcard.rs b/crates/services/p2p/src/codecs/postcard.rs index 5241efc529..7d59d357e2 100644 --- a/crates/services/p2p/src/codecs/postcard.rs +++ b/crates/services/p2p/src/codecs/postcard.rs @@ -30,7 +30,7 @@ impl RequestResponseMessageHandler { impl GossipsubMessageHandler { pub fn new() -> Self { GossipsubMessageHandler { - data_format: PostcardDataFormat, + codec: PostcardDataFormat, } } } @@ -55,7 +55,7 @@ where { type Error = io::Error; - fn decode(bytes: &[u8]) -> Result { + fn decode(&self, bytes: &[u8]) -> Result { postcard::from_bytes(bytes) .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string())) } @@ -228,7 +228,7 @@ mod tests { let deserialized_as_v1 = // We cannot access the codec trait from an old node here, // so we deserialize directly using the `V1ResponseMessage` type. - >::decode(&buf).expect("Deserialization as V1ResponseMessage should succeed"); + codec.data_format.decode(&buf).expect("Deserialization as V1ResponseMessage should succeed"); // Then assert!(matches!( diff --git a/crates/services/p2p/src/codecs/request_response.rs b/crates/services/p2p/src/codecs/request_response.rs index 1188944853..238a4afccb 100644 --- a/crates/services/p2p/src/codecs/request_response.rs +++ b/crates/services/p2p/src/codecs/request_response.rs @@ -68,7 +68,7 @@ where .take(self.max_response_size as u64) .read_to_end(&mut response) .await?; - Format::decode(&response) + self.data_format.decode(&response) } async fn read_response( @@ -87,13 +87,11 @@ where match protocol { RequestResponseProtocol::V1 => { - let v1_response = - >::decode(&response)?; + let v1_response: V1ResponseMessage = + self.data_format.decode(&response)?; Ok(v1_response.into()) } - RequestResponseProtocol::V2 => { - >::decode(&response) - } + RequestResponseProtocol::V2 => self.data_format.decode(&response), } }