cs_mwc_libp2p_gossipsub/error.rs
1// Copyright 2020 Sigma Prime Pty Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21//! Error types that can result from gossipsub.
22
23use mwc_libp2p_core::identity::error::SigningError;
24use mwc_libp2p_core::upgrade::ProtocolError;
25use std::fmt;
26
27/// Error associated with publishing a gossipsub message.
28#[derive(Debug)]
29pub enum PublishError {
30 /// This message has already been published.
31 Duplicate,
32 /// An error occurred whilst signing the message.
33 SigningError(SigningError),
34 /// There were no peers to send this message to.
35 InsufficientPeers,
36 /// The overall message was too large. This could be due to excessive topics or an excessive
37 /// message size.
38 MessageTooLarge,
39 /// The compression algorithm failed.
40 TransformFailed(std::io::Error),
41}
42
43/// Error associated with subscribing to a topic.
44#[derive(Debug)]
45pub enum SubscriptionError {
46 /// Couldn't publish our subscription
47 PublishError(PublishError),
48 /// We are not allowed to subscribe to this topic by the subscription filter
49 NotAllowed,
50}
51
52impl From<SigningError> for PublishError {
53 fn from(error: SigningError) -> Self {
54 PublishError::SigningError(error)
55 }
56}
57
58/// Errors that can occur in the protocols handler.
59#[derive(Debug)]
60pub enum GossipsubHandlerError {
61 /// The maximum number of inbound substreams created has been exceeded.
62 MaxInboundSubstreams,
63 /// The maximum number of outbound substreams created has been exceeded.
64 MaxOutboundSubstreams,
65 /// The message exceeds the maximum transmission size.
66 MaxTransmissionSize,
67 /// Protocol negotiation timeout.
68 NegotiationTimeout,
69 /// Protocol negotiation failed.
70 NegotiationProtocolError(ProtocolError),
71 /// IO error.
72 Io(std::io::Error),
73}
74
75#[derive(Debug, Clone, Copy)]
76pub enum ValidationError {
77 /// The message has an invalid signature,
78 InvalidSignature,
79 /// The sequence number was empty, expected a value.
80 EmptySequenceNumber,
81 /// The sequence number was the incorrect size
82 InvalidSequenceNumber,
83 /// The PeerId was invalid
84 InvalidPeerId,
85 /// Signature existed when validation has been sent to
86 /// [`crate::behaviour::MessageAuthenticity::Anonymous`].
87 SignaturePresent,
88 /// Sequence number existed when validation has been sent to
89 /// [`crate::behaviour::MessageAuthenticity::Anonymous`].
90 SequenceNumberPresent,
91 /// Message source existed when validation has been sent to
92 /// [`crate::behaviour::MessageAuthenticity::Anonymous`].
93 MessageSourcePresent,
94 /// The data transformation failed.
95 TransformFailed,
96}
97
98impl From<std::io::Error> for GossipsubHandlerError {
99 fn from(error: std::io::Error) -> GossipsubHandlerError {
100 GossipsubHandlerError::Io(error)
101 }
102}
103
104impl From<std::io::Error> for PublishError {
105 fn from(error: std::io::Error) -> PublishError {
106 PublishError::TransformFailed(error)
107 }
108}
109
110impl fmt::Display for GossipsubHandlerError {
111 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112 write!(f, "{:?}", self)
113 }
114}
115
116impl fmt::Display for PublishError {
117 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118 write!(f, "{:?}", self)
119 }
120}
121
122impl std::error::Error for GossipsubHandlerError {
123 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
124 match self {
125 GossipsubHandlerError::Io(io) => Some(io),
126 _ => None,
127 }
128 }
129}