libp2p_gossipsub/lib.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//! Implementation of the [Gossipsub](https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/README.md) protocol.
22//!
23//! Gossipsub is a P2P pubsub (publish/subscription) routing layer designed to extend upon
24//! floodsub and meshsub routing protocols.
25//!
26//! # Overview
27//!
28//! *Note: The gossipsub protocol specifications
29//! (<https://github.com/libp2p/specs/tree/master/pubsub/gossipsub>) provide an outline for the
30//! routing protocol. They should be consulted for further detail.*
31//!
32//! Gossipsub is a blend of meshsub for data and randomsub for mesh metadata. It provides bounded
33//! degree and amplification factor with the meshsub construction and augments it using gossip
34//! propagation of metadata with the randomsub technique.
35//!
36//! The router maintains an overlay mesh network of peers on which to efficiently send messages and
37//! metadata. Peers use control messages to broadcast and request known messages and
38//! subscribe/unsubscribe from topics in the mesh network.
39//!
40//! # Important Discrepancies
41//!
42//! This section outlines the current implementation's potential discrepancies from that of other
43//! implementations, due to undefined elements in the current specification.
44//!
45//! - **Topics** - In gossipsub, topics configurable by the `hash_topics` configuration parameter.
46//! Topics are of type [`TopicHash`]. The current go implementation uses raw utf-8 strings, and
47//! this is default configuration in rust-libp2p. Topics can be hashed (SHA256 hashed then base64
48//! encoded) by setting the `hash_topics` configuration parameter to true.
49//!
50//! - **Sequence Numbers** - A message on the gossipsub network is identified by the source
51//! [`PeerId`](libp2p_identity::PeerId) and a nonce (sequence number) of the message. The sequence
52//! numbers in this implementation are sent as raw bytes across the wire. They are 64-bit
53//! big-endian unsigned integers. When messages are signed, they are monotonically increasing
54//! integers starting from a random value and wrapping around u64::MAX. When messages are
55//! unsigned, they are chosen at random. NOTE: These numbers are sequential in the current go
56//! implementation.
57//!
58//! # Peer Discovery
59//!
60//! Gossipsub does not provide peer discovery by itself. Peer discovery is the process by which
61//! peers in a p2p network exchange information about each other among other reasons to become
62//! resistant against the failure or replacement of the
63//! [boot nodes](https://docs.libp2p.io/reference/glossary/#boot-node) of the network.
64//!
65//! Peer
66//! discovery can e.g. be implemented with the help of the [Kademlia](https://github.com/libp2p/specs/blob/master/kad-dht/README.md) protocol
67//! in combination with the [Identify](https://github.com/libp2p/specs/tree/master/identify) protocol. See the
68//! Kademlia implementation documentation for more information.
69//!
70//! # Using Gossipsub
71//!
72//! ## Gossipsub Config
73//!
74//! The [`Config`] struct specifies various network performance/tuning configuration
75//! parameters. Specifically it specifies:
76//!
77//! [`Config`]: struct.Config.html
78//!
79//! This struct implements the [`Default`] trait and can be initialised via
80//! [`Config::default()`].
81//!
82//!
83//! ## Behaviour
84//!
85//! The [`Behaviour`] struct implements the [`libp2p_swarm::NetworkBehaviour`] trait allowing it to
86//! act as the routing behaviour in a [`libp2p_swarm::Swarm`]. This struct requires an instance of
87//! [`PeerId`](libp2p_identity::PeerId) and [`Config`].
88//!
89//! [`Behaviour`]: struct.Behaviour.html
90
91//! ## Example
92//!
93//! For an example on how to use gossipsub, see the [chat-example](https://github.com/libp2p/rust-libp2p/tree/master/examples/chat).
94
95#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
96
97mod backoff;
98mod behaviour;
99mod config;
100mod error;
101mod gossip_promises;
102mod handler;
103mod mcache;
104mod metrics;
105mod peer_score;
106mod protocol;
107mod rpc;
108mod rpc_proto;
109mod subscription_filter;
110mod time_cache;
111mod topic;
112mod transform;
113mod types;
114
115pub use self::{
116 behaviour::{Behaviour, Event, MessageAuthenticity},
117 config::{Config, ConfigBuilder, ValidationMode, Version},
118 error::{ConfigBuilderError, PublishError, SubscriptionError, ValidationError},
119 metrics::Config as MetricsConfig,
120 peer_score::{
121 score_parameter_decay, score_parameter_decay_with_base, PeerScoreParams,
122 PeerScoreThresholds, TopicScoreParams,
123 },
124 subscription_filter::{
125 AllowAllSubscriptionFilter, CallbackSubscriptionFilter, CombinedSubscriptionFilters,
126 MaxCountSubscriptionFilter, RegexSubscriptionFilter, TopicSubscriptionFilter,
127 WhitelistSubscriptionFilter,
128 },
129 topic::{Hasher, Topic, TopicHash},
130 transform::{DataTransform, IdentityTransform},
131 types::{FailedMessages, Message, MessageAcceptance, MessageId, RawMessage},
132};
133
134#[deprecated(note = "Will be removed from the public API.")]
135pub type Rpc = self::types::Rpc;
136
137pub type IdentTopic = Topic<self::topic::IdentityHash>;
138pub type Sha256Topic = Topic<self::topic::Sha256Hash>;