Skip to content

Commit

Permalink
Use circuitv2 code (#1183)
Browse files Browse the repository at this point in the history
* move circuitv2 to p2p/protocol

* update circuitv2 imports

* RIP circuit v2; use circuitv2

* fix autorelay test

* fix holepunch test

* fix relay example
  • Loading branch information
vyzo authored Sep 10, 2021
1 parent 0c5978b commit 79d9954
Show file tree
Hide file tree
Showing 32 changed files with 79 additions and 72 deletions.
22 changes: 4 additions & 18 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -76,7 +76,6 @@ type Config struct {

RelayCustom bool
Relay bool
RelayOpts []circuit.RelayOpt

ListenAddrs []ma.Multiaddr
AddrsFactory bhost.AddrsFactory
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}

Expand Down
7 changes: 6 additions & 1 deletion examples/relay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 8 additions & 17 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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
Expand Down
59 changes: 40 additions & 19 deletions p2p/host/relay/autorelay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -148,27 +148,48 @@ 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")
}
}

// 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)
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 5 additions & 1 deletion p2p/protocol/holepunch/coordination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 79d9954

Please sign in to comment.