Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cmd/gossamer: Generate random name if --name flag not set #1506

Merged
merged 5 commits into from
Apr 6, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, " ")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is len(randomNames) > 1 always?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, bip39.NewMnemonic always returns 12 words when provided with a 128 bit entropy.

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