diff --git a/options.go b/options.go index 3509c61d4c..5d54fe751d 100644 --- a/options.go +++ b/options.go @@ -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). diff --git a/p2p/host/autorelay/autorelay_test.go b/p2p/host/autorelay/autorelay_test.go index e7ac6d99a6..bbdec41d8f 100644 --- a/p2p/host/autorelay/autorelay_test.go +++ b/p2p/host/autorelay/autorelay_test.go @@ -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) +} diff --git a/p2p/host/autorelay/relay_finder.go b/p2p/host/autorelay/relay_finder.go index 4760511bce..533c748b42 100644 --- a/p2p/host/autorelay/relay_finder.go +++ b/p2p/host/autorelay/relay_finder.go @@ -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,