Browse Source

routed host: return Connect error if FindPeer doesn't yield new addresses (#1946)

fix-rsa-key-generation
Marten Seemann 2 years ago
committed by GitHub
parent
commit
735891d229
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      p2p/host/routed/routed.go
  2. 53
      p2p/host/routed/routed_test.go

9
p2p/host/routed/routed.go

@ -107,8 +107,7 @@ func (rh *RoutedHost) Connect(ctx context.Context, pi peer.AddrInfo) error {
// if we're here, we got some addrs. let's use our wrapped host to connect. // if we're here, we got some addrs. let's use our wrapped host to connect.
pi.Addrs = addrs pi.Addrs = addrs
err := rh.host.Connect(ctx, pi) if cerr := rh.host.Connect(ctx, pi); cerr != nil {
if err != nil {
// We couldn't connect. Let's check if we have the most // We couldn't connect. Let's check if we have the most
// up-to-date addresses for the given peer. If there // up-to-date addresses for the given peer. If there
// are addresses we didn't know about previously, we // are addresses we didn't know about previously, we
@ -135,10 +134,10 @@ func (rh *RoutedHost) Connect(ctx context.Context, pi peer.AddrInfo) error {
pi.Addrs = newAddrs pi.Addrs = newAddrs
return rh.host.Connect(ctx, pi) return rh.host.Connect(ctx, pi)
} }
// No appropriate new address found.
return err // Return the original dial error.
return cerr
} }
return nil return nil
} }

53
p2p/host/routed/routed_test.go

@ -7,8 +7,8 @@ import (
"github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/peer"
basic "github.com/libp2p/go-libp2p/p2p/host/basic" basic "github.com/libp2p/go-libp2p/p2p/host/basic"
swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing" swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -25,8 +25,6 @@ func (m *mockRouting) FindPeer(ctx context.Context, pid peer.ID) (peer.AddrInfo,
} }
func TestRoutedHostConnectToObsoleteAddresses(t *testing.T) { func TestRoutedHostConnectToObsoleteAddresses(t *testing.T) {
ctx := context.Background()
h1, err := basic.NewHost(swarmt.GenSwarm(t), nil) h1, err := basic.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err) require.NoError(t, err)
defer h1.Close() defer h1.Close()
@ -35,23 +33,19 @@ func TestRoutedHostConnectToObsoleteAddresses(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
defer h2.Close() defer h2.Close()
// construct a wrong multi address for host 2, so that
// the initial connection attempt will fail
// (we have obsolete, old multi address information)
maddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1234")
require.NoError(t, err)
// assemble the AddrInfo struct to use for the connection attempt // assemble the AddrInfo struct to use for the connection attempt
pi := peer.AddrInfo{ pi := peer.AddrInfo{
ID: h2.ID(), ID: h2.ID(),
Addrs: []ma.Multiaddr{maddr}, // Use a wrong multi address for host 2, so that the initial connection attempt will fail
// (we have obsolete, old multi address information)
Addrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/1234")},
} }
// Build mock routing module and replace the FindPeer function. // Build mock routing module and replace the FindPeer function.
// Now, that function will return the correct multi addresses for host 2 // Now, that function will return the correct multi addresses for host 2
// (we have fetched the most up-to-date data from the DHT) // (we have fetched the most up-to-date data from the DHT)
mr := &mockRouting{ mr := &mockRouting{
findPeerFn: func(ctx context.Context, pi peer.ID) (peer.AddrInfo, error) { findPeerFn: func(context.Context, peer.ID) (peer.AddrInfo, error) {
return peer.AddrInfo{ return peer.AddrInfo{
ID: h2.ID(), ID: h2.ID(),
Addrs: h2.Addrs(), Addrs: h2.Addrs(),
@ -61,13 +55,36 @@ func TestRoutedHostConnectToObsoleteAddresses(t *testing.T) {
// Build routed host // Build routed host
rh := Wrap(h1, mr) rh := Wrap(h1, mr)
// Connection establishment should have worked without an error
require.NoError(t, rh.Connect(context.Background(), pi))
require.Equal(t, 1, mr.callCount, "the mocked FindPeer function should have been called")
}
// Try to connect func TestRoutedHostConnectFindPeerNoUsefulAddrs(t *testing.T) {
err = rh.Connect(ctx, pi) h1, err := basic.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
defer h1.Close()
// Connection establishment should have worked without an error h2, err := basic.NewHost(swarmt.GenSwarm(t), nil)
assert.NoError(t, err) require.NoError(t, err)
defer h2.Close()
// assemble the AddrInfo struct to use for the connection attempt
pi := peer.AddrInfo{
ID: h2.ID(),
// Use a wrong multi address for host 2, so that the initial connection attempt will fail
// (we have obsolete, old multi address information)
Addrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/1234")},
}
// The mocked FindPeer function should have been called // Build mock routing module and replace the FindPeer function.
assert.Equal(t, 1, mr.callCount) // Now, that function will return the correct multi addresses for host 2
// (we have fetched the most up-to-date data from the DHT)
mr := &mockRouting{findPeerFn: func(context.Context, peer.ID) (peer.AddrInfo, error) { return pi, nil }}
// Build routed host
rh := Wrap(h1, mr)
// Connection establishment should fail, since we didn't provide any useful addresses in FindPeer.
require.Error(t, rh.Connect(context.Background(), pi))
require.Equal(t, 1, mr.callCount, "the mocked FindPeer function should have been called")
} }

Loading…
Cancel
Save