@@ -8,12 +8,14 @@ import (
8
8
9
9
"github.com/ipfs/go-datastore"
10
10
"github.com/ipfs/go-datastore/sync"
11
+ "github.com/libp2p/go-libp2p"
11
12
libhost "github.com/libp2p/go-libp2p/core/host"
12
13
"github.com/libp2p/go-libp2p/core/network"
13
14
"github.com/libp2p/go-libp2p/core/peer"
14
15
"github.com/libp2p/go-libp2p/core/peerstore"
15
16
blankhost "github.com/libp2p/go-libp2p/p2p/host/blank"
16
17
"github.com/libp2p/go-libp2p/p2p/net/conngater"
18
+ "github.com/libp2p/go-libp2p/p2p/net/connmgr"
17
19
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
18
20
swarm "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
19
21
"github.com/stretchr/testify/assert"
@@ -242,7 +244,7 @@ func TestExchange_RequestFullRangeHeaders(t *testing.T) {
242
244
require .NoError (t , err )
243
245
servers [index ].Start (context .Background ()) //nolint:errcheck
244
246
exchange .peerTracker .peerLk .Lock ()
245
- exchange .peerTracker .trackedPeers [hosts [index ].ID ()] = & peerStat { peerID : hosts [ index ]. ID () }
247
+ exchange .peerTracker .trackedPeers [hosts [index ].ID ()] = struct {}{ }
246
248
exchange .peerTracker .peerLk .Unlock ()
247
249
}
248
250
@@ -262,30 +264,38 @@ func TestExchange_RequestFullRangeHeaders(t *testing.T) {
262
264
// TestExchange_RequestHeadersFromAnotherPeer tests that the Exchange instance will request range
263
265
// from another peer with lower score after receiving header.ErrNotFound
264
266
func TestExchange_RequestHeadersFromAnotherPeer (t * testing.T ) {
265
- hosts := createMocknet (t , 3 )
267
+ hosts := quicHosts (t , 3 )
268
+
266
269
// create client + server(it does not have needed headers)
267
270
exchg , store := createP2PExAndServer (t , hosts [0 ], hosts [1 ])
271
+
272
+ serverSideStore := headertest .NewStore [* headertest.DummyHeader ](t , headertest .NewTestSuite (t ), 10 )
273
+ tmServerSideStore := & timedOutStore {timeout : time .Millisecond * 200 , Store : * serverSideStore }
274
+
275
+ hosts [0 ].ConnManager ().TagPeer (hosts [1 ].ID (), string (protocolID (networkID )), 100 )
276
+ hosts [0 ].ConnManager ().TagPeer (hosts [2 ].ID (), string (protocolID (networkID )), 90 )
277
+
268
278
// create one more server(with more headers in the store)
269
279
serverSideEx , err := NewExchangeServer [* headertest.DummyHeader ](
270
- hosts [2 ], headertest . NewStore [ * headertest. DummyHeader ]( t , headertest . NewTestSuite ( t ), 10 ) ,
280
+ hosts [2 ], tmServerSideStore ,
271
281
WithNetworkID [ServerParameters ](networkID ),
272
282
)
273
283
require .NoError (t , err )
274
284
require .NoError (t , serverSideEx .Start (context .Background ()))
275
285
t .Cleanup (func () {
276
286
serverSideEx .Stop (context .Background ()) //nolint:errcheck
277
287
})
288
+
278
289
exchg .peerTracker .peerLk .Lock ()
279
- exchg .peerTracker .trackedPeers [hosts [2 ].ID ()] = & peerStat { peerID : hosts [ 2 ]. ID (), peerScore : 20 }
290
+ exchg .peerTracker .trackedPeers [hosts [2 ].ID ()] = struct {}{ }
280
291
exchg .peerTracker .peerLk .Unlock ()
281
-
282
292
h , err := store .GetByHeight (context .Background (), 5 )
283
293
require .NoError (t , err )
284
294
285
295
_ , err = exchg .GetRangeByHeight (context .Background (), h , 8 )
286
296
require .NoError (t , err )
287
297
// ensure that peerScore for the second peer is changed
288
- newPeerScore := exchg .peerTracker .trackedPeers [ hosts [2 ].ID ()]. score ( )
298
+ newPeerScore := score ( t , exchg .peerTracker .host , hosts [2 ].ID ())
289
299
require .NotEqual (t , 20 , newPeerScore )
290
300
}
291
301
@@ -464,7 +474,9 @@ func TestExchange_HandleHeaderWithDifferentChainID(t *testing.T) {
464
474
func TestExchange_RequestHeadersFromAnotherPeerWhenTimeout (t * testing.T ) {
465
475
// create blankhost because mocknet does not support deadlines
466
476
swarm0 := swarm .GenSwarm (t )
467
- host0 := blankhost .NewBlankHost (swarm0 )
477
+ mngr , err := connmgr .NewConnManager (0 , 50 )
478
+ require .NoError (t , err )
479
+ host0 := blankhost .NewBlankHost (swarm0 , blankhost .WithConnectionManager (mngr ))
468
480
swarm1 := swarm .GenSwarm (t )
469
481
host1 := blankhost .NewBlankHost (swarm1 )
470
482
swarm2 := swarm .GenSwarm (t )
@@ -495,24 +507,25 @@ func TestExchange_RequestHeadersFromAnotherPeerWhenTimeout(t *testing.T) {
495
507
t .Cleanup (func () {
496
508
serverSideEx .Stop (context .Background ()) //nolint:errcheck
497
509
})
498
- prevScore := exchg .peerTracker . trackedPeers [ host1 .ID ()]. score ( )
510
+ prevScore := score ( t , exchg .host , host1 .ID ())
499
511
exchg .peerTracker .peerLk .Lock ()
500
- exchg .peerTracker .trackedPeers [host2 .ID ()] = & peerStat {peerID : host2 .ID (), peerScore : 200 }
512
+ host0 .ConnManager ().TagPeer (host2 .ID (), string (protocolID (networkID )), 100 )
513
+ exchg .peerTracker .trackedPeers [host2 .ID ()] = struct {}{}
501
514
exchg .peerTracker .peerLk .Unlock ()
502
515
503
516
gen , err := store .GetByHeight (context .Background (), 1 )
504
517
require .NoError (t , err )
505
518
506
519
_ , err = exchg .GetRangeByHeight (context .Background (), gen , 3 )
507
520
require .NoError (t , err )
508
- newPeerScore := exchg .peerTracker . trackedPeers [ host1 .ID ()]. score ( )
521
+ newPeerScore := score ( t , exchg .host , host1 .ID ())
509
522
assert .NotEqual (t , newPeerScore , prevScore )
510
523
}
511
524
512
525
// TestExchange_RequestPartialRange enusres in case of receiving a partial response
513
526
// from server, Exchange will re-request remaining headers from another peer
514
527
func TestExchange_RequestPartialRange (t * testing.T ) {
515
- hosts := createMocknet (t , 3 )
528
+ hosts := quicHosts (t , 3 )
516
529
exchg , store := createP2PExAndServer (t , hosts [0 ], hosts [1 ])
517
530
518
531
// create one more server(with more headers in the store)
@@ -523,13 +536,14 @@ func TestExchange_RequestPartialRange(t *testing.T) {
523
536
ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
524
537
t .Cleanup (cancel )
525
538
539
+ hosts [0 ].ConnManager ().TagPeer (hosts [1 ].ID (), string (protocolID (networkID )), 100 )
540
+
526
541
require .NoError (t , err )
527
542
require .NoError (t , serverSideEx .Start (ctx ))
528
543
exchg .peerTracker .peerLk .Lock ()
529
- prevScoreBefore1 := exchg .peerTracker .trackedPeers [hosts [1 ].ID ()].peerScore
530
- prevScoreBefore2 := 50
531
- // reducing peerScore of the second server, so our exchange will request host[1] first.
532
- exchg .peerTracker .trackedPeers [hosts [2 ].ID ()] = & peerStat {peerID : hosts [2 ].ID (), peerScore : 50 }
544
+ prevScoreBefore1 := score (t , exchg .host , hosts [1 ].ID ())
545
+ prevScoreBefore2 := score (t , exchg .host , hosts [2 ].ID ())
546
+ exchg .peerTracker .trackedPeers [hosts [2 ].ID ()] = struct {}{}
533
547
exchg .peerTracker .peerLk .Unlock ()
534
548
535
549
gen , err := store .GetByHeight (context .Background (), 1 )
@@ -540,8 +554,8 @@ func TestExchange_RequestPartialRange(t *testing.T) {
540
554
require .NoError (t , err )
541
555
542
556
exchg .peerTracker .peerLk .Lock ()
543
- prevScoreAfter1 := exchg .peerTracker . trackedPeers [ hosts [1 ].ID ()]. peerScore
544
- prevScoreAfter2 := exchg .peerTracker . trackedPeers [ hosts [2 ].ID ()]. peerScore
557
+ prevScoreAfter1 := score ( t , exchg .host , hosts [1 ].ID ())
558
+ prevScoreAfter2 := score ( t , exchg .host , hosts [2 ].ID ())
545
559
exchg .peerTracker .peerLk .Unlock ()
546
560
547
561
assert .NotEqual (t , prevScoreBefore1 , prevScoreAfter1 )
@@ -561,7 +575,6 @@ func createP2PExAndServer(
561
575
host , tpeer libhost.Host ,
562
576
) (* Exchange [* headertest.DummyHeader ], * headertest.Store [* headertest.DummyHeader ]) {
563
577
store := headertest .NewStore [* headertest.DummyHeader ](t , headertest .NewTestSuite (t ), 5 )
564
-
565
578
serverSideEx , err := NewExchangeServer [* headertest.DummyHeader ](tpeer , store ,
566
579
WithNetworkID [ServerParameters ](networkID ),
567
580
)
@@ -582,7 +595,7 @@ func createP2PExAndServer(
582
595
time .Sleep (time .Millisecond * 100 ) // give peerTracker time to add a trusted peer
583
596
584
597
ex .peerTracker .peerLk .Lock ()
585
- ex .peerTracker .trackedPeers [tpeer .ID ()] = & peerStat { peerID : tpeer . ID (), peerScore : 100.0 }
598
+ ex .peerTracker .trackedPeers [tpeer .ID ()] = struct {}{ }
586
599
ex .peerTracker .peerLk .Unlock ()
587
600
588
601
t .Cleanup (func () {
@@ -595,12 +608,15 @@ func createP2PExAndServer(
595
608
596
609
func quicHosts (t * testing.T , n int ) []libhost.Host {
597
610
hosts := make ([]libhost.Host , n )
611
+ var err error
598
612
for i := range hosts {
599
- swrm := swarm . GenSwarm (t , swarm . OptDisableTCP )
600
- hosts [i ] = blankhost . NewBlankHost ( swrm )
613
+ require . NoError (t , err )
614
+ hosts [i ], err = libp2p . New ( )
601
615
for _ , host := range hosts [:i ] {
602
616
hosts [i ].Peerstore ().AddAddrs (host .ID (), host .Network ().ListenAddresses (), peerstore .PermanentAddrTTL )
603
617
host .Peerstore ().AddAddrs (hosts [i ].ID (), hosts [i ].Network ().ListenAddresses (), peerstore .PermanentAddrTTL )
618
+ err = hosts [i ].Connect (context .Background (), peer.AddrInfo {ID : host .ID (), Addrs : host .Addrs ()})
619
+ require .NoError (t , err )
604
620
}
605
621
}
606
622
@@ -647,3 +663,15 @@ func (t *timedOutStore) Head(context.Context, ...header.HeadOption[*headertest.D
647
663
time .Sleep (t .timeout )
648
664
return nil , header .ErrNoHead
649
665
}
666
+
667
+ func (t * timedOutStore ) GetRange (ctx context.Context , from , to uint64 ) ([]* headertest.DummyHeader , error ) {
668
+ time .Sleep (t .timeout )
669
+ return t .Store .GetRange (ctx , from , to )
670
+ }
671
+
672
+ func score (t * testing.T , h libhost.Host , id peer.ID ) int {
673
+ t .Helper ()
674
+ tags := h .ConnManager ().GetTagInfo (id )
675
+ tag , _ := tags .Tags [string (protocolID (networkID ))]
676
+ return tag
677
+ }
0 commit comments