@@ -6,8 +6,9 @@ use crate::{
6
6
cluster:: ClusterNodeId ,
7
7
node:: RustNodeTestingConfig ,
8
8
scenarios:: {
9
- add_rust_nodes, as_connection_finalized_event, connection_finalized_event,
10
- wait_for_nodes_listening_on_localhost, ClusterRunner , Driver , connection_finalized_with_res_event,
9
+ add_rust_nodes, add_rust_nodes_with, as_connection_finalized_event,
10
+ connection_finalized_event, connection_finalized_with_res_event,
11
+ wait_for_nodes_listening_on_localhost, ClusterRunner , Driver ,
11
12
} ,
12
13
} ;
13
14
@@ -142,7 +143,7 @@ impl DontConnectToNodeWithSameId {
142
143
let ( node, _) = driver. add_rust_node (
143
144
RustNodeTestingConfig :: berkeley_default ( )
144
145
. libp2p_port ( port)
145
- . with_peer_id ( bytes)
146
+ . with_peer_id ( bytes) ,
146
147
) ;
147
148
// wait for it to be ready
148
149
assert ! (
@@ -156,7 +157,7 @@ impl DontConnectToNodeWithSameId {
156
157
let ( node_ut, _) = driver. add_rust_node (
157
158
RustNodeTestingConfig :: berkeley_default ( )
158
159
. libp2p_port ( node_ut_port)
159
- . with_peer_id ( bytes)
160
+ . with_peer_id ( bytes) ,
160
161
) ;
161
162
162
163
driver
@@ -175,7 +176,10 @@ impl DontConnectToNodeWithSameId {
175
176
. await
176
177
. unwrap ( ) ;
177
178
178
- assert ! ( connected. is_none( ) , "the node sholdn't try to connect to itself" ) ;
179
+ assert ! (
180
+ connected. is_none( ) ,
181
+ "the node sholdn't try to connect to itself"
182
+ ) ;
179
183
}
180
184
}
181
185
@@ -189,7 +193,10 @@ impl DontConnectToSelfInitialPeer {
189
193
190
194
let port = 11109 ;
191
195
let bytes: [ u8 ; 32 ] = rand:: random ( ) ;
192
- let peer_id = SecretKey :: from_bytes ( bytes. clone ( ) ) . public_key ( ) . peer_id ( ) . to_libp2p_string ( ) ;
196
+ let peer_id = SecretKey :: from_bytes ( bytes. clone ( ) )
197
+ . public_key ( )
198
+ . peer_id ( )
199
+ . to_libp2p_string ( ) ;
193
200
let self_maddr = format ! ( "/ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}" )
194
201
. parse ( )
195
202
. unwrap ( ) ;
@@ -214,7 +221,10 @@ impl DontConnectToSelfInitialPeer {
214
221
. await
215
222
. unwrap ( ) ;
216
223
217
- assert ! ( connected. is_none( ) , "the node sholdn't try to connect to itself" ) ;
224
+ assert ! (
225
+ connected. is_none( ) ,
226
+ "the node sholdn't try to connect to itself"
227
+ ) ;
218
228
}
219
229
}
220
230
@@ -229,8 +239,10 @@ impl DontConnectToInitialPeerWithSameId {
229
239
let port = 11108 ;
230
240
let node_ut_port = 11109 ;
231
241
let bytes: [ u8 ; 32 ] = rand:: random ( ) ;
232
- let peer_id = SecretKey :: from_bytes ( bytes. clone ( ) ) . public_key ( ) . peer_id ( ) . to_libp2p_string ( ) ;
233
-
242
+ let peer_id = SecretKey :: from_bytes ( bytes. clone ( ) )
243
+ . public_key ( )
244
+ . peer_id ( )
245
+ . to_libp2p_string ( ) ;
234
246
235
247
let self_maddr = format ! ( "/ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}" )
236
248
. parse ( )
@@ -240,7 +252,7 @@ impl DontConnectToInitialPeerWithSameId {
240
252
let ( node, _) = driver. add_rust_node (
241
253
RustNodeTestingConfig :: berkeley_default ( )
242
254
. libp2p_port ( port)
243
- . with_peer_id ( bytes)
255
+ . with_peer_id ( bytes) ,
244
256
) ;
245
257
// wait for it to be ready
246
258
assert ! (
@@ -266,6 +278,143 @@ impl DontConnectToInitialPeerWithSameId {
266
278
. await
267
279
. unwrap ( ) ;
268
280
269
- assert ! ( connected. is_none( ) , "the node sholdn't try to connect to itself" ) ;
281
+ assert ! (
282
+ connected. is_none( ) ,
283
+ "the node sholdn't try to connect to itself"
284
+ ) ;
285
+ }
286
+ }
287
+
288
+ /// Node should be able to connect to all initial peers.
289
+ #[ derive( documented:: Documented , Default , Clone , Copy ) ]
290
+ pub struct ConnectToInitialPeers ;
291
+
292
+ impl ConnectToInitialPeers {
293
+ pub async fn run < ' cluster > ( self , runner : ClusterRunner < ' cluster > ) {
294
+ const MAX : u8 = 32 ;
295
+
296
+ let mut driver = Driver :: new ( runner) ;
297
+
298
+ let ( peers, port_peer_ids) : ( Vec < ClusterNodeId > , Vec < _ > ) = add_rust_nodes_with (
299
+ & mut driver,
300
+ MAX ,
301
+ RustNodeTestingConfig :: berkeley_default ( ) ,
302
+ |state| {
303
+ let config = & state. p2p . config ;
304
+ let port = config. libp2p_port . unwrap ( ) ;
305
+ let peer_id = config. identity_pub_key . peer_id ( ) ;
306
+ ( port, peer_id)
307
+ } ,
308
+ ) ;
309
+
310
+ // wait for all peers to listen
311
+ let satisfied = wait_for_nodes_listening_on_localhost (
312
+ & mut driver,
313
+ Duration :: from_secs ( 3 * 60 ) ,
314
+ peers. clone ( ) ,
315
+ )
316
+ . await
317
+ . unwrap ( ) ;
318
+ assert ! ( satisfied, "all peers should be listening" ) ;
319
+
320
+ let initial_peers = port_peer_ids
321
+ . iter ( )
322
+ . map ( |( port, peer_id) | {
323
+ format ! (
324
+ "/ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}" ,
325
+ peer_id = peer_id. clone( ) . to_libp2p_string( )
326
+ )
327
+ . parse ( )
328
+ . unwrap ( )
329
+ } )
330
+ . collect :: < Vec < _ > > ( ) ;
331
+ let ( node_ut, _) = driver
332
+ . add_rust_node ( RustNodeTestingConfig :: berkeley_default ( ) . initial_peers ( initial_peers) ) ;
333
+
334
+ // matches event "the node established connection with peer"
335
+ let mut peer_ids =
336
+ BTreeSet :: from_iter ( port_peer_ids. into_iter ( ) . map ( |( _, peer_id) | peer_id) ) ;
337
+ let pred = |node_id, event : & _ , _state : & _ | {
338
+ if node_id != node_ut {
339
+ false
340
+ } else if let Some ( ( peer_id, res) ) = as_connection_finalized_event ( event) {
341
+ assert ! ( res. is_ok( ) , "connection to {peer_id} should succeed" ) ;
342
+ peer_ids. remove ( & peer_id) ;
343
+ peer_ids. is_empty ( )
344
+ } else {
345
+ false
346
+ }
347
+ } ;
348
+
349
+ let satisfied = driver
350
+ . run_until ( Duration :: from_secs ( 3 * 60 ) , pred)
351
+ . await
352
+ . unwrap ( ) ;
353
+ assert ! ( satisfied, "did not connect to peers: {:?}" , peer_ids) ;
354
+ }
355
+ }
356
+
357
+ /// Node should be able to connect to all initial peers after they become ready.
358
+ #[ derive( documented:: Documented , Default , Clone , Copy ) ]
359
+ pub struct ConnectToInitialPeersBecomeReady ;
360
+
361
+ impl ConnectToInitialPeersBecomeReady {
362
+ pub async fn run < ' cluster > ( self , runner : ClusterRunner < ' cluster > ) {
363
+ const MAX : u16 = 32 ;
364
+
365
+ let mut driver = Driver :: new ( runner) ;
366
+
367
+ let port_bytes: Vec < _ > = ( 0 ..MAX )
368
+ . into_iter ( )
369
+ . map ( |p| ( 12000 + p, rand:: random :: < [ u8 ; 32 ] > ( ) ) )
370
+ . collect ( ) ;
371
+
372
+ let initial_peers = port_bytes
373
+ . iter ( )
374
+ . map ( |( port, bytes) | {
375
+ let secret_key = SecretKey :: from_bytes ( bytes. clone ( ) ) ;
376
+ format ! (
377
+ "/ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}" ,
378
+ peer_id = secret_key. public_key( ) . peer_id( ) . to_libp2p_string( )
379
+ )
380
+ . parse ( )
381
+ . unwrap ( )
382
+ } )
383
+ . collect :: < Vec < _ > > ( ) ;
384
+ let ( node_ut, _) = driver
385
+ . add_rust_node ( RustNodeTestingConfig :: berkeley_default ( ) . initial_peers ( initial_peers) ) ;
386
+
387
+ driver. wait_for ( Duration :: from_secs ( 10 ) , |_, _, _| false ) . await . unwrap ( ) ;
388
+
389
+ let ( _peers, mut peer_ids) : ( Vec < ClusterNodeId > , BTreeSet < PeerId > ) = port_bytes
390
+ . into_iter ( )
391
+ . map ( |( port, bytes) | {
392
+ driver. add_rust_node (
393
+ RustNodeTestingConfig :: berkeley_default ( )
394
+ . libp2p_port ( port)
395
+ . with_peer_id ( bytes) ,
396
+ )
397
+ } )
398
+ . unzip ( ) ;
399
+
400
+ // matches event "the node established connection with peer"
401
+ let pred = |node_id, event : & _ , _state : & _ | {
402
+ if node_id != node_ut {
403
+ false
404
+ } else if let Some ( ( peer_id, res) ) = as_connection_finalized_event ( event) {
405
+ if res. is_ok ( ) {
406
+ peer_ids. remove ( & peer_id) ;
407
+ }
408
+ peer_ids. is_empty ( )
409
+ } else {
410
+ false
411
+ }
412
+ } ;
413
+
414
+ let satisfied = driver
415
+ . run_until ( Duration :: from_secs ( 3 * 60 ) , pred)
416
+ . await
417
+ . unwrap ( ) ;
418
+ assert ! ( satisfied, "did not connect to peers: {:?}" , peer_ids) ;
270
419
}
271
420
}
0 commit comments