diff --git a/cmd/gossamer/config.go b/cmd/gossamer/config.go index 4f08f54e83..3f0abca651 100644 --- a/cmd/gossamer/config.go +++ b/cmd/gossamer/config.go @@ -17,6 +17,7 @@ package main import ( + "encoding/binary" "fmt" "strconv" "strings" @@ -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" @@ -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 @@ -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 @@ -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 diff --git a/cmd/gossamer/config_test.go b/cmd/gossamer/config_test.go index 4a3fbcc5e7..f9fa04c0d4 100644 --- a/cmd/gossamer/config_test.go +++ b/cmd/gossamer/config_test.go @@ -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(), }, } @@ -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, @@ -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", @@ -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, @@ -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, @@ -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, @@ -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, @@ -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) @@ -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) @@ -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) diff --git a/cmd/gossamer/export_test.go b/cmd/gossamer/export_test.go index bab4be37d4..b89e664471 100644 --- a/cmd/gossamer/export_test.go +++ b/cmd/gossamer/export_test.go @@ -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{