1
1
use std:: collections:: HashMap ;
2
2
use std:: hash:: { DefaultHasher , Hash , Hasher } ;
3
3
use std:: io:: { Error , ErrorKind } ;
4
- use std:: str:: FromStr ;
5
4
use std:: time:: Duration ;
6
5
7
6
use libp2p:: {
@@ -15,7 +14,7 @@ use libp2p::{
15
14
tcp, yamux, Multiaddr , PeerId , StreamProtocol , Swarm , SwarmBuilder ,
16
15
} ;
17
16
use log:: { debug, error, info, warn} ;
18
- use primitives:: p2p:: PeerInfo ;
17
+ use primitives:: p2p:: { PeerInfo , WPeerId } ;
19
18
20
19
use crate :: service:: p2p:: { P2PError , DEFAULT_REGISTRATION_TTL } ;
21
20
@@ -26,7 +25,7 @@ pub struct BootstrapBehaviour {
26
25
pub rendezvous : rendezvous:: server:: Behaviour ,
27
26
pub identify : identify:: Behaviour ,
28
27
pub gossipsub : gossipsub:: Behaviour ,
29
- pub request_response : request_response:: cbor:: Behaviour < String , PeerInfo > ,
28
+ pub request_response : request_response:: cbor:: Behaviour < WPeerId , PeerInfo > ,
30
29
}
31
30
32
31
pub struct BootstrapConfig {
@@ -109,8 +108,10 @@ pub(crate) async fn bootstrap(
109
108
info ! ( "Starting P2P bootstrap node at {addr}" ) ;
110
109
swarm. listen_on ( addr) ?;
111
110
for addr in bootstrap_addresses {
112
- info ! ( "Dialing {addr}" ) ;
113
- swarm. dial ( addr) ?;
111
+ info ! ( "Attempting to dial peer at {addr}" ) ;
112
+ if swarm. dial ( addr. clone ( ) ) . is_err ( ) {
113
+ warn ! ( "Failed to dial peer" ) ;
114
+ }
114
115
}
115
116
swarm
116
117
. behaviour_mut ( )
@@ -146,11 +147,18 @@ fn on_rendezvous_event(
146
147
) {
147
148
match event {
148
149
rendezvous:: server:: Event :: RegistrationExpired ( registration) => {
150
+ let id = registration. record . peer_id ( ) ;
149
151
info ! (
150
152
"Registration for peer {} expired in namespace {}" ,
151
- registration. record. peer_id( ) ,
152
- registration. namespace
153
+ id, registration. namespace
153
154
) ;
155
+ // Registration expired, remove entry from hashmap
156
+ if registrations. remove ( & id) . is_none ( ) {
157
+ error ! (
158
+ "Could not remove registration for {:?} because it was not found" ,
159
+ id
160
+ ) ;
161
+ }
154
162
}
155
163
rendezvous:: server:: Event :: PeerRegistered { peer, registration } => {
156
164
info ! (
@@ -161,21 +169,32 @@ fn on_rendezvous_event(
161
169
peer_id : peer,
162
170
multiaddrs : registration. record . addresses ( ) . to_vec ( ) ,
163
171
} ;
172
+ // Serialize PeerInfo
164
173
let encoded_peer_info = match bincode:: serialize ( & peer_info) {
165
174
Ok ( info) => info,
166
175
Err ( ..) => {
167
176
error ! ( "Failed to serialize peer_info" ) ;
168
177
return ;
169
178
}
170
179
} ;
171
- registrations. insert ( peer, peer_info) ;
180
+ registrations
181
+ . entry ( peer)
182
+ . and_modify ( |info| {
183
+ for addr in peer_info. multiaddrs . iter ( ) {
184
+ if !info. multiaddrs . contains ( addr) {
185
+ info. multiaddrs . push ( addr. clone ( ) )
186
+ }
187
+ }
188
+ } )
189
+ . or_insert ( peer_info) ;
190
+ // Send registration information to other bootstrap nodes.
172
191
match swarm
173
192
. behaviour_mut ( )
174
193
. gossipsub
175
194
. publish ( IdentTopic :: new ( GOSSIP_TOPIC ) , encoded_peer_info)
176
195
{
177
- Ok ( ..) => info ! ( "Successfully published new peer info" ) ,
178
- Err ( ..) => error ! ( "Failed to publish new peer info" ) ,
196
+ Ok ( ..) => info ! ( "Successfully published new peer info for peer {peer} " ) ,
197
+ Err ( ..) => error ! ( "Failed to publish new peer info for peer {peer} " ) ,
179
198
}
180
199
}
181
200
other => debug ! ( "Encountered other rendezvous event: {other:?}" ) ,
@@ -189,6 +208,7 @@ fn on_gossipsub_event(
189
208
registrations : & mut HashMap < PeerId , PeerInfo > ,
190
209
) {
191
210
match event {
211
+ // Received a message with peer information from another bootstrap node.
192
212
gossipsub:: Event :: Message {
193
213
propagation_source : peer_id,
194
214
message_id : id,
@@ -208,7 +228,16 @@ fn on_gossipsub_event(
208
228
"Got registration: {:?} with id: {} from peer: {:?}" ,
209
229
peer_info, id, peer_id
210
230
) ;
211
- registrations. insert ( peer_info. peer_id , peer_info) ;
231
+ registrations
232
+ . entry ( peer_info. peer_id )
233
+ . and_modify ( |info| {
234
+ for addr in peer_info. multiaddrs . iter ( ) {
235
+ if !info. multiaddrs . contains ( addr) {
236
+ info. multiaddrs . push ( addr. clone ( ) )
237
+ }
238
+ }
239
+ } )
240
+ . or_insert ( peer_info) ;
212
241
}
213
242
other => debug ! ( "Encountered other gossipsub event: {other:?}" ) ,
214
243
}
@@ -217,10 +246,11 @@ fn on_gossipsub_event(
217
246
/// Handles events within the request_response protocol
218
247
fn on_request_response_event (
219
248
swarm : & mut Swarm < BootstrapBehaviour > ,
220
- event : request_response:: Event < String , PeerInfo > ,
249
+ event : request_response:: Event < WPeerId , PeerInfo > ,
221
250
registrations : & HashMap < PeerId , PeerInfo > ,
222
251
) {
223
252
match event {
253
+ // Message received, looking up the mapping
224
254
request_response:: Event :: Message { peer, message } => {
225
255
if let Message :: Request {
226
256
request,
@@ -229,22 +259,17 @@ fn on_request_response_event(
229
259
} = message
230
260
{
231
261
info ! ( "Got request with id {request_id} from {peer}" ) ;
232
- let id = match PeerId :: from_str ( & request) {
233
- Ok ( id) => id,
234
- Err ( ..) => {
235
- error ! ( "Received invalid peer ID" ) ;
236
- return ;
237
- }
238
- } ;
239
- info ! ( "Looking up {id:?}" ) ;
262
+ let id: PeerId = request. into ( ) ;
240
263
match registrations. get ( & id) {
241
264
Some ( peer_info) => {
265
+ // Sending the peer information back to the client who opened the channel.
242
266
if swarm
243
267
. behaviour_mut ( )
244
268
. request_response
245
269
. send_response ( channel, peer_info. clone ( ) )
246
270
. is_err ( )
247
271
{
272
+ // Could add retries here.
248
273
error ! ( "Failed to send peer info to {peer:?}" ) ;
249
274
}
250
275
}
@@ -263,7 +288,7 @@ fn on_request_response_event(
263
288
error,
264
289
} => warn ! ( "Failed to receive message with id {request_id} from {peer}: {error}" ) ,
265
290
request_response:: Event :: ResponseSent { peer, request_id } => {
266
- info ! ( "Request with id {request_id} sent to {peer}" )
291
+ debug ! ( "Request with id {request_id} sent to {peer}" )
267
292
}
268
293
}
269
294
}
0 commit comments