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