Skip to content

Commit 69c2d95

Browse files
committed
update trait return types and remove non used impl
1 parent 046ad47 commit 69c2d95

File tree

4 files changed

+20
-53
lines changed

4 files changed

+20
-53
lines changed

protocols/gossipsub/src/behaviour.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -796,9 +796,11 @@ where
796796
) -> Result<(), PublishError> {
797797
let topic_id = topic.into();
798798

799-
let available_parts = partial_message.available_parts().map(|p| p.to_vec());
800-
let missing_parts = partial_message.missing_parts().map(|p| p.to_vec());
801-
let group_id = partial_message.group_id().to_vec();
799+
let available_parts = partial_message
800+
.available_parts()
801+
.map(|p| p.as_ref().to_vec());
802+
let missing_parts = partial_message.missing_parts().map(|p| p.as_ref().to_vec());
803+
let group_id = partial_message.group_id().as_ref().to_vec();
802804

803805
// TODO: should we construct a recipient list just for partials?
804806
let recipient_peers = self.get_publish_peers(&topic_id);
@@ -814,8 +816,9 @@ where
814816
let peer_partials = peer.partial_messages.entry(topic_id.clone()).or_default();
815817
let peer_partial = peer_partials.entry(group_id.clone()).or_default();
816818

817-
let Ok((message_data, rest_wanted)) =
818-
partial_message.partial_message_bytes_from_metadata(&peer_partial.wanted)
819+
let Ok((message_data, rest_wanted)) = partial_message
820+
.partial_message_bytes_from_metadata(&peer_partial.wanted)
821+
.map(|(m, r)| (m.as_ref().to_vec(), r.map(|r| r.as_ref().to_vec())))
819822
else {
820823
tracing::error!(peer = %peer_id, group_id = ?group_id,
821824
"Could not reconstruct message bytes for peer metadata");

protocols/gossipsub/src/error.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,8 @@ pub enum PartialMessageError {
180180

181181
/// The partial data doesn't belong to this message group.
182182
WrongGroup {
183-
/// Expected minimum number of bytes.
184-
expected: usize,
185-
/// Actual number of bytes received.
186-
received: usize,
183+
/// Group Id of the received message.
184+
received: Vec<u8>,
187185
},
188186

189187
/// The partial data is a duplicate of already received data.
@@ -214,12 +212,8 @@ impl std::fmt::Display for PartialMessageError {
214212
Self::InvalidFormat => {
215213
write!(f, "Invalid data format")
216214
}
217-
Self::WrongGroup { expected, received } => {
218-
write!(
219-
f,
220-
"Wrong group ID: expected {:?}, got {:?}",
221-
expected, received
222-
)
215+
Self::WrongGroup { received } => {
216+
write!(f, "Wrong group ID: got {:?}", received)
223217
}
224218
Self::DuplicateData(part_id) => {
225219
write!(f, "Duplicate data for part {:?}", part_id)

protocols/gossipsub/src/partial.rs

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub trait Partial {
3838
/// All partial messages belonging to the same logical message should return
3939
/// the same group ID. This is used to associate partial messages together
4040
/// during reconstruction.
41-
fn group_id(&self) -> &[u8];
41+
fn group_id(&self) -> impl AsRef<[u8]>;
4242

4343
/// Returns metadata describing which parts of the message are missing.
4444
///
@@ -48,7 +48,7 @@ pub trait Partial {
4848
///
4949
/// The returned bytes will be sent in PartialIWANT messages to request
5050
/// missing parts from peers.
51-
fn missing_parts(&self) -> Option<&[u8]>;
51+
fn missing_parts(&self) -> Option<impl AsRef<[u8]>>;
5252

5353
/// Returns metadata describing which parts of the message are available.
5454
///
@@ -58,7 +58,7 @@ pub trait Partial {
5858
///
5959
/// The returned bytes will be sent in PartialIHAVE messages to advertise
6060
/// available parts to peers.
61-
fn available_parts(&self) -> Option<&[u8]>;
61+
fn available_parts(&self) -> Option<impl AsRef<[u8]>>;
6262

6363
/// Generates partial message bytes from the given metadata.
6464
///
@@ -71,8 +71,8 @@ pub trait Partial {
7171
/// - Optional remaining metadata if more parts are still available after this one
7272
fn partial_message_bytes_from_metadata(
7373
&self,
74-
metadata: &[u8],
75-
) -> Result<(Vec<u8>, Option<Vec<u8>>), PartialMessageError>;
74+
metadata: impl AsRef<[u8]>,
75+
) -> Result<(impl AsRef<[u8]>, Option<impl AsRef<[u8]>>), PartialMessageError>;
7676

7777
/// Extends this message with received partial message data.
7878
///
@@ -87,34 +87,3 @@ pub trait Partial {
8787
data: &[u8],
8888
) -> Result<(), PartialMessageError>;
8989
}
90-
91-
/// Default implementation that disables partial messages.
92-
impl Partial for () {
93-
fn group_id(&self) -> &[u8] {
94-
&[]
95-
}
96-
97-
fn missing_parts(&self) -> Option<&[u8]> {
98-
None
99-
}
100-
101-
fn available_parts(&self) -> Option<&[u8]> {
102-
None
103-
}
104-
105-
fn partial_message_bytes_from_metadata(
106-
&self,
107-
_metadata: &[u8],
108-
) -> Result<(Vec<u8>, Option<Vec<u8>>), PartialMessageError> {
109-
Ok((vec![], None))
110-
}
111-
112-
fn extend_from_encoded_partial_message(
113-
&mut self,
114-
_data: &[u8],
115-
) -> Result<(), PartialMessageError> {
116-
// This should never be called since we never advertise having or wanting parts,
117-
// but if it is called, just ignore the data silently
118-
Ok(())
119-
}
120-
}

protocols/gossipsub/src/types.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ use libp2p_swarm::ConnectionId;
3131
use quick_protobuf::MessageWrite;
3232
#[cfg(feature = "serde")]
3333
use serde::{Deserialize, Serialize};
34+
#[cfg(feature = "partial_messages")]
35+
use std::collections::HashMap;
3436
use web_time::Instant;
3537

3638
use crate::{queue::Queue, rpc_proto::proto, TopicHash};
@@ -103,8 +105,7 @@ pub(crate) struct PeerDetails {
103105
pub(crate) dont_send: LinkedHashMap<MessageId, Instant>,
104106
/// Peer Partial messages.
105107
#[cfg(feature = "partial_messages")]
106-
pub(crate) partial_messages:
107-
std::collections::HashMap<TopicHash, HashMap<Vec<u8>, PartialData>>,
108+
pub(crate) partial_messages: HashMap<TopicHash, HashMap<Vec<u8>, PartialData>>,
108109
/// Message queue consumed by the connection handler.
109110
pub(crate) messages: Queue,
110111
}

0 commit comments

Comments
 (0)