@@ -272,7 +272,7 @@ func TestSynchronisation60(t *testing.T) {
272
272
// Tests that simple synchronization against a canonical chain works correctly.
273
273
// In this test common ancestor lookup should be short circuited and not require
274
274
// binary searching.
275
- func TestCanonicalSynchronisation (t * testing.T ) {
275
+ func TestCanonicalSynchronisation61 (t * testing.T ) {
276
276
// Create a small enough block chain to download
277
277
targetBlocks := blockCacheLimit - 15
278
278
hashes , blocks := makeChain (targetBlocks , 0 , genesis )
@@ -291,69 +291,16 @@ func TestCanonicalSynchronisation(t *testing.T) {
291
291
292
292
// Tests that if a large batch of blocks are being downloaded, it is throttled
293
293
// until the cached blocks are retrieved.
294
- func TestThrottling60 (t * testing.T ) {
295
- // Create a long block chain to download and the tester
296
- targetBlocks := 8 * blockCacheLimit
297
- hashes , blocks := makeChain (targetBlocks , 0 , genesis )
298
-
299
- tester := newTester ()
300
- tester .newPeer ("peer" , eth60 , hashes , blocks )
301
-
302
- // Wrap the importer to allow stepping
303
- done := make (chan int )
304
- tester .downloader .insertChain = func (blocks types.Blocks ) (int , error ) {
305
- n , err := tester .insertChain (blocks )
306
- done <- n
307
- return n , err
308
- }
309
- // Start a synchronisation concurrently
310
- errc := make (chan error )
311
- go func () {
312
- errc <- tester .sync ("peer" )
313
- }()
314
- // Iteratively take some blocks, always checking the retrieval count
315
- for len (tester .ownBlocks ) < targetBlocks + 1 {
316
- // Wait a bit for sync to throttle itself
317
- var cached int
318
- for start := time .Now (); time .Since (start ) < 3 * time .Second ; {
319
- time .Sleep (25 * time .Millisecond )
320
-
321
- cached = len (tester .downloader .queue .blockPool )
322
- if cached == blockCacheLimit || len (tester .ownBlocks )+ cached == targetBlocks + 1 {
323
- break
324
- }
325
- }
326
- // Make sure we filled up the cache, then exhaust it
327
- time .Sleep (25 * time .Millisecond ) // give it a chance to screw up
328
- if cached != blockCacheLimit && len (tester .ownBlocks )+ cached < targetBlocks + 1 {
329
- t .Fatalf ("block count mismatch: have %v, want %v" , cached , blockCacheLimit )
330
- }
331
- <- done // finish previous blocking import
332
- for cached > maxBlockProcess {
333
- cached -= <- done
334
- }
335
- time .Sleep (25 * time .Millisecond ) // yield to the insertion
336
- }
337
- <- done // finish the last blocking import
294
+ func TestThrottling60 (t * testing.T ) { testThrottling (t , eth60 ) }
295
+ func TestThrottling61 (t * testing.T ) { testThrottling (t , eth61 ) }
338
296
339
- // Check that we haven't pulled more blocks than available
340
- if len (tester .ownBlocks ) > targetBlocks + 1 {
341
- t .Fatalf ("target block count mismatch: have %v, want %v" , len (tester .ownBlocks ), targetBlocks + 1 )
342
- }
343
- if err := <- errc ; err != nil {
344
- t .Fatalf ("block synchronization failed: %v" , err )
345
- }
346
- }
347
-
348
- // Tests that if a large batch of blocks are being downloaded, it is throttled
349
- // until the cached blocks are retrieved.
350
- func TestThrottling (t * testing.T ) {
297
+ func testThrottling (t * testing.T , protocol int ) {
351
298
// Create a long block chain to download and the tester
352
299
targetBlocks := 8 * blockCacheLimit
353
300
hashes , blocks := makeChain (targetBlocks , 0 , genesis )
354
301
355
302
tester := newTester ()
356
- tester .newPeer ("peer" , eth61 , hashes , blocks )
303
+ tester .newPeer ("peer" , protocol , hashes , blocks )
357
304
358
305
// Wrap the importer to allow stepping
359
306
done := make (chan int )
@@ -404,7 +351,7 @@ func TestThrottling(t *testing.T) {
404
351
// Tests that simple synchronization against a forked chain works correctly. In
405
352
// this test common ancestor lookup should *not* be short circuited, and a full
406
353
// binary search should be executed.
407
- func TestForkedSynchronisation (t * testing.T ) {
354
+ func TestForkedSynchronisation61 (t * testing.T ) {
408
355
// Create a long enough forked chain
409
356
common , fork := MaxHashFetch , 2 * MaxHashFetch
410
357
hashesA , hashesB , blocksA , blocksB := makeChainFork (common + fork , fork , genesis )
@@ -443,33 +390,10 @@ func TestInactiveDownloader(t *testing.T) {
443
390
}
444
391
445
392
// Tests that a canceled download wipes all previously accumulated state.
446
- func TestCancel60 (t * testing.T ) {
447
- // Create a small enough block chain to download and the tester
448
- targetBlocks := blockCacheLimit - 15
449
- hashes , blocks := makeChain (targetBlocks , 0 , genesis )
450
-
451
- tester := newTester ()
452
- tester .newPeer ("peer" , eth60 , hashes , blocks )
393
+ func TestCancel60 (t * testing.T ) { testCancel (t , eth60 ) }
394
+ func TestCancel61 (t * testing.T ) { testCancel (t , eth61 ) }
453
395
454
- // Make sure canceling works with a pristine downloader
455
- tester .downloader .cancel ()
456
- hashCount , blockCount := tester .downloader .queue .Size ()
457
- if hashCount > 0 || blockCount > 0 {
458
- t .Errorf ("block or hash count mismatch: %d hashes, %d blocks, want 0" , hashCount , blockCount )
459
- }
460
- // Synchronise with the peer, but cancel afterwards
461
- if err := tester .sync ("peer" ); err != nil {
462
- t .Fatalf ("failed to synchronise blocks: %v" , err )
463
- }
464
- tester .downloader .cancel ()
465
- hashCount , blockCount = tester .downloader .queue .Size ()
466
- if hashCount > 0 || blockCount > 0 {
467
- t .Errorf ("block or hash count mismatch: %d hashes, %d blocks, want 0" , hashCount , blockCount )
468
- }
469
- }
470
-
471
- // Tests that a canceled download wipes all previously accumulated state.
472
- func TestCancel (t * testing.T ) {
396
+ func testCancel (t * testing.T , protocol int ) {
473
397
// Create a small enough block chain to download and the tester
474
398
targetBlocks := blockCacheLimit - 15
475
399
if targetBlocks >= MaxHashFetch {
@@ -478,7 +402,7 @@ func TestCancel(t *testing.T) {
478
402
hashes , blocks := makeChain (targetBlocks , 0 , genesis )
479
403
480
404
tester := newTester ()
481
- tester .newPeer ("peer" , eth61 , hashes , blocks )
405
+ tester .newPeer ("peer" , protocol , hashes , blocks )
482
406
483
407
// Make sure canceling works with a pristine downloader
484
408
tester .downloader .cancel ()
@@ -498,7 +422,10 @@ func TestCancel(t *testing.T) {
498
422
}
499
423
500
424
// Tests that synchronisation from multiple peers works as intended (multi thread sanity test).
501
- func TestMultiSynchronisation (t * testing.T ) {
425
+ func TestMultiSynchronisation60 (t * testing.T ) { testMultiSynchronisation (t , eth60 ) }
426
+ func TestMultiSynchronisation61 (t * testing.T ) { testMultiSynchronisation (t , eth61 ) }
427
+
428
+ func testMultiSynchronisation (t * testing.T , protocol int ) {
502
429
// Create various peers with various parts of the chain
503
430
targetPeers := 16
504
431
targetBlocks := targetPeers * blockCacheLimit - 15
@@ -507,7 +434,7 @@ func TestMultiSynchronisation(t *testing.T) {
507
434
tester := newTester ()
508
435
for i := 0 ; i < targetPeers ; i ++ {
509
436
id := fmt .Sprintf ("peer #%d" , i )
510
- tester .newPeer (id , eth60 , hashes [i * blockCacheLimit :], blocks )
437
+ tester .newPeer (id , protocol , hashes [i * blockCacheLimit :], blocks )
511
438
}
512
439
// Synchronise with the middle peer and make sure half of the blocks were retrieved
513
440
id := fmt .Sprintf ("peer #%d" , targetPeers / 2 )
@@ -528,7 +455,7 @@ func TestMultiSynchronisation(t *testing.T) {
528
455
529
456
// Tests that synchronising with a peer who's very slow at network IO does not
530
457
// stall the other peers in the system.
531
- func TestSlowSynchronisation (t * testing.T ) {
458
+ func TestSlowSynchronisation60 (t * testing.T ) {
532
459
tester := newTester ()
533
460
534
461
// Create a batch of blocks, with a slow and a full speed peer
@@ -557,7 +484,7 @@ func TestSlowSynchronisation(t *testing.T) {
557
484
558
485
// Tests that if a peer returns an invalid chain with a block pointing to a non-
559
486
// existing parent, it is correctly detected and handled.
560
- func TestNonExistingParentAttack (t * testing.T ) {
487
+ func TestNonExistingParentAttack60 (t * testing.T ) {
561
488
tester := newTester ()
562
489
563
490
// Forge a single-link chain with a forged header
@@ -587,7 +514,7 @@ func TestNonExistingParentAttack(t *testing.T) {
587
514
588
515
// Tests that if a malicious peers keeps sending us repeating hashes, we don't
589
516
// loop indefinitely.
590
- func TestRepeatingHashAttack (t * testing.T ) { // TODO: Is this thing valid??
517
+ func TestRepeatingHashAttack60 (t * testing.T ) { // TODO: Is this thing valid??
591
518
tester := newTester ()
592
519
593
520
// Create a valid chain, but drop the last link
@@ -617,7 +544,7 @@ func TestRepeatingHashAttack(t *testing.T) { // TODO: Is this thing valid??
617
544
618
545
// Tests that if a malicious peers returns a non-existent block hash, it should
619
546
// eventually time out and the sync reattempted.
620
- func TestNonExistingBlockAttack (t * testing.T ) {
547
+ func TestNonExistingBlockAttack60 (t * testing.T ) {
621
548
tester := newTester ()
622
549
623
550
// Create a valid chain, but forge the last link
@@ -639,7 +566,7 @@ func TestNonExistingBlockAttack(t *testing.T) {
639
566
640
567
// Tests that if a malicious peer is returning hashes in a weird order, that the
641
568
// sync throttler doesn't choke on them waiting for the valid blocks.
642
- func TestInvalidHashOrderAttack (t * testing.T ) {
569
+ func TestInvalidHashOrderAttack60 (t * testing.T ) {
643
570
tester := newTester ()
644
571
645
572
// Create a valid long chain, but reverse some hashes within
@@ -667,7 +594,7 @@ func TestInvalidHashOrderAttack(t *testing.T) {
667
594
668
595
// Tests that if a malicious peer makes up a random hash chain and tries to push
669
596
// indefinitely, it actually gets caught with it.
670
- func TestMadeupHashChainAttack (t * testing.T ) {
597
+ func TestMadeupHashChainAttack60 (t * testing.T ) {
671
598
tester := newTester ()
672
599
blockSoftTTL = 100 * time .Millisecond
673
600
crossCheckCycle = 25 * time .Millisecond
@@ -697,7 +624,7 @@ func TestMadeupHashChainAttack(t *testing.T) {
697
624
// indefinitely, one hash at a time, it actually gets caught with it. The reason
698
625
// this is separate from the classical made up chain attack is that sending hashes
699
626
// one by one prevents reliable block/parent verification.
700
- func TestMadeupHashChainDrippingAttack (t * testing.T ) {
627
+ func TestMadeupHashChainDrippingAttack60 (t * testing.T ) {
701
628
// Create a random chain of hashes to drip
702
629
randomHashes := make ([]common.Hash , 16 * blockCacheLimit )
703
630
for i := range randomHashes {
@@ -716,7 +643,7 @@ func TestMadeupHashChainDrippingAttack(t *testing.T) {
716
643
717
644
// Tests that if a malicious peer makes up a random block chain, and tried to
718
645
// push indefinitely, it actually gets caught with it.
719
- func TestMadeupBlockChainAttack (t * testing.T ) {
646
+ func TestMadeupBlockChainAttack60 (t * testing.T ) {
720
647
defaultBlockTTL := blockSoftTTL
721
648
defaultCrossCheckCycle := crossCheckCycle
722
649
@@ -748,7 +675,7 @@ func TestMadeupBlockChainAttack(t *testing.T) {
748
675
// Tests that if one/multiple malicious peers try to feed a banned blockchain to
749
676
// the downloader, it will not keep refetching the same chain indefinitely, but
750
677
// gradually block pieces of it, until its head is also blocked.
751
- func TestBannedChainStarvationAttack (t * testing.T ) {
678
+ func TestBannedChainStarvationAttack60 (t * testing.T ) {
752
679
n := 8 * blockCacheLimit
753
680
fork := n / 2 - 23
754
681
hashes , forkHashes , blocks , forkBlocks := makeChainFork (n , fork , genesis )
@@ -792,7 +719,7 @@ func TestBannedChainStarvationAttack(t *testing.T) {
792
719
// Tests that if a peer sends excessively many/large invalid chains that are
793
720
// gradually banned, it will have an upper limit on the consumed memory and also
794
721
// the origin bad hashes will not be evacuated.
795
- func TestBannedChainMemoryExhaustionAttack (t * testing.T ) {
722
+ func TestBannedChainMemoryExhaustionAttack60 (t * testing.T ) {
796
723
// Construct a banned chain with more chunks than the ban limit
797
724
n := 8 * blockCacheLimit
798
725
fork := n / 2 - 23
@@ -848,7 +775,7 @@ func TestBannedChainMemoryExhaustionAttack(t *testing.T) {
848
775
// internal state problems
849
776
//
850
777
// No, don't delete this test, it actually did happen!
851
- func TestOverlappingDeliveryAttack (t * testing.T ) {
778
+ func TestOverlappingDeliveryAttack60 (t * testing.T ) {
852
779
// Create an arbitrary batch of blocks ( < cache-size not to block)
853
780
targetBlocks := blockCacheLimit - 23
854
781
hashes , blocks := makeChain (targetBlocks , 0 , genesis )
@@ -875,6 +802,16 @@ func TestOverlappingDeliveryAttack(t *testing.T) {
875
802
}
876
803
}
877
804
805
+ // Tests that a peer advertising an high TD doesn't get to stall the downloader
806
+ // afterwards by not sending any useful hashes.
807
+ func TestHighTDStarvationAttack61 (t * testing.T ) {
808
+ tester := newTester ()
809
+ tester .newPeer ("attack" , eth61 , []common.Hash {genesis .Hash ()}, nil )
810
+ if err := tester .sync ("attack" ); err != errStallingPeer {
811
+ t .Fatalf ("synchronisation error mismatch: have %v, want %v" , err , errStallingPeer )
812
+ }
813
+ }
814
+
878
815
// Tests that misbehaving peers are disconnected, whilst behaving ones are not.
879
816
func TestHashAttackerDropping (t * testing.T ) {
880
817
// Define the disconnection requirement for individual hash fetch errors
0 commit comments