@@ -11,18 +11,15 @@ use anyhow::{anyhow, Result};
11
11
use cfg_if:: cfg_if;
12
12
use libp2p:: {
13
13
autonat,
14
- core:: upgrade,
15
- dns,
16
14
futures:: StreamExt ,
17
15
gossipsub:: { self , IdentTopic , MessageAuthenticity , TopicHash } ,
18
16
identify,
19
17
kad:: { self , store:: MemoryStore } ,
20
18
mdns,
21
19
multiaddr:: { Multiaddr , Protocol } ,
22
- noise,
23
20
request_response:: { self , OutboundFailure , ProtocolSupport } ,
24
- swarm:: { self , dial_opts:: DialOpts , NetworkBehaviour , SwarmEvent } ,
25
- tcp , yamux , PeerId , StreamProtocol , Swarm , Transport ,
21
+ swarm:: { dial_opts:: DialOpts , NetworkBehaviour , SwarmEvent } ,
22
+ PeerId , StreamProtocol , Swarm ,
26
23
} ;
27
24
use tokio:: {
28
25
select,
@@ -101,51 +98,45 @@ impl P2pNode {
101
98
let peer_id = PeerId :: from ( key_pair. public ( ) ) ;
102
99
info ! ( %peer_id) ;
103
100
104
- let transport = tcp :: tokio :: Transport :: new ( tcp :: Config :: default ( ) )
105
- . upgrade ( upgrade :: Version :: V1 )
106
- . authenticate ( noise :: Config :: new ( & key_pair ) ? )
107
- . multiplex ( yamux :: Config :: default ( ) )
108
- . boxed ( ) ;
109
- let transport = dns :: tokio :: Transport :: system ( transport ) ? . boxed ( ) ;
110
-
111
- let behaviour = Behaviour {
112
- request_response : request_response :: cbor :: Behaviour :: new (
113
- iter :: once ( ( StreamProtocol :: new ( "/zq2-message/1" ) , ProtocolSupport :: Full ) ) ,
114
- Default :: default ( ) ,
115
- ) ,
116
- gossipsub : gossipsub:: Behaviour :: new (
117
- MessageAuthenticity :: Signed ( key_pair . clone ( ) ) ,
118
- gossipsub :: ConfigBuilder :: default ( )
119
- . max_transmit_size ( 524288 )
120
- . build ( )
101
+ let swarm = libp2p :: SwarmBuilder :: with_existing_identity ( key_pair )
102
+ . with_tokio ( )
103
+ . with_quic ( )
104
+ . with_dns ( ) ?
105
+ . with_behaviour ( |key_pair| {
106
+ Ok ( Behaviour {
107
+ request_response : request_response :: cbor :: Behaviour :: new (
108
+ iter :: once ( ( StreamProtocol :: new ( "/zq2-message/1" ) , ProtocolSupport :: Full ) ) ,
109
+ Default :: default ( ) ,
110
+ ) ,
111
+ gossipsub : gossipsub :: Behaviour :: new (
112
+ MessageAuthenticity :: Signed ( key_pair . clone ( ) ) ,
113
+ gossipsub:: ConfigBuilder :: default ( )
114
+ . max_transmit_size ( 524288 )
115
+ . build ( )
116
+ . map_err ( |e| anyhow ! ( e ) ) ? ,
117
+ )
121
118
. map_err ( |e| anyhow ! ( e) ) ?,
122
- )
123
- . map_err ( |e| anyhow ! ( e) ) ?,
124
- mdns : mdns:: Behaviour :: new ( Default :: default ( ) , peer_id) ?,
125
- autonat : autonat:: Behaviour :: new (
126
- peer_id,
127
- autonat:: Config {
128
- // Config changes to speed up autonat initialization. Set back to default if too aggressive.
129
- retry_interval : Duration :: from_secs ( 10 ) ,
130
- refresh_interval : Duration :: from_secs ( 30 ) ,
131
- boot_delay : Duration :: from_secs ( 5 ) ,
132
- throttle_server_period : Duration :: ZERO ,
133
- ..Default :: default ( )
134
- } ,
135
- ) ,
136
- identify : identify:: Behaviour :: new ( identify:: Config :: new (
137
- "/ipfs/id/1.0.0" . to_owned ( ) ,
138
- key_pair. public ( ) ,
139
- ) ) ,
140
- kademlia : kad:: Behaviour :: new ( peer_id, MemoryStore :: new ( peer_id) ) ,
141
- } ;
142
-
143
- let swarm = Swarm :: new (
144
- transport,
145
- behaviour,
146
- peer_id,
147
- swarm:: Config :: with_tokio_executor ( ) ,
148
- ) ;
119
+ mdns : mdns:: Behaviour :: new ( Default :: default ( ) , peer_id) ?,
120
+ autonat : autonat:: Behaviour :: new (
121
+ peer_id,
122
+ autonat:: Config {
123
+ // Config changes to speed up autonat initialization.
124
+ // Set back to default if too aggressive.
125
+ retry_interval : Duration :: from_secs ( 10 ) ,
126
+ refresh_interval : Duration :: from_secs ( 30 ) ,
127
+ boot_delay : Duration :: from_secs ( 5 ) ,
128
+ throttle_server_period : Duration :: ZERO ,
129
+ ..Default :: default ( )
130
+ } ,
131
+ ) ,
132
+ identify : identify:: Behaviour :: new ( identify:: Config :: new (
133
+ "/ipfs/id/1.0.0" . to_owned ( ) ,
134
+ key_pair. public ( ) ,
135
+ ) ) ,
136
+ kademlia : kad:: Behaviour :: new ( peer_id, MemoryStore :: new ( peer_id) ) ,
137
+ } )
138
+ } ) ?
139
+ . build ( ) ;
149
140
150
141
Ok ( Self {
151
142
shard_nodes : HashMap :: new ( ) ,
@@ -223,10 +214,12 @@ impl P2pNode {
223
214
}
224
215
225
216
pub async fn start ( & mut self ) -> Result < ( ) > {
226
- let mut addr: Multiaddr = "/ip4/0.0.0.0" . parse ( ) . unwrap ( ) ;
227
- addr. push ( Protocol :: Tcp ( self . config . p2p_port ) ) ;
228
-
229
- self . swarm . listen_on ( addr) ?;
217
+ self . swarm . listen_on (
218
+ Multiaddr :: empty ( )
219
+ . with ( Protocol :: Ip4 ( std:: net:: Ipv4Addr :: UNSPECIFIED ) )
220
+ . with ( Protocol :: Udp ( self . config . p2p_port ) )
221
+ . with ( Protocol :: QuicV1 ) ,
222
+ ) ?;
230
223
231
224
if let Some ( bootstrap_address) = & self . config . bootstrap_address {
232
225
self . swarm
@@ -242,6 +235,7 @@ impl P2pNode {
242
235
if let Some ( ( peer, address) ) = & self . config . bootstrap_address {
243
236
self . swarm . dial (
244
237
DialOpts :: peer_id ( * peer)
238
+ . override_role ( ) // hole-punch
245
239
. addresses ( vec ! [ address. clone( ) ] )
246
240
. build ( ) ,
247
241
) ?;
@@ -263,7 +257,7 @@ impl P2pNode {
263
257
debug!( ?event, "swarm event" ) ;
264
258
match event {
265
259
SwarmEvent :: NewListenAddr { address, .. } => {
266
- info!( %address, "started listening" ) ;
260
+ info!( %address, "P2P swarm listening on " ) ;
267
261
}
268
262
SwarmEvent :: Behaviour ( BehaviourEvent :: Mdns ( mdns:: Event :: Discovered ( list) ) ) => {
269
263
for ( peer_id, addr) in list {
0 commit comments