tetsy-libp2p-gossipsub

Gossipsub is a P2P pubsub (publish/subscription) routing layer designed to extend upon floodsub 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 [tetsy_libp2p_swarm::NetworkBehaviour] trait allowing it to act as the routing behaviour in a [tetsy_libp2p_swarm::Swarm]. This struct requires an instance of [tetsy_libp2p_core::PeerId] and [GossipsubConfig].

Example

An example of initialising a gossipsub compatible swarm:

``` use tetsylibp2pgossipsub::GossipsubEvent; use tetsylibp2pcore::{identity::Keypair,transport::{Transport, MemoryTransport}, Multiaddr}; use tetsylibp2pgossipsub::MessageAuthenticity; let localkey = Keypair::generateed25519(); let localpeerid = tetsylibp2pcore::PeerId::from(local_key.public());

// Set up an encrypted TCP Transport over the Mplex // This is test transport (memory). let noisekeys = tetsylibp2pnoise::Keypair::::new().intoauthentic(&localkey).unwrap(); let transport = MemoryTransport::default() .upgrade(tetsylibp2pcore::upgrade::Version::V1) .authenticate(tetsylibp2pnoise::NoiseConfig::xx(noisekeys).intoauthenticated()) .multiplex(tetsylibp2p_mplex::MplexConfig::new()) .boxed();

// Create a Gossipsub topic let topic = tetsylibp2pgossipsub::IdentTopic::new("example");

// Set the message authenticity - How we expect to publish messages // Here we expect the publisher to sign the message with their key. let messageauthenticity = MessageAuthenticity::Signed(localkey);

// Create a Swarm to manage peers and events let mut swarm = { // set default parameters for gossipsub let gossipsubconfig = tetsylibp2pgossipsub::GossipsubConfig::default(); // build a gossipsub network behaviour let mut gossipsub: tetsylibp2pgossipsub::Gossipsub = tetsylibp2pgossipsub::Gossipsub::new(messageauthenticity, gossipsubconfig).unwrap(); // subscribe to the topic gossipsub.subscribe(&topic); // create the swarm tetsylibp2pswarm::Swarm::new( transport, gossipsub, localpeer_id, ) };

// Listen on a memory transport. let memory: Multiaddr = tetsylibp2pcore::multiaddr::Protocol::Memory(10).into(); let addr = tetsylibp2pswarm::Swarm::listen_on(&mut swarm, memory).unwrap(); println!("Listening on {:?}", addr); ```