Skip to content

Commit a858d14

Browse files
moved rt size check after request
1 parent 0dda69a commit a858d14

File tree

3 files changed

+44
-29
lines changed

3 files changed

+44
-29
lines changed

dht.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,14 +376,12 @@ func makeDHT(h host.Host, cfg dhtcfg.Config) (*IpfsDHT, error) {
376376
// lookupCheck performs a lookup request to a remote peer.ID, verifying that it is able to
377377
// answer it correctly
378378
func (dht *IpfsDHT) lookupCheck(ctx context.Context, p peer.ID) error {
379-
if dht.routingTable.Size() < dht.bucketSize {
380-
// no need to be picky if we don't have enough peers
381-
return nil
382-
}
383379
// lookup request to p requesting for its own peer.ID
384380
peerids, err := dht.protoMessenger.GetClosestPeers(ctx, p, p)
385-
// p should return at least its own peerid
386-
if err == nil && len(peerids) == 0 {
381+
// p is expected to return at least 1 peer id, unless our routing table has
382+
// less than bucketSize peers, in which case we aren't picky about who we
383+
// add to the routing table.
384+
if err == nil && len(peerids) == 0 && dht.routingTable.Size() >= dht.bucketSize {
387385
return fmt.Errorf("peer %s failed to return its closest peers, got %d", p, len(peerids))
388386
}
389387
return err

dht_test.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/libp2p/go-libp2p/core/peerstore"
2424
"github.com/libp2p/go-libp2p/core/protocol"
2525
"github.com/libp2p/go-libp2p/core/routing"
26+
"github.com/libp2p/go-msgio"
2627
ma "github.com/multiformats/go-multiaddr"
2728
manet "github.com/multiformats/go-multiaddr/net"
2829
"github.com/multiformats/go-multihash"
@@ -1490,32 +1491,48 @@ func TestInvalidServer(t *testing.T) {
14901491

14911492
s0 := setupDHT(ctx, t, false, BucketSize(2)) // server
14921493
s1 := setupDHT(ctx, t, false, BucketSize(2)) // server
1493-
c0 := setupDHT(ctx, t, true, BucketSize(2)) // client advertising server protocols
1494-
c1 := setupDHT(ctx, t, true, BucketSize(2)) // client advertising server protocols
1495-
// note that the bucket size is 2
1494+
m0 := setupDHT(ctx, t, false, BucketSize(2)) // misbehabing server
1495+
m1 := setupDHT(ctx, t, false, BucketSize(2)) // misbehabing server
14961496

1497-
// make c0 and c1 advertise all dht server protocols, but hang on all requests
1497+
// make m0 and m1 advertise all dht server protocols, but hang on all requests
14981498
for _, proto := range s0.serverProtocols {
1499-
for _, c := range []*IpfsDHT{c0, c1} {
1499+
for _, m := range []*IpfsDHT{m0, m1} {
15001500
// Hang on every request.
1501-
c.host.SetStreamHandler(proto, func(s network.Stream) {
1502-
defer s.Reset() // nolint
1503-
<-ctx.Done()
1501+
m.host.SetStreamHandler(proto, func(s network.Stream) {
1502+
r := msgio.NewVarintReaderSize(s, network.MessageSizeMax)
1503+
msgbytes, err := r.ReadMsg()
1504+
if err != nil {
1505+
t.Fatal(err)
1506+
}
1507+
var req pb.Message
1508+
err = req.Unmarshal(msgbytes)
1509+
if err != nil {
1510+
t.Fatal(err)
1511+
}
1512+
1513+
// answer with an empty response message
1514+
resp := pb.NewMessage(req.GetType(), nil, req.GetClusterLevel())
1515+
1516+
// send out response msg
1517+
err = net.WriteMsg(s, resp)
1518+
if err != nil {
1519+
t.Fatal(err)
1520+
}
15041521
})
15051522
}
15061523
}
15071524

1508-
// connect s0 and c0
1509-
connectNoSync(t, ctx, s0, c0)
1525+
// connect s0 and m0
1526+
connectNoSync(t, ctx, s0, m0)
15101527

15111528
// add a provider (p) for a key (k) to s0
15121529
k := testCaseCids[0]
15131530
p := peer.ID("TestPeer")
15141531
s0.ProviderStore().AddProvider(ctx, k.Hash(), peer.AddrInfo{ID: p})
15151532
time.Sleep(time.Millisecond * 5) // just in case...
15161533

1517-
// find the provider for k from c0
1518-
provs, err := c0.FindProviders(ctx, k)
1534+
// find the provider for k from m0
1535+
provs, err := m0.FindProviders(ctx, k)
15191536
if err != nil {
15201537
t.Fatal(err)
15211538
}
@@ -1526,27 +1543,27 @@ func TestInvalidServer(t *testing.T) {
15261543
t.Fatal("expected it to be our test peer")
15271544
}
15281545

1529-
// verify that c0 and s0 contain each other in their routing tables
1530-
if s0.routingTable.Find(c0.self) == "" {
1531-
// c0 is added to s0 routing table even though it is misbehaving, because
1546+
// verify that m0 and s0 contain each other in their routing tables
1547+
if s0.routingTable.Find(m0.self) == "" {
1548+
// m0 is added to s0 routing table even though it is misbehaving, because
15321549
// s0's routing table is not well populated, so s0 isn't picky about who it adds.
15331550
t.Fatal("Misbehaving DHT servers should be added to routing table if not well populated")
15341551
}
1535-
if c0.routingTable.Find(s0.self) == "" {
1552+
if m0.routingTable.Find(s0.self) == "" {
15361553
t.Fatal("DHT server should have been added to the misbehaving server routing table")
15371554
}
15381555

1539-
// connect s0 to both s1 and c1
1556+
// connect s0 to both s1 and m1
15401557
connectNoSync(t, ctx, s0, s1)
1541-
connectNoSync(t, ctx, s0, c1)
1558+
connectNoSync(t, ctx, s0, m1)
15421559

15431560
// s1 should be added to s0's routing table. Then, because s0's routing table
1544-
// contains more than bucketSize (2) entries, lookupCheck is enabled and c1
1561+
// contains more than bucketSize (2) entries, lookupCheck is enabled and m1
15451562
// shouldn't be added, because it fails the lookupCheck (hang on all requests).
15461563
if s0.routingTable.Find(s1.self) == "" {
15471564
t.Fatal("Well behaving DHT server should have been added to the server routing table")
15481565
}
1549-
if s0.routingTable.Find(c1.self) != "" {
1566+
if s0.routingTable.Find(m1.self) != "" {
15501567
t.Fatal("Misbehaving DHT servers should not be added to routing table if well populated")
15511568
}
15521569
}

ext_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestInvalidRemotePeers(t *testing.T) {
4242

4343
time.Sleep(100 * time.Millisecond)
4444

45-
// hosts[1] is added to the routing table even though is is not responding
46-
// because d has no other peers in its routing table, hence it isn't
47-
require.Equal(t, 1, d.routingTable.Size())
45+
// hosts[1] isn't added to the routing table because it isn't responding to
46+
// the DHT request
47+
require.Equal(t, 0, d.routingTable.Size())
4848
}

0 commit comments

Comments
 (0)