|
| 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 | +
|
| 23 | +use libp2p_core::identity::error::SigningError; |
| 24 | +use libp2p_core::upgrade::ProtocolError; |
| 25 | +use thiserror::Error; |
| 26 | + |
| 27 | +/// Error associated with publishing a gossipsub message. |
| 28 | +#[derive(Debug)] |
| 29 | +pub 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 | +impl std::fmt::Display for PublishError { |
| 44 | + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 45 | + write!(f, "{self:?}") |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl std::error::Error for PublishError { |
| 50 | + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 51 | + match self { |
| 52 | + Self::SigningError(err) => Some(err), |
| 53 | + Self::TransformFailed(err) => Some(err), |
| 54 | + _ => None, |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// Error associated with subscribing to a topic. |
| 60 | +#[derive(Debug)] |
| 61 | +pub enum SubscriptionError { |
| 62 | + /// Couldn't publish our subscription |
| 63 | + PublishError(PublishError), |
| 64 | + /// We are not allowed to subscribe to this topic by the subscription filter |
| 65 | + NotAllowed, |
| 66 | +} |
| 67 | + |
| 68 | +impl std::fmt::Display for SubscriptionError { |
| 69 | + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 70 | + write!(f, "{self:?}") |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl std::error::Error for SubscriptionError { |
| 75 | + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 76 | + match self { |
| 77 | + Self::PublishError(err) => Some(err), |
| 78 | + _ => None, |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl From<SigningError> for PublishError { |
| 84 | + fn from(error: SigningError) -> Self { |
| 85 | + PublishError::SigningError(error) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/// Errors that can occur in the protocols handler. |
| 90 | +#[derive(Debug, Error)] |
| 91 | +pub enum HandlerError { |
| 92 | + #[error("The maximum number of inbound substreams created has been exceeded.")] |
| 93 | + MaxInboundSubstreams, |
| 94 | + #[error("The maximum number of outbound substreams created has been exceeded.")] |
| 95 | + MaxOutboundSubstreams, |
| 96 | + #[error("The message exceeds the maximum transmission size.")] |
| 97 | + MaxTransmissionSize, |
| 98 | + #[error("Protocol negotiation timeout.")] |
| 99 | + NegotiationTimeout, |
| 100 | + #[error("Protocol negotiation failed.")] |
| 101 | + NegotiationProtocolError(ProtocolError), |
| 102 | + #[error("Failed to encode or decode")] |
| 103 | + Codec(#[from] prost_codec::Error), |
| 104 | +} |
| 105 | + |
| 106 | +#[derive(Debug, Clone, Copy)] |
| 107 | +pub enum ValidationError { |
| 108 | + /// The message has an invalid signature, |
| 109 | + InvalidSignature, |
| 110 | + /// The sequence number was empty, expected a value. |
| 111 | + EmptySequenceNumber, |
| 112 | + /// The sequence number was the incorrect size |
| 113 | + InvalidSequenceNumber, |
| 114 | + /// The PeerId was invalid |
| 115 | + InvalidPeerId, |
| 116 | + /// Signature existed when validation has been sent to |
| 117 | + /// [`crate::behaviour::MessageAuthenticity::Anonymous`]. |
| 118 | + SignaturePresent, |
| 119 | + /// Sequence number existed when validation has been sent to |
| 120 | + /// [`crate::behaviour::MessageAuthenticity::Anonymous`]. |
| 121 | + SequenceNumberPresent, |
| 122 | + /// Message source existed when validation has been sent to |
| 123 | + /// [`crate::behaviour::MessageAuthenticity::Anonymous`]. |
| 124 | + MessageSourcePresent, |
| 125 | + /// The data transformation failed. |
| 126 | + TransformFailed, |
| 127 | +} |
| 128 | + |
| 129 | +impl std::fmt::Display for ValidationError { |
| 130 | + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 131 | + write!(f, "{self:?}") |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +impl std::error::Error for ValidationError {} |
| 136 | + |
| 137 | +impl From<std::io::Error> for HandlerError { |
| 138 | + fn from(error: std::io::Error) -> HandlerError { |
| 139 | + HandlerError::Codec(prost_codec::Error::from(error)) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +impl From<std::io::Error> for PublishError { |
| 144 | + fn from(error: std::io::Error) -> PublishError { |
| 145 | + PublishError::TransformFailed(error) |
| 146 | + } |
| 147 | +} |
0 commit comments