Skip to content

Commit

Permalink
implement listen-addr cli flag
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardmack committed Feb 9, 2023
1 parent 92ab319 commit 8a074d5
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 1 deletion.
7 changes: 7 additions & 0 deletions cmd/gossamer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,8 @@ func setDotNetworkConfig(ctx *cli.Context, tomlCfg ctoml.NetworkConfig, cfg *dot
cfg.MaxPeers = tomlCfg.MaxPeers
cfg.PersistentPeers = tomlCfg.PersistentPeers
cfg.DiscoveryInterval = time.Second * time.Duration(tomlCfg.DiscoveryInterval)
cfg.NodeKey = tomlCfg.NodeKey
cfg.ListenAddress = tomlCfg.ListenAddress

// check --port flag and update node configuration
if port := ctx.Uint(PortFlag.Name); port != 0 {
Expand Down Expand Up @@ -693,6 +695,11 @@ func setDotNetworkConfig(ctx *cli.Context, tomlCfg ctoml.NetworkConfig, cfg *dot
cfg.NodeKey = nodekey
}

// checx --listen-addr flag and update node configuration
if listenAddress := ctx.String(ListenAddressFlag.Name); listenAddress != "" {
cfg.ListenAddress = listenAddress
}

if len(cfg.PersistentPeers) == 0 {
cfg.PersistentPeers = []string(nil)
}
Expand Down
12 changes: 12 additions & 0 deletions cmd/gossamer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func TestInitConfigFromFlags(t *testing.T) {

// TestGlobalConfigFromFlags tests createDotGlobalConfig using relevant global flags
func TestGlobalConfigFromFlags(t *testing.T) {
t.Parallel()
defaultGlobalConfig := dot.GlobalConfig{
Name: "carpet-drill-8904",
ID: "gssmr",
Expand Down Expand Up @@ -400,6 +401,7 @@ func TestCoreConfigFromFlags(t *testing.T) {

// TestNetworkConfigFromFlags tests createDotNetworkConfig using relevant network flags
func TestNetworkConfigFromFlags(t *testing.T) {
t.Parallel()
defaultNetworkCfg := dot.NetworkConfig{
Port: 7001,
MinPeers: 1,
Expand Down Expand Up @@ -500,6 +502,16 @@ func TestNetworkConfigFromFlags(t *testing.T) {
NodeKey: "testkey",
},
},
"Test_gossamer_--listen-addr": {
[]string{"app", "--listen-addr", "/ip4/0.0.0.0/tcp/1234/ws"},
dot.NetworkConfig{
Port: defaultNetworkCfg.Port,
DiscoveryInterval: defaultNetworkCfg.DiscoveryInterval,
MinPeers: defaultNetworkCfg.MinPeers,
MaxPeers: defaultNetworkCfg.MaxPeers,
ListenAddress: "/ip4/0.0.0.0/tcp/1234/ws",
},
},
}

for key, c := range testcases {
Expand Down
6 changes: 6 additions & 0 deletions cmd/gossamer/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ var (
Name: "node-key",
Usage: "Overrides the secret Ed25519 key to use for libp2p",
}
// ListenAddressFlag uses the supplied multiaddress string as the address to listen on
ListenAddressFlag = cli.StringFlag{
Name: "listen-addr",
Usage: "Listen on this multiaddress",
}
)

// RPC service configuration flags
Expand Down Expand Up @@ -418,6 +423,7 @@ var (
&PublicIPFlag,
&PublicDNSFlag,
&NodeKeyFlag,
&ListenAddressFlag,

// rpc flags
&RPCEnabledFlag,
Expand Down
1 change: 1 addition & 0 deletions dot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type NetworkConfig struct {
PublicIP string
PublicDNS string
NodeKey string
ListenAddress string
}

// CoreConfig is to marshal/unmarshal toml core config vars
Expand Down
2 changes: 2 additions & 0 deletions dot/config/toml/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type NetworkConfig struct {
DiscoveryInterval int `toml:"discovery-interval,omitempty"`
PublicIP string `toml:"public-ip,omitempty"`
PublicDNS string `toml:"public-dns,omitempty"`
NodeKey string `toml:"node-key,omitempty"`
ListenAddress string `toml:"listen-addr,omitempty"`
}

// CoreConfig is to marshal/unmarshal toml core config vars
Expand Down
2 changes: 2 additions & 0 deletions dot/network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ type Config struct {
NoBootstrap bool
// NoMDNS disables MDNS discovery
NoMDNS bool
// ListenAddress multiaddress to listen on
ListenAddress string

MinPeers int
MaxPeers int
Expand Down
6 changes: 5 additions & 1 deletion dot/network/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ type host struct {

func newHost(ctx context.Context, cfg *Config) (*host, error) {
// create multiaddress (without p2p identity)
addr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", cfg.Port))
addrString := fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", cfg.Port)
if cfg.ListenAddress != "" {
addrString = cfg.ListenAddress
}
addr, err := ma.NewMultiaddr(addrString)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ func (nodeBuilder) createNetworkService(cfg *Config, stateSrvc *state.Service,
PublicDNS: cfg.Network.PublicDNS,
Metrics: metrics.NewIntervalConfig(cfg.Global.PublishMetrics),
NodeKey: cfg.Network.NodeKey,
ListenAddress: cfg.Network.ListenAddress,
}

networkSrvc, err := network.NewService(&networkConfig)
Expand Down

0 comments on commit 8a074d5

Please sign in to comment.