protocols/gossipsub: Add Gossipsub v1.1 support

This commit upgrades the current gossipsub implementation to support the [v1.1
spec](https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.1.md).

It adds a number of features, bug fixes and performance improvements. 

Besides support for all new 1.1 features, other improvements that are of particular note: 

- Improved duplicate LRU-time cache (this was previously a severe bottleneck for
  large message throughput topics)
- Extended message validation configuration options
- Arbitrary topics (users can now implement their own hashing schemes)
- Improved message validation handling - Invalid messages are no longer dropped
  but sent to the behaviour for application-level processing (including scoring)
- Support for floodsub, gossipsub v1 and gossipsub v2
- Protobuf encoding has been shifted into the behaviour. This has permitted two
  improvements:
     1. Message size verification during publishing (report to the user if the
        message is too large before attempting to send).
     2. Message fragmentation. If an RPC is too large it is fragmented into its
        sub components and sent in smaller chunks.

Additional Notes

The peer eXchange protocol defined in the v1.1 spec is inactive in its current
form. The current implementation permits sending `PeerId` in `PRUNE` messages,
however a `PeerId` is not sufficient to form a new connection to a peer. A
`Signed Address Record` is required to safely transmit peer identity
information. Once these are confirmed (https://github.com/libp2p/specs/pull/217)
a future PR will implement these and make PX usable.

Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Rüdiger Klaehn <rklaehn@protonmail.com>
Co-authored-by: blacktemplar <blacktemplar@a1.net>
Co-authored-by: Rüdiger Klaehn <rklaehn@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Roman S. Borschel <roman@parity.io>
Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
Co-authored-by: David Craven <david@craven.ch>
This commit is contained in:
Age Manning
2021-01-07 18:19:31 +11:00
committed by GitHub
parent d918e9a79d
commit df7e73ec47
26 changed files with 12071 additions and 1385 deletions

View File

@ -21,6 +21,8 @@
//! Error types that can result from gossipsub.
use libp2p_core::identity::error::SigningError;
use libp2p_core::upgrade::ProtocolError;
use std::fmt;
/// Error associated with publishing a gossipsub message.
#[derive(Debug)]
@ -31,6 +33,20 @@ pub enum PublishError {
SigningError(SigningError),
/// There were no peers to send this message to.
InsufficientPeers,
/// The overall message was too large. This could be due to excessive topics or an excessive
/// message size.
MessageTooLarge,
/// The compression algorithm failed.
TransformFailed(std::io::Error),
}
/// Error associated with subscribing to a topic.
#[derive(Debug)]
pub enum SubscriptionError {
/// Couldn't publish our subscription
PublishError(PublishError),
/// We are not allowed to subscribe to this topic by the subscription filter
NotAllowed,
}
impl From<SigningError> for PublishError {
@ -38,3 +54,70 @@ 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,
/// Protocol negotiation timeout.
NegotiationTimeout,
/// Protocol negotiation failed.
NegotiationProtocolError(ProtocolError),
/// IO error.
Io(std::io::Error),
}
#[derive(Debug, Clone, Copy)]
pub enum ValidationError {
/// The message has an invalid signature,
InvalidSignature,
/// The sequence number was empty, expected a value.
EmptySequenceNumber,
/// The sequence number was the incorrect size
InvalidSequenceNumber,
/// The PeerId was invalid
InvalidPeerId,
/// Signature existed when validation has been sent to
/// [`crate::behaviour::MessageAuthenticity::Anonymous`].
SignaturePresent,
/// Sequence number existed when validation has been sent to
/// [`crate::behaviour::MessageAuthenticity::Anonymous`].
SequenceNumberPresent,
/// Message source existed when validation has been sent to
/// [`crate::behaviour::MessageAuthenticity::Anonymous`].
MessageSourcePresent,
/// The data transformation failed.
TransformFailed,
}
impl From<std::io::Error> for GossipsubHandlerError {
fn from(error: std::io::Error) -> GossipsubHandlerError {
GossipsubHandlerError::Io(error)
}
}
impl From<std::io::Error> for PublishError {
fn from(error: std::io::Error) -> PublishError {
PublishError::TransformFailed(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,
}
}
}