@@ -159,6 +159,37 @@ func (f *fetcherTester) makeFetcher(blocks map[common.Hash]*types.Block) blockRe
159
159
}
160
160
}
161
161
162
+ // verifyImportEvent verifies that one single event arrive on an import channel.
163
+ func verifyImportEvent (t * testing.T , imported chan * types.Block ) {
164
+ select {
165
+ case <- imported :
166
+ case <- time .After (time .Second ):
167
+ t .Fatalf ("import timeout" )
168
+ }
169
+ }
170
+
171
+ // verifyImportCount verifies that exactly count number of events arrive on an
172
+ // import hook channel.
173
+ func verifyImportCount (t * testing.T , imported chan * types.Block , count int ) {
174
+ for i := 0 ; i < count ; i ++ {
175
+ select {
176
+ case <- imported :
177
+ case <- time .After (time .Second ):
178
+ t .Fatalf ("block %d: import timeout" , i )
179
+ }
180
+ }
181
+ verifyImportDone (t , imported )
182
+ }
183
+
184
+ // verifyImportDone verifies that no more events are arriving on an import channel.
185
+ func verifyImportDone (t * testing.T , imported chan * types.Block ) {
186
+ select {
187
+ case <- imported :
188
+ t .Fatalf ("extra block imported" )
189
+ case <- time .After (50 * time .Millisecond ):
190
+ }
191
+ }
192
+
162
193
// Tests that a fetcher accepts block announcements and initiates retrievals for
163
194
// them, successfully importing into the local chain.
164
195
func TestSequentialAnnouncements (t * testing.T ) {
@@ -176,18 +207,9 @@ func TestSequentialAnnouncements(t *testing.T) {
176
207
177
208
for i := len (hashes ) - 2 ; i >= 0 ; i -- {
178
209
tester .fetcher .Notify ("valid" , hashes [i ], time .Now ().Add (- arriveTimeout ), fetcher )
179
-
180
- select {
181
- case <- imported :
182
- case <- time .After (time .Second ):
183
- t .Fatalf ("block %d: import timeout" , len (hashes )- i )
184
- }
185
- }
186
- select {
187
- case <- imported :
188
- t .Fatalf ("extra block imported" )
189
- case <- time .After (50 * time .Millisecond ):
210
+ verifyImportEvent (t , imported )
190
211
}
212
+ verifyImportDone (t , imported )
191
213
}
192
214
193
215
// Tests that if blocks are announced by multiple peers (or even the same buggy
@@ -216,17 +238,10 @@ func TestConcurrentAnnouncements(t *testing.T) {
216
238
tester .fetcher .Notify ("second" , hashes [i ], time .Now ().Add (- arriveTimeout + time .Millisecond ), wrapper )
217
239
tester .fetcher .Notify ("second" , hashes [i ], time .Now ().Add (- arriveTimeout - time .Millisecond ), wrapper )
218
240
219
- select {
220
- case <- imported :
221
- case <- time .After (time .Second ):
222
- t .Fatalf ("block %d: import timeout" , len (hashes )- i )
223
- }
224
- }
225
- select {
226
- case <- imported :
227
- t .Fatalf ("extra block imported" )
228
- case <- time .After (50 * time .Millisecond ):
241
+ verifyImportEvent (t , imported )
229
242
}
243
+ verifyImportDone (t , imported )
244
+
230
245
// Make sure no blocks were retrieved twice
231
246
if int (counter ) != targetBlocks {
232
247
t .Fatalf ("retrieval count mismatch: have %v, want %v" , counter , targetBlocks )
@@ -259,18 +274,7 @@ func TestOverlappingAnnouncements(t *testing.T) {
259
274
}
260
275
}
261
276
// Wait for all the imports to complete and check count
262
- for i := 0 ; i < len (hashes )- 1 ; i ++ {
263
- select {
264
- case <- imported :
265
- case <- time .After (time .Second ):
266
- t .Fatalf ("block %d: import timeout" , i )
267
- }
268
- }
269
- select {
270
- case <- imported :
271
- t .Fatalf ("extra block imported" )
272
- case <- time .After (50 * time .Millisecond ):
273
- }
277
+ verifyImportCount (t , imported , len (hashes )- 1 )
274
278
}
275
279
276
280
// Tests that announces already being retrieved will not be duplicated.
@@ -334,19 +338,7 @@ func TestRandomArrivalImport(t *testing.T) {
334
338
}
335
339
// Finally announce the skipped entry and check full import
336
340
tester .fetcher .Notify ("valid" , hashes [skip ], time .Now ().Add (- arriveTimeout ), fetcher )
337
-
338
- for i := 0 ; i < len (hashes )- 1 ; i ++ {
339
- select {
340
- case <- imported :
341
- case <- time .After (time .Second ):
342
- t .Fatalf ("block %d: import timeout" , i )
343
- }
344
- }
345
- select {
346
- case <- imported :
347
- t .Fatalf ("extra block imported" )
348
- case <- time .After (50 * time .Millisecond ):
349
- }
341
+ verifyImportCount (t , imported , len (hashes )- 1 )
350
342
}
351
343
352
344
// Tests that direct block enqueues (due to block propagation vs. hash announce)
@@ -372,19 +364,7 @@ func TestQueueGapFill(t *testing.T) {
372
364
}
373
365
// Fill the missing block directly as if propagated
374
366
tester .fetcher .Enqueue ("valid" , blocks [hashes [skip ]])
375
-
376
- for i := 0 ; i < len (hashes )- 1 ; i ++ {
377
- select {
378
- case <- imported :
379
- case <- time .After (time .Second ):
380
- t .Fatalf ("block %d: import timeout" , i )
381
- }
382
- }
383
- select {
384
- case <- imported :
385
- t .Fatalf ("extra block imported" )
386
- case <- time .After (50 * time .Millisecond ):
387
- }
367
+ verifyImportCount (t , imported , len (hashes )- 1 )
388
368
}
389
369
390
370
// Tests that blocks arriving from various sources (multiple propagations, hash
@@ -419,16 +399,8 @@ func TestImportDeduplication(t *testing.T) {
419
399
420
400
// Fill the missing block directly as if propagated, and check import uniqueness
421
401
tester .fetcher .Enqueue ("valid" , blocks [hashes [1 ]])
422
- for done := false ; ! done ; {
423
- select {
424
- case <- imported :
425
- case <- time .After (50 * time .Millisecond ):
426
- done = true
427
- }
428
- }
429
- if imported := len (tester .blocks ); imported != 3 {
430
- t .Fatalf ("synchronised block mismatch: have %v, want %v" , imported , 3 )
431
- }
402
+ verifyImportCount (t , imported , 2 )
403
+
432
404
if counter != 2 {
433
405
t .Fatalf ("import invocation count mismatch: have %v, want %v" , counter , 2 )
434
406
}
@@ -491,32 +463,14 @@ func TestHashMemoryExhaustionAttack(t *testing.T) {
491
463
t .Fatalf ("queued announce count mismatch: have %d, want %d" , len (tester .fetcher .announced ), hashLimit + maxQueueDist )
492
464
}
493
465
// Wait for fetches to complete
494
- for i := 0 ; i < maxQueueDist ; i ++ {
495
- select {
496
- case <- imported :
497
- case <- time .After (time .Second ):
498
- t .Fatalf ("block %d: import timeout" , i )
499
- }
500
- }
501
- select {
502
- case <- imported :
503
- t .Fatalf ("extra block imported" )
504
- case <- time .After (50 * time .Millisecond ):
505
- }
466
+ verifyImportCount (t , imported , maxQueueDist )
467
+
506
468
// Feed the remaining valid hashes to ensure DOS protection state remains clean
507
469
for i := len (hashes ) - maxQueueDist - 2 ; i >= 0 ; i -- {
508
470
tester .fetcher .Notify ("valid" , hashes [i ], time .Now ().Add (- arriveTimeout ), valid )
509
- select {
510
- case <- imported :
511
- case <- time .After (time .Second ):
512
- t .Fatalf ("block %d: import timeout" , len (hashes )- i )
513
- }
514
- }
515
- select {
516
- case <- imported :
517
- t .Fatalf ("extra block imported" )
518
- case <- time .After (50 * time .Millisecond ):
471
+ verifyImportEvent (t , imported )
519
472
}
473
+ verifyImportDone (t , imported )
520
474
}
521
475
522
476
// Tests that blocks sent to the fetcher (either through propagation or via hash
@@ -559,28 +513,12 @@ func TestBlockMemoryExhaustionAttack(t *testing.T) {
559
513
}
560
514
// Insert the missing piece (and sanity check the import)
561
515
tester .fetcher .Enqueue ("valid" , blocks [hashes [len (hashes )- 2 ]])
562
- for i := 0 ; i < maxQueueDist ; i ++ {
563
- select {
564
- case <- imported :
565
- case <- time .After (time .Second ):
566
- t .Fatalf ("block %d: import timeout" , i )
567
- }
568
- }
569
- select {
570
- case <- imported :
571
- t .Fatalf ("extra block imported" )
572
- case <- time .After (50 * time .Millisecond ):
573
- }
516
+ verifyImportCount (t , imported , maxQueueDist )
517
+
574
518
// Insert the remaining blocks in chunks to ensure clean DOS protection
575
519
for i := maxQueueDist ; i < len (hashes )- 1 ; i ++ {
576
520
tester .fetcher .Enqueue ("valid" , blocks [hashes [len (hashes )- 2 - i ]])
577
- select {
578
- case <- imported :
579
- case <- time .After (time .Second ):
580
- t .Fatalf ("block %d: import timeout" , len (hashes )- i )
581
- }
582
- }
583
- if imported := len (tester .blocks ); imported != len (hashes ) {
584
- t .Fatalf ("synchronised block mismatch: have %v, want %v" , imported , len (hashes ))
521
+ verifyImportEvent (t , imported )
585
522
}
523
+ verifyImportDone (t , imported )
586
524
}
0 commit comments