mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-03 21:21:23 +00:00
feat: add QUIC transport to the chat example
See: https://github.com/libp2p/rust-libp2p/issues/3501#issuecomment-1461492407 Pull-Request: #3635.
This commit is contained in:
parent
fd0983567c
commit
7ffa63b656
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -660,6 +660,7 @@ dependencies = [
|
|||||||
"env_logger 0.10.0",
|
"env_logger 0.10.0",
|
||||||
"futures",
|
"futures",
|
||||||
"libp2p",
|
"libp2p",
|
||||||
|
"libp2p-quic",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -10,4 +10,5 @@ async-std = { version = "1.12", features = ["attributes"] }
|
|||||||
async-trait = "0.1"
|
async-trait = "0.1"
|
||||||
env_logger = "0.10.0"
|
env_logger = "0.10.0"
|
||||||
futures = "0.3.27"
|
futures = "0.3.27"
|
||||||
libp2p = { path = "../../libp2p", features = ["async-std", "dns", "gossipsub", "mdns", "mplex", "noise", "macros", "tcp", "websocket", "yamux"] }
|
libp2p = { path = "../../libp2p", features = ["async-std", "gossipsub", "mdns", "mplex", "noise", "macros", "tcp", "yamux"] }
|
||||||
|
libp2p-quic = { path = "../../transports/quic", features = ["async-std"] }
|
||||||
|
@ -46,34 +46,50 @@
|
|||||||
//! event and remove the expired peer from the list of known peers.
|
//! event and remove the expired peer from the list of known peers.
|
||||||
|
|
||||||
use async_std::io;
|
use async_std::io;
|
||||||
use futures::{prelude::*, select};
|
use futures::{future::Either, prelude::*, select};
|
||||||
use libp2p::{
|
use libp2p::{
|
||||||
gossipsub, identity, mdns,
|
core::{muxing::StreamMuxerBox, transport::OrTransport, upgrade},
|
||||||
|
gossipsub, identity, mdns, noise,
|
||||||
swarm::NetworkBehaviour,
|
swarm::NetworkBehaviour,
|
||||||
swarm::{SwarmBuilder, SwarmEvent},
|
swarm::{SwarmBuilder, SwarmEvent},
|
||||||
PeerId,
|
tcp, yamux, PeerId, Transport,
|
||||||
};
|
};
|
||||||
|
use libp2p_quic as quic;
|
||||||
use std::collections::hash_map::DefaultHasher;
|
use std::collections::hash_map::DefaultHasher;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
// We create a custom network behaviour that combines Gossipsub and Mdns.
|
||||||
|
#[derive(NetworkBehaviour)]
|
||||||
|
struct MyBehaviour {
|
||||||
|
gossipsub: gossipsub::Behaviour,
|
||||||
|
mdns: mdns::async_io::Behaviour,
|
||||||
|
}
|
||||||
|
|
||||||
#[async_std::main]
|
#[async_std::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
// Create a random PeerId
|
// Create a random PeerId
|
||||||
let local_key = identity::Keypair::generate_ed25519();
|
let id_keys = identity::Keypair::generate_ed25519();
|
||||||
let local_peer_id = PeerId::from(local_key.public());
|
let local_peer_id = PeerId::from(id_keys.public());
|
||||||
println!("Local peer id: {local_peer_id}");
|
println!("Local peer id: {local_peer_id}");
|
||||||
|
|
||||||
// Set up an encrypted DNS-enabled TCP Transport over the Mplex protocol.
|
// Set up an encrypted DNS-enabled TCP Transport over the Mplex protocol.
|
||||||
let transport = libp2p::development_transport(local_key.clone()).await?;
|
let tcp_transport = tcp::async_io::Transport::new(tcp::Config::default().nodelay(true))
|
||||||
|
.upgrade(upgrade::Version::V1)
|
||||||
// We create a custom network behaviour that combines Gossipsub and Mdns.
|
.authenticate(
|
||||||
#[derive(NetworkBehaviour)]
|
noise::NoiseAuthenticated::xx(&id_keys).expect("signing libp2p-noise static keypair"),
|
||||||
struct MyBehaviour {
|
)
|
||||||
gossipsub: gossipsub::Behaviour,
|
.multiplex(yamux::YamuxConfig::default())
|
||||||
mdns: mdns::async_io::Behaviour,
|
.timeout(std::time::Duration::from_secs(20))
|
||||||
}
|
.boxed();
|
||||||
|
let quic_transport = quic::async_std::Transport::new(quic::Config::new(&id_keys));
|
||||||
|
let transport = OrTransport::new(quic_transport, tcp_transport)
|
||||||
|
.map(|either_output, _| match either_output {
|
||||||
|
Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
|
||||||
|
Either::Right((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
|
||||||
|
})
|
||||||
|
.boxed();
|
||||||
|
|
||||||
// To content-address message, we can take the hash of message and use it as an ID.
|
// To content-address message, we can take the hash of message and use it as an ID.
|
||||||
let message_id_fn = |message: &gossipsub::Message| {
|
let message_id_fn = |message: &gossipsub::Message| {
|
||||||
@ -92,14 +108,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
|
|
||||||
// build a gossipsub network behaviour
|
// build a gossipsub network behaviour
|
||||||
let mut gossipsub = gossipsub::Behaviour::new(
|
let mut gossipsub = gossipsub::Behaviour::new(
|
||||||
gossipsub::MessageAuthenticity::Signed(local_key),
|
gossipsub::MessageAuthenticity::Signed(id_keys),
|
||||||
gossipsub_config,
|
gossipsub_config,
|
||||||
)
|
)
|
||||||
.expect("Correct configuration");
|
.expect("Correct configuration");
|
||||||
|
|
||||||
// Create a Gossipsub topic
|
// Create a Gossipsub topic
|
||||||
let topic = gossipsub::IdentTopic::new("test-net");
|
let topic = gossipsub::IdentTopic::new("test-net");
|
||||||
|
|
||||||
// subscribes to our topic
|
// subscribes to our topic
|
||||||
gossipsub.subscribe(&topic)?;
|
gossipsub.subscribe(&topic)?;
|
||||||
|
|
||||||
@ -114,6 +128,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let mut stdin = io::BufReader::new(io::stdin()).lines().fuse();
|
let mut stdin = io::BufReader::new(io::stdin()).lines().fuse();
|
||||||
|
|
||||||
// Listen on all interfaces and whatever port the OS assigns
|
// Listen on all interfaces and whatever port the OS assigns
|
||||||
|
swarm.listen_on("/ip4/0.0.0.0/udp/0/quic-v1".parse()?)?;
|
||||||
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
|
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
|
||||||
|
|
||||||
println!("Enter messages via STDIN and they will be sent to connected peers using Gossipsub");
|
println!("Enter messages via STDIN and they will be sent to connected peers using Gossipsub");
|
||||||
@ -149,6 +164,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
"Got message: '{}' with id: {id} from peer: {peer_id}",
|
"Got message: '{}' with id: {id} from peer: {peer_id}",
|
||||||
String::from_utf8_lossy(&message.data),
|
String::from_utf8_lossy(&message.data),
|
||||||
),
|
),
|
||||||
|
SwarmEvent::NewListenAddr { address, .. } => {
|
||||||
|
println!("Local node is listening on {address}");
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user