@@ -8,14 +8,12 @@ use libp2p::kad::{GetClosestPeersError, GetClosestPeersOk, QueryResult};
8
8
use libp2p:: {
9
9
autonat, gossipsub, identify, kad, multiaddr:: Protocol , noise, swarm:: SwarmEvent , tcp, yamux,
10
10
} ;
11
- use libp2p:: { Multiaddr , PeerId , Swarm , SwarmBuilder } ;
11
+ use libp2p:: { Multiaddr , PeerId , StreamProtocol , Swarm , SwarmBuilder } ;
12
12
use libp2p_identity:: Keypair ;
13
- use std:: time:: Duration ;
14
- use std:: time:: Instant ;
15
- use versioning:: { P2P_KADEMLIA_PREFIX , P2P_KADEMLIA_PROTOCOL , P2P_PROTOCOL_STRING } ;
13
+ use std:: time:: { Duration , Instant } ;
16
14
17
15
/// P2P client, exposes a simple interface to handle P2P communication.
18
- pub struct DriaP2P {
16
+ pub struct DriaP2PClient {
19
17
/// `Swarm` instance, everything is accesses through this one.
20
18
swarm : Swarm < DriaBehaviour > ,
21
19
/// Peer count for All and Mesh peers.
@@ -25,6 +23,10 @@ pub struct DriaP2P {
25
23
peer_count : ( usize , usize ) ,
26
24
/// Last time the peer count was refreshed.
27
25
peer_last_refreshed : Instant ,
26
+ /// Identity protocol string to be used for the Identity behaviour.
27
+ identity_protocol : String ,
28
+ /// Kademlia protocol, must match with other peers in the network.
29
+ kademlia_protocol : StreamProtocol ,
28
30
}
29
31
30
32
/// Number of seconds before an idle connection is closed.
@@ -33,14 +35,24 @@ const IDLE_CONNECTION_TIMEOUT_SECS: u64 = 60;
33
35
/// Number of seconds between refreshing the Kademlia DHT.
34
36
const PEER_REFRESH_INTERVAL_SECS : u64 = 30 ;
35
37
36
- impl DriaP2P {
38
+ impl DriaP2PClient {
37
39
/// Creates a new P2P client with the given keypair and listen address.
40
+ ///
41
+ /// Can provide a list of bootstrap and relay nodes to connect to as well at the start.
42
+ ///
43
+ /// The `version` is used to create the protocol strings for the client, and its very important that
44
+ /// they match with the clients existing within the network.
38
45
pub fn new (
39
46
keypair : Keypair ,
40
47
listen_addr : Multiaddr ,
41
48
bootstraps : & [ Multiaddr ] ,
42
49
relays : & [ Multiaddr ] ,
50
+ version : & str ,
43
51
) -> Result < Self > {
52
+ let identity_protocol = format ! ( "{}{}" , P2P_IDENTITY_PREFIX , version) ;
53
+ let kademlia_protocol =
54
+ StreamProtocol :: try_from_owned ( format ! ( "{}{}" , P2P_KADEMLIA_PREFIX , version) ) ?;
55
+
44
56
// this is our peerId
45
57
let node_peerid = keypair. public ( ) . to_peer_id ( ) ;
46
58
log:: info!( "Compute node peer address: {}" , node_peerid) ;
@@ -54,7 +66,14 @@ impl DriaP2P {
54
66
) ?
55
67
. with_quic ( )
56
68
. with_relay_client ( noise:: Config :: new, yamux:: Config :: default) ?
57
- . with_behaviour ( |key, relay_behavior| Ok ( DriaBehaviour :: new ( key, relay_behavior) ) ) ?
69
+ . with_behaviour ( |key, relay_behavior| {
70
+ Ok ( DriaBehaviour :: new (
71
+ key,
72
+ relay_behavior,
73
+ identity_protocol. clone ( ) ,
74
+ kademlia_protocol. clone ( ) ,
75
+ ) )
76
+ } ) ?
58
77
. with_swarm_config ( |c| {
59
78
c. with_idle_connection_timeout ( Duration :: from_secs ( IDLE_CONNECTION_TIMEOUT_SECS ) )
60
79
} )
@@ -107,6 +126,8 @@ impl DriaP2P {
107
126
swarm,
108
127
peer_count : ( 0 , 0 ) ,
109
128
peer_last_refreshed : Instant :: now ( ) ,
129
+ identity_protocol,
130
+ kademlia_protocol,
110
131
} )
111
132
}
112
133
@@ -231,12 +252,12 @@ impl DriaP2P {
231
252
/// - For Kademlia, we check the kademlia protocol and then add the address to the Kademlia routing table.
232
253
fn handle_identify_event ( & mut self , peer_id : PeerId , info : identify:: Info ) {
233
254
// check identify protocol string
234
- if info. protocol_version != P2P_PROTOCOL_STRING {
255
+ if info. protocol_version != self . identity_protocol {
235
256
log:: warn!(
236
257
"Identify: Peer {} has different Identify protocol: (them {}, you {})" ,
237
258
peer_id,
238
259
info. protocol_version,
239
- P2P_PROTOCOL_STRING
260
+ self . identity_protocol
240
261
) ;
241
262
return ;
242
263
}
@@ -248,7 +269,7 @@ impl DriaP2P {
248
269
. find ( |p| p. to_string ( ) . starts_with ( P2P_KADEMLIA_PREFIX ) )
249
270
{
250
271
// if it matches our protocol, add it to the Kademlia routing table
251
- if * kad_protocol == P2P_KADEMLIA_PROTOCOL {
272
+ if * kad_protocol == self . kademlia_protocol {
252
273
// filter listen addresses
253
274
let addrs = info. listen_addrs . into_iter ( ) . filter ( |listen_addr| {
254
275
if let Some ( Protocol :: Ip4 ( ipv4_addr) ) = listen_addr. iter ( ) . next ( ) {
@@ -279,7 +300,7 @@ impl DriaP2P {
279
300
"Identify: Peer {} has different Kademlia version: (them {}, you {})" ,
280
301
peer_id,
281
302
kad_protocol,
282
- P2P_KADEMLIA_PROTOCOL
303
+ self . kademlia_protocol
283
304
) ;
284
305
}
285
306
}
0 commit comments