Skip to content

Commit 7ffa63b

Browse files
authored
feat: add QUIC transport to the chat example
See: #3501 (comment) Pull-Request: #3635.
1 parent fd09835 commit 7ffa63b

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/chat-example/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ async-std = { version = "1.12", features = ["attributes"] }
1010
async-trait = "0.1"
1111
env_logger = "0.10.0"
1212
futures = "0.3.27"
13-
libp2p = { path = "../../libp2p", features = ["async-std", "dns", "gossipsub", "mdns", "mplex", "noise", "macros", "tcp", "websocket", "yamux"] }
13+
libp2p = { path = "../../libp2p", features = ["async-std", "gossipsub", "mdns", "mplex", "noise", "macros", "tcp", "yamux"] }
14+
libp2p-quic = { path = "../../transports/quic", features = ["async-std"] }

examples/chat-example/src/main.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,50 @@
4646
//! event and remove the expired peer from the list of known peers.
4747
4848
use async_std::io;
49-
use futures::{prelude::*, select};
49+
use futures::{future::Either, prelude::*, select};
5050
use libp2p::{
51-
gossipsub, identity, mdns,
51+
core::{muxing::StreamMuxerBox, transport::OrTransport, upgrade},
52+
gossipsub, identity, mdns, noise,
5253
swarm::NetworkBehaviour,
5354
swarm::{SwarmBuilder, SwarmEvent},
54-
PeerId,
55+
tcp, yamux, PeerId, Transport,
5556
};
57+
use libp2p_quic as quic;
5658
use std::collections::hash_map::DefaultHasher;
5759
use std::error::Error;
5860
use std::hash::{Hash, Hasher};
5961
use std::time::Duration;
6062

63+
// We create a custom network behaviour that combines Gossipsub and Mdns.
64+
#[derive(NetworkBehaviour)]
65+
struct MyBehaviour {
66+
gossipsub: gossipsub::Behaviour,
67+
mdns: mdns::async_io::Behaviour,
68+
}
69+
6170
#[async_std::main]
6271
async fn main() -> Result<(), Box<dyn Error>> {
6372
// Create a random PeerId
64-
let local_key = identity::Keypair::generate_ed25519();
65-
let local_peer_id = PeerId::from(local_key.public());
73+
let id_keys = identity::Keypair::generate_ed25519();
74+
let local_peer_id = PeerId::from(id_keys.public());
6675
println!("Local peer id: {local_peer_id}");
6776

6877
// Set up an encrypted DNS-enabled TCP Transport over the Mplex protocol.
69-
let transport = libp2p::development_transport(local_key.clone()).await?;
70-
71-
// We create a custom network behaviour that combines Gossipsub and Mdns.
72-
#[derive(NetworkBehaviour)]
73-
struct MyBehaviour {
74-
gossipsub: gossipsub::Behaviour,
75-
mdns: mdns::async_io::Behaviour,
76-
}
78+
let tcp_transport = tcp::async_io::Transport::new(tcp::Config::default().nodelay(true))
79+
.upgrade(upgrade::Version::V1)
80+
.authenticate(
81+
noise::NoiseAuthenticated::xx(&id_keys).expect("signing libp2p-noise static keypair"),
82+
)
83+
.multiplex(yamux::YamuxConfig::default())
84+
.timeout(std::time::Duration::from_secs(20))
85+
.boxed();
86+
let quic_transport = quic::async_std::Transport::new(quic::Config::new(&id_keys));
87+
let transport = OrTransport::new(quic_transport, tcp_transport)
88+
.map(|either_output, _| match either_output {
89+
Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
90+
Either::Right((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
91+
})
92+
.boxed();
7793

7894
// To content-address message, we can take the hash of message and use it as an ID.
7995
let message_id_fn = |message: &gossipsub::Message| {
@@ -92,14 +108,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
92108

93109
// build a gossipsub network behaviour
94110
let mut gossipsub = gossipsub::Behaviour::new(
95-
gossipsub::MessageAuthenticity::Signed(local_key),
111+
gossipsub::MessageAuthenticity::Signed(id_keys),
96112
gossipsub_config,
97113
)
98114
.expect("Correct configuration");
99-
100115
// Create a Gossipsub topic
101116
let topic = gossipsub::IdentTopic::new("test-net");
102-
103117
// subscribes to our topic
104118
gossipsub.subscribe(&topic)?;
105119

@@ -114,6 +128,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
114128
let mut stdin = io::BufReader::new(io::stdin()).lines().fuse();
115129

116130
// Listen on all interfaces and whatever port the OS assigns
131+
swarm.listen_on("/ip4/0.0.0.0/udp/0/quic-v1".parse()?)?;
117132
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
118133

119134
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>> {
149164
"Got message: '{}' with id: {id} from peer: {peer_id}",
150165
String::from_utf8_lossy(&message.data),
151166
),
167+
SwarmEvent::NewListenAddr { address, .. } => {
168+
println!("Local node is listening on {address}");
169+
}
152170
_ => {}
153171
}
154172
}

0 commit comments

Comments
 (0)