Skip to content

Commit 3c05a88

Browse files
authored
fix: cmd/gossamer: Generate random name if --name flag not set (#1506)
* generate random name if --name flag not set * update tests with name flag to handle random names * update tests that reference name cfg
1 parent 6e4298e commit 3c05a88

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

cmd/gossamer/config.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package main
1818

1919
import (
20+
"encoding/binary"
2021
"fmt"
2122
"strconv"
2223
"strings"
@@ -32,6 +33,7 @@ import (
3233
"github.com/ChainSafe/gossamer/lib/runtime/life"
3334
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
3435
"github.com/ChainSafe/gossamer/lib/runtime/wasmtime"
36+
"github.com/cosmos/go-bip39"
3537

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

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

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

738-
// check genesis name and use genesis name if --name flag not set
739-
if !ctx.GlobalIsSet(NameFlag.Name) {
740-
cfg.Global.Name = gen.Name
741-
}
742-
743746
// check genesis id and use genesis id if --chain flag not set
744747
if !ctx.GlobalIsSet(ChainFlag.Name) {
745748
cfg.Global.ID = gen.ID

cmd/gossamer/config_test.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,20 @@ func TestConfigFromChainFlag(t *testing.T) {
4545
}{
4646
{
4747
"Test gossamer --chain gssmr",
48-
[]string{"chain"},
49-
[]interface{}{"gssmr"},
48+
[]string{"chain", "name"},
49+
[]interface{}{"gssmr", dot.GssmrConfig().Global.Name},
5050
dot.GssmrConfig(),
5151
},
5252
{
5353
"Test gossamer --chain kusama",
54-
[]string{"chain"},
55-
[]interface{}{"kusama"},
54+
[]string{"chain", "name"},
55+
[]interface{}{"kusama", dot.KusamaConfig().Global.Name},
5656
dot.KusamaConfig(),
5757
},
5858
{
5959
"Test gossamer --chain polkadot",
60-
[]string{"chain"},
61-
[]interface{}{"polkadot"},
60+
[]string{"chain", "name"},
61+
[]interface{}{"polkadot", dot.PolkadotConfig().Global.Name},
6262
dot.PolkadotConfig(),
6363
},
6464
}
@@ -133,8 +133,8 @@ func TestGlobalConfigFromFlags(t *testing.T) {
133133
}{
134134
{
135135
"Test gossamer --config",
136-
[]string{"config"},
137-
[]interface{}{testCfgFile.Name()},
136+
[]string{"config", "name"},
137+
[]interface{}{testCfgFile.Name(), testCfg.Global.Name},
138138
dot.GlobalConfig{
139139
Name: testCfg.Global.Name,
140140
ID: testCfg.Global.ID,
@@ -146,8 +146,8 @@ func TestGlobalConfigFromFlags(t *testing.T) {
146146
},
147147
{
148148
"Test kusama --chain",
149-
[]string{"config", "chain"},
150-
[]interface{}{testCfgFile.Name(), "kusama"},
149+
[]string{"config", "chain", "name"},
150+
[]interface{}{testCfgFile.Name(), "kusama", dot.KusamaConfig().Global.Name},
151151
dot.GlobalConfig{
152152
Name: dot.KusamaConfig().Global.Name,
153153
ID: "ksmcc3",
@@ -172,8 +172,8 @@ func TestGlobalConfigFromFlags(t *testing.T) {
172172
},
173173
{
174174
"Test gossamer --basepath",
175-
[]string{"config", "basepath"},
176-
[]interface{}{testCfgFile.Name(), "test_basepath"},
175+
[]string{"config", "basepath", "name"},
176+
[]interface{}{testCfgFile.Name(), "test_basepath", testCfg.Global.Name},
177177
dot.GlobalConfig{
178178
Name: testCfg.Global.Name,
179179
ID: testCfg.Global.ID,
@@ -185,8 +185,8 @@ func TestGlobalConfigFromFlags(t *testing.T) {
185185
},
186186
{
187187
"Test gossamer --roles",
188-
[]string{"config", "roles"},
189-
[]interface{}{testCfgFile.Name(), "1"},
188+
[]string{"config", "roles", "name"},
189+
[]interface{}{testCfgFile.Name(), "1", testCfg.Global.Name},
190190
dot.GlobalConfig{
191191
Name: testCfg.Global.Name,
192192
ID: testCfg.Global.ID,
@@ -198,8 +198,8 @@ func TestGlobalConfigFromFlags(t *testing.T) {
198198
},
199199
{
200200
"Test gossamer --publish-metrics",
201-
[]string{"config", "publish-metrics"},
202-
[]interface{}{testCfgFile.Name(), true},
201+
[]string{"config", "publish-metrics", "name"},
202+
[]interface{}{testCfgFile.Name(), true, testCfg.Global.Name},
203203
dot.GlobalConfig{
204204
Name: testCfg.Global.Name,
205205
ID: testCfg.Global.ID,
@@ -211,8 +211,8 @@ func TestGlobalConfigFromFlags(t *testing.T) {
211211
},
212212
{
213213
"Test gossamer --metrics-port",
214-
[]string{"config", "metrics-port"},
215-
[]interface{}{testCfgFile.Name(), "9871"},
214+
[]string{"config", "metrics-port", "name"},
215+
[]interface{}{testCfgFile.Name(), "9871", testCfg.Global.Name},
216216
dot.GlobalConfig{
217217
Name: testCfg.Global.Name,
218218
ID: testCfg.Global.ID,
@@ -650,8 +650,8 @@ func TestUpdateConfigFromGenesisJSON(t *testing.T) {
650650

651651
ctx, err := newTestContext(
652652
t.Name(),
653-
[]string{"config", "genesis"},
654-
[]interface{}{testCfgFile.Name(), genFile.Name()},
653+
[]string{"config", "genesis", "name"},
654+
[]interface{}{testCfgFile.Name(), genFile.Name(), testCfg.Global.Name},
655655
)
656656
require.Nil(t, err)
657657

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

703703
ctx, err := newTestContext(
704704
t.Name(),
705-
[]string{"config", "genesis"},
706-
[]interface{}{testCfgFile.Name(), ""},
705+
[]string{"config", "genesis", "name"},
706+
[]interface{}{testCfgFile.Name(), "", testCfg.Global.Name},
707707
)
708708
require.Nil(t, err)
709709

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

754754
ctx, err := newTestContext(
755755
t.Name(),
756-
[]string{"config", "genesis"},
757-
[]interface{}{testCfgFile.Name(), genFile.Name()},
756+
[]string{"config", "genesis", "name"},
757+
[]interface{}{testCfgFile.Name(), genFile.Name(), testCfg.Global.Name},
758758
)
759759
require.Nil(t, err)
760760

cmd/gossamer/export_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ func TestExportCommand(t *testing.T) {
122122
},
123123
{
124124
"Test gossamer export --config --genesis --protocol --log --force",
125-
[]string{"config", "genesis", "protocol", "force"},
126-
[]interface{}{testConfig, genFile.Name(), testProtocol, "true"},
125+
[]string{"config", "genesis", "protocol", "force", "name"},
126+
[]interface{}{testConfig, genFile.Name(), testProtocol, "true", "Gossamer"},
127127
&dot.Config{
128128
Global: testCfg.Global,
129129
Init: dot.InitConfig{

0 commit comments

Comments
 (0)