1
+ use crate :: p2p:: AvailableNodes ;
1
2
use libp2p:: futures:: StreamExt ;
2
3
use libp2p:: gossipsub:: {
3
4
Message , MessageAcceptance , MessageId , PublishError , SubscriptionError , TopicHash ,
4
5
} ;
5
6
use libp2p:: kad:: { GetClosestPeersError , GetClosestPeersOk , QueryResult } ;
6
- use libp2p:: { gossipsub, identify, kad, multiaddr:: Protocol , noise, swarm:: SwarmEvent , tcp, yamux} ;
7
+ use libp2p:: {
8
+ autonat, gossipsub, identify, kad, multiaddr:: Protocol , noise, swarm:: SwarmEvent , tcp, yamux,
9
+ } ;
7
10
use libp2p:: { Multiaddr , PeerId , Swarm , SwarmBuilder } ;
8
11
use libp2p_identity:: Keypair ;
9
12
use tokio:: time:: Duration ;
10
- use tokio_util:: sync:: CancellationToken ;
11
-
12
- use crate :: p2p:: AvailableNodes ;
13
+ use tokio:: time:: Instant ;
13
14
14
15
use super :: { DriaBehaviour , DriaBehaviourEvent , DRIA_PROTO_NAME } ;
15
16
16
17
/// Underlying libp2p client.
17
18
pub struct P2PClient {
18
19
swarm : Swarm < DriaBehaviour > ,
19
- cancellation : CancellationToken ,
20
20
/// Peer count for (All, Mesh).
21
21
peer_count : ( usize , usize ) ,
22
- peer_last_refreshed : tokio :: time :: Instant ,
22
+ peer_last_refreshed : Instant ,
23
23
}
24
24
25
25
/// Number of seconds before an idle connection is closed.
@@ -34,7 +34,6 @@ impl P2PClient {
34
34
keypair : Keypair ,
35
35
listen_addr : Multiaddr ,
36
36
available_nodes : & AvailableNodes ,
37
- cancellation : CancellationToken ,
38
37
) -> Result < Self , String > {
39
38
// this is our peerId
40
39
let node_peerid = keypair. public ( ) . to_peer_id ( ) ;
@@ -115,9 +114,8 @@ impl P2PClient {
115
114
116
115
Ok ( Self {
117
116
swarm,
118
- cancellation,
119
117
peer_count : ( 0 , 0 ) ,
120
- peer_last_refreshed : tokio :: time :: Instant :: now ( ) ,
118
+ peer_last_refreshed : Instant :: now ( ) ,
121
119
} )
122
120
}
123
121
@@ -169,6 +167,8 @@ impl P2PClient {
169
167
propagation_source : & PeerId ,
170
168
acceptance : MessageAcceptance ,
171
169
) -> Result < ( ) , PublishError > {
170
+ log:: debug!( "Validating message ({}): {:?}" , msg_id, acceptance) ;
171
+
172
172
let msg_was_in_cache = self
173
173
. swarm
174
174
. behaviour_mut ( )
@@ -191,65 +191,74 @@ impl P2PClient {
191
191
}
192
192
193
193
/// Listens to the Swarm for incoming messages.
194
+ ///
194
195
/// This method should be called in a loop to keep the client running.
195
- /// When a message is received, it will be returned.
196
+ /// When a GossipSub message is received, it will be returned.
196
197
pub async fn process_events ( & mut self ) -> Option < ( PeerId , MessageId , Message ) > {
197
198
loop {
198
199
// refresh peers
199
200
self . refresh_peer_counts ( ) . await ;
200
201
201
202
// wait for next event
202
- tokio:: select! {
203
- event = self . swarm. select_next_some( ) => match event {
204
- SwarmEvent :: Behaviour ( DriaBehaviourEvent :: Kademlia (
205
- kad:: Event :: OutboundQueryProgressed {
206
- result: QueryResult :: GetClosestPeers ( result) ,
207
- ..
208
- } ,
209
- ) ) => self . handle_closest_peers_result( result) ,
210
- SwarmEvent :: Behaviour ( DriaBehaviourEvent :: Identify ( identify:: Event :: Received {
211
- peer_id,
212
- info,
203
+ match self . swarm . select_next_some ( ) . await {
204
+ SwarmEvent :: Behaviour ( DriaBehaviourEvent :: Kademlia (
205
+ kad:: Event :: OutboundQueryProgressed {
206
+ result : QueryResult :: GetClosestPeers ( result) ,
213
207
..
214
- } ) ) => self . handle_identify_event( peer_id, info) ,
215
- SwarmEvent :: Behaviour ( DriaBehaviourEvent :: Gossipsub ( gossipsub:: Event :: Message {
208
+ } ,
209
+ ) ) => self . handle_closest_peers_result ( result) ,
210
+ SwarmEvent :: Behaviour ( DriaBehaviourEvent :: Identify (
211
+ identify:: Event :: Received { peer_id, info, .. } ,
212
+ ) ) => self . handle_identify_event ( peer_id, info) ,
213
+ SwarmEvent :: Behaviour ( DriaBehaviourEvent :: Gossipsub (
214
+ gossipsub:: Event :: Message {
216
215
propagation_source : peer_id,
217
216
message_id,
218
217
message,
219
- } ) ) => {
220
- return Some ( ( peer_id, message_id, message) ) ;
221
- }
222
- SwarmEvent :: NewListenAddr { address, .. } => {
223
- log:: info!( "Local node is listening on {}" , address) ;
224
- }
225
- _ => log:: trace!( "Unhandled Swarm Event: {:?}" , event) ,
226
- } ,
227
- _ = self . cancellation. cancelled( ) => {
228
- return None ;
218
+ } ,
219
+ ) ) => {
220
+ return Some ( ( peer_id, message_id, message) ) ;
221
+ }
222
+ SwarmEvent :: Behaviour ( DriaBehaviourEvent :: Autonat (
223
+ autonat:: Event :: StatusChanged { old, new } ,
224
+ ) ) => {
225
+ log:: warn!( "AutoNAT status changed from {:?} to {:?}" , old, new) ;
226
+ }
227
+ SwarmEvent :: NewListenAddr { address, .. } => {
228
+ log:: warn!( "Local node is listening on {}" , address) ;
229
229
}
230
+ SwarmEvent :: ExternalAddrConfirmed { address } => {
231
+ log:: warn!( "External address confirmed: {}" , address) ;
232
+ }
233
+ event => log:: trace!( "Unhandled Swarm Event: {:?}" , event) ,
230
234
}
231
235
}
232
236
}
233
237
234
238
/// Handles identify events to add peer addresses to Kademlia, if protocols match.
235
239
fn handle_identify_event ( & mut self , peer_id : PeerId , info : identify:: Info ) {
236
240
let protocol_match = info. protocols . iter ( ) . any ( |p| * p == DRIA_PROTO_NAME ) ;
237
- for addr in info. listen_addrs {
238
- if protocol_match {
239
- // if it matches our protocol, add it to the Kademlia routing table
240
- log:: info!( "Identify: Peer {} identified at {}" , peer_id, addr) ;
241
+ let addr = info. observed_addr ;
241
242
242
- self . swarm
243
- . behaviour_mut ( )
244
- . kademlia
245
- . add_address ( & peer_id, addr) ;
246
- } else {
247
- log:: trace!(
248
- "Identify: Incoming from different protocol, address {}. PeerID is {}" ,
249
- addr,
250
- peer_id
251
- ) ;
252
- }
243
+ if protocol_match {
244
+ // if it matches our protocol, add it to the Kademlia routing table
245
+ log:: info!(
246
+ "Identify: {} peer {} identified at {}" ,
247
+ info. protocol_version,
248
+ peer_id,
249
+ addr
250
+ ) ;
251
+
252
+ self . swarm
253
+ . behaviour_mut ( )
254
+ . kademlia
255
+ . add_address ( & peer_id, addr) ;
256
+ } else {
257
+ log:: trace!(
258
+ "Identify: Incoming from different protocol, address {}. PeerID is {}" ,
259
+ addr,
260
+ peer_id
261
+ ) ;
253
262
}
254
263
}
255
264
@@ -309,7 +318,7 @@ impl P2PClient {
309
318
. behaviour_mut ( )
310
319
. kademlia
311
320
. get_closest_peers ( random_peer) ;
312
- self . peer_last_refreshed = tokio :: time :: Instant :: now ( ) ;
321
+ self . peer_last_refreshed = Instant :: now ( ) ;
313
322
314
323
// get peer count
315
324
let gossipsub = & self . swarm . behaviour ( ) . gossipsub ;
0 commit comments