mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-07-30 16:31:57 +00:00
protocols/gossipsub: Add Gossipsub v1.1 support
This commit upgrades the current gossipsub implementation to support the [v1.1 spec](https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.1.md). It adds a number of features, bug fixes and performance improvements. Besides support for all new 1.1 features, other improvements that are of particular note: - Improved duplicate LRU-time cache (this was previously a severe bottleneck for large message throughput topics) - Extended message validation configuration options - Arbitrary topics (users can now implement their own hashing schemes) - Improved message validation handling - Invalid messages are no longer dropped but sent to the behaviour for application-level processing (including scoring) - Support for floodsub, gossipsub v1 and gossipsub v2 - Protobuf encoding has been shifted into the behaviour. This has permitted two improvements: 1. Message size verification during publishing (report to the user if the message is too large before attempting to send). 2. Message fragmentation. If an RPC is too large it is fragmented into its sub components and sent in smaller chunks. Additional Notes The peer eXchange protocol defined in the v1.1 spec is inactive in its current form. The current implementation permits sending `PeerId` in `PRUNE` messages, however a `PeerId` is not sufficient to form a new connection to a peer. A `Signed Address Record` is required to safely transmit peer identity information. Once these are confirmed (https://github.com/libp2p/specs/pull/217) a future PR will implement these and make PX usable. Co-authored-by: Max Inden <mail@max-inden.de> Co-authored-by: Rüdiger Klaehn <rklaehn@protonmail.com> Co-authored-by: blacktemplar <blacktemplar@a1.net> Co-authored-by: Rüdiger Klaehn <rklaehn@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Roman S. Borschel <roman@parity.io> Co-authored-by: Roman Borschel <romanb@users.noreply.github.com> Co-authored-by: David Craven <david@craven.ch>
This commit is contained in:
@@ -34,14 +34,16 @@
|
||||
use async_std::{io, task};
|
||||
use futures::{future, prelude::*};
|
||||
use libp2p::{
|
||||
core::{either::EitherTransport, transport, transport::upgrade::Version, muxing::StreamMuxerBox},
|
||||
core::{
|
||||
either::EitherTransport, muxing::StreamMuxerBox, transport, transport::upgrade::Version,
|
||||
},
|
||||
gossipsub::{self, Gossipsub, GossipsubConfigBuilder, GossipsubEvent, MessageAuthenticity},
|
||||
identify::{Identify, IdentifyEvent},
|
||||
identity,
|
||||
multiaddr::Protocol,
|
||||
noise,
|
||||
ping::{self, Ping, PingConfig, PingEvent},
|
||||
pnet::{PnetConfig, PreSharedKey},
|
||||
noise,
|
||||
swarm::NetworkBehaviourEventProcess,
|
||||
tcp::TcpConfig,
|
||||
yamux::YamuxConfig,
|
||||
@@ -61,9 +63,10 @@ use std::{
|
||||
pub fn build_transport(
|
||||
key_pair: identity::Keypair,
|
||||
psk: Option<PreSharedKey>,
|
||||
) -> transport::Boxed<(PeerId, StreamMuxerBox)>
|
||||
{
|
||||
let noise_keys = noise::Keypair::<noise::X25519Spec>::new().into_authentic(&key_pair).unwrap();
|
||||
) -> transport::Boxed<(PeerId, StreamMuxerBox)> {
|
||||
let noise_keys = noise::Keypair::<noise::X25519Spec>::new()
|
||||
.into_authentic(&key_pair)
|
||||
.unwrap();
|
||||
let noise_config = noise::NoiseConfig::xx(noise_keys).into_authenticated();
|
||||
let yamux_config = YamuxConfig::default();
|
||||
|
||||
@@ -157,7 +160,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
let transport = build_transport(local_key.clone(), psk);
|
||||
|
||||
// Create a Gosspipsub topic
|
||||
let gossipsub_topic = gossipsub::Topic::new("chat".into());
|
||||
let gossipsub_topic = gossipsub::IdentTopic::new("chat");
|
||||
|
||||
// We create a custom network behaviour that combines gossipsub, ping and identify.
|
||||
#[derive(NetworkBehaviour)]
|
||||
@@ -178,7 +181,11 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
// Called when `gossipsub` produces an event.
|
||||
fn inject_event(&mut self, event: GossipsubEvent) {
|
||||
match event {
|
||||
GossipsubEvent::Message(peer_id, id, message) => println!(
|
||||
GossipsubEvent::Message {
|
||||
propagation_source: peer_id,
|
||||
message_id: id,
|
||||
message,
|
||||
} => println!(
|
||||
"Got message: {} with id: {} from peer: {:?}",
|
||||
String::from_utf8_lossy(&message.data),
|
||||
id,
|
||||
@@ -228,11 +235,16 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
// Create a Swarm to manage peers and events
|
||||
let mut swarm = {
|
||||
let gossipsub_config = GossipsubConfigBuilder::new()
|
||||
let gossipsub_config = GossipsubConfigBuilder::default()
|
||||
.max_transmit_size(262144)
|
||||
.build();
|
||||
.build()
|
||||
.expect("valid config");
|
||||
let mut behaviour = MyBehaviour {
|
||||
gossipsub: Gossipsub::new(MessageAuthenticity::Signed(local_key.clone()), gossipsub_config),
|
||||
gossipsub: Gossipsub::new(
|
||||
MessageAuthenticity::Signed(local_key.clone()),
|
||||
gossipsub_config,
|
||||
)
|
||||
.expect("Valid configuration"),
|
||||
identify: Identify::new(
|
||||
"/ipfs/0.1.0".into(),
|
||||
"rust-ipfs-example".into(),
|
||||
@@ -242,7 +254,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
};
|
||||
|
||||
println!("Subscribing to {:?}", gossipsub_topic);
|
||||
behaviour.gossipsub.subscribe(gossipsub_topic.clone());
|
||||
behaviour.gossipsub.subscribe(&gossipsub_topic).unwrap();
|
||||
Swarm::new(transport, behaviour, local_peer_id.clone())
|
||||
};
|
||||
|
||||
@@ -264,9 +276,9 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
task::block_on(future::poll_fn(move |cx: &mut Context<'_>| {
|
||||
loop {
|
||||
if let Err(e) = match stdin.try_poll_next_unpin(cx)? {
|
||||
Poll::Ready(Some(line)) => {
|
||||
swarm.gossipsub.publish(&gossipsub_topic, line.as_bytes())
|
||||
}
|
||||
Poll::Ready(Some(line)) => swarm
|
||||
.gossipsub
|
||||
.publish(gossipsub_topic.clone(), line.as_bytes()),
|
||||
Poll::Ready(None) => panic!("Stdin closed"),
|
||||
Poll::Pending => break,
|
||||
} {
|
||||
|
Reference in New Issue
Block a user