Skip to content

Commit

Permalink
Merge pull request #976 from libp2p/update-libp2p-more-fixes
Browse files Browse the repository at this point in the history
chore: fix connectedness
  • Loading branch information
MarcoPolo authored Aug 20, 2024
2 parents 354cf0b + 7004c77 commit 96c96e3
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ 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 {
if HasValidConnectedness(dht.host, id) {
return dht.peerstore.PeerInfo(id)
}
return peer.AddrInfo{}
Expand Down Expand Up @@ -926,7 +926,7 @@ 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 {
if p == dht.self || HasValidConnectedness(dht.host, p) {
return
}
dht.peerstore.AddAddrs(p, dht.filterAddrs(addrs), ttl)
Expand Down
5 changes: 5 additions & 0 deletions dht_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,8 @@ func inAddrRange(ip net.IP, ipnets []*net.IPNet) bool {

return false
}

func HasValidConnectedness(host host.Host, id peer.ID) bool {
connectedness := host.Network().Connectedness(id)
return connectedness == network.Connected || connectedness == network.Limited
}
7 changes: 3 additions & 4 deletions fullrt/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -1459,8 +1459,7 @@ func (dht *FullRT) 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.h.Network().Connectedness(id)
if connectedness == network.Connected {
if kaddht.HasValidConnectedness(dht.h, id) {

Check warning on line 1462 in fullrt/dht.go

View check run for this annotation

Codecov / codecov/patch

fullrt/dht.go#L1462

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

Expand Down Expand Up @@ -1538,15 +1537,15 @@ func (dht *FullRT) getRecordFromDatastore(ctx context.Context, dskey ds.Key) (*r

// 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 {
if kaddht.HasValidConnectedness(dht.h, id) {

Check warning on line 1540 in fullrt/dht.go

View check run for this annotation

Codecov / codecov/patch

fullrt/dht.go#L1540

Added line #L1540 was not covered by tests
return dht.h.Peerstore().PeerInfo(id)
}
return peer.AddrInfo{}
}

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 {
if p == dht.h.ID() || kaddht.HasValidConnectedness(dht.h, p) {

Check warning on line 1548 in fullrt/dht.go

View check run for this annotation

Codecov / codecov/patch

fullrt/dht.go#L1548

Added line #L1548 was not covered by tests
return
}
dht.h.Peerstore().AddAddrs(p, addrs, ttl)
Expand Down
8 changes: 4 additions & 4 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func TestRTEvictionOnFailedQuery(t *testing.T) {
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")
Expand Down
6 changes: 2 additions & 4 deletions routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sync"
"time"

"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/core/routing"
Expand Down Expand Up @@ -664,7 +663,7 @@ 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
return HasValidConnectedness(dht.host, id)
},
)

Expand All @@ -685,8 +684,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 || HasValidConnectedness(dht.host, id) {
return dht.peerstore.PeerInfo(id), nil
}

Expand Down

0 comments on commit 96c96e3

Please sign in to comment.