@@ -170,7 +170,8 @@ impl BlockSync {
170
170
}
171
171
172
172
/// Request recent blocks from a randomly chosen peer.
173
- fn block_sync (
173
+ /// pub for testing
174
+ pub fn block_sync (
174
175
& mut self ,
175
176
chain : & Chain ,
176
177
highest_height_peers : & [ HighestHeightPeerInfo ] ,
@@ -301,200 +302,3 @@ enum BlockSyncDue {
301
302
/// Too far behind, drop BlockSync and do StateSync instead.
302
303
StateSync ,
303
304
}
304
-
305
- #[ cfg( test) ]
306
- mod test {
307
- use std:: sync:: Arc ;
308
-
309
- use near_async:: messaging:: IntoMultiSender ;
310
- use near_chain:: Provenance ;
311
- use near_chain:: test_utils:: wait_for_all_blocks_in_processing;
312
- use near_chain_configs:: GenesisConfig ;
313
- use near_crypto:: { KeyType , PublicKey } ;
314
- use near_network:: test_utils:: MockPeerManagerAdapter ;
315
- use near_o11y:: testonly:: TracingCapture ;
316
-
317
- use near_primitives:: network:: PeerId ;
318
- use near_primitives:: utils:: MaybeValidated ;
319
-
320
- use super :: * ;
321
- use crate :: test_utils:: TestEnv ;
322
- use near_network:: types:: PeerInfo ;
323
-
324
- use std:: collections:: HashSet ;
325
-
326
- /// Helper function for block sync tests
327
- fn collect_hashes_from_network_adapter (
328
- network_adapter : & MockPeerManagerAdapter ,
329
- ) -> HashSet < CryptoHash > {
330
- let mut network_request = network_adapter. requests . write ( ) . unwrap ( ) ;
331
- network_request
332
- . drain ( ..)
333
- . map ( |request| match request {
334
- PeerManagerMessageRequest :: NetworkRequests ( NetworkRequests :: BlockRequest {
335
- hash,
336
- ..
337
- } ) => hash,
338
- _ => panic ! ( "unexpected network request {:?}" , request) ,
339
- } )
340
- . collect ( )
341
- }
342
-
343
- fn check_hashes_from_network_adapter (
344
- network_adapter : & MockPeerManagerAdapter ,
345
- expected_hashes : Vec < CryptoHash > ,
346
- ) {
347
- let collected_hashes = collect_hashes_from_network_adapter ( network_adapter) ;
348
- assert_eq ! ( collected_hashes, expected_hashes. into_iter( ) . collect:: <HashSet <_>>( ) ) ;
349
- }
350
-
351
- fn create_highest_height_peer_infos ( num_peers : usize ) -> Vec < HighestHeightPeerInfo > {
352
- ( 0 ..num_peers)
353
- . map ( |_| HighestHeightPeerInfo {
354
- peer_info : PeerInfo {
355
- id : PeerId :: new ( PublicKey :: empty ( KeyType :: ED25519 ) ) ,
356
- addr : None ,
357
- account_id : None ,
358
- } ,
359
- genesis_id : Default :: default ( ) ,
360
- highest_block_height : 0 ,
361
- highest_block_hash : Default :: default ( ) ,
362
- tracked_shards : vec ! [ ] ,
363
- archival : false ,
364
- } )
365
- . collect ( )
366
- }
367
-
368
- #[ test]
369
- fn test_block_sync ( ) {
370
- let mut capture = TracingCapture :: enable ( ) ;
371
- let network_adapter = Arc :: new ( MockPeerManagerAdapter :: default ( ) ) ;
372
- let block_fetch_horizon = 10 ;
373
- let max_block_requests = 10 ;
374
- let mut block_sync = BlockSync :: new (
375
- Clock :: real ( ) ,
376
- network_adapter. as_multi_sender ( ) ,
377
- block_fetch_horizon,
378
- false ,
379
- true ,
380
- ) ;
381
- let mut genesis_config = GenesisConfig :: test ( Clock :: real ( ) ) ;
382
- genesis_config. epoch_length = 100 ;
383
- let mut env =
384
- TestEnv :: builder ( & genesis_config) . clients_count ( 2 ) . mock_epoch_managers ( ) . build ( ) ;
385
- let mut blocks = vec ! [ ] ;
386
- for i in 1 ..5 * max_block_requests + 1 {
387
- let block = env. clients [ 0 ] . produce_block ( i as u64 ) . unwrap ( ) . unwrap ( ) ;
388
- blocks. push ( block. clone ( ) ) ;
389
- env. process_block ( 0 , block, Provenance :: PRODUCED ) ;
390
- }
391
- let block_headers = blocks. iter ( ) . map ( |b| b. header ( ) . clone ( ) ) . collect :: < Vec < _ > > ( ) ;
392
- let peer_infos = create_highest_height_peer_infos ( 2 ) ;
393
- let mut challenges = vec ! [ ] ;
394
- env. clients [ 1 ] . chain . sync_block_headers ( block_headers, & mut challenges) . unwrap ( ) ;
395
- assert ! ( challenges. is_empty( ) ) ;
396
-
397
- // fetch three blocks at a time
398
- for i in 0 ..3 {
399
- block_sync. block_sync ( & env. clients [ 1 ] . chain , & peer_infos, max_block_requests) . unwrap ( ) ;
400
-
401
- let expected_blocks: Vec < _ > =
402
- blocks[ i * max_block_requests..( i + 1 ) * max_block_requests] . to_vec ( ) ;
403
- check_hashes_from_network_adapter (
404
- & network_adapter,
405
- expected_blocks. iter ( ) . map ( |b| * b. hash ( ) ) . collect ( ) ,
406
- ) ;
407
-
408
- for block in expected_blocks {
409
- env. process_block ( 1 , block, Provenance :: NONE ) ;
410
- }
411
- }
412
-
413
- // Now test when the node receives the block out of order
414
- // fetch the next three blocks
415
- block_sync. block_sync ( & env. clients [ 1 ] . chain , & peer_infos, max_block_requests) . unwrap ( ) ;
416
- check_hashes_from_network_adapter (
417
- & network_adapter,
418
- ( 3 * max_block_requests..4 * max_block_requests) . map ( |h| * blocks[ h] . hash ( ) ) . collect ( ) ,
419
- ) ;
420
- // assumes that we only get block[4*max_block_requests-1]
421
- let _ = env. clients [ 1 ] . process_block_test (
422
- MaybeValidated :: from ( blocks[ 4 * max_block_requests - 1 ] . clone ( ) ) ,
423
- Provenance :: NONE ,
424
- ) ;
425
-
426
- // the next block sync should not request block[4*max_block_requests-1] again
427
- block_sync. block_sync ( & env. clients [ 1 ] . chain , & peer_infos, max_block_requests) . unwrap ( ) ;
428
- check_hashes_from_network_adapter (
429
- & network_adapter,
430
- ( 3 * max_block_requests..4 * max_block_requests - 1 )
431
- . map ( |h| * blocks[ h] . hash ( ) )
432
- . collect ( ) ,
433
- ) ;
434
-
435
- // Receive all blocks. Should not request more. As an extra
436
- // complication, pause the processing of one block.
437
- env. pause_block_processing ( & mut capture, blocks[ 4 * max_block_requests - 1 ] . hash ( ) ) ;
438
- for i in 3 * max_block_requests..5 * max_block_requests {
439
- let _ = env. clients [ 1 ]
440
- . process_block_test ( MaybeValidated :: from ( blocks[ i] . clone ( ) ) , Provenance :: NONE ) ;
441
- }
442
-
443
- block_sync. block_sync ( & env. clients [ 1 ] . chain , & peer_infos, max_block_requests) . unwrap ( ) ;
444
- let requested_block_hashes = collect_hashes_from_network_adapter ( & network_adapter) ;
445
- assert ! ( requested_block_hashes. is_empty( ) , "{:?}" , requested_block_hashes) ;
446
-
447
- // Now finish paused processing and sanity check that we
448
- // still are fully synced.
449
- env. resume_block_processing ( blocks[ 4 * max_block_requests - 1 ] . hash ( ) ) ;
450
- wait_for_all_blocks_in_processing ( & mut env. clients [ 1 ] . chain ) ;
451
- let requested_block_hashes = collect_hashes_from_network_adapter ( & network_adapter) ;
452
- assert ! ( requested_block_hashes. is_empty( ) , "{:?}" , requested_block_hashes) ;
453
- }
454
-
455
- #[ test]
456
- fn test_block_sync_archival ( ) {
457
- let network_adapter = Arc :: new ( MockPeerManagerAdapter :: default ( ) ) ;
458
- let block_fetch_horizon = 10 ;
459
- let max_block_requests = 10 ;
460
- let mut block_sync = BlockSync :: new (
461
- Clock :: real ( ) ,
462
- network_adapter. as_multi_sender ( ) ,
463
- block_fetch_horizon,
464
- true ,
465
- true ,
466
- ) ;
467
- let mut genesis_config = GenesisConfig :: test ( Clock :: real ( ) ) ;
468
- genesis_config. epoch_length = 5 ;
469
- let mut env =
470
- TestEnv :: builder ( & genesis_config) . clients_count ( 2 ) . mock_epoch_managers ( ) . build ( ) ;
471
- let mut blocks = vec ! [ ] ;
472
- for i in 1 ..41 {
473
- let block = env. clients [ 0 ] . produce_block ( i) . unwrap ( ) . unwrap ( ) ;
474
- blocks. push ( block. clone ( ) ) ;
475
- env. process_block ( 0 , block, Provenance :: PRODUCED ) ;
476
- }
477
- let block_headers = blocks. iter ( ) . map ( |b| b. header ( ) . clone ( ) ) . collect :: < Vec < _ > > ( ) ;
478
- let peer_infos = create_highest_height_peer_infos ( 2 ) ;
479
- let mut challenges = vec ! [ ] ;
480
- env. clients [ 1 ] . chain . sync_block_headers ( block_headers, & mut challenges) . unwrap ( ) ;
481
- assert ! ( challenges. is_empty( ) ) ;
482
-
483
- block_sync. block_sync ( & env. clients [ 1 ] . chain , & peer_infos, max_block_requests) . unwrap ( ) ;
484
- let requested_block_hashes = collect_hashes_from_network_adapter ( & network_adapter) ;
485
- // We don't have archival peers, and thus cannot request any blocks
486
- assert_eq ! ( requested_block_hashes, HashSet :: new( ) ) ;
487
-
488
- let mut peer_infos = create_highest_height_peer_infos ( 2 ) ;
489
- for peer in peer_infos. iter_mut ( ) {
490
- peer. archival = true ;
491
- }
492
-
493
- block_sync. block_sync ( & env. clients [ 1 ] . chain , & peer_infos, max_block_requests) . unwrap ( ) ;
494
- let requested_block_hashes = collect_hashes_from_network_adapter ( & network_adapter) ;
495
- assert_eq ! (
496
- requested_block_hashes,
497
- blocks. iter( ) . take( max_block_requests) . map( |b| * b. hash( ) ) . collect:: <HashSet <_>>( )
498
- ) ;
499
- }
500
- }
0 commit comments