Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix connectedness #976

Merged
merged 4 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,8 @@ func (dht *IpfsDHT) FindLocal(ctx context.Context, id peer.ID) peer.AddrInfo {
_, span := internal.StartSpan(ctx, "IpfsDHT.FindLocal", trace.WithAttributes(attribute.Stringer("PeerID", id)))
defer span.End()

if dht.host.Network().Connectedness(id) == network.Connected {
connectedness := dht.host.Network().Connectedness(id)
if connectedness == network.Connected || connectedness == network.Limited {
2color marked this conversation as resolved.
Show resolved Hide resolved
return dht.peerstore.PeerInfo(id)
}
return peer.AddrInfo{}
Expand Down Expand Up @@ -926,7 +927,8 @@ func (dht *IpfsDHT) newContextWithLocalTags(ctx context.Context, extraTags ...ta

func (dht *IpfsDHT) maybeAddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {
// Don't add addresses for self or our connected peers. We have better ones.
if p == dht.self || dht.host.Network().Connectedness(p) == network.Connected {
connectedness := dht.host.Network().Connectedness(p)
if p == dht.self || connectedness == network.Connected || connectedness == network.Limited {
return
}
dht.peerstore.AddAddrs(p, dht.filterAddrs(addrs), ttl)
Expand Down
8 changes: 5 additions & 3 deletions fullrt/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,7 @@
// Return peer information if we tried to dial the peer during the query or we are (or recently were) connected
// to the peer.
connectedness := dht.h.Network().Connectedness(id)
if connectedness == network.Connected {
if connectedness == network.Connected || connectedness == network.Limited {

Check warning on line 1463 in fullrt/dht.go

View check run for this annotation

Codecov / codecov/patch

fullrt/dht.go#L1463

Added line #L1463 was not covered by tests
return dht.h.Peerstore().PeerInfo(id), nil
}

Expand Down Expand Up @@ -1538,15 +1538,17 @@

// FindLocal looks for a peer with a given ID connected to this dht and returns the peer and the table it was found in.
func (dht *FullRT) FindLocal(id peer.ID) peer.AddrInfo {
if dht.h.Network().Connectedness(id) == network.Connected {
connectedness := dht.h.Network().Connectedness(id)
if connectedness == network.Connected || connectedness == network.Limited {

Check warning on line 1542 in fullrt/dht.go

View check run for this annotation

Codecov / codecov/patch

fullrt/dht.go#L1541-L1542

Added lines #L1541 - L1542 were not covered by tests
return dht.h.Peerstore().PeerInfo(id)
}
return peer.AddrInfo{}

Check warning on line 1545 in fullrt/dht.go

View check run for this annotation

Codecov / codecov/patch

fullrt/dht.go#L1545

Added line #L1545 was not covered by tests
}

func (dht *FullRT) maybeAddAddrs(p peer.ID, addrs []multiaddr.Multiaddr, ttl time.Duration) {
// Don't add addresses for self or our connected peers. We have better ones.
if p == dht.h.ID() || dht.h.Network().Connectedness(p) == network.Connected {
connectedness := dht.h.Network().Connectedness(p)
if p == dht.h.ID() || connectedness == network.Connected || connectedness == network.Limited {

Check warning on line 1551 in fullrt/dht.go

View check run for this annotation

Codecov / codecov/patch

fullrt/dht.go#L1550-L1551

Added lines #L1550 - L1551 were not covered by tests
return
}
dht.h.Peerstore().AddAddrs(p, addrs, ttl)
Expand Down
14 changes: 7 additions & 7 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ func TestRTEvictionOnFailedQuery(t *testing.T) {
// peers should be in the RT because of fixLowPeers
require.NoError(t, tu.WaitFor(ctx, func() error {
if !checkRoutingTable(d1, d2) {
return fmt.Errorf("should have routes 0")
return fmt.Errorf("should have routes")
}
return nil
}))

// close both hosts so query fails
require.NoError(t, d1.host.Close())
require.NoError(t, d2.host.Close())
// peers will still be in the RT because we have decoupled membership from connectivity
// clear the addresses of the peers so that the next queries fail
d1.host.Peerstore().ClearAddrs(d2.self)
d2.host.Peerstore().ClearAddrs(d1.self)
// peers will still be in the RT because RT is decoupled with the host and peerstore
require.NoError(t, tu.WaitFor(ctx, func() error {
if !checkRoutingTable(d1, d2) {
return fmt.Errorf("should have routes 1")
return fmt.Errorf("should have routes")
}
return nil
}))
Expand All @@ -59,7 +59,7 @@ func TestRTEvictionOnFailedQuery(t *testing.T) {

require.NoError(t, tu.WaitFor(ctx, func() error {
if checkRoutingTable(d1, d2) {
return fmt.Errorf("should not have routes 2")
return fmt.Errorf("should not have routes")
}
return nil
}))
Expand Down
5 changes: 3 additions & 2 deletions routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,8 @@ func (dht *IpfsDHT) FindPeer(ctx context.Context, id peer.ID) (pi peer.AddrInfo,
return peers, err
},
func(*qpeerset.QueryPeerset) bool {
return dht.host.Network().Connectedness(id) == network.Connected
connectedness := dht.host.Network().Connectedness(id)
return connectedness == network.Connected || connectedness == network.Limited
},
)

Expand All @@ -686,7 +687,7 @@ func (dht *IpfsDHT) FindPeer(ctx context.Context, id peer.ID) (pi peer.AddrInfo,
// Return peer information if we tried to dial the peer during the query or we are (or recently were) connected
// to the peer.
connectedness := dht.host.Network().Connectedness(id)
if dialedPeerDuringQuery || connectedness == network.Connected {
if dialedPeerDuringQuery || connectedness == network.Connected || connectedness == network.Limited {
return dht.peerstore.PeerInfo(id), nil
}

Expand Down
Loading