Skip to content

Commit

Permalink
Decode trait function takes &self as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
acerone85 committed Oct 28, 2024
1 parent 58ffe67 commit c564941
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion crates/fuel-core/src/p2p_test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/services/p2p/src/codecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub trait Encode<T: ?Sized> {
pub trait Decode<T> {
type Error;
/// Decodes the type `T` from the bytes.
fn decode(bytes: &[u8]) -> Result<T, Self::Error>;
fn decode(&self, bytes: &[u8]) -> Result<T, Self::Error>;
}

impl<'a> Encoder for Cow<'a, [u8]> {
Expand Down
2 changes: 1 addition & 1 deletion crates/services/p2p/src/codecs/gossipsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ where
) -> Result<Self::ResponseMessage, io::Error> {
let decoded_response = match gossipsub_tag {
GossipTopicTag::NewTx => {
GossipsubMessage::NewTx(Format::decode(encoded_data)?)
GossipsubMessage::NewTx(self.data_format.decode(encoded_data)?)
}
};

Expand Down
6 changes: 3 additions & 3 deletions crates/services/p2p/src/codecs/postcard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl RequestResponseMessageHandler<PostcardDataFormat> {
impl GossipsubMessageHandler<PostcardDataFormat> {
pub fn new() -> Self {
GossipsubMessageHandler {
data_format: PostcardDataFormat,
codec: PostcardDataFormat,
}
}
}
Expand All @@ -55,7 +55,7 @@ where
{
type Error = io::Error;

fn decode(bytes: &[u8]) -> Result<T, Self::Error> {
fn decode(&self, bytes: &[u8]) -> Result<T, Self::Error> {
postcard::from_bytes(bytes)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))
}
Expand Down Expand Up @@ -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.
<PostcardDataFormat as Decode<V1ResponseMessage>>::decode(&buf).expect("Deserialization as V1ResponseMessage should succeed");
codec.data_format.decode(&buf).expect("Deserialization as V1ResponseMessage should succeed");

// Then
assert!(matches!(
Expand Down
10 changes: 4 additions & 6 deletions crates/services/p2p/src/codecs/request_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
Expand All @@ -87,13 +87,11 @@ where

match protocol {
RequestResponseProtocol::V1 => {
let v1_response =
<Format as Decode<V1ResponseMessage>>::decode(&response)?;
let v1_response: V1ResponseMessage =
self.data_format.decode(&response)?;
Ok(v1_response.into())
}
RequestResponseProtocol::V2 => {
<Format as Decode<V2ResponseMessage>>::decode(&response)
}
RequestResponseProtocol::V2 => self.data_format.decode(&response),
}
}

Expand Down

0 comments on commit c564941

Please sign in to comment.