Skip to content

Commit

Permalink
fix: cmd/gossamer: Generate random name if --name flag not set (#1506)
Browse files Browse the repository at this point in the history
* generate random name if --name flag not set

* update tests with name flag to handle random names

* update tests that reference name cfg
  • Loading branch information
edwardmack committed Apr 6, 2021
1 parent 6e4298e commit 3c05a88
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
15 changes: 9 additions & 6 deletions cmd/gossamer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"encoding/binary"
"fmt"
"strconv"
"strings"
Expand All @@ -32,6 +33,7 @@ import (
"github.com/ChainSafe/gossamer/lib/runtime/life"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/runtime/wasmtime"
"github.com/cosmos/go-bip39"

log "github.com/ChainSafe/log15"
"github.com/urfave/cli"
Expand Down Expand Up @@ -408,6 +410,13 @@ func setDotGlobalConfig(ctx *cli.Context, tomlCfg *ctoml.Config, cfg *dot.Global
// check --name flag and update node configuration
if name := ctx.GlobalString(NameFlag.Name); name != "" {
cfg.Name = name
} else {
// generate random name
entropy, _ := bip39.NewEntropy(128)
randomNamesString, _ := bip39.NewMnemonic(entropy)
randomNames := strings.Split(randomNamesString, " ")
number := binary.BigEndian.Uint16(entropy)
cfg.Name = randomNames[0] + "-" + randomNames[1] + "-" + fmt.Sprint(number)
}

// check --basepath flag and update node configuration
Expand Down Expand Up @@ -701,7 +710,6 @@ func updateDotConfigFromGenesisJSONRaw(tomlCfg ctoml.Config, cfg *dot.Config) {
return // exit
}

cfg.Global.Name = gen.Name
cfg.Global.ID = gen.ID
cfg.Network.Bootnodes = gen.Bootnodes
cfg.Network.ProtocolID = gen.ProtocolID
Expand Down Expand Up @@ -735,11 +743,6 @@ func updateDotConfigFromGenesisData(ctx *cli.Context, cfg *dot.Config) error {
return fmt.Errorf("failed to load genesis data: %s", err)
}

// check genesis name and use genesis name if --name flag not set
if !ctx.GlobalIsSet(NameFlag.Name) {
cfg.Global.Name = gen.Name
}

// check genesis id and use genesis id if --chain flag not set
if !ctx.GlobalIsSet(ChainFlag.Name) {
cfg.Global.ID = gen.ID
Expand Down
48 changes: 24 additions & 24 deletions cmd/gossamer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ func TestConfigFromChainFlag(t *testing.T) {
}{
{
"Test gossamer --chain gssmr",
[]string{"chain"},
[]interface{}{"gssmr"},
[]string{"chain", "name"},
[]interface{}{"gssmr", dot.GssmrConfig().Global.Name},
dot.GssmrConfig(),
},
{
"Test gossamer --chain kusama",
[]string{"chain"},
[]interface{}{"kusama"},
[]string{"chain", "name"},
[]interface{}{"kusama", dot.KusamaConfig().Global.Name},
dot.KusamaConfig(),
},
{
"Test gossamer --chain polkadot",
[]string{"chain"},
[]interface{}{"polkadot"},
[]string{"chain", "name"},
[]interface{}{"polkadot", dot.PolkadotConfig().Global.Name},
dot.PolkadotConfig(),
},
}
Expand Down Expand Up @@ -133,8 +133,8 @@ func TestGlobalConfigFromFlags(t *testing.T) {
}{
{
"Test gossamer --config",
[]string{"config"},
[]interface{}{testCfgFile.Name()},
[]string{"config", "name"},
[]interface{}{testCfgFile.Name(), testCfg.Global.Name},
dot.GlobalConfig{
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
Expand All @@ -146,8 +146,8 @@ func TestGlobalConfigFromFlags(t *testing.T) {
},
{
"Test kusama --chain",
[]string{"config", "chain"},
[]interface{}{testCfgFile.Name(), "kusama"},
[]string{"config", "chain", "name"},
[]interface{}{testCfgFile.Name(), "kusama", dot.KusamaConfig().Global.Name},
dot.GlobalConfig{
Name: dot.KusamaConfig().Global.Name,
ID: "ksmcc3",
Expand All @@ -172,8 +172,8 @@ func TestGlobalConfigFromFlags(t *testing.T) {
},
{
"Test gossamer --basepath",
[]string{"config", "basepath"},
[]interface{}{testCfgFile.Name(), "test_basepath"},
[]string{"config", "basepath", "name"},
[]interface{}{testCfgFile.Name(), "test_basepath", testCfg.Global.Name},
dot.GlobalConfig{
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
Expand All @@ -185,8 +185,8 @@ func TestGlobalConfigFromFlags(t *testing.T) {
},
{
"Test gossamer --roles",
[]string{"config", "roles"},
[]interface{}{testCfgFile.Name(), "1"},
[]string{"config", "roles", "name"},
[]interface{}{testCfgFile.Name(), "1", testCfg.Global.Name},
dot.GlobalConfig{
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
Expand All @@ -198,8 +198,8 @@ func TestGlobalConfigFromFlags(t *testing.T) {
},
{
"Test gossamer --publish-metrics",
[]string{"config", "publish-metrics"},
[]interface{}{testCfgFile.Name(), true},
[]string{"config", "publish-metrics", "name"},
[]interface{}{testCfgFile.Name(), true, testCfg.Global.Name},
dot.GlobalConfig{
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
Expand All @@ -211,8 +211,8 @@ func TestGlobalConfigFromFlags(t *testing.T) {
},
{
"Test gossamer --metrics-port",
[]string{"config", "metrics-port"},
[]interface{}{testCfgFile.Name(), "9871"},
[]string{"config", "metrics-port", "name"},
[]interface{}{testCfgFile.Name(), "9871", testCfg.Global.Name},
dot.GlobalConfig{
Name: testCfg.Global.Name,
ID: testCfg.Global.ID,
Expand Down Expand Up @@ -650,8 +650,8 @@ func TestUpdateConfigFromGenesisJSON(t *testing.T) {

ctx, err := newTestContext(
t.Name(),
[]string{"config", "genesis"},
[]interface{}{testCfgFile.Name(), genFile.Name()},
[]string{"config", "genesis", "name"},
[]interface{}{testCfgFile.Name(), genFile.Name(), testCfg.Global.Name},
)
require.Nil(t, err)

Expand Down Expand Up @@ -702,8 +702,8 @@ func TestUpdateConfigFromGenesisJSON_Default(t *testing.T) {

ctx, err := newTestContext(
t.Name(),
[]string{"config", "genesis"},
[]interface{}{testCfgFile.Name(), ""},
[]string{"config", "genesis", "name"},
[]interface{}{testCfgFile.Name(), "", testCfg.Global.Name},
)
require.Nil(t, err)

Expand Down Expand Up @@ -753,8 +753,8 @@ func TestUpdateConfigFromGenesisData(t *testing.T) {

ctx, err := newTestContext(
t.Name(),
[]string{"config", "genesis"},
[]interface{}{testCfgFile.Name(), genFile.Name()},
[]string{"config", "genesis", "name"},
[]interface{}{testCfgFile.Name(), genFile.Name(), testCfg.Global.Name},
)
require.Nil(t, err)

Expand Down
4 changes: 2 additions & 2 deletions cmd/gossamer/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ func TestExportCommand(t *testing.T) {
},
{
"Test gossamer export --config --genesis --protocol --log --force",
[]string{"config", "genesis", "protocol", "force"},
[]interface{}{testConfig, genFile.Name(), testProtocol, "true"},
[]string{"config", "genesis", "protocol", "force", "name"},
[]interface{}{testConfig, genFile.Name(), testProtocol, "true", "Gossamer"},
&dot.Config{
Global: testCfg.Global,
Init: dot.InitConfig{
Expand Down

0 comments on commit 3c05a88

Please sign in to comment.