Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
141 commits
Select commit Hold shift + click to select a range
aeba523
Gossipsub message signing
AgeManning May 19, 2020
573e4b6
Update ipfs-private example
AgeManning May 19, 2020
d2babd4
Add optimisation to prevent key calculation each message
AgeManning May 22, 2020
8353474
Support multiple versions
AgeManning May 22, 2020
b7db121
Initial porting of peer_scoring from go-libp2p
AgeManning May 22, 2020
ac3c977
Handle forwarding of signed messages and reviewers comments
AgeManning May 25, 2020
f3ff66b
Merge latest gossipsub signing
AgeManning May 25, 2020
c8335cc
Progress to porting scoring functions
AgeManning May 25, 2020
e9afd95
Merge branch 'master' into gossipsub-signing
AgeManning May 27, 2020
984a662
Finish first port of score params
AgeManning May 27, 2020
80ea7df
Add defaults to scoring
AgeManning May 27, 2020
58f61b4
Generalise topic hashing
AgeManning May 27, 2020
238e305
Update topic hashing structure
AgeManning May 27, 2020
fec112a
Update v1.1 tests
AgeManning May 27, 2020
bc09eb7
Add more unit tests
AgeManning May 27, 2020
3d1a025
protocols/gossipsub/src/protocol: Use quickcheck to test signing (#31)
mxinden Jun 1, 2020
31b6849
Shift signing into behaviour
AgeManning Jun 1, 2020
a8cec0b
Merge latest master
AgeManning Jul 1, 2020
2b38112
Address reviewers suggestions
AgeManning Jul 1, 2020
7d678f5
Send subscriptions to all peers
AgeManning Jul 1, 2020
dc050dd
Update examples
AgeManning Jul 1, 2020
5bf1cfc
Shift signing option into the behaviour
AgeManning Jul 1, 2020
4ffe728
Revert changes to core/
AgeManning Jul 9, 2020
24b738c
Reviewers suggestion
AgeManning Jul 9, 2020
a2e4bf0
Merge remote-tracking branch 'origin/master' into gossipsub-signing
AgeManning Jul 9, 2020
2f20571
Merge latest master
AgeManning Jul 23, 2020
2c50ba2
Revert changes to core/
AgeManning Jul 23, 2020
4de878e
Switch gossipsub state to use sets instead of vectors
rklaehn Jul 23, 2020
e43f0a0
Make tests pass
rklaehn Jul 23, 2020
57f07aa
Some clippy
rklaehn Jul 23, 2020
b6eeb3c
Prevent duplicate finding during JOIN
AgeManning Jul 24, 2020
0d5bde4
Merge #1675
AgeManning Jul 24, 2020
aec51e0
Update tests, improve logging
AgeManning Jul 24, 2020
759a5ad
Update the default d_lo constant
AgeManning Jul 24, 2020
495ffe4
Handle errors correctly
AgeManning Jul 24, 2020
1ea2dd9
Add privacy and validation options in the config
AgeManning Jul 24, 2020
8e69465
Merge branch 'master' into gossipsub-v1.1
blacktemplar Jul 24, 2020
1f73ded
A few improvements (#32)
rklaehn Jul 24, 2020
7b76c8f
Improve signing validation logic
AgeManning Jul 24, 2020
f5d32b2
Change the gossipsub rpc protocol to use bytes for message ids (#34)
rklaehn Jul 25, 2020
c3a7757
Add optional privacy and validation settings
AgeManning Jul 26, 2020
0c43ed4
Correct doc link
AgeManning Jul 27, 2020
efa40e3
Prevent invalid messages from being gossiped
AgeManning Jul 27, 2020
a514c60
Remove unvalidated messages from gossip. Reintroduce duplicate cache
AgeManning Jul 27, 2020
b257025
Send grafts when subscriptions are added to the mesh
AgeManning Jul 28, 2020
b20771b
Merge latest master
AgeManning Jul 28, 2020
cacadc0
Merge gossipsub-signing
AgeManning Jul 28, 2020
6566925
implement explicit peers
blacktemplar Jul 28, 2020
81338d5
Merge branch 'gossipsub-v1.1' into gossipsub-v1.1-explicit-peers
blacktemplar Jul 28, 2020
ecb2d8b
Fix ipfs-kad example
AgeManning Jul 28, 2020
2a830b9
Merge branch 'gossipsub-v1.1' into gossipsub-v1.1-explicit-peers
blacktemplar Jul 28, 2020
b78d771
finish merging + rustfmt
blacktemplar Jul 28, 2020
8bffa91
undo changes in ipfs-private
blacktemplar Jul 28, 2020
2f4a50a
Only add messages to memcache if not duplicates
AgeManning Jul 28, 2020
99a37bb
Merge latest signing
AgeManning Jul 28, 2020
284bf1c
Merge pull request #36 from blacktemplar/gossipsub-v1.1-explicit-peers
blacktemplar Jul 28, 2020
be7c9e2
Add mesh maintenance tests and remove excess peers from mesh
AgeManning Jul 29, 2020
e049ff7
Apply reviewers suggestions
AgeManning Jul 29, 2020
74d2779
Merge latest master
AgeManning Jul 29, 2020
81fca56
Merge latest gossipsub-signing
AgeManning Jul 29, 2020
6bac865
Wrap comments
AgeManning Jul 29, 2020
6e0a6b3
implement px and prune backoff + simplify tests for explicit peers
blacktemplar Jul 29, 2020
1257639
Merge pull request #37 from sigp/gossipsub-v1.1-prune-backoff-and-px
blacktemplar Jul 29, 2020
6c77022
improved data structure for storing backoffs + adding configurable sl…
blacktemplar Jul 30, 2020
8b17725
Merge pull request #38 from sigp/gossipsub-v1.1-prune-backoff-and-px
blacktemplar Jul 30, 2020
2518f9f
allow flood publishing (on by default)
blacktemplar Jul 30, 2020
1994c88
Merge pull request #39 from sigp/gossipsub-v1.1-flood-publishing
blacktemplar Jul 30, 2020
dbe0a84
implement adaptive gossip dissemination + complete config debug output
blacktemplar Jul 30, 2020
7155f5c
Merge pull request #40 from sigp/gossipsub-v1.1-adaptive-gossip-disse…
blacktemplar Jul 31, 2020
da34368
fix bug in backoff storage data structure
blacktemplar Jul 31, 2020
66730dd
make GossipsubConfig fields private + add getters + improve validatio…
blacktemplar Jul 31, 2020
8a0b278
implements a configurable minimum number of mesh peers that are outbound
blacktemplar Jul 31, 2020
4e1375a
Merge pull request #41 from sigp/gossipsub-v1.1-outbound-mesh-quota
blacktemplar Aug 1, 2020
94469cf
Ensure sequence number is sent
AgeManning Aug 2, 2020
60f8a1e
Merge latest master
AgeManning Aug 2, 2020
fef66dd
Maintain the debug trait
AgeManning Aug 2, 2020
799137c
use the score module in the behaviour, calling the score update metho…
blacktemplar Aug 6, 2020
efb59ac
Merge pull request #42 from sigp/gossipsub-v1.1-score-wiring
blacktemplar Aug 6, 2020
b6fbf05
implement opportunistic grafting
blacktemplar Aug 6, 2020
189ffc4
remove done TODO
blacktemplar Aug 6, 2020
f405d2a
remove done TODO
blacktemplar Aug 6, 2020
6fe395a
cargo fmt
blacktemplar Aug 6, 2020
47855d6
Merge branch 'gossipsub-v1.1' into gossip-v1.1-opportunistic-grafting
blacktemplar Aug 6, 2020
5954403
ignore grafts in unknown topics
blacktemplar Aug 6, 2020
a228f3b
Merge pull request #43 from sigp/gossip-v1.1-opportunistic-grafting
blacktemplar Aug 6, 2020
3d69944
Merge branch 'gossipsub-v1.1' into gossipsub-v1.1-spam-protection-mea…
blacktemplar Aug 6, 2020
afba384
cap the number of gossipsub retransmission for the iwants of the same…
blacktemplar Aug 6, 2020
223f0d8
cap the number of processed ihaves per peer per heartbeat, plus the m…
blacktemplar Aug 7, 2020
1ced56e
Replace lru cache with custom duplicatecache
AgeManning Aug 7, 2020
27ac942
track iwant messages we send and penalize peers not responding to them
blacktemplar Aug 7, 2020
04d3e2f
removes todo
blacktemplar Aug 7, 2020
5c52843
Improve stream management and error handling
AgeManning Aug 7, 2020
a135fc0
Merge pull request #44 from sigp/gossipsub-v1.1-spam-protection-measures
blacktemplar Aug 7, 2020
d9ee8e4
Merge remote-tracking branch 'origin/gossipsub-signing' into gossipsu…
blacktemplar Aug 7, 2020
714fee3
Merge branch 'master' into gossipsub-v1.1
blacktemplar Aug 7, 2020
7f7fd17
cargo fmt
blacktemplar Aug 7, 2020
ee7dd71
Gossipsub v1.1 check fmt (#45)
blacktemplar Aug 7, 2020
5109fe7
Merge duplicate cache
AgeManning Aug 10, 2020
b748618
Merge substream updates
AgeManning Aug 10, 2020
7bdb378
Propagate signature verification and peer kinds to behaviour (#46)
AgeManning Aug 12, 2020
83280db
treat peers that do not support the protocol as disconnected (#48)
blacktemplar Aug 12, 2020
5d93c0a
Gossipsub v1.1 various improvements (#49)
blacktemplar Aug 17, 2020
3206da5
Gossipsub 1.1 ammendments (#50)
AgeManning Aug 24, 2020
59034e2
Complete porting of go scoring tests (#51)
AgeManning Aug 25, 2020
521ecdb
Add peer blacklisting (#52)
AgeManning Aug 25, 2020
261f78e
Estimate message size (#53)
AgeManning Aug 26, 2020
f71a1bb
Message fragmentation (#54)
AgeManning Aug 27, 2020
c762d6c
Correct tests and merge latest master (#55)
AgeManning Aug 27, 2020
8ec6b66
Merge latest master
AgeManning Aug 27, 2020
4f92599
Remove github workflow
AgeManning Aug 27, 2020
618334a
Cleanup code add logs (#56)
AgeManning Aug 28, 2020
9a03f94
Further improve logging (#57)
AgeManning Aug 28, 2020
e031bce
Remove lru_time_cache_dep (#58)
AgeManning Aug 30, 2020
c7f0716
Various improvements (#60)
blacktemplar Sep 25, 2020
9da2af7
Cache published message ids (#61)
blacktemplar Oct 2, 2020
464c4da
use generic type for message data + cache message ids (#67)
blacktemplar Oct 8, 2020
88ee95e
Adds a fast message id function and an additional cache for it (#68)
blacktemplar Oct 9, 2020
238e5cf
Merge branch 'master' into gossipsub-v1.1
blacktemplar Oct 9, 2020
6ee2c5f
export FastMessageId
blacktemplar Oct 9, 2020
4c95f86
Gossipsub v1.1 bug fixes (#71)
blacktemplar Oct 14, 2020
c2c4a26
add subscription filters + some useful default filters + refactor tes…
blacktemplar Oct 14, 2020
201d533
remove support for multi-topic messages (#73)
blacktemplar Oct 20, 2020
87633a3
also consider allow self origin when using a published message ids di…
blacktemplar Oct 21, 2020
0e28cba
fix clippy warnings (#83)
blacktemplar Oct 21, 2020
9ba9b8a
Merge branch 'master' into gossipsub-v1.1
blacktemplar Oct 22, 2020
6bb58ec
Merge latest master
AgeManning Nov 3, 2020
7a316e8
Merge latest master
AgeManning Nov 6, 2020
d306455
Address reviewers comments (#88)
AgeManning Nov 8, 2020
2d1170b
implement as_ref and as_static_ref to get a &str from PeerKind (#91)
blacktemplar Nov 16, 2020
8bdc378
Merge latest master
AgeManning Nov 17, 2020
34f58fb
Update to latest master
AgeManning Nov 17, 2020
5157e70
only respond to IWANT if message got already validated (#96)
blacktemplar Nov 26, 2020
82be02d
protocols/gossipsub: Review (#93)
mxinden Dec 7, 2020
ffee445
Merge latest master
AgeManning Dec 7, 2020
23d7d04
Add optional compression to gossipsub (#100)
AgeManning Dec 16, 2020
1b5a35e
Remove feature gating from gossipsub
AgeManning Dec 16, 2020
1b19338
Merge latest master
AgeManning Dec 17, 2020
a15b246
Remove compression in favour of a more general data transform
AgeManning Dec 17, 2020
4cca4d3
Loosen trait bounds on gossipsub
AgeManning Dec 18, 2020
88ed4e5
Merge latest master
AgeManning Jan 6, 2021
f47ae78
Fix doclinks
AgeManning Jan 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions protocols/gossipsub/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//! Error types that can result from gossipsub.

use libp2p_core::identity::error::SigningError;
use std::fmt;

/// Error associated with publishing a gossipsub message.
#[derive(Debug)]
Expand All @@ -38,3 +39,39 @@ impl From<SigningError> for PublishError {
PublishError::SigningError(error)
}
}

/// Errors that can occur in the protocols handler.
#[derive(Debug)]
pub enum GossipsubHandlerError {
/// The maximum number of inbound substreams created has been exceeded.
MaxInboundSubstreams,
/// The maximum number of outbound substreams created has been exceeded.
MaxOutboundSubstreams,
/// The message exceeds the maximum transmission size.
MaxTransmissionSize,
/// The message is invalid.
InvalidMessage(&'static str),
/// IO error.
Io(std::io::Error),
}

impl From<std::io::Error> for GossipsubHandlerError {
fn from(error: std::io::Error) -> GossipsubHandlerError {
GossipsubHandlerError::Io(error)
}
}

impl fmt::Display for GossipsubHandlerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}

impl std::error::Error for GossipsubHandlerError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
GossipsubHandlerError::Io(io) => Some(io),
_ => None,
}
}
}
85 changes: 64 additions & 21 deletions protocols/gossipsub/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use crate::behaviour::GossipsubRpc;
use crate::config::ValidationMode;
use crate::error::GossipsubHandlerError;
use crate::protocol::{GossipsubCodec, ProtocolConfig};
use futures::prelude::*;
use futures::StreamExt;
Expand All @@ -29,14 +30,21 @@ use libp2p_swarm::protocols_handler::{
KeepAlive, ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr, SubstreamProtocol,
};
use libp2p_swarm::NegotiatedSubstream;
use log::{debug, error, trace, warn};
use log::{error, trace, warn};
use smallvec::SmallVec;
use std::{
io,
pin::Pin,
task::{Context, Poll},
};

// The maximum number of substreams we accept or create before disconnecting from the peer.
//
// Gossipsub is supposed to have a single long-lived inbound and outbound substream. On failure we
// attempt to recreate these. This imposes an upper bound of new substreams before we consider the
// connection faulty and disconnect. This also prevents against potential substream creation loops.
const MAX_SUBSTREAM_CREATION: usize = 5;

/// Protocol Handler that manages a single long-lived substream with a peer.
pub struct GossipsubHandler {
/// Upgrade configuration for the gossipsub protocol.
Expand All @@ -55,6 +63,12 @@ pub struct GossipsubHandler {
/// requests.
outbound_substream_establishing: bool,

/// The number of outbound substreams we have created.
outbound_substreams_created: usize,

/// The number of inbound substreams that have been created by the peer.
inbound_substreams_created: usize,

/// Flag determining whether to maintain the connection to the peer.
keep_alive: KeepAlive,
}
Expand Down Expand Up @@ -99,6 +113,8 @@ impl GossipsubHandler {
inbound_substream: None,
outbound_substream: None,
outbound_substream_establishing: false,
outbound_substreams_created: 0,
inbound_substreams_created: 0,
send_queue: SmallVec::new(),
keep_alive: KeepAlive::Yes,
}
Expand All @@ -108,7 +124,7 @@ impl GossipsubHandler {
impl ProtocolsHandler for GossipsubHandler {
type InEvent = GossipsubRpc;
type OutEvent = GossipsubRpc;
type Error = io::Error;
type Error = GossipsubHandlerError;
type InboundProtocol = ProtocolConfig;
type OutboundProtocol = ProtocolConfig;
type OutboundOpenInfo = GossipsubRpc;
Expand All @@ -121,6 +137,8 @@ impl ProtocolsHandler for GossipsubHandler {
&mut self,
substream: <Self::InboundProtocol as InboundUpgrade<NegotiatedSubstream>>::Output,
) {
self.inbound_substreams_created += 1;

// new inbound substream. Replace the current one, if it exists.
trace!("New inbound substream request");
self.inbound_substream = Some(InboundSubstreamState::WaitingInput(substream));
Expand All @@ -132,6 +150,8 @@ impl ProtocolsHandler for GossipsubHandler {
message: Self::OutboundOpenInfo,
) {
self.outbound_substream_establishing = false;
self.outbound_substreams_created += 1;

// Should never establish a new outbound substream if one already exists.
// If this happens, an outbound message is not sent.
if self.outbound_substream.is_some() {
Expand All @@ -150,11 +170,12 @@ impl ProtocolsHandler for GossipsubHandler {
fn inject_dial_upgrade_error(
&mut self,
_: Self::OutboundOpenInfo,
_: ProtocolsHandlerUpgrErr<
e: ProtocolsHandlerUpgrErr<
<Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Error,
>,
) {
self.outbound_substream_establishing = false;
warn!("Dial upgrade error {:?}", e);
// Ignore upgrade errors for now.
// If a peer doesn't support this protocol, this will just ignore them, but not disconnect
// them.
Expand All @@ -175,11 +196,23 @@ impl ProtocolsHandler for GossipsubHandler {
Self::Error,
>,
> {
if self.inbound_substreams_created > MAX_SUBSTREAM_CREATION {
// Too many inbound substreams have been created, end the connection.
return Poll::Ready(ProtocolsHandlerEvent::Close(
GossipsubHandlerError::MaxInboundSubstreams,
));
}

// determine if we need to create the stream
if !self.send_queue.is_empty()
&& self.outbound_substream.is_none()
&& !self.outbound_substream_establishing
{
if self.outbound_substreams_created >= MAX_SUBSTREAM_CREATION {
return Poll::Ready(ProtocolsHandlerEvent::Close(
GossipsubHandlerError::MaxOutboundSubstreams,
));
}
let message = self.send_queue.remove(0);
self.send_queue.shrink_to_fit();
self.outbound_substream_establishing = true;
Expand All @@ -202,15 +235,21 @@ impl ProtocolsHandler for GossipsubHandler {
Some(InboundSubstreamState::WaitingInput(substream));
return Poll::Ready(ProtocolsHandlerEvent::Custom(message));
}
Poll::Ready(Some(Err(e))) => {
match e.kind() {
std::io::ErrorKind::InvalidData => {
Poll::Ready(Some(Err(error))) => {
match error {
GossipsubHandlerError::InvalidMessage(reason) => {
// Invalid message, ignore it and reset to waiting
warn!("Invalid message received. Error: {}", e);
warn!("Invalid message received. Error: {}", reason);
self.inbound_substream =
Some(InboundSubstreamState::WaitingInput(substream));
}
GossipsubHandlerError::MaxTransmissionSize => {
warn!("Message exceeded the maximum transmission size");
self.inbound_substream =
Some(InboundSubstreamState::WaitingInput(substream));
}
_ => {
warn!("Inbound stream error: {}", error);
// More serious errors, close this side of the stream. If the
// peer is still around, they will re-establish their
// connection
Expand All @@ -221,6 +260,7 @@ impl ProtocolsHandler for GossipsubHandler {
}
// peer closed the stream
Poll::Ready(None) => {
warn!("Peer closed their outbound stream");
self.inbound_substream =
Some(InboundSubstreamState::Closing(substream));
}
Expand All @@ -238,7 +278,7 @@ impl ProtocolsHandler for GossipsubHandler {
// Don't close the connection but just drop the inbound substream.
// In case the remote has more to send, they will open up a new
// substream.
debug!("Inbound substream error while closing: {:?}", e);
warn!("Inbound substream error while closing: {:?}", e);
}

self.inbound_substream = None;
Expand Down Expand Up @@ -290,19 +330,19 @@ impl ProtocolsHandler for GossipsubHandler {
self.outbound_substream =
Some(OutboundSubstreamState::PendingFlush(substream))
}
Err(GossipsubHandlerError::MaxTransmissionSize) => {
error!("Message exceeded the maximum transmission size and was not sent.");
self.outbound_substream =
Some(OutboundSubstreamState::WaitingOutput(substream));
}
Err(e) => {
if let io::ErrorKind::PermissionDenied = e.kind() {
error!("Message over the maximum transmission limit was not sent.");
self.outbound_substream =
Some(OutboundSubstreamState::WaitingOutput(substream));
} else {
return Poll::Ready(ProtocolsHandlerEvent::Close(e));
}
error!("Error sending message: {}", e);
return Poll::Ready(ProtocolsHandlerEvent::Close(e));
}
}
}
Poll::Ready(Err(e)) => {
debug!("Outbound substream error while sending output: {:?}", e);
error!("Outbound substream error while sending output: {:?}", e);
return Poll::Ready(ProtocolsHandlerEvent::Close(e));
}
Poll::Pending => {
Expand Down Expand Up @@ -337,11 +377,14 @@ impl ProtocolsHandler for GossipsubHandler {
break;
}
Poll::Ready(Err(e)) => {
debug!("Outbound substream error while closing: {:?}", e);
return Poll::Ready(ProtocolsHandlerEvent::Close(io::Error::new(
io::ErrorKind::BrokenPipe,
"Failed to close outbound substream",
)));
warn!("Outbound substream error while closing: {:?}", e);
return Poll::Ready(ProtocolsHandlerEvent::Close(
io::Error::new(
io::ErrorKind::BrokenPipe,
"Failed to close outbound substream",
)
.into(),
));
}
Poll::Pending => {
self.outbound_substream =
Expand Down
38 changes: 21 additions & 17 deletions protocols/gossipsub/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use crate::behaviour::GossipsubRpc;
use crate::config::ValidationMode;
use crate::error::GossipsubHandlerError;
use crate::rpc_proto;
use crate::topic::TopicHash;
use byteorder::{BigEndian, ByteOrder};
Expand Down Expand Up @@ -81,7 +82,7 @@ where
TSocket: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
type Output = Framed<TSocket, GossipsubCodec>;
type Error = io::Error;
type Error = GossipsubHandlerError;
type Future = Pin<Box<dyn Future<Output = Result<Self::Output, Self::Error>> + Send>>;

fn upgrade_inbound(self, socket: TSocket, _: Self::Info) -> Self::Future {
Expand All @@ -99,7 +100,7 @@ where
TSocket: AsyncWrite + AsyncRead + Unpin + Send + 'static,
{
type Output = Framed<TSocket, GossipsubCodec>;
type Error = io::Error;
type Error = GossipsubHandlerError;
type Future = Pin<Box<dyn Future<Output = Result<Self::Output, Self::Error>> + Send>>;

fn upgrade_outbound(self, socket: TSocket, _: Self::Info) -> Self::Future {
Expand Down Expand Up @@ -196,7 +197,7 @@ impl GossipsubCodec {

impl Encoder for GossipsubCodec {
type Item = GossipsubRpc;
type Error = io::Error;
type Error = GossipsubHandlerError;

fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
// Messages
Expand Down Expand Up @@ -301,21 +302,29 @@ impl Encoder for GossipsubCodec {
.expect("Buffer has sufficient capacity");

// length prefix the protobuf message, ensuring the max limit is not hit
self.length_codec.encode(Bytes::from(buf), dst)
self.length_codec
.encode(Bytes::from(buf), dst)
.map_err(|_| GossipsubHandlerError::MaxTransmissionSize)
}
}

impl Decoder for GossipsubCodec {
type Item = GossipsubRpc;
type Error = io::Error;
type Error = GossipsubHandlerError;

fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
let packet = match self.length_codec.decode(src)? {
let packet = match self.length_codec.decode(src).map_err(|e| {
if let std::io::ErrorKind::PermissionDenied = e.kind() {
GossipsubHandlerError::MaxTransmissionSize
} else {
GossipsubHandlerError::Io(e)
}
})? {
Some(p) => p,
None => return Ok(None),
};

let rpc = rpc_proto::Rpc::decode(&packet[..])?;
let rpc = rpc_proto::Rpc::decode(&packet[..]).map_err(|e| std::io::Error::from(e))?;

let mut messages = Vec::with_capacity(rpc.publish.len());
for message in rpc.publish.into_iter() {
Expand Down Expand Up @@ -375,15 +384,11 @@ impl Decoder for GossipsubCodec {
// ensure the sequence number is a u64
let sequence_number = if verify_sequence_no {
let seq_no = message.seqno.ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
"sequence number was not provided",
)
GossipsubHandlerError::InvalidMessage("Empty sequence number")
})?;
if seq_no.len() != 8 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"sequence number has an incorrect size",
return Err(GossipsubHandlerError::InvalidMessage(
"Invalid sequence number",
));
}
Some(BigEndian::read_u64(&seq_no))
Expand All @@ -393,9 +398,8 @@ impl Decoder for GossipsubCodec {

let source = if verify_source {
Some(
PeerId::from_bytes(message.from.unwrap_or_default()).map_err(|_| {
io::Error::new(io::ErrorKind::InvalidData, "Invalid Peer Id")
})?,
PeerId::from_bytes(message.from.unwrap_or_default())
.map_err(|_| GossipsubHandlerError::InvalidMessage("Invalid Peer Id"))?,
)
} else {
None
Expand Down