@@ -97,8 +97,18 @@ func newTester() *downloadTester {
97
97
}
98
98
99
99
// sync starts synchronizing with a remote peer, blocking until it completes.
100
- func (dl * downloadTester ) sync (id string ) error {
101
- err := dl .downloader .synchronise (id , dl .peerHashes [id ][0 ])
100
+ func (dl * downloadTester ) sync (id string , td * big.Int ) error {
101
+ hash := dl .peerHashes [id ][0 ]
102
+
103
+ // If no particular TD was requested, load from the peer's blockchain
104
+ if td == nil {
105
+ td = big .NewInt (1 )
106
+ if block , ok := dl.peerBlocks [id ][hash ]; ok {
107
+ td = block .Td
108
+ }
109
+ }
110
+ err := dl .downloader .synchronise (id , hash , td )
111
+
102
112
for {
103
113
// If the queue is empty and processing stopped, break
104
114
hashes , blocks := dl .downloader .queue .Size ()
@@ -261,7 +271,7 @@ func TestSynchronisation60(t *testing.T) {
261
271
tester .newPeer ("peer" , eth60 , hashes , blocks )
262
272
263
273
// Synchronise with the peer and make sure all blocks were retrieved
264
- if err := tester .sync ("peer" ); err != nil {
274
+ if err := tester .sync ("peer" , nil ); err != nil {
265
275
t .Fatalf ("failed to synchronise blocks: %v" , err )
266
276
}
267
277
if imported := len (tester .ownBlocks ); imported != targetBlocks + 1 {
@@ -281,7 +291,7 @@ func TestCanonicalSynchronisation61(t *testing.T) {
281
291
tester .newPeer ("peer" , eth61 , hashes , blocks )
282
292
283
293
// Synchronise with the peer and make sure all blocks were retrieved
284
- if err := tester .sync ("peer" ); err != nil {
294
+ if err := tester .sync ("peer" , nil ); err != nil {
285
295
t .Fatalf ("failed to synchronise blocks: %v" , err )
286
296
}
287
297
if imported := len (tester .ownBlocks ); imported != targetBlocks + 1 {
@@ -312,7 +322,7 @@ func testThrottling(t *testing.T, protocol int) {
312
322
// Start a synchronisation concurrently
313
323
errc := make (chan error )
314
324
go func () {
315
- errc <- tester .sync ("peer" )
325
+ errc <- tester .sync ("peer" , nil )
316
326
}()
317
327
// Iteratively take some blocks, always checking the retrieval count
318
328
for len (tester .ownBlocks ) < targetBlocks + 1 {
@@ -361,14 +371,14 @@ func TestForkedSynchronisation61(t *testing.T) {
361
371
tester .newPeer ("fork B" , eth61 , hashesB , blocksB )
362
372
363
373
// Synchronise with the peer and make sure all blocks were retrieved
364
- if err := tester .sync ("fork A" ); err != nil {
374
+ if err := tester .sync ("fork A" , nil ); err != nil {
365
375
t .Fatalf ("failed to synchronise blocks: %v" , err )
366
376
}
367
377
if imported := len (tester .ownBlocks ); imported != common + fork + 1 {
368
378
t .Fatalf ("synchronised block mismatch: have %v, want %v" , imported , common + fork + 1 )
369
379
}
370
380
// Synchronise with the second peer and make sure that fork is pulled too
371
- if err := tester .sync ("fork B" ); err != nil {
381
+ if err := tester .sync ("fork B" , nil ); err != nil {
372
382
t .Fatalf ("failed to synchronise blocks: %v" , err )
373
383
}
374
384
if imported := len (tester .ownBlocks ); imported != common + 2 * fork + 1 {
@@ -411,7 +421,7 @@ func testCancel(t *testing.T, protocol int) {
411
421
t .Errorf ("block or hash count mismatch: %d hashes, %d blocks, want 0" , hashCount , blockCount )
412
422
}
413
423
// Synchronise with the peer, but cancel afterwards
414
- if err := tester .sync ("peer" ); err != nil {
424
+ if err := tester .sync ("peer" , nil ); err != nil {
415
425
t .Fatalf ("failed to synchronise blocks: %v" , err )
416
426
}
417
427
tester .downloader .cancel ()
@@ -438,14 +448,14 @@ func testMultiSynchronisation(t *testing.T, protocol int) {
438
448
}
439
449
// Synchronise with the middle peer and make sure half of the blocks were retrieved
440
450
id := fmt .Sprintf ("peer #%d" , targetPeers / 2 )
441
- if err := tester .sync (id ); err != nil {
451
+ if err := tester .sync (id , nil ); err != nil {
442
452
t .Fatalf ("failed to synchronise blocks: %v" , err )
443
453
}
444
454
if imported := len (tester .ownBlocks ); imported != len (tester .peerHashes [id ]) {
445
455
t .Fatalf ("synchronised block mismatch: have %v, want %v" , imported , len (tester .peerHashes [id ]))
446
456
}
447
457
// Synchronise with the best peer and make sure everything is retrieved
448
- if err := tester .sync ("peer #0" ); err != nil {
458
+ if err := tester .sync ("peer #0" , nil ); err != nil {
449
459
t .Fatalf ("failed to synchronise blocks: %v" , err )
450
460
}
451
461
if imported := len (tester .ownBlocks ); imported != targetBlocks + 1 {
@@ -469,7 +479,7 @@ func TestSlowSynchronisation60(t *testing.T) {
469
479
470
480
// Try to sync with the peers (pull hashes from fast)
471
481
start := time .Now ()
472
- if err := tester .sync ("fast" ); err != nil {
482
+ if err := tester .sync ("fast" , nil ); err != nil {
473
483
t .Fatalf ("failed to synchronise blocks: %v" , err )
474
484
}
475
485
if imported := len (tester .ownBlocks ); imported != targetBlocks + 1 {
@@ -497,14 +507,14 @@ func TestNonExistingParentAttack60(t *testing.T) {
497
507
tester .newPeer ("attack" , eth60 , hashes , blocks )
498
508
499
509
// Try and sync with the malicious node and check that it fails
500
- if err := tester .sync ("attack" ); err == nil {
510
+ if err := tester .sync ("attack" , nil ); err == nil {
501
511
t .Fatalf ("block synchronization succeeded" )
502
512
}
503
513
if tester .hasBlock (hashes [0 ]) {
504
514
t .Fatalf ("tester accepted unknown-parent block: %v" , blocks [hashes [0 ]])
505
515
}
506
516
// Try to synchronize with the valid chain and make sure it succeeds
507
- if err := tester .sync ("valid" ); err != nil {
517
+ if err := tester .sync ("valid" , nil ); err != nil {
508
518
t .Fatalf ("failed to synchronise blocks: %v" , err )
509
519
}
510
520
if ! tester .hasBlock (tester .peerHashes ["valid" ][0 ]) {
@@ -525,7 +535,7 @@ func TestRepeatingHashAttack60(t *testing.T) { // TODO: Is this thing valid??
525
535
// Try and sync with the malicious node
526
536
errc := make (chan error )
527
537
go func () {
528
- errc <- tester .sync ("attack" )
538
+ errc <- tester .sync ("attack" , nil )
529
539
}()
530
540
// Make sure that syncing returns and does so with a failure
531
541
select {
@@ -537,7 +547,7 @@ func TestRepeatingHashAttack60(t *testing.T) { // TODO: Is this thing valid??
537
547
}
538
548
}
539
549
// Ensure that a valid chain can still pass sync
540
- if err := tester .sync ("valid" ); err != nil {
550
+ if err := tester .sync ("valid" , nil ); err != nil {
541
551
t .Fatalf ("failed to synchronise blocks: %v" , err )
542
552
}
543
553
}
@@ -555,11 +565,11 @@ func TestNonExistingBlockAttack60(t *testing.T) {
555
565
tester .newPeer ("attack" , eth60 , hashes , blocks )
556
566
557
567
// Try and sync with the malicious node and check that it fails
558
- if err := tester .sync ("attack" ); err != errPeersUnavailable {
568
+ if err := tester .sync ("attack" , nil ); err != errPeersUnavailable {
559
569
t .Fatalf ("synchronisation error mismatch: have %v, want %v" , err , errPeersUnavailable )
560
570
}
561
571
// Ensure that a valid chain can still pass sync
562
- if err := tester .sync ("valid" ); err != nil {
572
+ if err := tester .sync ("valid" , nil ); err != nil {
563
573
t .Fatalf ("failed to synchronise blocks: %v" , err )
564
574
}
565
575
}
@@ -583,11 +593,11 @@ func TestInvalidHashOrderAttack60(t *testing.T) {
583
593
tester .newPeer ("attack" , eth60 , hashes , blocks )
584
594
585
595
// Try and sync with the malicious node and check that it fails
586
- if err := tester .sync ("attack" ); err != errInvalidChain {
596
+ if err := tester .sync ("attack" , nil ); err != errInvalidChain {
587
597
t .Fatalf ("synchronisation error mismatch: have %v, want %v" , err , errInvalidChain )
588
598
}
589
599
// Ensure that a valid chain can still pass sync
590
- if err := tester .sync ("valid" ); err != nil {
600
+ if err := tester .sync ("valid" , nil ); err != nil {
591
601
t .Fatalf ("failed to synchronise blocks: %v" , err )
592
602
}
593
603
}
@@ -611,11 +621,11 @@ func TestMadeupHashChainAttack60(t *testing.T) {
611
621
tester .newPeer ("attack" , eth60 , randomHashes , nil )
612
622
613
623
// Try and sync with the malicious node and check that it fails
614
- if err := tester .sync ("attack" ); err != errCrossCheckFailed {
624
+ if err := tester .sync ("attack" , nil ); err != errCrossCheckFailed {
615
625
t .Fatalf ("synchronisation error mismatch: have %v, want %v" , err , errCrossCheckFailed )
616
626
}
617
627
// Ensure that a valid chain can still pass sync
618
- if err := tester .sync ("valid" ); err != nil {
628
+ if err := tester .sync ("valid" , nil ); err != nil {
619
629
t .Fatalf ("failed to synchronise blocks: %v" , err )
620
630
}
621
631
}
@@ -636,7 +646,7 @@ func TestMadeupHashChainDrippingAttack60(t *testing.T) {
636
646
// Try and sync with the attacker, one hash at a time
637
647
tester .maxHashFetch = 1
638
648
tester .newPeer ("attack" , eth60 , randomHashes , nil )
639
- if err := tester .sync ("attack" ); err != errStallingPeer {
649
+ if err := tester .sync ("attack" , nil ); err != errStallingPeer {
640
650
t .Fatalf ("synchronisation error mismatch: have %v, want %v" , err , errStallingPeer )
641
651
}
642
652
}
@@ -659,15 +669,15 @@ func TestMadeupBlockChainAttack60(t *testing.T) {
659
669
// Try and sync with the malicious node and check that it fails
660
670
tester := newTester ()
661
671
tester .newPeer ("attack" , eth60 , gapped , blocks )
662
- if err := tester .sync ("attack" ); err != errCrossCheckFailed {
672
+ if err := tester .sync ("attack" , nil ); err != errCrossCheckFailed {
663
673
t .Fatalf ("synchronisation error mismatch: have %v, want %v" , err , errCrossCheckFailed )
664
674
}
665
675
// Ensure that a valid chain can still pass sync
666
676
blockSoftTTL = defaultBlockTTL
667
677
crossCheckCycle = defaultCrossCheckCycle
668
678
669
679
tester .newPeer ("valid" , eth60 , hashes , blocks )
670
- if err := tester .sync ("valid" ); err != nil {
680
+ if err := tester .sync ("valid" , nil ); err != nil {
671
681
t .Fatalf ("failed to synchronise blocks: %v" , err )
672
682
}
673
683
}
@@ -690,7 +700,7 @@ func TestBannedChainStarvationAttack60(t *testing.T) {
690
700
// the head of the invalid chain is blocked too.
691
701
for banned := tester .downloader .banned .Size (); ; {
692
702
// Try to sync with the attacker, check hash chain failure
693
- if err := tester .sync ("attack" ); err != errInvalidChain {
703
+ if err := tester .sync ("attack" , nil ); err != errInvalidChain {
694
704
if tester .downloader .banned .Has (forkHashes [0 ]) && err == errBannedHead {
695
705
break
696
706
}
@@ -711,7 +721,7 @@ func TestBannedChainStarvationAttack60(t *testing.T) {
711
721
t .Fatalf ("banned attacker registered: %v" , peer )
712
722
}
713
723
// Ensure that a valid chain can still pass sync
714
- if err := tester .sync ("valid" ); err != nil {
724
+ if err := tester .sync ("valid" , nil ); err != nil {
715
725
t .Fatalf ("failed to synchronise blocks: %v" , err )
716
726
}
717
727
}
@@ -743,7 +753,7 @@ func TestBannedChainMemoryExhaustionAttack60(t *testing.T) {
743
753
// the head of the invalid chain is blocked too.
744
754
for {
745
755
// Try to sync with the attacker, check hash chain failure
746
- if err := tester .sync ("attack" ); err != errInvalidChain {
756
+ if err := tester .sync ("attack" , nil ); err != errInvalidChain {
747
757
t .Fatalf ("synchronisation error mismatch: have %v, want %v" , err , errInvalidChain )
748
758
}
749
759
// Short circuit if the entire chain was banned.
@@ -754,7 +764,7 @@ func TestBannedChainMemoryExhaustionAttack60(t *testing.T) {
754
764
if bans := tester .downloader .banned .Size (); bans > maxBannedHashes {
755
765
t .Fatalf ("ban cap exceeded: have %v, want max %v" , bans , maxBannedHashes )
756
766
}
757
- for hash , _ := range core .BadHashes {
767
+ for hash := range core .BadHashes {
758
768
if ! tester .downloader .banned .Has (hash ) {
759
769
t .Fatalf ("hard coded ban evacuated: %x" , hash )
760
770
}
@@ -764,7 +774,7 @@ func TestBannedChainMemoryExhaustionAttack60(t *testing.T) {
764
774
MaxBlockFetch = defaultMaxBlockFetch
765
775
maxBannedHashes = defaultMaxBannedHashes
766
776
767
- if err := tester .sync ("valid" ); err != nil {
777
+ if err := tester .sync ("valid" , nil ); err != nil {
768
778
t .Fatalf ("failed to synchronise blocks: %v" , err )
769
779
}
770
780
}
@@ -790,7 +800,7 @@ func TestOverlappingDeliveryAttack60(t *testing.T) {
790
800
return rawGetBlocks (append (request , hashes [0 ]))
791
801
}
792
802
// Test that synchronisation can complete, check for import success
793
- if err := tester .sync ("attack" ); err != nil {
803
+ if err := tester .sync ("attack" , nil ); err != nil {
794
804
t .Fatalf ("failed to synchronise blocks: %v" , err )
795
805
}
796
806
start := time .Now ()
@@ -807,7 +817,7 @@ func TestOverlappingDeliveryAttack60(t *testing.T) {
807
817
func TestHighTDStarvationAttack61 (t * testing.T ) {
808
818
tester := newTester ()
809
819
tester .newPeer ("attack" , eth61 , []common.Hash {genesis .Hash ()}, nil )
810
- if err := tester .sync ("attack" ); err != errStallingPeer {
820
+ if err := tester .sync ("attack" , big . NewInt ( 1000000 ) ); err != errStallingPeer {
811
821
t .Fatalf ("synchronisation error mismatch: have %v, want %v" , err , errStallingPeer )
812
822
}
813
823
}
@@ -849,7 +859,7 @@ func TestHashAttackerDropping(t *testing.T) {
849
859
// Simulate a synchronisation and check the required result
850
860
tester .downloader .synchroniseMock = func (string , common.Hash ) error { return tt .result }
851
861
852
- tester .downloader .Synchronise (id , genesis .Hash ())
862
+ tester .downloader .Synchronise (id , genesis .Hash (), big . NewInt ( 1000 ) )
853
863
if _ , ok := tester .peerHashes [id ]; ! ok != tt .drop {
854
864
t .Errorf ("test %d: peer drop mismatch for %v: have %v, want %v" , i , tt .result , ! ok , tt .drop )
855
865
}
0 commit comments