Commit 41207dbb authored by vyzo's avatar vyzo Committed by GitHub
Browse files

Merge pull request #208 from libp2p/fix/routed/peer-addrs

RoutedHost: NewStream ensures we have the means to connect to the peer
Showing with 25 additions and 8 deletions
+25 -8
......@@ -59,18 +59,12 @@ func (rh *RoutedHost) Connect(ctx context.Context, pi pstore.PeerInfo) error {
// Check if we have some addresses in our recent memory.
addrs := rh.Peerstore().Addrs(pi.ID)
if len(addrs) < 1 {
// no addrs? find some with the routing system.
pi2, err := rh.route.FindPeer(ctx, pi.ID)
var err error
addrs, err = rh.findPeerAddrs(ctx, pi.ID)
if err != nil {
return err // couldnt find any :(
}
if pi2.ID != pi.ID {
err = fmt.Errorf("routing failure: provided addrs for different peer")
logRoutingErrDifferentPeers(ctx, pi.ID, pi2.ID, err)
return err
}
addrs = pi2.Addrs
}
// if we're here, we got some addrs. let's use our wrapped host to connect.
......@@ -78,6 +72,21 @@ func (rh *RoutedHost) Connect(ctx context.Context, pi pstore.PeerInfo) error {
return rh.host.Connect(ctx, pi)
}
func (rh *RoutedHost) findPeerAddrs(ctx context.Context, id peer.ID) ([]ma.Multiaddr, error) {
pi, err := rh.route.FindPeer(ctx, id)
if err != nil {
return nil, err // couldnt find any :(
}
if pi.ID != id {
err = fmt.Errorf("routing failure: provided addrs for different peer")
logRoutingErrDifferentPeers(ctx, id, pi.ID, err)
return nil, err
}
return pi.Addrs, nil
}
func logRoutingErrDifferentPeers(ctx context.Context, wanted, got peer.ID, err error) {
lm := make(lgbl.DeferredMap)
lm["error"] = err
......@@ -119,6 +128,14 @@ func (rh *RoutedHost) RemoveStreamHandler(pid protocol.ID) {
}
func (rh *RoutedHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.ID) (inet.Stream, error) {
// Ensure we have a connection, with peer addresses resolved by the routing system (#207)
// It is not sufficient to let the underlying host connect, it will most likely not have
// any addresses for the peer without any prior connections.
err := rh.Connect(ctx, pstore.PeerInfo{ID: p})
if err != nil {
return nil, err
}
return rh.host.NewStream(ctx, p, pids...)
}
func (rh *RoutedHost) Close() error {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment