Skip to content

Commit

Permalink
Add some guard rails and docs (#1863)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Nov 9, 2022
1 parent da3adbc commit 0575c19
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ func EnableRelayService(opts ...relayv2.Option) Option {
//
// Dependencies:
// - Relay (enabled by default)
// - Routing (to find relays), or StaticRelays/DefaultStaticRelays.
// - Either:
// 1. A list of static relays
// 2. A PeerSource function that provides a chan of relays. See `autorelay.WithPeerSource`
//
// This subsystem performs automatic address rewriting to advertise relay addresses when it
// detects that the node is publicly unreachable (e.g. behind a NAT).
Expand Down
12 changes: 12 additions & 0 deletions p2p/host/autorelay/autorelay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,15 @@ func TestMaxAge(t *testing.T) {
}
require.Contains(t, ids, relays[0])
}

func TestIncorrectInit(t *testing.T) {
// Check if we panic if we do not correctly initialize the autorelay system.
// Common since it's easy to initialize without passing in the correct options: https://github.com/libp2p/go-libp2p/issues/1852

defer func() {
if r := recover(); r == nil {
t.Errorf("Expected to panic")
}
}()
_ = newPrivateNode(t)
}
4 changes: 4 additions & 0 deletions p2p/host/autorelay/relay_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ type relayFinder struct {
}

func newRelayFinder(host *basic.BasicHost, peerSource func(context.Context, int) <-chan peer.AddrInfo, conf *config) *relayFinder {
if peerSource == nil && len(conf.staticRelays) == 0 {
panic("Can not create a new relayFinder. Need a Peer Source fn or a list of static relays. Refer to the documentation around `libp2p.EnableAutoRelay`")
}

return &relayFinder{
bootTime: conf.clock.Now(),
host: host,
Expand Down

0 comments on commit 0575c19

Please sign in to comment.