Crate libp2p_gossipsub[][src]

Gossipsub is a P2P pubsub (publish/subscription) routing layer designed to extend upon flooodsub and meshsub routing protocols.

Overview

Note: The gossipsub protocol specifications (https://github.com/libp2p/specs/tree/master/pubsub/gossipsub) provide an outline for the routing protocol. They should be consulted for further detail.

Gossipsub is a blend of meshsub for data and randomsub for mesh metadata. It provides bounded degree and amplification factor with the meshsub construction and augments it using gossip propagation of metadata with the randomsub technique.

The router maintains an overlay mesh network of peers on which to efficiently send messages and metadata. Peers use control messages to broadcast and request known messages and subscribe/unsubscribe from topics in the mesh network.

Important Discrepancies

This section outlines the current implementation’s potential discrepancies from that of other implementations, due to undefined elements in the current specification.

Using Gossipsub

GossipsubConfig

The GossipsubConfig struct specifies various network performance/tuning configuration parameters. Specifically it specifies:

This struct implements the Default trait and can be initialised via GossipsubConfig::default().

Gossipsub

The Gossipsub struct implements the NetworkBehaviour trait allowing it to act as the routing behaviour in a Swarm. This struct requires an instance of PeerId and GossipsubConfig.

Example

An example of initialising a gossipsub compatible swarm:

#extern crate libp2p;
#extern crate futures;
#extern crate tokio;
#use libp2p::gossipsub::GossipsubEvent;
#use libp2p::{identity, gossipsub,
#};
let local_key = identity::Keypair::generate_ed25519();
let local_pub_key = local_key.public();

// Set up an encrypted TCP Transport over the Mplex and Yamux protocols
let transport = libp2p::build_development_transport(local_key);

// Create a Floodsub/Gossipsub topic
let topic = libp2p::floodsub::TopicBuilder::new("example").build();

// Create a Swarm to manage peers and events
let mut swarm = {
    // set default parameters for gossipsub
    let gossipsub_config = gossipsub::GossipsubConfig::default();
    // build a gossipsub network behaviour
    let mut gossipsub =
        gossipsub::Gossipsub::new(local_pub_key.clone().into_peer_id(), gossipsub_config);
    gossipsub.subscribe(topic.clone());
    libp2p::Swarm::new(
        transport,
        gossipsub,
        libp2p::core::topology::MemoryTopology::empty(local_pub_key),
    )
};

// Listen on all interfaces and whatever port the OS assigns
let addr = libp2p::Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse().unwrap()).unwrap();
println!("Listening on {:?}", addr);

Re-exports

pub use self::protocol::GossipsubMessage;
pub use self::protocol::MessageId;

Modules

error

Error types that can result from gossipsub.

protocol

Structs

Gossipsub

Network behaviour that handles the gossipsub protocol.

GossipsubConfig

Configuration parameters that define the performance of the gossipsub network.

GossipsubConfigBuilder

The builder struct for constructing a gossipsub configuration.

GossipsubRpc

An RPC received/sent.

Topic

A gossipsub topic.

TopicHash

Enums

GossipsubEvent

Event that can happen on the gossipsub behaviour.

MessageAuthenticity

Determines if published messages should be signed or not.

ValidationMode

The types of message validation that can be employed by gossipsub.