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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -58,6 +58,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
* (x/consensus) [#19483](https://github.com/cosmos/cosmos-sdk/pull/19483) Add consensus messages registration to consensus module.
* (types) [#19759](https://github.com/cosmos/cosmos-sdk/pull/19759) Align SignerExtractionAdapter in PriorityNonceMempool Remove.
* (client) [#19870](https://github.com/cosmos/cosmos-sdk/pull/19870) Add new query command `wait-tx`. Alias `event-query-tx-for` to `wait-tx` for backward compatibility.
* (genutil) [#19971](https://github.com/cosmos/cosmos-sdk/pull/19971) Allow manually setting the consensus key type in genesis
Copy link
Contributor

Choose a reason for hiding this comment

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

The changelog entry clearly communicates the addition of the new feature allowing manual setting of the consensus key type during genesis. However, consider adding more detail about the impact or benefits of this change to provide better context for users.

- * (genutil) [#19971](https://github.com/cosmos/cosmos-sdk/pull/19971) Allow manually setting the consensus key type in genesis
+ * (genutil) [#19971](https://github.com/cosmos/cosmos-sdk/pull/19971) Allow manually setting the consensus key type in genesis, enhancing flexibility and control over cryptographic parameters during network initialization.

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
* (genutil) [#19971](https://github.com/cosmos/cosmos-sdk/pull/19971) Allow manually setting the consensus key type in genesis
* (genutil) [#19971](https://github.com/cosmos/cosmos-sdk/pull/19971) Allow manually setting the consensus key type in genesis, enhancing flexibility and control over cryptographic parameters during network initialization.


### Improvements

Expand Down
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
}