protocols/gossipsub: Add message signing and verification configuration (#1583)

This adds optional message signing and verification to the gossipsub protocol as
per the libp2p specifications.

In addition this commit:

- Removes the LruCache received cache and simply uses the memcache in it's
  place.

- Send subscriptions to all peers

- Prevent invalid messages from being gossiped

- Send grafts when subscriptions are added to the mesh

Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Rüdiger Klaehn <rklaehn@protonmail.com>
Co-authored-by: Rüdiger Klaehn <rklaehn@gmail.com>
This commit is contained in:
Age Manning
2020-08-03 18:13:43 +10:00
committed by GitHub
parent d0ad105433
commit d8ad7bddf5
14 changed files with 1358 additions and 568 deletions

View File

@ -35,7 +35,7 @@ use async_std::{io, task};
use futures::{future, prelude::*};
use libp2p::{
core::{either::EitherTransport, transport::upgrade::Version, StreamMuxer},
gossipsub::{self, Gossipsub, GossipsubConfigBuilder, GossipsubEvent},
gossipsub::{self, Gossipsub, GossipsubConfigBuilder, GossipsubEvent, MessageAuthenticity},
identify::{Identify, IdentifyEvent},
identity,
multiaddr::Protocol,
@ -178,18 +178,14 @@ fn main() -> Result<(), Box<dyn Error>> {
ping: Ping,
}
impl NetworkBehaviourEventProcess<IdentifyEvent>
for MyBehaviour
{
impl NetworkBehaviourEventProcess<IdentifyEvent> for MyBehaviour {
// Called when `identify` produces an event.
fn inject_event(&mut self, event: IdentifyEvent) {
println!("identify: {:?}", event);
}
}
impl NetworkBehaviourEventProcess<GossipsubEvent>
for MyBehaviour
{
impl NetworkBehaviourEventProcess<GossipsubEvent> for MyBehaviour {
// Called when `gossipsub` produces an event.
fn inject_event(&mut self, event: GossipsubEvent) {
match event {
@ -204,9 +200,7 @@ fn main() -> Result<(), Box<dyn Error>> {
}
}
impl NetworkBehaviourEventProcess<PingEvent>
for MyBehaviour
{
impl NetworkBehaviourEventProcess<PingEvent> for MyBehaviour {
// Called when `ping` produces an event.
fn inject_event(&mut self, event: PingEvent) {
use ping::handler::{PingFailure, PingSuccess};
@ -245,11 +239,11 @@ fn main() -> Result<(), Box<dyn Error>> {
// Create a Swarm to manage peers and events
let mut swarm = {
let gossipsub_config = GossipsubConfigBuilder::default()
let gossipsub_config = GossipsubConfigBuilder::new()
.max_transmit_size(262144)
.build();
let mut behaviour = MyBehaviour {
gossipsub: Gossipsub::new(local_peer_id.clone(), gossipsub_config),
gossipsub: Gossipsub::new(MessageAuthenticity::Signed(local_key.clone()), gossipsub_config),
identify: Identify::new(
"/ipfs/0.1.0".into(),
"rust-ipfs-example".into(),
@ -280,12 +274,14 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut listening = false;
task::block_on(future::poll_fn(move |cx: &mut Context<'_>| {
loop {
match stdin.try_poll_next_unpin(cx)? {
if let Err(e) = match stdin.try_poll_next_unpin(cx)? {
Poll::Ready(Some(line)) => {
swarm.gossipsub.publish(&gossipsub_topic, line.as_bytes());
swarm.gossipsub.publish(&gossipsub_topic, line.as_bytes())
}
Poll::Ready(None) => panic!("Stdin closed"),
Poll::Pending => break,
} {
println!("Publish error: {:?}", e);
}
}
loop {