From 7ea00ec8f19c1109eb32a7739f500f3c54ef8ce3 Mon Sep 17 00:00:00 2001 From: vyzo Date: Fri, 10 Sep 2021 11:08:17 +0300 Subject: [PATCH 1/6] move circuitv2 to p2p/protocol --- p2p/{host => protocol}/circuitv2/client/client.go | 0 p2p/{host => protocol}/circuitv2/client/conn.go | 0 p2p/{host => protocol}/circuitv2/client/dial.go | 0 p2p/{host => protocol}/circuitv2/client/handlers.go | 0 p2p/{host => protocol}/circuitv2/client/listen.go | 0 p2p/{host => protocol}/circuitv2/client/reservation.go | 0 p2p/{host => protocol}/circuitv2/client/transport.go | 0 p2p/{host => protocol}/circuitv2/pb/Makefile | 0 p2p/{host => protocol}/circuitv2/pb/circuit.pb.go | 0 p2p/{host => protocol}/circuitv2/pb/circuit.proto | 0 p2p/{host => protocol}/circuitv2/pb/voucher.pb.go | 0 p2p/{host => protocol}/circuitv2/pb/voucher.proto | 0 p2p/{host => protocol}/circuitv2/proto/protocol.go | 0 p2p/{host => protocol}/circuitv2/proto/voucher.go | 0 p2p/{host => protocol}/circuitv2/proto/voucher_test.go | 0 p2p/{host => protocol}/circuitv2/relay/acl.go | 0 p2p/{host => protocol}/circuitv2/relay/constraints.go | 0 p2p/{host => protocol}/circuitv2/relay/constraints_test.go | 0 p2p/{host => protocol}/circuitv2/relay/options.go | 0 p2p/{host => protocol}/circuitv2/relay/relay.go | 0 p2p/{host => protocol}/circuitv2/relay/resources.go | 0 p2p/{host => protocol}/circuitv2/test/compat_test.go | 0 p2p/{host => protocol}/circuitv2/test/e2e_test.go | 0 p2p/{host => protocol}/circuitv2/test/empty.go | 0 p2p/{host => protocol}/circuitv2/util/io.go | 0 p2p/{host => protocol}/circuitv2/util/ma.go | 0 p2p/{host => protocol}/circuitv2/util/pbconv.go | 0 27 files changed, 0 insertions(+), 0 deletions(-) rename p2p/{host => protocol}/circuitv2/client/client.go (100%) rename p2p/{host => protocol}/circuitv2/client/conn.go (100%) rename p2p/{host => protocol}/circuitv2/client/dial.go (100%) rename p2p/{host => protocol}/circuitv2/client/handlers.go (100%) rename p2p/{host => protocol}/circuitv2/client/listen.go (100%) rename p2p/{host => protocol}/circuitv2/client/reservation.go (100%) rename p2p/{host => protocol}/circuitv2/client/transport.go (100%) rename p2p/{host => protocol}/circuitv2/pb/Makefile (100%) rename p2p/{host => protocol}/circuitv2/pb/circuit.pb.go (100%) rename p2p/{host => protocol}/circuitv2/pb/circuit.proto (100%) rename p2p/{host => protocol}/circuitv2/pb/voucher.pb.go (100%) rename p2p/{host => protocol}/circuitv2/pb/voucher.proto (100%) rename p2p/{host => protocol}/circuitv2/proto/protocol.go (100%) rename p2p/{host => protocol}/circuitv2/proto/voucher.go (100%) rename p2p/{host => protocol}/circuitv2/proto/voucher_test.go (100%) rename p2p/{host => protocol}/circuitv2/relay/acl.go (100%) rename p2p/{host => protocol}/circuitv2/relay/constraints.go (100%) rename p2p/{host => protocol}/circuitv2/relay/constraints_test.go (100%) rename p2p/{host => protocol}/circuitv2/relay/options.go (100%) rename p2p/{host => protocol}/circuitv2/relay/relay.go (100%) rename p2p/{host => protocol}/circuitv2/relay/resources.go (100%) rename p2p/{host => protocol}/circuitv2/test/compat_test.go (100%) rename p2p/{host => protocol}/circuitv2/test/e2e_test.go (100%) rename p2p/{host => protocol}/circuitv2/test/empty.go (100%) rename p2p/{host => protocol}/circuitv2/util/io.go (100%) rename p2p/{host => protocol}/circuitv2/util/ma.go (100%) rename p2p/{host => protocol}/circuitv2/util/pbconv.go (100%) diff --git a/p2p/host/circuitv2/client/client.go b/p2p/protocol/circuitv2/client/client.go similarity index 100% rename from p2p/host/circuitv2/client/client.go rename to p2p/protocol/circuitv2/client/client.go diff --git a/p2p/host/circuitv2/client/conn.go b/p2p/protocol/circuitv2/client/conn.go similarity index 100% rename from p2p/host/circuitv2/client/conn.go rename to p2p/protocol/circuitv2/client/conn.go diff --git a/p2p/host/circuitv2/client/dial.go b/p2p/protocol/circuitv2/client/dial.go similarity index 100% rename from p2p/host/circuitv2/client/dial.go rename to p2p/protocol/circuitv2/client/dial.go diff --git a/p2p/host/circuitv2/client/handlers.go b/p2p/protocol/circuitv2/client/handlers.go similarity index 100% rename from p2p/host/circuitv2/client/handlers.go rename to p2p/protocol/circuitv2/client/handlers.go diff --git a/p2p/host/circuitv2/client/listen.go b/p2p/protocol/circuitv2/client/listen.go similarity index 100% rename from p2p/host/circuitv2/client/listen.go rename to p2p/protocol/circuitv2/client/listen.go diff --git a/p2p/host/circuitv2/client/reservation.go b/p2p/protocol/circuitv2/client/reservation.go similarity index 100% rename from p2p/host/circuitv2/client/reservation.go rename to p2p/protocol/circuitv2/client/reservation.go diff --git a/p2p/host/circuitv2/client/transport.go b/p2p/protocol/circuitv2/client/transport.go similarity index 100% rename from p2p/host/circuitv2/client/transport.go rename to p2p/protocol/circuitv2/client/transport.go diff --git a/p2p/host/circuitv2/pb/Makefile b/p2p/protocol/circuitv2/pb/Makefile similarity index 100% rename from p2p/host/circuitv2/pb/Makefile rename to p2p/protocol/circuitv2/pb/Makefile diff --git a/p2p/host/circuitv2/pb/circuit.pb.go b/p2p/protocol/circuitv2/pb/circuit.pb.go similarity index 100% rename from p2p/host/circuitv2/pb/circuit.pb.go rename to p2p/protocol/circuitv2/pb/circuit.pb.go diff --git a/p2p/host/circuitv2/pb/circuit.proto b/p2p/protocol/circuitv2/pb/circuit.proto similarity index 100% rename from p2p/host/circuitv2/pb/circuit.proto rename to p2p/protocol/circuitv2/pb/circuit.proto diff --git a/p2p/host/circuitv2/pb/voucher.pb.go b/p2p/protocol/circuitv2/pb/voucher.pb.go similarity index 100% rename from p2p/host/circuitv2/pb/voucher.pb.go rename to p2p/protocol/circuitv2/pb/voucher.pb.go diff --git a/p2p/host/circuitv2/pb/voucher.proto b/p2p/protocol/circuitv2/pb/voucher.proto similarity index 100% rename from p2p/host/circuitv2/pb/voucher.proto rename to p2p/protocol/circuitv2/pb/voucher.proto diff --git a/p2p/host/circuitv2/proto/protocol.go b/p2p/protocol/circuitv2/proto/protocol.go similarity index 100% rename from p2p/host/circuitv2/proto/protocol.go rename to p2p/protocol/circuitv2/proto/protocol.go diff --git a/p2p/host/circuitv2/proto/voucher.go b/p2p/protocol/circuitv2/proto/voucher.go similarity index 100% rename from p2p/host/circuitv2/proto/voucher.go rename to p2p/protocol/circuitv2/proto/voucher.go diff --git a/p2p/host/circuitv2/proto/voucher_test.go b/p2p/protocol/circuitv2/proto/voucher_test.go similarity index 100% rename from p2p/host/circuitv2/proto/voucher_test.go rename to p2p/protocol/circuitv2/proto/voucher_test.go diff --git a/p2p/host/circuitv2/relay/acl.go b/p2p/protocol/circuitv2/relay/acl.go similarity index 100% rename from p2p/host/circuitv2/relay/acl.go rename to p2p/protocol/circuitv2/relay/acl.go diff --git a/p2p/host/circuitv2/relay/constraints.go b/p2p/protocol/circuitv2/relay/constraints.go similarity index 100% rename from p2p/host/circuitv2/relay/constraints.go rename to p2p/protocol/circuitv2/relay/constraints.go diff --git a/p2p/host/circuitv2/relay/constraints_test.go b/p2p/protocol/circuitv2/relay/constraints_test.go similarity index 100% rename from p2p/host/circuitv2/relay/constraints_test.go rename to p2p/protocol/circuitv2/relay/constraints_test.go diff --git a/p2p/host/circuitv2/relay/options.go b/p2p/protocol/circuitv2/relay/options.go similarity index 100% rename from p2p/host/circuitv2/relay/options.go rename to p2p/protocol/circuitv2/relay/options.go diff --git a/p2p/host/circuitv2/relay/relay.go b/p2p/protocol/circuitv2/relay/relay.go similarity index 100% rename from p2p/host/circuitv2/relay/relay.go rename to p2p/protocol/circuitv2/relay/relay.go diff --git a/p2p/host/circuitv2/relay/resources.go b/p2p/protocol/circuitv2/relay/resources.go similarity index 100% rename from p2p/host/circuitv2/relay/resources.go rename to p2p/protocol/circuitv2/relay/resources.go diff --git a/p2p/host/circuitv2/test/compat_test.go b/p2p/protocol/circuitv2/test/compat_test.go similarity index 100% rename from p2p/host/circuitv2/test/compat_test.go rename to p2p/protocol/circuitv2/test/compat_test.go diff --git a/p2p/host/circuitv2/test/e2e_test.go b/p2p/protocol/circuitv2/test/e2e_test.go similarity index 100% rename from p2p/host/circuitv2/test/e2e_test.go rename to p2p/protocol/circuitv2/test/e2e_test.go diff --git a/p2p/host/circuitv2/test/empty.go b/p2p/protocol/circuitv2/test/empty.go similarity index 100% rename from p2p/host/circuitv2/test/empty.go rename to p2p/protocol/circuitv2/test/empty.go diff --git a/p2p/host/circuitv2/util/io.go b/p2p/protocol/circuitv2/util/io.go similarity index 100% rename from p2p/host/circuitv2/util/io.go rename to p2p/protocol/circuitv2/util/io.go diff --git a/p2p/host/circuitv2/util/ma.go b/p2p/protocol/circuitv2/util/ma.go similarity index 100% rename from p2p/host/circuitv2/util/ma.go rename to p2p/protocol/circuitv2/util/ma.go diff --git a/p2p/host/circuitv2/util/pbconv.go b/p2p/protocol/circuitv2/util/pbconv.go similarity index 100% rename from p2p/host/circuitv2/util/pbconv.go rename to p2p/protocol/circuitv2/util/pbconv.go From 88f48021e55838293737ffddcdc5f182e1dc4b6f Mon Sep 17 00:00:00 2001 From: vyzo Date: Fri, 10 Sep 2021 11:23:59 +0300 Subject: [PATCH 2/6] update circuitv2 imports --- p2p/protocol/circuitv2/client/client.go | 2 +- p2p/protocol/circuitv2/client/dial.go | 6 +++--- p2p/protocol/circuitv2/client/handlers.go | 4 ++-- p2p/protocol/circuitv2/client/reservation.go | 6 +++--- p2p/protocol/circuitv2/proto/voucher.go | 2 +- p2p/protocol/circuitv2/relay/relay.go | 6 +++--- p2p/protocol/circuitv2/test/e2e_test.go | 4 ++-- p2p/protocol/circuitv2/util/pbconv.go | 2 +- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/p2p/protocol/circuitv2/client/client.go b/p2p/protocol/circuitv2/client/client.go index 60d7e758f4..e6ee5a4b94 100644 --- a/p2p/protocol/circuitv2/client/client.go +++ b/p2p/protocol/circuitv2/client/client.go @@ -4,7 +4,7 @@ import ( "context" "sync" - "github.com/libp2p/go-libp2p/p2p/host/circuitv2/proto" + "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/proto" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" diff --git a/p2p/protocol/circuitv2/client/dial.go b/p2p/protocol/circuitv2/client/dial.go index 535521fdc2..0dd9d875fc 100644 --- a/p2p/protocol/circuitv2/client/dial.go +++ b/p2p/protocol/circuitv2/client/dial.go @@ -6,9 +6,9 @@ import ( "time" pbv1 "github.com/libp2p/go-libp2p-circuit/pb" - pbv2 "github.com/libp2p/go-libp2p/p2p/host/circuitv2/pb" - "github.com/libp2p/go-libp2p/p2p/host/circuitv2/proto" - "github.com/libp2p/go-libp2p/p2p/host/circuitv2/util" + pbv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/pb" + "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/proto" + "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/util" "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/peer" diff --git a/p2p/protocol/circuitv2/client/handlers.go b/p2p/protocol/circuitv2/client/handlers.go index 78704846b2..bda2fcc1c2 100644 --- a/p2p/protocol/circuitv2/client/handlers.go +++ b/p2p/protocol/circuitv2/client/handlers.go @@ -4,8 +4,8 @@ import ( "time" pbv1 "github.com/libp2p/go-libp2p-circuit/pb" - pbv2 "github.com/libp2p/go-libp2p/p2p/host/circuitv2/pb" - "github.com/libp2p/go-libp2p/p2p/host/circuitv2/util" + pbv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/pb" + "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/util" "github.com/libp2p/go-libp2p-core/network" ) diff --git a/p2p/protocol/circuitv2/client/reservation.go b/p2p/protocol/circuitv2/client/reservation.go index 479a147631..36185ecdef 100644 --- a/p2p/protocol/circuitv2/client/reservation.go +++ b/p2p/protocol/circuitv2/client/reservation.go @@ -5,9 +5,9 @@ import ( "fmt" "time" - pbv2 "github.com/libp2p/go-libp2p/p2p/host/circuitv2/pb" - "github.com/libp2p/go-libp2p/p2p/host/circuitv2/proto" - "github.com/libp2p/go-libp2p/p2p/host/circuitv2/util" + pbv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/pb" + "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/proto" + "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/util" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" diff --git a/p2p/protocol/circuitv2/proto/voucher.go b/p2p/protocol/circuitv2/proto/voucher.go index 6b4c24c8ac..fe8e3aeece 100644 --- a/p2p/protocol/circuitv2/proto/voucher.go +++ b/p2p/protocol/circuitv2/proto/voucher.go @@ -3,7 +3,7 @@ package proto import ( "time" - pbv2 "github.com/libp2p/go-libp2p/p2p/host/circuitv2/pb" + pbv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/pb" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/record" diff --git a/p2p/protocol/circuitv2/relay/relay.go b/p2p/protocol/circuitv2/relay/relay.go index 8ddddd1774..bc52dd890f 100644 --- a/p2p/protocol/circuitv2/relay/relay.go +++ b/p2p/protocol/circuitv2/relay/relay.go @@ -8,9 +8,9 @@ import ( "sync/atomic" "time" - pbv2 "github.com/libp2p/go-libp2p/p2p/host/circuitv2/pb" - "github.com/libp2p/go-libp2p/p2p/host/circuitv2/proto" - "github.com/libp2p/go-libp2p/p2p/host/circuitv2/util" + pbv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/pb" + "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/proto" + "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/util" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/network" diff --git a/p2p/protocol/circuitv2/test/e2e_test.go b/p2p/protocol/circuitv2/test/e2e_test.go index b822f913d1..cf4c9bd2b9 100644 --- a/p2p/protocol/circuitv2/test/e2e_test.go +++ b/p2p/protocol/circuitv2/test/e2e_test.go @@ -9,8 +9,8 @@ import ( "testing" "time" - "github.com/libp2p/go-libp2p/p2p/host/circuitv2/client" - "github.com/libp2p/go-libp2p/p2p/host/circuitv2/relay" + "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client" + "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay" "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/host" diff --git a/p2p/protocol/circuitv2/util/pbconv.go b/p2p/protocol/circuitv2/util/pbconv.go index 63074acc1a..923b2226cb 100644 --- a/p2p/protocol/circuitv2/util/pbconv.go +++ b/p2p/protocol/circuitv2/util/pbconv.go @@ -4,7 +4,7 @@ import ( "errors" pbv1 "github.com/libp2p/go-libp2p-circuit/pb" - pbv2 "github.com/libp2p/go-libp2p/p2p/host/circuitv2/pb" + pbv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/pb" "github.com/libp2p/go-libp2p-core/peer" From 1868e2cea6be32e15fef03a2ce10894a224cb1b6 Mon Sep 17 00:00:00 2001 From: vyzo Date: Fri, 10 Sep 2021 11:48:12 +0300 Subject: [PATCH 3/6] RIP circuit v2; use circuitv2 --- config/config.go | 22 ++++------------------ options.go | 25 ++++++++----------------- 2 files changed, 12 insertions(+), 35 deletions(-) diff --git a/config/config.go b/config/config.go index 48df4d20a8..bbb3914e09 100644 --- a/config/config.go +++ b/config/config.go @@ -21,11 +21,11 @@ import ( bhost "github.com/libp2p/go-libp2p/p2p/host/basic" "github.com/libp2p/go-libp2p/p2p/host/relay" routed "github.com/libp2p/go-libp2p/p2p/host/routed" + circuitv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client" holepunch "github.com/libp2p/go-libp2p/p2p/protocol/holepunch" autonat "github.com/libp2p/go-libp2p-autonat" blankhost "github.com/libp2p/go-libp2p-blankhost" - circuit "github.com/libp2p/go-libp2p-circuit" discovery "github.com/libp2p/go-libp2p-discovery" swarm "github.com/libp2p/go-libp2p-swarm" tptu "github.com/libp2p/go-libp2p-transport-upgrader" @@ -76,7 +76,6 @@ type Config struct { RelayCustom bool Relay bool - RelayOpts []circuit.RelayOpt ListenAddrs []ma.Multiaddr AddrsFactory bhost.AddrsFactory @@ -173,7 +172,7 @@ func (cfg *Config) addTransports(ctx context.Context, h host.Host) (err error) { } if cfg.Relay { - err := circuit.AddRelayTransport(ctx, h, upgrader, cfg.RelayOpts...) + err := circuitv2.AddTransport(ctx, h, upgrader) if err != nil { h.Close() return err @@ -259,15 +258,7 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) { return nil, fmt.Errorf("cannot enable autorelay; relay is not enabled") } - hop := false - for _, opt := range cfg.RelayOpts { - if opt == circuit.OptHop { - hop = true - break - } - } - - if !hop && len(cfg.StaticRelays) > 0 { + if len(cfg.StaticRelays) > 0 { _ = relay.NewAutoRelay(ctx, h, nil, router, cfg.StaticRelays) } else { if router == nil { @@ -283,12 +274,7 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) { discovery := discovery.NewRoutingDiscovery(crouter) - if hop { - // advertise ourselves - relay.Advertise(ctx, discovery) - } else { - _ = relay.NewAutoRelay(ctx, h, discovery, router, cfg.StaticRelays) - } + _ = relay.NewAutoRelay(ctx, h, discovery, router, cfg.StaticRelays) } } diff --git a/options.go b/options.go index 75549d34e5..b82236c0df 100644 --- a/options.go +++ b/options.go @@ -8,7 +8,6 @@ import ( "fmt" "time" - circuit "github.com/libp2p/go-libp2p-circuit" "github.com/libp2p/go-libp2p-core/connmgr" "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/metrics" @@ -210,17 +209,15 @@ func AddrsFactory(factory config.AddrsFactory) Option { } } -// EnableRelay configures libp2p to enable the relay transport with -// configuration options. By default, this option only configures libp2p to -// accept inbound connections from relays and make outbound connections -// _through_ relays when requested by the remote peer. (default: enabled) -// -// To _act_ as a relay, pass the circuit.OptHop option. -func EnableRelay(options ...circuit.RelayOpt) Option { +// EnableRelay configures libp2p to enable the relay transport. +// This option only configures libp2p to accept inbound connections from relays +// and make outbound connections_through_ relays when requested by the remote peer. +// This option supports both circuit v1 and v2 connections. +// (default: enabled) +func EnableRelay() Option { return func(cfg *Config) error { cfg.RelayCustom = true cfg.Relay = true - cfg.RelayOpts = options return nil } } @@ -240,14 +237,8 @@ func DisableRelay() Option { // * Relay (enabled by default) // * Routing (to find relays), or StaticRelays/DefaultStaticRelays. // -// This subsystem performs two functions: -// -// 1. When this libp2p node is configured to act as a relay "hop" -// (circuit.OptHop is passed to EnableRelay), this node will advertise itself -// as a public relay using the provided routing system. -// 2. When this libp2p node is _not_ configured as a relay "hop", it will -// automatically detect if it is unreachable (e.g., behind a NAT). If so, it will -// find, configure, and announce a set of public relays. +// This subsystem performs automatic address rewriting to advertise relay addresses when it +// detects that the node is publicly unreachable (e.g. behind a NAT). func EnableAutoRelay() Option { return func(cfg *Config) error { cfg.EnableAutoRelay = true From 1014608e92ec22f53050a97b42771a57ac626f6a Mon Sep 17 00:00:00 2001 From: vyzo Date: Fri, 10 Sep 2021 11:48:31 +0300 Subject: [PATCH 4/6] fix autorelay test --- p2p/host/relay/autorelay_test.go | 59 ++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/p2p/host/relay/autorelay_test.go b/p2p/host/relay/autorelay_test.go index 1b466245e4..dd3b632d44 100644 --- a/p2p/host/relay/autorelay_test.go +++ b/p2p/host/relay/autorelay_test.go @@ -11,13 +11,15 @@ import ( "github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p/p2p/host/relay" - circuit "github.com/libp2p/go-libp2p-circuit" "github.com/libp2p/go-libp2p-core/event" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/routing" + circuit "github.com/libp2p/go-libp2p-circuit" + discovery "github.com/libp2p/go-libp2p-discovery" + "github.com/ipfs/go-cid" ma "github.com/multiformats/go-multiaddr" manet "github.com/multiformats/go-multiaddr/net" @@ -120,21 +122,19 @@ func TestAutoRelay(t *testing.T) { defer cancel() mtab := newMockRoutingTable() - makeRouting := func(h host.Host) (routing.PeerRouting, error) { + makeRouting := func(h host.Host) (*mockRouting, error) { mr := newMockRouting(h, mtab) return mr, nil } - - h1, err := libp2p.New(ctx, libp2p.EnableRelay()) - if err != nil { - t.Fatal(err) + makePeerRouting := func(h host.Host) (routing.PeerRouting, error) { + return makeRouting(h) } + + // this is the relay host // announce dns addrs because filter out private addresses from relays, // and we consider dns addresses "public". - _, err = libp2p.New(ctx, - libp2p.EnableRelay(circuit.OptHop), - libp2p.EnableAutoRelay(), - libp2p.Routing(makeRouting), + relayHost, err := libp2p.New(ctx, + libp2p.DisableRelay(), libp2p.AddrsFactory(func(addrs []ma.Multiaddr) []ma.Multiaddr { for i, addr := range addrs { saddr := addr.String() @@ -148,17 +148,38 @@ func TestAutoRelay(t *testing.T) { if err != nil { t.Fatal(err) } - h3, err := libp2p.New(ctx, libp2p.EnableRelay(), libp2p.EnableAutoRelay(), libp2p.Routing(makeRouting)) + + // instantiate the relay + _, err = circuit.NewRelay(ctx, relayHost, nil, circuit.OptHop) + if err != nil { + t.Fatal(err) + } + + // advertise the relay + relayRouting, err := makeRouting(relayHost) + if err != nil { + t.Fatal(err) + } + relayDiscovery := discovery.NewRoutingDiscovery(relayRouting) + relay.Advertise(ctx, relayDiscovery) + + // the client hosts + h1, err := libp2p.New(ctx, libp2p.EnableRelay()) + if err != nil { + t.Fatal(err) + } + + h2, err := libp2p.New(ctx, libp2p.EnableRelay(), libp2p.EnableAutoRelay(), libp2p.Routing(makePeerRouting)) if err != nil { t.Fatal(err) } - h4, err := libp2p.New(ctx, libp2p.EnableRelay()) + h3, err := libp2p.New(ctx, libp2p.EnableRelay()) if err != nil { t.Fatal(err) } // verify that we don't advertise relay addrs initially - for _, addr := range h3.Addrs() { + for _, addr := range h2.Addrs() { _, err := addr.ValueForProtocol(ma.P_CIRCUIT) if err == nil { t.Fatal("relay addr advertised before auto detection") @@ -166,9 +187,9 @@ func TestAutoRelay(t *testing.T) { } // connect to AutoNAT, have it resolve to private. - connect(t, h1, h3) + connect(t, h1, h2) time.Sleep(300 * time.Millisecond) - privEmitter, _ := h3.EventBus().Emitter(new(event.EvtLocalReachabilityChanged)) + privEmitter, _ := h2.EventBus().Emitter(new(event.EvtLocalReachabilityChanged)) privEmitter.Emit(event.EvtLocalReachabilityChanged{Reachability: network.ReachabilityPrivate}) // Wait for detection to do its magic time.Sleep(3000 * time.Millisecond) @@ -180,7 +201,7 @@ func TestAutoRelay(t *testing.T) { } haveRelay := false - for _, addr := range h3.Addrs() { + for _, addr := range h2.Addrs() { if addr.Equal(unspecificRelay) { t.Fatal("unspecific relay addr advertised") } @@ -197,21 +218,21 @@ func TestAutoRelay(t *testing.T) { // verify that we can connect through the relay var raddrs []ma.Multiaddr - for _, addr := range h3.Addrs() { + for _, addr := range h2.Addrs() { _, err := addr.ValueForProtocol(ma.P_CIRCUIT) if err == nil { raddrs = append(raddrs, addr) } } - err = h4.Connect(ctx, peer.AddrInfo{ID: h3.ID(), Addrs: raddrs}) + err = h3.Connect(ctx, peer.AddrInfo{ID: h2.ID(), Addrs: raddrs}) if err != nil { t.Fatal(err) } // verify that we have pushed relay addrs to connected peers haveRelay = false - for _, addr := range h1.Peerstore().Addrs(h3.ID()) { + for _, addr := range h1.Peerstore().Addrs(h2.ID()) { if addr.Equal(unspecificRelay) { t.Fatal("unspecific relay addr advertised") } From ef67c340fb6916f71c658aebc740d0d0bb05c4a3 Mon Sep 17 00:00:00 2001 From: vyzo Date: Fri, 10 Sep 2021 11:55:54 +0300 Subject: [PATCH 5/6] fix holepunch test --- p2p/protocol/holepunch/coordination_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/p2p/protocol/holepunch/coordination_test.go b/p2p/protocol/holepunch/coordination_test.go index 8b33206308..c314e17b30 100644 --- a/p2p/protocol/holepunch/coordination_test.go +++ b/p2p/protocol/holepunch/coordination_test.go @@ -324,9 +324,13 @@ func makeRelayedHosts(t *testing.T, h1Opt holepunch.Option, addHolePuncher bool) var err error relay, err = libp2p.New(context.Background(), libp2p.ListenAddrs(ma.StringCast("/ip4/127.0.0.1/tcp/0"), ma.StringCast("/ip6/::1/tcp/0")), - libp2p.EnableRelay(circuit.OptHop), + libp2p.DisableRelay(), ) require.NoError(t, err) + + _, err = circuit.NewRelay(context.Background(), relay, nil, circuit.OptHop) + require.NoError(t, err) + h2 = mkHostWithStaticAutoRelay(t, context.Background(), relay) if addHolePuncher { hps = addHolePunchService(t, h2) From fe8c7ba49726041c2793e98be295911d3d973210 Mon Sep 17 00:00:00 2001 From: vyzo Date: Fri, 10 Sep 2021 12:02:41 +0300 Subject: [PATCH 6/6] fix relay example --- examples/relay/main.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/examples/relay/main.go b/examples/relay/main.go index 3df5c48ab3..daf655b129 100644 --- a/examples/relay/main.go +++ b/examples/relay/main.go @@ -30,11 +30,16 @@ func run() { // Tell the host to relay connections for other peers (The ability to *use* // a relay vs the ability to *be* a relay) - h2, err := libp2p.New(context.Background(), libp2p.EnableRelay(circuit.OptHop)) + h2, err := libp2p.New(context.Background(), libp2p.DisableRelay()) if err != nil { log.Printf("Failed to create h2: %v", err) return } + _, err = circuit.NewRelay(context.Background(), h2, nil, circuit.OptHop) + if err != nil { + log.Printf("Failed to instantiate h2 relay: %v", err) + return + } // Zero out the listen addresses for the host, so it can only communicate // via p2p-circuit for our example