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

fix: re-validate peers whenever their state changes #607

Merged
merged 2 commits into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/jbenet/goprocess"
goprocessctx "github.com/jbenet/goprocess/context"
"github.com/multiformats/go-base32"
ma "github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multihash"
)

Expand Down Expand Up @@ -702,3 +703,11 @@ func (dht *IpfsDHT) newContextWithLocalTags(ctx context.Context, extraTags ...ta
) // ignoring error as it is unrelated to the actual function of this code.
return ctx
}

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not adding addresses because the peer is connected seems counter intuitive. when are the peer's addresses added in that case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we don't want someone else to tell us the wrong addresses for the peer, overwriting the good ones in our peerstore.

return
}
dht.peerstore.AddAddrs(p, addrs, ttl)
}
2 changes: 1 addition & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func (q *query) queryPeer(ctx context.Context, ch chan<- *queryUpdate, p peer.ID

// add their addresses to the dialer's peerstore
if q.dht.queryPeerFilter(q.dht, *next) {
q.dht.peerstore.AddAddrs(next.ID, next.Addrs, pstore.TempAddrTTL)
q.dht.maybeAddAddrs(next.ID, next.Addrs, pstore.TempAddrTTL)
saw = append(saw, next.ID)
}
}
Expand Down
4 changes: 1 addition & 3 deletions routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,7 @@ func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key multihash

// Add unique providers from request, up to 'count'
for _, prov := range provs {
if prov.ID != dht.self {
dht.peerstore.AddAddrs(prov.ID, prov.Addrs, peerstore.TempAddrTTL)
}
dht.maybeAddAddrs(prov.ID, prov.Addrs, peerstore.TempAddrTTL)
logger.Debugf("got provider: %s", prov)
if ps.TryAdd(prov.ID) {
logger.Debugf("using provider: %s", prov)
Expand Down
35 changes: 7 additions & 28 deletions subscriber_notifee.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ func (nn *subscriberNotifee) subscribe(proc goprocess.Process) {
default:
}
case event.EvtPeerProtocolsUpdated:
handlePeerProtocolsUpdatedEvent(dht, evt)
handlePeerChangeEvent(dht, evt.Peer)
case event.EvtPeerIdentificationCompleted:
handlePeerIdentificationCompletedEvent(dht, evt)
handlePeerChangeEvent(dht, evt.Peer)
case event.EvtLocalReachabilityChanged:
if dht.auto == ModeAuto || dht.auto == ModeAutoServer {
handleLocalReachabilityChangedEvent(dht, evt)
Expand All @@ -112,40 +112,19 @@ func (nn *subscriberNotifee) subscribe(proc goprocess.Process) {
}
}

func handlePeerIdentificationCompletedEvent(dht *IpfsDHT, e event.EvtPeerIdentificationCompleted) {
dht.plk.Lock()
defer dht.plk.Unlock()
if dht.host.Network().Connectedness(e.Peer) != network.Connected {
return
}

// if the peer supports the DHT protocol, add it to our RT and kick a refresh if needed
valid, err := dht.validRTPeer(e.Peer)
func handlePeerChangeEvent(dht *IpfsDHT, p peer.ID) {
valid, err := dht.validRTPeer(p)
if err != nil {
logger.Errorf("could not check peerstore for protocol support: err: %s", err)
return
} else if valid {
dht.peerFound(dht.ctx, e.Peer, false)
dht.peerFound(dht.ctx, p, false)
dht.fixRTIfNeeded()
} else {
dht.peerStoppedDHT(dht.ctx, p)
}
}

func handlePeerProtocolsUpdatedEvent(dht *IpfsDHT, e event.EvtPeerProtocolsUpdated) {
valid, err := dht.validRTPeer(e.Peer)
if err != nil {
logger.Errorf("could not check peerstore for protocol support: err: %s", err)
return
}

if !valid {
dht.peerStoppedDHT(dht.ctx, e.Peer)
return
}

// we just might have discovered a peer that supports the DHT protocol
dht.fixRTIfNeeded()
}

func handleLocalReachabilityChangedEvent(dht *IpfsDHT, e event.EvtLocalReachabilityChanged) {
var target mode

Expand Down