Skip to content

Commit

Permalink
fix: check connectedness in separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
2color committed Aug 14, 2024
1 parent 53fb5ad commit be9a8e7
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 17 deletions.
6 changes: 2 additions & 4 deletions dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +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()

connectedness := dht.host.Network().Connectedness(id)
if connectedness == network.Connected || connectedness == network.Limited {
if HasValidConnectedness(dht.host, id) {
return dht.peerstore.PeerInfo(id)
}
return peer.AddrInfo{}
Expand Down Expand Up @@ -927,8 +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.
connectedness := dht.host.Network().Connectedness(p)
if p == dht.self || connectedness == network.Connected || connectedness == network.Limited {
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
}
9 changes: 3 additions & 6 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 || connectedness == network.Limited {
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,17 +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 {
connectedness := dht.h.Network().Connectedness(id)
if connectedness == network.Connected || connectedness == network.Limited {
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{}

Check warning on line 1543 in fullrt/dht.go

View check run for this annotation

Codecov / codecov/patch

fullrt/dht.go#L1543

Added line #L1543 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.
connectedness := dht.h.Network().Connectedness(p)
if p == dht.h.ID() || connectedness == network.Connected || connectedness == network.Limited {
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
3 changes: 1 addition & 2 deletions query.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"
pstore "github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/core/routing"
Expand Down Expand Up @@ -530,7 +529,7 @@ func (dht *IpfsDHT) dialPeer(ctx context.Context, p peer.ID) error {
defer span.End()

// short-circuit if we're already connected.
if dht.host.Network().Connectedness(p) == network.Connected {
if HasValidConnectedness(dht.host, p) {
return nil
}

Expand Down
7 changes: 2 additions & 5 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,8 +663,7 @@ func (dht *IpfsDHT) FindPeer(ctx context.Context, id peer.ID) (pi peer.AddrInfo,
return peers, err
},
func(*qpeerset.QueryPeerset) bool {
connectedness := dht.host.Network().Connectedness(id)
return connectedness == network.Connected || connectedness == network.Limited
return HasValidConnectedness(dht.host, id)
},
)

Expand All @@ -686,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 || connectedness == network.Limited {
if dialedPeerDuringQuery || HasValidConnectedness(dht.host, id) {
return dht.peerstore.PeerInfo(id), nil
}

Expand Down

0 comments on commit be9a8e7

Please sign in to comment.