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 libp2p_identity::SigningError;
24
25/// Error associated with publishing a gossipsub message.
26#[derive(Debug)]
27pub enum PublishError {
28 /// This message has already been published.
29 Duplicate,
30 /// An error occurred whilst signing the message.
31 SigningError(SigningError),
32 /// No peers are currently subscribed to receive messages on this topic.
33 /// Wait for peers to subscribe or check your network connectivity.
34 NoPeersSubscribedToTopic,
35 /// The overall message was too large. This could be due to excessive topics or an excessive
36 /// message size.
37 MessageTooLarge,
38 /// The compression algorithm failed.
39 TransformFailed(std::io::Error),
40 /// Messages could not be sent because the queues for all peers were full. The usize represents
41 /// the number of peers that were attempted.
42 AllQueuesFull(usize),
43}
44
45impl std::fmt::Display for PublishError {
46 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
47 write!(f, "{self:?}")
48 }
49}
50
51impl std::error::Error for PublishError {
52 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
53 match self {
54 Self::SigningError(err) => Some(err),
55 Self::TransformFailed(err) => Some(err),
56 _ => None,
57 }
58 }
59}
60
61/// Error associated with subscribing to a topic.
62#[derive(Debug)]
63pub enum SubscriptionError {
64 /// Couldn't publish our subscription
65 PublishError(PublishError),
66 /// We are not allowed to subscribe to this topic by the subscription filter
67 NotAllowed,
68}
69
70impl std::fmt::Display for SubscriptionError {
71 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
72 write!(f, "{self:?}")
73 }
74}
75
76impl std::error::Error for SubscriptionError {
77 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
78 match self {
79 Self::PublishError(err) => Some(err),
80 _ => None,
81 }
82 }
83}
84
85impl From<SigningError> for PublishError {
86 fn from(error: SigningError) -> Self {
87 PublishError::SigningError(error)
88 }
89}
90
91#[derive(Debug, Clone, Copy)]
92pub enum ValidationError {
93 /// The message has an invalid signature,
94 InvalidSignature,
95 /// The sequence number was empty, expected a value.
96 EmptySequenceNumber,
97 /// The sequence number was the incorrect size
98 InvalidSequenceNumber,
99 /// The PeerId was invalid
100 InvalidPeerId,
101 /// Signature existed when validation has been sent to
102 /// [`crate::behaviour::MessageAuthenticity::Anonymous`].
103 SignaturePresent,
104 /// Sequence number existed when validation has been sent to
105 /// [`crate::behaviour::MessageAuthenticity::Anonymous`].
106 SequenceNumberPresent,
107 /// Message source existed when validation has been sent to
108 /// [`crate::behaviour::MessageAuthenticity::Anonymous`].
109 MessageSourcePresent,
110 /// The data transformation failed.
111 TransformFailed,
112}
113
114impl std::fmt::Display for ValidationError {
115 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
116 write!(f, "{self:?}")
117 }
118}
119
120impl std::error::Error for ValidationError {}
121
122impl From<std::io::Error> for PublishError {
123 fn from(error: std::io::Error) -> PublishError {
124 PublishError::TransformFailed(error)
125 }
126}
127
128/// Error associated with Config building.
129#[derive(Debug)]
130pub enum ConfigBuilderError {
131 /// Maximum transmission size is too small.
132 MaxTransmissionSizeTooSmall,
133 /// History length less than history gossip length.
134 HistoryLengthTooSmall,
135 /// The ineauality doesn't hold mesh_outbound_min <= mesh_n_low <= mesh_n <= mesh_n_high
136 MeshParametersInvalid,
137 /// The inequality doesn't hold mesh_outbound_min <= self.config.mesh_n / 2
138 MeshOutboundInvalid,
139 /// unsubscribe_backoff is zero
140 UnsubscribeBackoffIsZero,
141 /// Invalid protocol
142 InvalidProtocol,
143}
144
145impl std::error::Error for ConfigBuilderError {}
146
147impl std::fmt::Display for ConfigBuilderError {
148 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
149 match self {
150 Self::MaxTransmissionSizeTooSmall => {
151 write!(f, "Maximum transmission size is too small")
152 }
153 Self::HistoryLengthTooSmall => write!(f, "History length less than history gossip length"),
154 Self::MeshParametersInvalid => write!(f, "The ineauality doesn't hold mesh_outbound_min <= mesh_n_low <= mesh_n <= mesh_n_high"),
155 Self::MeshOutboundInvalid => write!(f, "The inequality doesn't hold mesh_outbound_min <= self.config.mesh_n / 2"),
156 Self::UnsubscribeBackoffIsZero => write!(f, "unsubscribe_backoff is zero"),
157 Self::InvalidProtocol => write!(f, "Invalid protocol"),
158 }
159 }
160}