-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add host with circuitv2 to integration test #3162
base: master
Are you sure you want to change the base?
Changes from 3 commits
75e779f
a208956
f636442
7723e4f
beb97ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,8 @@ import ( | |
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager" | ||
"github.com/libp2p/go-libp2p/p2p/muxer/yamux" | ||
"github.com/libp2p/go-libp2p/p2p/net/swarm" | ||
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client" | ||
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay" | ||
"github.com/libp2p/go-libp2p/p2p/protocol/ping" | ||
"github.com/libp2p/go-libp2p/p2p/security/noise" | ||
tls "github.com/libp2p/go-libp2p/p2p/security/tls" | ||
|
@@ -46,6 +48,7 @@ type TransportTestCase struct { | |
type TransportTestCaseOpts struct { | ||
NoListen bool | ||
NoRcmgr bool | ||
EnabledRelay bool | ||
ConnGater connmgr.ConnectionGater | ||
ResourceManager network.ResourceManager | ||
} | ||
|
@@ -66,6 +69,16 @@ func transformOpts(opts TransportTestCaseOpts) []config.Option { | |
return libp2pOpts | ||
} | ||
|
||
func connect(t *testing.T, a, b host.Host) { | ||
pi := peer.AddrInfo{ID: a.ID(), Addrs: a.Addrs()} | ||
err := b.Connect(context.Background(), pi) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
const RelayTestOnTransportName = "QUIC" | ||
|
||
var transportsToTest = []TransportTestCase{ | ||
{ | ||
Name: "TCP / Noise / Yamux", | ||
|
@@ -78,6 +91,9 @@ var transportsToTest = []TransportTestCase{ | |
} else { | ||
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0")) | ||
} | ||
if opts.EnabledRelay { | ||
libp2pOpts = append(libp2pOpts, libp2p.EnableRelay(), libp2p.EnableRelayService()) | ||
} | ||
h, err := libp2p.New(libp2pOpts...) | ||
require.NoError(t, err) | ||
return h | ||
|
@@ -94,6 +110,9 @@ var transportsToTest = []TransportTestCase{ | |
} else { | ||
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0")) | ||
} | ||
if opts.EnabledRelay { | ||
libp2pOpts = append(libp2pOpts, libp2p.EnableRelay(), libp2p.EnableRelayService()) | ||
} | ||
h, err := libp2p.New(libp2pOpts...) | ||
require.NoError(t, err) | ||
return h | ||
|
@@ -111,6 +130,9 @@ var transportsToTest = []TransportTestCase{ | |
} else { | ||
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0")) | ||
} | ||
if opts.EnabledRelay { | ||
libp2pOpts = append(libp2pOpts, libp2p.EnableRelay(), libp2p.EnableRelayService()) | ||
} | ||
h, err := libp2p.New(libp2pOpts...) | ||
require.NoError(t, err) | ||
return h | ||
|
@@ -126,6 +148,9 @@ var transportsToTest = []TransportTestCase{ | |
} else { | ||
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0/ws")) | ||
} | ||
if opts.EnabledRelay { | ||
libp2pOpts = append(libp2pOpts, libp2p.EnableRelay(), libp2p.EnableRelayService()) | ||
} | ||
h, err := libp2p.New(libp2pOpts...) | ||
require.NoError(t, err) | ||
return h | ||
|
@@ -140,6 +165,9 @@ var transportsToTest = []TransportTestCase{ | |
} else { | ||
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0/ws")) | ||
} | ||
if opts.EnabledRelay { | ||
libp2pOpts = append(libp2pOpts, libp2p.EnableRelay(), libp2p.EnableRelayService()) | ||
} | ||
badgooooor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
h, err := libp2p.New(libp2pOpts...) | ||
require.NoError(t, err) | ||
return h | ||
|
@@ -154,6 +182,9 @@ var transportsToTest = []TransportTestCase{ | |
} else { | ||
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/udp/0/quic-v1")) | ||
} | ||
if opts.EnabledRelay { | ||
libp2pOpts = append(libp2pOpts, libp2p.EnableRelay(), libp2p.EnableRelayService()) | ||
} | ||
h, err := libp2p.New(libp2pOpts...) | ||
require.NoError(t, err) | ||
return h | ||
|
@@ -168,6 +199,9 @@ var transportsToTest = []TransportTestCase{ | |
} else { | ||
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/udp/0/quic-v1/webtransport")) | ||
} | ||
if opts.EnabledRelay { | ||
libp2pOpts = append(libp2pOpts, libp2p.EnableRelay(), libp2p.EnableRelayService()) | ||
} | ||
h, err := libp2p.New(libp2pOpts...) | ||
require.NoError(t, err) | ||
return h | ||
|
@@ -183,6 +217,9 @@ var transportsToTest = []TransportTestCase{ | |
} else { | ||
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/udp/0/webrtc-direct")) | ||
} | ||
if opts.EnabledRelay { | ||
libp2pOpts = append(libp2pOpts, libp2p.EnableRelay(), libp2p.EnableRelayService()) | ||
} | ||
h, err := libp2p.New(libp2pOpts...) | ||
require.NoError(t, err) | ||
return h | ||
|
@@ -830,3 +867,129 @@ func TestConnClosedWhenRemoteCloses(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestConnRelay(t *testing.T) { | ||
for _, tc := range transportsToTest { | ||
if tc.Name != RelayTestOnTransportName { | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't want a separate test for Relay. We want to run all integration tests with a Relay transport. This package has a bunch of tests that we want all transports to satisfy, like ConnGater Test, ResourceManager Test, PingPong on streams, etc. We should add another transport to the list of already present transports which connects hosts over a circuit relay address. At the moment it's fine to ignore which underlying transport is used to make the relay connection, and just use QUIC as the underlying transport. Ideally, any problem in the underlying transports should be caught when testing that specifc transport. |
||
} | ||
|
||
t.Run(tc.Name, func(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
var hosts []host.Host | ||
// Set host | ||
for i := 0; i < 3; i++ { | ||
h := tc.HostGenerator(t, TransportTestCaseOpts{EnabledRelay: true}) | ||
hosts = append(hosts, h) | ||
} | ||
|
||
// Set stream handler | ||
rch := make(chan []byte, 1) | ||
hosts[0].SetStreamHandler("test", func(s network.Stream) { | ||
defer s.Close() | ||
defer close(rch) | ||
|
||
buf := make([]byte, 1024) | ||
nread := 0 | ||
for nread < len(buf) { | ||
n, err := s.Read(buf[nread:]) | ||
nread += n | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
rch <- buf[:nread] | ||
}) | ||
|
||
r, err := relay.New(hosts[1]) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer r.Close() | ||
|
||
connect(t, hosts[0], hosts[1]) | ||
connect(t, hosts[1], hosts[2]) | ||
|
||
rinfo := hosts[1].Peerstore().PeerInfo(hosts[1].ID()) | ||
rsvp, err := client.Reserve(ctx, hosts[0], rinfo) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if rsvp.Voucher == nil { | ||
t.Fatal("no reservation voucher") | ||
} | ||
|
||
raddr, err := ma.NewMultiaddr(fmt.Sprintf("/p2p/%s/p2p-circuit/p2p/%s", hosts[1].ID(), hosts[0].ID())) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
sub, err := hosts[2].EventBus().Subscribe(new(event.EvtPeerConnectednessChanged)) | ||
require.NoError(t, err) | ||
|
||
err = hosts[2].Connect(ctx, peer.AddrInfo{ID: hosts[0].ID(), Addrs: []ma.Multiaddr{raddr}}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
for { | ||
var e interface{} | ||
select { | ||
case e = <-sub.Out(): | ||
case <-time.After(2 * time.Second): | ||
t.Fatal("expected limited connectivity event") | ||
} | ||
evt, ok := e.(event.EvtPeerConnectednessChanged) | ||
if !ok { | ||
t.Fatalf("invalid event: %s", e) | ||
} | ||
if evt.Peer == hosts[0].ID() { | ||
if evt.Connectedness != network.Limited { | ||
t.Fatalf("expected limited connectivity %s", evt.Connectedness) | ||
} | ||
break | ||
} | ||
} | ||
|
||
conns := hosts[2].Network().ConnsToPeer(hosts[0].ID()) | ||
if len(conns) != 1 { | ||
t.Fatalf("expected 1 connection, but got %d", len(conns)) | ||
} | ||
if !conns[0].Stat().Limited { | ||
t.Fatal("expected transient connection") | ||
} | ||
|
||
s, err := hosts[2].NewStream(network.WithAllowLimitedConn(ctx, "test"), hosts[0].ID(), "test") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
msg := []byte("relay works!") | ||
nwritten, err := s.Write(msg) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if nwritten != len(msg) { | ||
t.Fatalf("expected to write %d bytes, but wrote %d instead", len(msg), nwritten) | ||
} | ||
s.CloseWrite() | ||
|
||
got := <-rch | ||
if !bytes.Equal(msg, got) { | ||
t.Fatalf("Wrong echo; expected %s but got %s", string(msg), string(got)) | ||
} | ||
|
||
t.Cleanup(func() { | ||
for i := range len(hosts) { | ||
hosts[i].Close() | ||
} | ||
}) | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add another item to this list with Name: "circuit-v2"