46
46
//! event and remove the expired peer from the list of known peers.
47
47
48
48
use async_std:: io;
49
- use futures:: { prelude:: * , select} ;
49
+ use futures:: { future :: Either , prelude:: * , select} ;
50
50
use libp2p:: {
51
- gossipsub, identity, mdns,
51
+ core:: { muxing:: StreamMuxerBox , transport:: OrTransport , upgrade} ,
52
+ gossipsub, identity, mdns, noise,
52
53
swarm:: NetworkBehaviour ,
53
54
swarm:: { SwarmBuilder , SwarmEvent } ,
54
- PeerId ,
55
+ tcp , yamux , PeerId , Transport ,
55
56
} ;
57
+ use libp2p_quic as quic;
56
58
use std:: collections:: hash_map:: DefaultHasher ;
57
59
use std:: error:: Error ;
58
60
use std:: hash:: { Hash , Hasher } ;
59
61
use std:: time:: Duration ;
60
62
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
+
61
70
#[ async_std:: main]
62
71
async fn main ( ) -> Result < ( ) , Box < dyn Error > > {
63
72
// 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 ( ) ) ;
66
75
println ! ( "Local peer id: {local_peer_id}" ) ;
67
76
68
77
// 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 ( ) ;
77
93
78
94
// To content-address message, we can take the hash of message and use it as an ID.
79
95
let message_id_fn = |message : & gossipsub:: Message | {
@@ -92,14 +108,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
92
108
93
109
// build a gossipsub network behaviour
94
110
let mut gossipsub = gossipsub:: Behaviour :: new (
95
- gossipsub:: MessageAuthenticity :: Signed ( local_key ) ,
111
+ gossipsub:: MessageAuthenticity :: Signed ( id_keys ) ,
96
112
gossipsub_config,
97
113
)
98
114
. expect ( "Correct configuration" ) ;
99
-
100
115
// Create a Gossipsub topic
101
116
let topic = gossipsub:: IdentTopic :: new ( "test-net" ) ;
102
-
103
117
// subscribes to our topic
104
118
gossipsub. subscribe ( & topic) ?;
105
119
@@ -114,6 +128,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
114
128
let mut stdin = io:: BufReader :: new ( io:: stdin ( ) ) . lines ( ) . fuse ( ) ;
115
129
116
130
// Listen on all interfaces and whatever port the OS assigns
131
+ swarm. listen_on ( "/ip4/0.0.0.0/udp/0/quic-v1" . parse ( ) ?) ?;
117
132
swarm. listen_on ( "/ip4/0.0.0.0/tcp/0" . parse ( ) ?) ?;
118
133
119
134
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>> {
149
164
"Got message: '{}' with id: {id} from peer: {peer_id}" ,
150
165
String :: from_utf8_lossy( & message. data) ,
151
166
) ,
167
+ SwarmEvent :: NewListenAddr { address, .. } => {
168
+ println!( "Local node is listening on {address}" ) ;
169
+ }
152
170
_ => { }
153
171
}
154
172
}
0 commit comments