GossipSub
Introduction
A GossipSub is a publish/subscribe messaging mechanism for distributed networks of computers. It's an efficient, optimized way for scalable communication, in which a single peer can send data or requests to multiple or all peers.
Nodes in a GossipSub network can “subscribe” to a topic to receive messages that are “published” to it. The key feature of GossipSub is its self-optimizing, allowing each message to travel along the shortest path to each of its neighbors.
Setting up the gossipsub system is simple using the GossipSub template. All that is required is logic for receiving the message (this handles subscribing to a topic) and a way for peers to publish messages. Once a peer publishes a message, all other peers that subscribe to that topic will receive it via epidemic-style messaging.
Which peers subscribe to a topic can be role-based and validated via the topic_validator.
For example, if a subnet has a "trainer" role and a "validator" role, a topic_validator can be set up to ensure only the roles that should be publishing and or forwarding the messages are allowed to.
To build a gossip system, configure the following:
Configure how to handle receiving gossip messages
Develop how and where messages are published to a topic
Receiving Messages
Setting up Gossip topics is a simple approach using a configuration class that takes arguments to input into the gossip template:
A topic to subscribe to
Subscribing to a topic to receive all messages published to this topic.
A handler function to handle the message after receiving it
This can be used to store the data for later use.
A validator for the messages
Create a validator for all messages, such as:
Pydantic validation
Ensuring data is valid
Ensuring only specific roles can publish messages to a topic
Ensuring only specific roles can forward messages in a topic
etcetera
topic
topicThe name of the events to listen for.
topic_handler
topic_handlerThe function that will be called after receiving the topic message. The function can be non-async or async, and the format is:
Where the ID is the peer from whom the message is, and the data is the protobuf message. This looks like:
The from_peer_id is the peer that forwarded the message to you (the peer that originated the publishing of the message, not the peer that forwarded it to you).
topic_validator
topic_validatorThe validator function that validates the gossip message while in propagation.
The validator can be either a non-async function or an async function:
Where the ID is the forwarder_peer_id (not the peer that originated the publishing of the message, but the peer that forwarded it to you). This looks like:
Message Protobuf
The message protobuf rpc_pb2.Message is the following:
from_id is the peer ID that published and originated the message that peers are forwarded to all peers that subscribed to this topic.
data is the payload.
Example
This is an example where peers publish a vote and a peer state. For the "votes" topic, we create the function handle_vote that stores the message, then validate_vote that validates the peer forwarding the message has a proof of stake, the publishing peer is not a validator because only the "trainers" role can gossip votes, and ensures the data matches the Pyndantic schema.
Once this is set up, any peer running this code will listen and wait for other peers to publish messages to the topic or topics created in the GossipSub.
Publishing Messages
Once the receiver is set up, you can begin publishing messages to the GossipSub.
Publishing messages to peers subscribed to a topic is as simple as calling await self.pubsub.publish(topic, message_bytes) from any class or function that takes in the Pubsub class.
A basic peer state heartbeat publisher can look like:
Membership Validator
The above logic in the GossipReceiverTemplate handles both validating who can forward messages and who can originate messages in a GossipSub. Still, it doesn't handle which peers can listen to the topics.
This is what the topic membership validator template solves.
This is something that should be rarely used. In cases where it's a must, where role gossip messaging should be isolated from other roles, this should be used.
Application
GossipSub Research
Last updated