Skip to content

Commit

Permalink
support multiple listen addresses in net config
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Jan 29, 2024
1 parent 4b2ae9a commit 939bb02
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 60 deletions.
10 changes: 5 additions & 5 deletions cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ func MakeStartCommand(cfg *config.Config) *cobra.Command {
log.FeedbackFatalE(context.Background(), "Could not bind datastore.badger.valuelogfilesize", err)
}

cmd.Flags().String(
"p2paddr", cfg.Net.P2PAddress,
cmd.Flags().StringSlice(
"p2paddr", cfg.Net.P2PAddresses,
"Listener address for the p2p network (formatted as a libp2p MultiAddr)",
)
err = cfg.BindFlag("net.p2paddress", cmd.Flags().Lookup("p2paddr"))
err = cfg.BindFlag("net.p2paddresses", cmd.Flags().Lookup("p2paddr"))
if err != nil {
log.FeedbackFatalE(context.Background(), "Could not bind net.p2paddress", err)
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func start(ctx context.Context, cfg *config.Config) (*defraInstance, error) {
var node *net.Node
if !cfg.Net.P2PDisabled {
nodeOpts := []net.NodeOpt{
net.WithListenAddress(cfg.Net.P2PAddress),
net.WithListenAddresses(cfg.Net.P2PAddresses...),
net.WithEnablePubSub(cfg.Net.PubSubEnabled),
net.WithEnableRelay(cfg.Net.RelayEnabled),
}
Expand All @@ -235,7 +235,7 @@ func start(ctx context.Context, cfg *config.Config) (*defraInstance, error) {
}
nodeOpts = append(nodeOpts, net.WithPrivateKey(key))
}
log.FeedbackInfo(ctx, "Starting P2P node", logging.NewKV("P2P address", cfg.Net.P2PAddress))
log.FeedbackInfo(ctx, "Starting P2P node", logging.NewKV("P2P addresses", cfg.Net.P2PAddresses))
node, err = net.NewNode(ctx, db, nodeOpts...)
if err != nil {
db.Close()
Expand Down
21 changes: 14 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func (apicfg *APIConfig) AddressToURL() string {

// NetConfig configures aspects of network and peer-to-peer.
type NetConfig struct {
P2PAddress string
P2PAddresses []string
P2PDisabled bool
Peers string
PubSubEnabled bool `mapstructure:"pubsub"`
Expand All @@ -363,7 +363,10 @@ type NetConfig struct {

func defaultNetConfig() *NetConfig {
return &NetConfig{
P2PAddress: "/ip4/0.0.0.0/tcp/9171",
P2PAddresses: []string{
"/ip4/0.0.0.0/tcp/9171",
"/ip4/127.0.0.1/tcp/9171",
},
P2PDisabled: false,
Peers: "",
PubSubEnabled: true,
Expand All @@ -372,9 +375,11 @@ func defaultNetConfig() *NetConfig {
}

func (netcfg *NetConfig) validate() error {
_, err := ma.NewMultiaddr(netcfg.P2PAddress)
if err != nil {
return NewErrInvalidP2PAddress(err, netcfg.P2PAddress)
for _, addr := range netcfg.P2PAddresses {
_, err := ma.NewMultiaddr(addr)
if err != nil {
return NewErrInvalidP2PAddress(err, addr)
}
}
if len(netcfg.Peers) > 0 {
peers := strings.Split(netcfg.Peers, ",")
Expand Down Expand Up @@ -688,12 +693,14 @@ func (c *Config) String() string {
}

func (c *Config) toBytes() ([]byte, error) {
var buffer bytes.Buffer
tmpl := template.New("configTemplate")
tmpl := template.New("configTemplate").Funcs(template.FuncMap{
"join": strings.Join,
})
configTemplate, err := tmpl.Parse(defaultConfigTemplate)
if err != nil {
return nil, NewErrConfigTemplateFailed(err)
}
var buffer bytes.Buffer
if err := configTemplate.Execute(&buffer, c); err != nil {
return nil, NewErrConfigTemplateFailed(err)
}
Expand Down
6 changes: 3 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var envVarsDifferent = map[string]string{
"DEFRA_DATASTORE_BADGER_PATH": "defra_data",
"DEFRA_API_ADDRESS": "localhost:9999",
"DEFRA_NET_P2PDISABLED": "true",
"DEFRA_NET_P2PADDRESS": "/ip4/0.0.0.0/tcp/9876",
"DEFRA_NET_P2PADDRESSES": "/ip4/0.0.0.0/tcp/9876",
"DEFRA_NET_PUBSUB": "false",
"DEFRA_NET_RELAY": "false",
"DEFRA_LOG_LEVEL": "error",
Expand All @@ -37,7 +37,7 @@ var envVarsInvalid = map[string]string{
"DEFRA_DATASTORE_BADGER_PATH": "^=+()&**()*(&))",
"DEFRA_API_ADDRESS": "^=+()&**()*(&))",
"DEFRA_NET_P2PDISABLED": "^=+()&**()*(&))",
"DEFRA_NET_P2PADDRESS": "^=+()&**()*(&))",
"DEFRA_NET_P2PADDRESSES": "^=+()&**()*(&))",
"DEFRA_NET_PUBSUB": "^=+()&**()*(&))",
"DEFRA_NET_RELAY": "^=+()&**()*(&))",
"DEFRA_LOG_LEVEL": "^=+()&**()*(&))",
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestEnvVariablesAllConsidered(t *testing.T) {
assert.Equal(t, filepath.Join(cfg.Rootdir, "defra_data"), cfg.Datastore.Badger.Path)
assert.Equal(t, "memory", cfg.Datastore.Store)
assert.Equal(t, true, cfg.Net.P2PDisabled)
assert.Equal(t, "/ip4/0.0.0.0/tcp/9876", cfg.Net.P2PAddress)
assert.Equal(t, []string{"/ip4/0.0.0.0/tcp/9876", "/ip4/127.0.0.1/tcp/9171"}, cfg.Net.P2PAddresses)
assert.Equal(t, false, cfg.Net.PubSubEnabled)
assert.Equal(t, false, cfg.Net.RelayEnabled)
assert.Equal(t, "error", cfg.Log.Level)
Expand Down
9 changes: 7 additions & 2 deletions config/configfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"text/template"

Expand All @@ -24,7 +25,9 @@ import (
func TestConfigTemplateSerialize(t *testing.T) {
var buffer bytes.Buffer
cfg := DefaultConfig()
tmpl := template.New("configTemplate")
tmpl := template.New("configTemplate").Funcs(template.FuncMap{
"join": strings.Join,
})
configTemplate, err := tmpl.Parse(defaultConfigTemplate)
assert.NoError(t, err)
err = configTemplate.Execute(&buffer, cfg)
Expand All @@ -36,7 +39,9 @@ func TestConfigTemplateSerialize(t *testing.T) {
func TestConfigTemplateExecutes(t *testing.T) {
cfg := DefaultConfig()
var buffer bytes.Buffer
tmpl := template.New("configTemplate")
tmpl := template.New("configTemplate").Funcs(template.FuncMap{
"join": strings.Join,
})
configTemplate, err := tmpl.Parse(defaultConfigTemplate)
assert.NoError(t, err)
err = configTemplate.Execute(&buffer, cfg)
Expand Down
2 changes: 1 addition & 1 deletion config/configfile_yaml.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ net:
# Whether the P2P is disabled
p2pdisabled: {{ .Net.P2PDisabled }}
# Listening address of the P2P network
p2paddress: {{ .Net.P2PAddress }}
p2paddresses: [{{ join .Net.P2PAddresses ", " }}]
# Whether the node has pubsub enabled or not
pubsub: {{ .Net.PubSubEnabled }}
# Enable libp2p's Circuit relay transport protocol https://docs.libp2p.io/concepts/circuit-relay/
Expand Down
12 changes: 6 additions & 6 deletions net/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

// Options is the node options.
type Options struct {
ListenAddress string
ListenAddresses []string
PrivateKey crypto.PrivKey
EnablePubSub bool
EnableRelay bool
Expand All @@ -30,9 +30,9 @@ type Options struct {
// DefaultOptions returns the default net options.
func DefaultOptions() *Options {
return &Options{
ListenAddress: "/ip4/0.0.0.0/tcp/9171",
EnablePubSub: true,
EnableRelay: false,
ListenAddresses: []string{"/ip4/0.0.0.0/tcp/9171"},
EnablePubSub: true,
EnableRelay: false,
}
}

Expand Down Expand Up @@ -60,8 +60,8 @@ func WithEnableRelay(enable bool) NodeOpt {
}

// WithListenAddress sets the address to listen on given as a multiaddress string.
func WithListenAddress(address string) NodeOpt {
func WithListenAddresses(addresses ...string) NodeOpt {
return func(opt *Options) {
opt.ListenAddress = address
opt.ListenAddresses = addresses
}
}
7 changes: 4 additions & 3 deletions net/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import (
"github.com/stretchr/testify/assert"
)

func TestWithListenAddress(t *testing.T) {
func TestWithListenAddresses(t *testing.T) {
opts := &Options{}
WithListenAddress("/ip4/127.0.0.1/tcp/6666")(opts)
assert.Equal(t, "/ip4/127.0.0.1/tcp/6666", opts.ListenAddress)
addresses := []string{"/ip4/127.0.0.1/tcp/6666", "/ip4/0.0.0.0/tcp/6666"}
WithListenAddresses(addresses...)(opts)
assert.Equal(t, addresses, opts.ListenAddresses)
}

func TestWithEnableRelay(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions net/dialer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ func TestDial_WithConnectedPeer_NoError(t *testing.T) {
n1, err := NewNode(
ctx,
db1,
WithListenAddress("/ip4/0.0.0.0/tcp/0"),
WithListenAddresses("/ip4/0.0.0.0/tcp/0"),
)
assert.NoError(t, err)
defer n1.Close()
n2, err := NewNode(
ctx,
db2,
WithListenAddress("/ip4/0.0.0.0/tcp/0"),
WithListenAddresses("/ip4/0.0.0.0/tcp/0"),
)
assert.NoError(t, err)
defer n2.Close()
Expand All @@ -54,14 +54,14 @@ func TestDial_WithConnectedPeerAndSecondConnection_NoError(t *testing.T) {
n1, err := NewNode(
ctx,
db1,
WithListenAddress("/ip4/0.0.0.0/tcp/0"),
WithListenAddresses("/ip4/0.0.0.0/tcp/0"),
)
assert.NoError(t, err)
defer n1.Close()
n2, err := NewNode(
ctx,
db2,
WithListenAddress("/ip4/0.0.0.0/tcp/0"),
WithListenAddresses("/ip4/0.0.0.0/tcp/0"),
)
assert.NoError(t, err)
defer n2.Close()
Expand All @@ -84,14 +84,14 @@ func TestDial_WithConnectedPeerAndSecondConnectionWithConnectionShutdown_Closing
n1, err := NewNode(
ctx,
db1,
WithListenAddress("/ip4/0.0.0.0/tcp/0"),
WithListenAddresses("/ip4/0.0.0.0/tcp/0"),
)
assert.NoError(t, err)
defer n1.Close()
n2, err := NewNode(
ctx,
db2,
WithListenAddress("/ip4/0.0.0.0/tcp/0"),
WithListenAddresses("/ip4/0.0.0.0/tcp/0"),
)
assert.NoError(t, err)
defer n2.Close()
Expand Down
16 changes: 11 additions & 5 deletions net/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,19 @@ func NewNode(
for _, opt := range opts {
opt(options)
}

connManager, err := connmgr.NewConnManager(100, 400, connmgr.WithGracePeriod(time.Second*20))
if err != nil {
return nil, err
}
listenAddress, err := multiaddr.NewMultiaddr(options.ListenAddress)
if err != nil {
return nil, err

var listenAddresses []multiaddr.Multiaddr
for _, addr := range options.ListenAddresses {
listenAddress, err := multiaddr.NewMultiaddr(addr)
if err != nil {
return nil, err
}
listenAddresses = append(listenAddresses, listenAddress)
}

fin := finalizer.NewFinalizer()
Expand All @@ -115,7 +121,7 @@ func NewNode(
libp2p.ConnectionManager(connManager),
libp2p.DefaultTransports,
libp2p.Identity(options.PrivateKey),
libp2p.ListenAddrs(listenAddress),
libp2p.ListenAddrs(listenAddresses...),
libp2p.Peerstore(peerstore),
libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
// Delete this line and uncomment the next 6 lines once we remove batchable datastore support.
Expand All @@ -142,7 +148,7 @@ func NewNode(
ctx,
"Created LibP2P host",
logging.NewKV("PeerId", h.ID()),
logging.NewKV("Address", options.ListenAddress),
logging.NewKV("Address", options.ListenAddresses),
)

var ps *pubsub.PubSub
Expand Down
14 changes: 7 additions & 7 deletions net/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestNewNode_BootstrapWithNoPeer_NoError(t *testing.T) {
n1, err := NewNode(
ctx,
db,
WithListenAddress("/ip4/0.0.0.0/tcp/0"),
WithListenAddresses("/ip4/0.0.0.0/tcp/0"),
)
require.NoError(t, err)
defer n1.Close()
Expand All @@ -142,14 +142,14 @@ func TestNewNode_BootstrapWithOnePeer_NoError(t *testing.T) {
n1, err := NewNode(
ctx,
db,
WithListenAddress("/ip4/0.0.0.0/tcp/0"),
WithListenAddresses("/ip4/0.0.0.0/tcp/0"),
)
require.NoError(t, err)
defer n1.Close()
n2, err := NewNode(
ctx,
db,
WithListenAddress("/ip4/0.0.0.0/tcp/0"),
WithListenAddresses("/ip4/0.0.0.0/tcp/0"),
)
require.NoError(t, err)
defer n2.Close()
Expand All @@ -169,14 +169,14 @@ func TestNewNode_BootstrapWithOneValidPeerAndManyInvalidPeers_NoError(t *testing
n1, err := NewNode(
ctx,
db,
WithListenAddress("/ip4/0.0.0.0/tcp/0"),
WithListenAddresses("/ip4/0.0.0.0/tcp/0"),
)
require.NoError(t, err)
defer n1.Close()
n2, err := NewNode(
ctx,
db,
WithListenAddress("/ip4/0.0.0.0/tcp/0"),
WithListenAddresses("/ip4/0.0.0.0/tcp/0"),
)
require.NoError(t, err)
defer n2.Close()
Expand All @@ -190,15 +190,15 @@ func TestNewNode_BootstrapWithOneValidPeerAndManyInvalidPeers_NoError(t *testing
n2.Bootstrap(addrs)
}

func TestListenAddrs_WithListenAddress_NoError(t *testing.T) {
func TestListenAddrs_WithListenAddresses_NoError(t *testing.T) {
ctx := context.Background()
store := memory.NewDatastore(ctx)
db, err := db.NewDB(ctx, store, db.WithUpdateEvents())
require.NoError(t, err)
n, err := NewNode(
context.Background(),
db,
WithListenAddress("/ip4/0.0.0.0/tcp/0"),
WithListenAddresses("/ip4/0.0.0.0/tcp/0"),
)
require.NoError(t, err)
defer n.Close()
Expand Down
10 changes: 5 additions & 5 deletions net/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func newTestNode(ctx context.Context, t *testing.T) (client.DB, *Node) {
n, err := NewNode(
ctx,
db,
WithListenAddress(randomMultiaddr),
WithListenAddresses(randomMultiaddr),
)
require.NoError(t, err)

Expand Down Expand Up @@ -214,13 +214,13 @@ func TestStart_WithKnownPeer_NoError(t *testing.T) {
n1, err := NewNode(
ctx,
db1,
WithListenAddress("/ip4/0.0.0.0/tcp/0"),
WithListenAddresses("/ip4/0.0.0.0/tcp/0"),
)
require.NoError(t, err)
n2, err := NewNode(
ctx,
db2,
WithListenAddress("/ip4/0.0.0.0/tcp/0"),
WithListenAddresses("/ip4/0.0.0.0/tcp/0"),
)
require.NoError(t, err)

Expand Down Expand Up @@ -250,13 +250,13 @@ func TestStart_WithOfflineKnownPeer_NoError(t *testing.T) {
n1, err := NewNode(
ctx,
db1,
WithListenAddress("/ip4/0.0.0.0/tcp/0"),
WithListenAddresses("/ip4/0.0.0.0/tcp/0"),
)
require.NoError(t, err)
n2, err := NewNode(
ctx,
db2,
WithListenAddress("/ip4/0.0.0.0/tcp/0"),
WithListenAddresses("/ip4/0.0.0.0/tcp/0"),
)
require.NoError(t, err)

Expand Down
Loading

0 comments on commit 939bb02

Please sign in to comment.