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

feat(genutil): allow manually setting the consensus key type in genesis. #19971

Merged
merged 11 commits into from Apr 30, 2024
10 changes: 10 additions & 0 deletions x/genutil/client/cli/init.go
Expand Up @@ -35,6 +35,9 @@ const (

// FlagDefaultBondDenom defines the default denom to use in the genesis file.
FlagDefaultBondDenom = "default-denom"

// FlagConsensusKeyAlgo defines the algorithm to use for the consensus signing key.
FlagConsensusKeyAlgo = "consensus-key-algo"
)

type printInfo struct {
Expand Down Expand Up @@ -160,6 +163,12 @@ func InitCmd(mm *module.Manager) *cobra.Command {
Validators: nil,
}

consensusKey, err := cmd.Flags().GetString(FlagConsensusKeyAlgo)
if err != nil {
return errorsmod.Wrap(err, "Failed to get consensus key algo")
}
appGenesis.Consensus.Params.Validator.PubKeyTypes = []string{consensusKey}

Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure proper error handling for the GetString method call. The current implementation correctly wraps the error, which is good practice. However, consider adding a specific check for the case where the consensusKey is not one of the expected values (e.g., not "ed25519" or "secp256k1"). This could prevent configuration errors that might be hard to debug later.

+ if consensusKey != "ed25519" && consensusKey != "secp256k1" {
+     return errorsmod.Wrapf(err, "Invalid consensus key algo: %s", consensusKey)
+ }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
consensusKey, err := cmd.Flags().GetString(FlagConsensusKeyAlgo)
if err != nil {
return errorsmod.Wrap(err, "Failed to get consensus key algo")
}
appGenesis.Consensus.Params.Validator.PubKeyTypes = []string{consensusKey}
consensusKey, err := cmd.Flags().GetString(FlagConsensusKeyAlgo)
if err != nil {
return errorsmod.Wrap(err, "Failed to get consensus key algo")
}
+ if consensusKey != "ed25519" && consensusKey != "secp256k1" {
+ return errorsmod.Wrapf(err, "Invalid consensus key algo: %s", consensusKey)
+ }
appGenesis.Consensus.Params.Validator.PubKeyTypes = []string{consensusKey}

if err = genutil.ExportGenesisFile(appGenesis, genFile); err != nil {
return errorsmod.Wrap(err, "Failed to export genesis file")
}
Expand All @@ -176,6 +185,7 @@ func InitCmd(mm *module.Manager) *cobra.Command {
cmd.Flags().String(flags.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created")
cmd.Flags().String(FlagDefaultBondDenom, "", "genesis file default denomination, if left blank default value is 'stake'")
cmd.Flags().Int64(flags.FlagInitHeight, 1, "specify the initial block height at genesis")
cmd.Flags().String(FlagConsensusKeyAlgo, "ed25519", "algorithm to use for the consensus key")

return cmd
}