Skip to content

Commit 58af2e0

Browse files
authored
refactor(gossipsub): make error module private
Resolves #3392. Pull-Request: #3457.
1 parent 79b7cef commit 58af2e0

File tree

9 files changed

+180
-136
lines changed

9 files changed

+180
-136
lines changed

protocols/gossipsub/src/behaviour.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ use wasm_timer::Instant;
4848

4949
use crate::backoff::BackoffStorage;
5050
use crate::config::{Config, ValidationMode};
51-
use crate::error::{PublishError, SubscriptionError, ValidationError};
5251
use crate::gossip_promises::GossipPromises;
5352
use crate::handler::{Handler, HandlerEvent, HandlerIn};
5453
use crate::mcache::MessageCache;
@@ -65,6 +64,7 @@ use crate::types::{
6564
};
6665
use crate::types::{PeerConnections, PeerKind, Rpc};
6766
use crate::{rpc_proto, TopicScoreParams};
67+
use crate::{PublishError, SubscriptionError, ValidationError};
6868
use std::{cmp::Ordering::Equal, fmt::Debug};
6969
use wasm_timer::Interval;
7070

protocols/gossipsub/src/behaviour/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
// Collection of tests for the gossipsub network behaviour
2222

2323
use super::*;
24-
use crate::error::ValidationError;
2524
use crate::subscription_filter::WhitelistSubscriptionFilter;
2625
use crate::transform::{DataTransform, IdentityTransform};
2726
use crate::types::FastMessageId;
27+
use crate::ValidationError;
2828
use crate::{
2929
config::Config, config::ConfigBuilder, IdentTopic as Topic, Message, TopicScoreParams,
3030
};

protocols/gossipsub/src/error.rs

Lines changed: 22 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 Sigma Prime Pty Ltd.
1+
// Copyright 2023 Protocol Labs.
22
//
33
// Permission is hereby granted, free of charge, to any person obtaining a
44
// copy of this software and associated documentation files (the "Software"),
@@ -18,136 +18,32 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

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-
}
21+
#[deprecated(
22+
since = "0.44.0",
23+
note = "Use `libp2p::gossipsub::PublishError` instead, as the `error` module will become crate-private in the future."
24+
)]
25+
pub type PublishError = crate::error_priv::PublishError;
8226

83-
impl From<SigningError> for PublishError {
84-
fn from(error: SigningError) -> Self {
85-
PublishError::SigningError(error)
86-
}
87-
}
27+
#[deprecated(
28+
since = "0.44.0",
29+
note = "Use `libp2p::gossipsub::SubscriptionError` instead, as the `error` module will become crate-private in the future."
30+
)]
31+
pub type SubscriptionError = crate::error_priv::SubscriptionError;
8832

8933
#[deprecated(
9034
since = "0.44.0",
9135
note = "Use re-exports that omit `Gossipsub` prefix, i.e. `libp2p::gossipsub::HandlerError"
9236
)]
93-
pub type GossipsubHandlerError = HandlerError;
94-
95-
/// Errors that can occur in the protocols handler.
96-
#[derive(Debug, Error)]
97-
pub enum HandlerError {
98-
#[error("The maximum number of inbound substreams created has been exceeded.")]
99-
MaxInboundSubstreams,
100-
#[error("The maximum number of outbound substreams created has been exceeded.")]
101-
MaxOutboundSubstreams,
102-
#[error("The message exceeds the maximum transmission size.")]
103-
MaxTransmissionSize,
104-
#[error("Protocol negotiation timeout.")]
105-
NegotiationTimeout,
106-
#[error("Protocol negotiation failed.")]
107-
NegotiationProtocolError(ProtocolError),
108-
#[error("Failed to encode or decode")]
109-
Codec(#[from] prost_codec::Error),
110-
}
111-
112-
#[derive(Debug, Clone, Copy)]
113-
pub enum ValidationError {
114-
/// The message has an invalid signature,
115-
InvalidSignature,
116-
/// The sequence number was empty, expected a value.
117-
EmptySequenceNumber,
118-
/// The sequence number was the incorrect size
119-
InvalidSequenceNumber,
120-
/// The PeerId was invalid
121-
InvalidPeerId,
122-
/// Signature existed when validation has been sent to
123-
/// [`crate::behaviour::MessageAuthenticity::Anonymous`].
124-
SignaturePresent,
125-
/// Sequence number existed when validation has been sent to
126-
/// [`crate::behaviour::MessageAuthenticity::Anonymous`].
127-
SequenceNumberPresent,
128-
/// Message source existed when validation has been sent to
129-
/// [`crate::behaviour::MessageAuthenticity::Anonymous`].
130-
MessageSourcePresent,
131-
/// The data transformation failed.
132-
TransformFailed,
133-
}
37+
pub type GossipsubHandlerError = crate::error_priv::HandlerError;
13438

135-
impl std::fmt::Display for ValidationError {
136-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
137-
write!(f, "{self:?}")
138-
}
139-
}
140-
141-
impl std::error::Error for ValidationError {}
142-
143-
impl From<std::io::Error> for HandlerError {
144-
fn from(error: std::io::Error) -> HandlerError {
145-
HandlerError::Codec(prost_codec::Error::from(error))
146-
}
147-
}
39+
#[deprecated(
40+
since = "0.44.0",
41+
note = "Use `libp2p::gossipsub::HandlerError` instead, as the `error` module will become crate-private in the future."
42+
)]
43+
pub type HandlerError = crate::error_priv::HandlerError;
14844

149-
impl From<std::io::Error> for PublishError {
150-
fn from(error: std::io::Error) -> PublishError {
151-
PublishError::TransformFailed(error)
152-
}
153-
}
45+
#[deprecated(
46+
since = "0.44.0",
47+
note = "Use `libp2p::gossipsub::ValidationError` instead, as the `error` module will become crate-private in the future."
48+
)]
49+
pub type ValidationError = crate::error_priv::ValidationError;
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
}

protocols/gossipsub/src/gossip_promises.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
use crate::error::ValidationError;
2221
use crate::peer_score::RejectReason;
2322
use crate::MessageId;
23+
use crate::ValidationError;
2424
use libp2p_core::PeerId;
2525
use log::debug;
2626
use std::collections::HashMap;

protocols/gossipsub/src/handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
use crate::error::{HandlerError, ValidationError};
2221
use crate::protocol::{GossipsubCodec, ProtocolConfig};
2322
use crate::types::{PeerKind, RawMessage, Rpc};
23+
use crate::{HandlerError, ValidationError};
2424
use asynchronous_codec::Framed;
2525
use futures::prelude::*;
2626
use futures::StreamExt;

protocols/gossipsub/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,19 @@
138138
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
139139

140140
pub mod error;
141+
pub mod metrics;
141142
pub mod protocol;
143+
pub mod subscription_filter;
144+
pub mod time_cache;
142145

143146
mod backoff;
144147
mod behaviour;
145148
mod config;
149+
mod error_priv;
146150
mod gossip_promises;
147151
mod handler;
148152
mod mcache;
149-
pub mod metrics;
150153
mod peer_score;
151-
pub mod subscription_filter;
152-
pub mod time_cache;
153154
mod topic;
154155
mod transform;
155156
mod types;
@@ -158,7 +159,7 @@ mod rpc_proto;
158159

159160
pub use self::behaviour::{Behaviour, Event, MessageAuthenticity};
160161
pub use self::config::{Config, ConfigBuilder, ValidationMode, Version};
161-
pub use self::error::{HandlerError, PublishError, SubscriptionError, ValidationError};
162+
pub use self::error_priv::{HandlerError, PublishError, SubscriptionError, ValidationError};
162163
pub use self::peer_score::{
163164
score_parameter_decay, score_parameter_decay_with_base, PeerScoreParams, PeerScoreThresholds,
164165
TopicScoreParams,

0 commit comments

Comments
 (0)