Skip to content

Commit

Permalink
Merge pull request #50 from libp2p/feat/optional-discovery
Browse files Browse the repository at this point in the history
make relay discovery optional (disabled by default)
  • Loading branch information
Stebalien authored Oct 18, 2018
2 parents b8a9309 + 1d7a907 commit 616c88b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
17 changes: 12 additions & 5 deletions p2p/protocol/internal/circuitv1-deprecated/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ type Relay struct {
ctx context.Context
self peer.ID

active bool
hop bool
active bool
hop bool
discovery bool

incoming chan *Conn

Expand All @@ -49,8 +50,9 @@ type Relay struct {
type RelayOpt int

var (
OptActive = RelayOpt(0)
OptHop = RelayOpt(1)
OptActive = RelayOpt(0)
OptHop = RelayOpt(1)
OptDiscovery = RelayOpt(2)
)

type RelayError struct {
Expand Down Expand Up @@ -78,13 +80,18 @@ func NewRelay(ctx context.Context, h host.Host, upgrader *tptu.Upgrader, opts ..
r.active = true
case OptHop:
r.hop = true
case OptDiscovery:
r.discovery = true
default:
return nil, fmt.Errorf("unrecognized option: %d", opt)
}
}

h.SetStreamHandler(ProtoID, r.handleNewStream)
h.Network().Notify(r.Notifiee())

if r.discovery {
h.Network().Notify(r.Notifiee())
}

return r, nil
}
Expand Down
14 changes: 7 additions & 7 deletions p2p/protocol/internal/circuitv1-deprecated/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestBasicRelay(t *testing.T) {

time.Sleep(10 * time.Millisecond)

r1 := newTestRelay(t, ctx, hosts[0])
r1 := newTestRelay(t, ctx, hosts[0], OptDiscovery)

newTestRelay(t, ctx, hosts[1], OptHop)

Expand Down Expand Up @@ -122,7 +122,7 @@ func TestRelayReset(t *testing.T) {

time.Sleep(10 * time.Millisecond)

r1 := newTestRelay(t, ctx, hosts[0])
r1 := newTestRelay(t, ctx, hosts[0], OptDiscovery)

newTestRelay(t, ctx, hosts[1], OptHop)

Expand Down Expand Up @@ -181,7 +181,7 @@ func TestBasicRelayDial(t *testing.T) {

time.Sleep(10 * time.Millisecond)

r1 := newTestRelay(t, ctx, hosts[0])
r1 := newTestRelay(t, ctx, hosts[0], OptDiscovery)

_ = newTestRelay(t, ctx, hosts[1], OptHop)
r3 := newTestRelay(t, ctx, hosts[2])
Expand Down Expand Up @@ -230,7 +230,7 @@ func TestUnspecificRelayDial(t *testing.T) {

hosts := getNetHosts(t, ctx, 3)

r1 := newTestRelay(t, ctx, hosts[0])
r1 := newTestRelay(t, ctx, hosts[0], OptDiscovery)

newTestRelay(t, ctx, hosts[1], OptHop)

Expand Down Expand Up @@ -290,7 +290,7 @@ func TestRelayThroughNonHop(t *testing.T) {

time.Sleep(10 * time.Millisecond)

r1 := newTestRelay(t, ctx, hosts[0])
r1 := newTestRelay(t, ctx, hosts[0], OptDiscovery)

newTestRelay(t, ctx, hosts[1])

Expand Down Expand Up @@ -327,7 +327,7 @@ func TestRelayNoDestConnection(t *testing.T) {

time.Sleep(10 * time.Millisecond)

r1 := newTestRelay(t, ctx, hosts[0])
r1 := newTestRelay(t, ctx, hosts[0], OptDiscovery)

newTestRelay(t, ctx, hosts[1], OptHop)

Expand Down Expand Up @@ -362,7 +362,7 @@ func TestActiveRelay(t *testing.T) {

time.Sleep(10 * time.Millisecond)

r1 := newTestRelay(t, ctx, hosts[0])
r1 := newTestRelay(t, ctx, hosts[0], OptDiscovery)

newTestRelay(t, ctx, hosts[1], OptHop, OptActive)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var msg = []byte("relay works!")
func testSetupRelay(t *testing.T, ctx context.Context) []host.Host {
hosts := getNetHosts(t, ctx, 3)

err := AddRelayTransport(ctx, hosts[0], swarmt.GenUpgrader(hosts[0].Network().(*swarm.Swarm)))
err := AddRelayTransport(ctx, hosts[0], swarmt.GenUpgrader(hosts[0].Network().(*swarm.Swarm)), OptDiscovery)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 616c88b

Please sign in to comment.