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, PartialEq)]
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 /// Message size was too large for topic
113 MessageSizeTooLargeForTopic,
114}
115
116impl std::fmt::Display for ValidationError {
117 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
118 write!(f, "{self:?}")
119 }
120}
121
122impl std::error::Error for ValidationError {}
123
124impl From<std::io::Error> for PublishError {
125 fn from(error: std::io::Error) -> PublishError {
126 PublishError::TransformFailed(error)
127 }
128}
129
130/// Error associated with Config building.
131#[derive(Debug)]
132pub enum ConfigBuilderError {
133 /// Maximum transmission size is too small.
134 MaxTransmissionSizeTooSmall,
135 /// History length less than history gossip length.
136 HistoryLengthTooSmall,
137 /// The ineauality doesn't hold mesh_outbound_min <= mesh_n_low <= mesh_n <= mesh_n_high
138 MeshParametersInvalid,
139 /// The inequality doesn't hold mesh_outbound_min <= self.config.mesh_n / 2
140 MeshOutboundInvalid,
141 /// unsubscribe_backoff is zero
142 UnsubscribeBackoffIsZero,
143 /// Invalid protocol
144 InvalidProtocol,
145}
146
147impl std::error::Error for ConfigBuilderError {}
148
149impl std::fmt::Display for ConfigBuilderError {
150 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
151 match self {
152 Self::MaxTransmissionSizeTooSmall => {
153 write!(f, "Maximum transmission size is too small")
154 }
155 Self::HistoryLengthTooSmall => write!(f, "History length less than history gossip length"),
156 Self::MeshParametersInvalid => write!(f, "The ineauality doesn't hold mesh_outbound_min <= mesh_n_low <= mesh_n <= mesh_n_high"),
157 Self::MeshOutboundInvalid => write!(f, "The inequality doesn't hold mesh_outbound_min <= self.config.mesh_n / 2"),
158 Self::UnsubscribeBackoffIsZero => write!(f, "unsubscribe_backoff is zero"),
159 Self::InvalidProtocol => write!(f, "Invalid protocol"),
160 }
161 }
162}