@@ -153,7 +153,7 @@ func (f *fetcherTester) dropPeer(peer string) {
153
153
}
154
154
155
155
// makeHeaderFetcher retrieves a block header fetcher associated with a simulated peer.
156
- func (f * fetcherTester ) makeHeaderFetcher (blocks map [common.Hash ]* types.Block , drift time.Duration ) headerRequesterFn {
156
+ func (f * fetcherTester ) makeHeaderFetcher (peer string , blocks map [common.Hash ]* types.Block , drift time.Duration ) headerRequesterFn {
157
157
closure := make (map [common.Hash ]* types.Block )
158
158
for hash , block := range blocks {
159
159
closure [hash ] = block
@@ -166,14 +166,14 @@ func (f *fetcherTester) makeHeaderFetcher(blocks map[common.Hash]*types.Block, d
166
166
headers = append (headers , block .Header ())
167
167
}
168
168
// Return on a new thread
169
- go f .fetcher .FilterHeaders (headers , time .Now ().Add (drift ))
169
+ go f .fetcher .FilterHeaders (peer , headers , time .Now ().Add (drift ))
170
170
171
171
return nil
172
172
}
173
173
}
174
174
175
175
// makeBodyFetcher retrieves a block body fetcher associated with a simulated peer.
176
- func (f * fetcherTester ) makeBodyFetcher (blocks map [common.Hash ]* types.Block , drift time.Duration ) bodyRequesterFn {
176
+ func (f * fetcherTester ) makeBodyFetcher (peer string , blocks map [common.Hash ]* types.Block , drift time.Duration ) bodyRequesterFn {
177
177
closure := make (map [common.Hash ]* types.Block )
178
178
for hash , block := range blocks {
179
179
closure [hash ] = block
@@ -191,7 +191,7 @@ func (f *fetcherTester) makeBodyFetcher(blocks map[common.Hash]*types.Block, dri
191
191
}
192
192
}
193
193
// Return on a new thread
194
- go f .fetcher .FilterBodies (transactions , uncles , time .Now ().Add (drift ))
194
+ go f .fetcher .FilterBodies (peer , transactions , uncles , time .Now ().Add (drift ))
195
195
196
196
return nil
197
197
}
@@ -282,8 +282,8 @@ func testSequentialAnnouncements(t *testing.T, protocol int) {
282
282
hashes , blocks := makeChain (targetBlocks , 0 , genesis )
283
283
284
284
tester := newTester ()
285
- headerFetcher := tester .makeHeaderFetcher (blocks , - gatherSlack )
286
- bodyFetcher := tester .makeBodyFetcher (blocks , 0 )
285
+ headerFetcher := tester .makeHeaderFetcher ("valid" , blocks , - gatherSlack )
286
+ bodyFetcher := tester .makeBodyFetcher ("valid" , blocks , 0 )
287
287
288
288
// Iteratively announce blocks until all are imported
289
289
imported := make (chan * types.Block )
@@ -309,22 +309,28 @@ func testConcurrentAnnouncements(t *testing.T, protocol int) {
309
309
310
310
// Assemble a tester with a built in counter for the requests
311
311
tester := newTester ()
312
- headerFetcher := tester .makeHeaderFetcher (blocks , - gatherSlack )
313
- bodyFetcher := tester .makeBodyFetcher (blocks , 0 )
312
+ firstHeaderFetcher := tester .makeHeaderFetcher ("first" , blocks , - gatherSlack )
313
+ firstBodyFetcher := tester .makeBodyFetcher ("first" , blocks , 0 )
314
+ secondHeaderFetcher := tester .makeHeaderFetcher ("second" , blocks , - gatherSlack )
315
+ secondBodyFetcher := tester .makeBodyFetcher ("second" , blocks , 0 )
314
316
315
317
counter := uint32 (0 )
316
- headerWrapper := func (hash common.Hash ) error {
318
+ firstHeaderWrapper := func (hash common.Hash ) error {
319
+ atomic .AddUint32 (& counter , 1 )
320
+ return firstHeaderFetcher (hash )
321
+ }
322
+ secondHeaderWrapper := func (hash common.Hash ) error {
317
323
atomic .AddUint32 (& counter , 1 )
318
- return headerFetcher (hash )
324
+ return secondHeaderFetcher (hash )
319
325
}
320
326
// Iteratively announce blocks until all are imported
321
327
imported := make (chan * types.Block )
322
328
tester .fetcher .importedHook = func (block * types.Block ) { imported <- block }
323
329
324
330
for i := len (hashes ) - 2 ; i >= 0 ; i -- {
325
- tester .fetcher .Notify ("first" , hashes [i ], uint64 (len (hashes )- i - 1 ), time .Now ().Add (- arriveTimeout ), headerWrapper , bodyFetcher )
326
- tester .fetcher .Notify ("second" , hashes [i ], uint64 (len (hashes )- i - 1 ), time .Now ().Add (- arriveTimeout + time .Millisecond ), headerWrapper , bodyFetcher )
327
- tester .fetcher .Notify ("second" , hashes [i ], uint64 (len (hashes )- i - 1 ), time .Now ().Add (- arriveTimeout - time .Millisecond ), headerWrapper , bodyFetcher )
331
+ tester .fetcher .Notify ("first" , hashes [i ], uint64 (len (hashes )- i - 1 ), time .Now ().Add (- arriveTimeout ), firstHeaderWrapper , firstBodyFetcher )
332
+ tester .fetcher .Notify ("second" , hashes [i ], uint64 (len (hashes )- i - 1 ), time .Now ().Add (- arriveTimeout + time .Millisecond ), secondHeaderWrapper , secondBodyFetcher )
333
+ tester .fetcher .Notify ("second" , hashes [i ], uint64 (len (hashes )- i - 1 ), time .Now ().Add (- arriveTimeout - time .Millisecond ), secondHeaderWrapper , secondBodyFetcher )
328
334
verifyImportEvent (t , imported , true )
329
335
}
330
336
verifyImportDone (t , imported )
@@ -347,8 +353,8 @@ func testOverlappingAnnouncements(t *testing.T, protocol int) {
347
353
hashes , blocks := makeChain (targetBlocks , 0 , genesis )
348
354
349
355
tester := newTester ()
350
- headerFetcher := tester .makeHeaderFetcher (blocks , - gatherSlack )
351
- bodyFetcher := tester .makeBodyFetcher (blocks , 0 )
356
+ headerFetcher := tester .makeHeaderFetcher ("valid" , blocks , - gatherSlack )
357
+ bodyFetcher := tester .makeBodyFetcher ("valid" , blocks , 0 )
352
358
353
359
// Iteratively announce blocks, but overlap them continuously
354
360
overlap := 16
@@ -381,8 +387,8 @@ func testPendingDeduplication(t *testing.T, protocol int) {
381
387
382
388
// Assemble a tester with a built in counter and delayed fetcher
383
389
tester := newTester ()
384
- headerFetcher := tester .makeHeaderFetcher (blocks , - gatherSlack )
385
- bodyFetcher := tester .makeBodyFetcher (blocks , 0 )
390
+ headerFetcher := tester .makeHeaderFetcher ("repeater" , blocks , - gatherSlack )
391
+ bodyFetcher := tester .makeBodyFetcher ("repeater" , blocks , 0 )
386
392
387
393
delay := 50 * time .Millisecond
388
394
counter := uint32 (0 )
@@ -425,8 +431,8 @@ func testRandomArrivalImport(t *testing.T, protocol int) {
425
431
skip := targetBlocks / 2
426
432
427
433
tester := newTester ()
428
- headerFetcher := tester .makeHeaderFetcher (blocks , - gatherSlack )
429
- bodyFetcher := tester .makeBodyFetcher (blocks , 0 )
434
+ headerFetcher := tester .makeHeaderFetcher ("valid" , blocks , - gatherSlack )
435
+ bodyFetcher := tester .makeBodyFetcher ("valid" , blocks , 0 )
430
436
431
437
// Iteratively announce blocks, skipping one entry
432
438
imported := make (chan * types.Block , len (hashes )- 1 )
@@ -456,8 +462,8 @@ func testQueueGapFill(t *testing.T, protocol int) {
456
462
skip := targetBlocks / 2
457
463
458
464
tester := newTester ()
459
- headerFetcher := tester .makeHeaderFetcher (blocks , - gatherSlack )
460
- bodyFetcher := tester .makeBodyFetcher (blocks , 0 )
465
+ headerFetcher := tester .makeHeaderFetcher ("valid" , blocks , - gatherSlack )
466
+ bodyFetcher := tester .makeBodyFetcher ("valid" , blocks , 0 )
461
467
462
468
// Iteratively announce blocks, skipping one entry
463
469
imported := make (chan * types.Block , len (hashes )- 1 )
@@ -486,8 +492,8 @@ func testImportDeduplication(t *testing.T, protocol int) {
486
492
487
493
// Create the tester and wrap the importer with a counter
488
494
tester := newTester ()
489
- headerFetcher := tester .makeHeaderFetcher (blocks , - gatherSlack )
490
- bodyFetcher := tester .makeBodyFetcher (blocks , 0 )
495
+ headerFetcher := tester .makeHeaderFetcher ("valid" , blocks , - gatherSlack )
496
+ bodyFetcher := tester .makeBodyFetcher ("valid" , blocks , 0 )
491
497
492
498
counter := uint32 (0 )
493
499
tester .fetcher .insertChain = func (blocks types.Blocks ) (int , error ) {
@@ -570,8 +576,8 @@ func testDistantAnnouncementDiscarding(t *testing.T, protocol int) {
570
576
tester .blocks = map [common.Hash ]* types.Block {head : blocks [head ]}
571
577
tester .lock .Unlock ()
572
578
573
- headerFetcher := tester .makeHeaderFetcher (blocks , - gatherSlack )
574
- bodyFetcher := tester .makeBodyFetcher (blocks , 0 )
579
+ headerFetcher := tester .makeHeaderFetcher ("lower" , blocks , - gatherSlack )
580
+ bodyFetcher := tester .makeBodyFetcher ("lower" , blocks , 0 )
575
581
576
582
fetching := make (chan struct {}, 2 )
577
583
tester .fetcher .fetchingHook = func (hashes []common.Hash ) { fetching <- struct {}{} }
@@ -603,14 +609,14 @@ func testInvalidNumberAnnouncement(t *testing.T, protocol int) {
603
609
hashes , blocks := makeChain (1 , 0 , genesis )
604
610
605
611
tester := newTester ()
606
- headerFetcher := tester .makeHeaderFetcher (blocks , - gatherSlack )
607
- bodyFetcher := tester .makeBodyFetcher (blocks , 0 )
612
+ badHeaderFetcher := tester .makeHeaderFetcher ("bad" , blocks , - gatherSlack )
613
+ badBodyFetcher := tester .makeBodyFetcher ("bad" , blocks , 0 )
608
614
609
615
imported := make (chan * types.Block )
610
616
tester .fetcher .importedHook = func (block * types.Block ) { imported <- block }
611
617
612
618
// Announce a block with a bad number, check for immediate drop
613
- tester .fetcher .Notify ("bad" , hashes [0 ], 2 , time .Now ().Add (- arriveTimeout ), headerFetcher , bodyFetcher )
619
+ tester .fetcher .Notify ("bad" , hashes [0 ], 2 , time .Now ().Add (- arriveTimeout ), badHeaderFetcher , badBodyFetcher )
614
620
verifyImportEvent (t , imported , false )
615
621
616
622
tester .lock .RLock ()
@@ -620,8 +626,11 @@ func testInvalidNumberAnnouncement(t *testing.T, protocol int) {
620
626
if ! dropped {
621
627
t .Fatalf ("peer with invalid numbered announcement not dropped" )
622
628
}
629
+
630
+ goodHeaderFetcher := tester .makeHeaderFetcher ("good" , blocks , - gatherSlack )
631
+ goodBodyFetcher := tester .makeBodyFetcher ("good" , blocks , 0 )
623
632
// Make sure a good announcement passes without a drop
624
- tester .fetcher .Notify ("good" , hashes [0 ], 1 , time .Now ().Add (- arriveTimeout ), headerFetcher , bodyFetcher )
633
+ tester .fetcher .Notify ("good" , hashes [0 ], 1 , time .Now ().Add (- arriveTimeout ), goodHeaderFetcher , goodBodyFetcher )
625
634
verifyImportEvent (t , imported , true )
626
635
627
636
tester .lock .RLock ()
@@ -645,8 +654,8 @@ func testEmptyBlockShortCircuit(t *testing.T, protocol int) {
645
654
hashes , blocks := makeChain (32 , 0 , genesis )
646
655
647
656
tester := newTester ()
648
- headerFetcher := tester .makeHeaderFetcher (blocks , - gatherSlack )
649
- bodyFetcher := tester .makeBodyFetcher (blocks , 0 )
657
+ headerFetcher := tester .makeHeaderFetcher ("valid" , blocks , - gatherSlack )
658
+ bodyFetcher := tester .makeBodyFetcher ("valid" , blocks , 0 )
650
659
651
660
// Add a monitoring hook for all internal events
652
661
fetching := make (chan []common.Hash )
@@ -697,12 +706,12 @@ func testHashMemoryExhaustionAttack(t *testing.T, protocol int) {
697
706
// Create a valid chain and an infinite junk chain
698
707
targetBlocks := hashLimit + 2 * maxQueueDist
699
708
hashes , blocks := makeChain (targetBlocks , 0 , genesis )
700
- validHeaderFetcher := tester .makeHeaderFetcher (blocks , - gatherSlack )
701
- validBodyFetcher := tester .makeBodyFetcher (blocks , 0 )
709
+ validHeaderFetcher := tester .makeHeaderFetcher ("valid" , blocks , - gatherSlack )
710
+ validBodyFetcher := tester .makeBodyFetcher ("valid" , blocks , 0 )
702
711
703
712
attack , _ := makeChain (targetBlocks , 0 , unknownBlock )
704
- attackerHeaderFetcher := tester .makeHeaderFetcher (nil , - gatherSlack )
705
- attackerBodyFetcher := tester .makeBodyFetcher (nil , 0 )
713
+ attackerHeaderFetcher := tester .makeHeaderFetcher ("attacker" , nil , - gatherSlack )
714
+ attackerBodyFetcher := tester .makeBodyFetcher ("attacker" , nil , 0 )
706
715
707
716
// Feed the tester a huge hashset from the attacker, and a limited from the valid peer
708
717
for i := 0 ; i < len (attack ); i ++ {
0 commit comments