Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autorelay: Split libp2p.EnableAutoRelay into 2 functions #2022

Merged
merged 5 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package libp2p
// those are in defaults.go).

import (
"context"
"crypto/rand"
"encoding/binary"
"errors"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/metrics"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/core/pnet"
"github.com/libp2p/go-libp2p/core/protocol"
Expand Down Expand Up @@ -307,6 +309,8 @@ func EnableRelayService(opts ...relayv2.Option) Option {
//
// This subsystem performs automatic address rewriting to advertise relay addresses when it
// detects that the node is publicly unreachable (e.g. behind a NAT).
//
// Deprecated: Use EnableAutoRelayWithStaticRelays or EnableAutoRelayWithPeerSource
func EnableAutoRelay(opts ...autorelay.Option) Option {
return func(cfg *Config) error {
cfg.EnableAutoRelay = true
Expand All @@ -315,6 +319,31 @@ func EnableAutoRelay(opts ...autorelay.Option) Option {
}
}

// EnableAutoRelayWithStaticRelays configures libp2p to enable the AutoRelay subsystem using
// the provided slice of relays as relay candidates
sukunrt marked this conversation as resolved.
Show resolved Hide resolved
// 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 EnableAutoRelayWithStaticRelays(static []peer.AddrInfo, opts ...autorelay.Option) Option {
return func(cfg *Config) error {
cfg.EnableAutoRelay = true
cfg.AutoRelayOpts = append([]autorelay.Option{autorelay.WithStaticRelays(static)}, opts...)
return nil
}
}

// EnableAutoRelayWithPeerSource configures libp2p to enable the AutoRelay subsystem using
// the provided peerSource callback to get more relay candidates
sukunrt marked this conversation as resolved.
Show resolved Hide resolved
// 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 EnableAutoRelayWithPeerSource(peerSource func(context.Context, int) <-chan peer.AddrInfo,
opts ...autorelay.Option) Option {
return func(cfg *Config) error {
cfg.EnableAutoRelay = true
cfg.AutoRelayOpts = append([]autorelay.Option{autorelay.WithPeerSource(peerSource, 30*time.Second)}, opts...)
return nil
}
}

// ForceReachabilityPublic overrides automatic reachability detection in the AutoNAT subsystem,
// forcing the local node to believe it is reachable externally.
func ForceReachabilityPublic() Option {
Expand Down
106 changes: 79 additions & 27 deletions p2p/host/autorelay/autorelay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ func newPrivateNode(t *testing.T, opts ...autorelay.Option) host.Host {
return h
}

func newPrivateNodeWithPeerSource(t *testing.T, peerSource func(context.Context, int) <-chan peer.AddrInfo,
opts ...autorelay.Option) host.Host {
t.Helper()
h, err := libp2p.New(
libp2p.ForceReachabilityPrivate(),
libp2p.EnableAutoRelayWithPeerSource(peerSource, opts...),
)
require.NoError(t, err)
return h
}

func newPrivateNodeWithStaticRelays(t *testing.T, static []peer.AddrInfo, opts ...autorelay.Option) host.Host {
t.Helper()
h, err := libp2p.New(
libp2p.ForceReachabilityPrivate(),
libp2p.EnableAutoRelayWithStaticRelays(static, opts...),
)
require.NoError(t, err)
return h
}

func newRelay(t *testing.T) host.Host {
t.Helper()
h, err := libp2p.New(
Expand Down Expand Up @@ -116,8 +137,8 @@ func newRelayV1(t *testing.T) host.Host {

func TestSingleCandidate(t *testing.T) {
var counter int
h := newPrivateNode(t,
autorelay.WithPeerSource(func(_ context.Context, num int) <-chan peer.AddrInfo {
h := newPrivateNodeWithPeerSource(t,
func(_ context.Context, num int) <-chan peer.AddrInfo {
counter++
require.Equal(t, 1, num)
peerChan := make(chan peer.AddrInfo, num)
Expand All @@ -126,10 +147,11 @@ func TestSingleCandidate(t *testing.T) {
t.Cleanup(func() { r.Close() })
peerChan <- peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()}
return peerChan
}, time.Hour),
},
autorelay.WithMaxCandidates(1),
autorelay.WithNumRelays(99999),
autorelay.WithBootDelay(0),
autorelay.WithMinInterval(time.Hour),
)
defer h.Close()

Expand All @@ -150,16 +172,17 @@ func TestSingleRelay(t *testing.T) {
}
close(peerChan)

h := newPrivateNode(t,
autorelay.WithPeerSource(func(_ context.Context, num int) <-chan peer.AddrInfo {
h := newPrivateNodeWithPeerSource(t,
func(_ context.Context, num int) <-chan peer.AddrInfo {
require.False(t, called, "expected the peer source callback to only have been called once")
called = true
require.Equal(t, numCandidates, num)
return peerChan
}, time.Hour),
},
autorelay.WithMaxCandidates(numCandidates),
autorelay.WithNumRelays(1),
autorelay.WithBootDelay(0),
autorelay.WithMinInterval(time.Hour),
)
defer h.Close()

Expand All @@ -177,16 +200,17 @@ func TestPreferRelayV2(t *testing.T) {
t.Fatal("used relay v1")
})

h := newPrivateNode(t,
autorelay.WithPeerSource(func(context.Context, int) <-chan peer.AddrInfo {
h := newPrivateNodeWithPeerSource(t,
func(context.Context, int) <-chan peer.AddrInfo {
peerChan := make(chan peer.AddrInfo, 1)
defer close(peerChan)
peerChan <- peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()}
return peerChan
}, time.Hour),
},
autorelay.WithMaxCandidates(1),
autorelay.WithNumRelays(99999),
autorelay.WithBootDelay(0),
autorelay.WithMinInterval(time.Hour),
)
defer h.Close()

Expand All @@ -195,11 +219,12 @@ func TestPreferRelayV2(t *testing.T) {

func TestWaitForCandidates(t *testing.T) {
peerChan := make(chan peer.AddrInfo)
h := newPrivateNode(t,
autorelay.WithPeerSource(func(context.Context, int) <-chan peer.AddrInfo { return peerChan }, time.Hour),
h := newPrivateNodeWithPeerSource(t,
func(context.Context, int) <-chan peer.AddrInfo { return peerChan },
autorelay.WithMinCandidates(2),
autorelay.WithNumRelays(1),
autorelay.WithBootDelay(time.Hour),
autorelay.WithMinInterval(time.Hour),
)
defer h.Close()

Expand Down Expand Up @@ -243,19 +268,20 @@ func TestBackoff(t *testing.T) {
})

var counter int32 // to be used atomically
h := newPrivateNode(t,
autorelay.WithPeerSource(func(context.Context, int) <-chan peer.AddrInfo {
h := newPrivateNodeWithPeerSource(t,
func(context.Context, int) <-chan peer.AddrInfo {
// always return the same node, and make sure we don't try to connect to it too frequently
atomic.AddInt32(&counter, 1)
peerChan := make(chan peer.AddrInfo, 1)
peerChan <- peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()}
close(peerChan)
return peerChan
}, time.Second),
},
autorelay.WithNumRelays(1),
autorelay.WithBootDelay(0),
autorelay.WithBackoff(backoff),
autorelay.WithClock(cl),
autorelay.WithMinInterval(time.Second),
)
defer h.Close()

Expand All @@ -280,8 +306,8 @@ func TestStaticRelays(t *testing.T) {
staticRelays = append(staticRelays, peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()})
}

h := newPrivateNode(t,
autorelay.WithStaticRelays(staticRelays),
h := newPrivateNodeWithStaticRelays(t,
staticRelays,
autorelay.WithNumRelays(1),
)
defer h.Close()
Expand All @@ -297,9 +323,10 @@ func TestRelayV1(t *testing.T) {
peerChan <- peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()}
close(peerChan)

h := newPrivateNode(t,
autorelay.WithPeerSource(func(context.Context, int) <-chan peer.AddrInfo { return peerChan }, time.Hour),
h := newPrivateNodeWithPeerSource(t,
func(context.Context, int) <-chan peer.AddrInfo { return peerChan },
autorelay.WithBootDelay(0),
autorelay.WithMinInterval(time.Hour),
)
defer h.Close()

Expand All @@ -313,10 +340,11 @@ func TestRelayV1(t *testing.T) {
peerChan <- peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()}
close(peerChan)

h := newPrivateNode(t,
autorelay.WithPeerSource(func(context.Context, int) <-chan peer.AddrInfo { return peerChan }, time.Hour),
h := newPrivateNodeWithPeerSource(t,
func(context.Context, int) <-chan peer.AddrInfo { return peerChan },
autorelay.WithBootDelay(0),
autorelay.WithCircuitV1Support(),
autorelay.WithMinInterval(time.Hour),
)
defer h.Close()

Expand All @@ -337,12 +365,13 @@ func TestConnectOnDisconnect(t *testing.T) {
peerChan <- peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()}
relays = append(relays, r)
}
h := newPrivateNode(t,
autorelay.WithPeerSource(func(context.Context, int) <-chan peer.AddrInfo { return peerChan }, time.Hour),
h := newPrivateNodeWithPeerSource(t,
func(context.Context, int) <-chan peer.AddrInfo { return peerChan },
autorelay.WithMinCandidates(1),
autorelay.WithMaxCandidates(num),
autorelay.WithNumRelays(1),
autorelay.WithBootDelay(0),
autorelay.WithMinInterval(time.Hour),
)
defer h.Close()

Expand Down Expand Up @@ -386,19 +415,20 @@ func TestMaxAge(t *testing.T) {
peerChans <- peerChan2
close(peerChans)

h := newPrivateNode(t,
autorelay.WithPeerSource(func(context.Context, int) <-chan peer.AddrInfo {
h := newPrivateNodeWithPeerSource(t,
func(context.Context, int) <-chan peer.AddrInfo {
c, ok := <-peerChans
if !ok {
t.Fatal("unexpected call to PeerSource")
}
return c
}, time.Second),
},
autorelay.WithNumRelays(1),
autorelay.WithMaxCandidates(100),
autorelay.WithBootDelay(0),
autorelay.WithMaxCandidateAge(20*time.Minute),
autorelay.WithClock(cl),
autorelay.WithMinInterval(time.Second),
)
defer h.Close()

Expand Down Expand Up @@ -507,8 +537,8 @@ func TestReconnectToStaticRelays(t *testing.T) {
staticRelays = append(staticRelays, peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()})
}

h := newPrivateNode(t,
autorelay.WithStaticRelays(staticRelays),
h := newPrivateNodeWithStaticRelays(t,
staticRelays,
autorelay.WithClock(cl),
)

Expand All @@ -532,3 +562,25 @@ func TestReconnectToStaticRelays(t *testing.T) {
cl.Add(time.Hour)
expectDeltaInAddrUpdated(t, addrUpdated, -1)
}

func TestMinInterval(t *testing.T) {
h := newPrivateNodeWithPeerSource(t,
func(context.Context, int) <-chan peer.AddrInfo {
peerChan := make(chan peer.AddrInfo, 1)
defer close(peerChan)
r1 := newRelay(t)
t.Cleanup(func() { r1.Close() })
peerChan <- peer.AddrInfo{ID: r1.ID(), Addrs: r1.Addrs()}
return peerChan
},
autorelay.WithMinCandidates(2),
autorelay.WithNumRelays(1),
autorelay.WithBootDelay(time.Hour),
autorelay.WithMinInterval(1*time.Second),
)
defer h.Close()

// The second call to peerSource should happen after 1 second
require.Never(t, func() bool { return numRelays(h) > 0 }, 1*time.Second, 100*time.Millisecond)
require.Eventually(t, func() bool { return numRelays(h) > 0 }, 2*time.Second, 100*time.Millisecond)
}
11 changes: 10 additions & 1 deletion p2p/host/autorelay/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func WithPeerSource(f func(ctx context.Context, numPeers int) <-chan peer.AddrIn
return errAlreadyHavePeerSource
}
c.peerSource = f
c.minInterval = minInterval
WithMinInterval(minInterval)(c)
return nil
}
}
Expand Down Expand Up @@ -174,3 +174,12 @@ func WithClock(cl clock.Clock) Option {
return nil
}
}

// WithMinInterval sets the minimum interval after which peerSource callback will be called for more
// candidates even if AutoRelay needs new candidates
func WithMinInterval(interval time.Duration) Option {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the minimum interval both a parameter to WithPeerSource and a separate option here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to fix that, just wanted to confirm once if it's fine to change WithPeerSource method signature.

#1866 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed. Removed from WithPeerSource

return func(c *config) error {
c.minInterval = interval
return nil
}
}
4 changes: 2 additions & 2 deletions p2p/protocol/holepunch/holepunch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,9 @@ func mkHostWithStaticAutoRelay(t *testing.T, relay host.Host) host.Host {
h, err := libp2p.New(
libp2p.ListenAddrs(ma.StringCast("/ip4/127.0.0.1/tcp/0")),
libp2p.EnableRelay(),
libp2p.EnableAutoRelay(
libp2p.EnableAutoRelayWithStaticRelays(
[]peer.AddrInfo{pi},
autorelay.WithCircuitV1Support(),
autorelay.WithStaticRelays([]peer.AddrInfo{pi}),
),
libp2p.ForceReachabilityPrivate(),
libp2p.ResourceManager(&network.NullResourceManager{}),
Expand Down