-
Notifications
You must be signed in to change notification settings - Fork 203
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
refactor(init-cli): Refactor the Init CLI command to be callable from runtime #2468
Closed
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ | |
"github.com/berachain/beacon-kit/errors" | ||
"github.com/berachain/beacon-kit/primitives/crypto" | ||
"github.com/berachain/beacon-kit/primitives/encoding/json" | ||
cfg "github.com/cometbft/cometbft/config" | ||
cmtconfig "github.com/cometbft/cometbft/config" | ||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/client/input" | ||
|
@@ -92,7 +92,7 @@ | |
return err | ||
} | ||
|
||
//nolint:funlen,gocognit,mnd // based on cosmossdk implementation | ||
func InitCmd(mm interface { | ||
DefaultGenesis() map[string]json.RawMessage | ||
ValidateGenesis(genesisData map[string]json.RawMessage) error | ||
|
@@ -114,7 +114,6 @@ | |
if err != nil { | ||
return errors.New("failed to parse FlagChainID") | ||
} | ||
|
||
switch { | ||
case chainID != "": | ||
case clientCtx.ChainID != "": | ||
|
@@ -125,7 +124,7 @@ | |
if config.RootDir == "" { | ||
config.RootDir = clientCtx.HomeDir | ||
} | ||
|
||
config.Moniker = args[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reordered setting the moniker so its closer to where other |
||
// Get bip39 mnemonic | ||
var mnemonic string | ||
shouldRecover, err := cmd.Flags().GetBool(FlagRecover) | ||
|
@@ -145,7 +144,6 @@ | |
return errors.New("invalid mnemonic") | ||
} | ||
} | ||
|
||
// Get initial height | ||
initHeight, err := cmd.Flags().GetInt64(flags.FlagInitHeight) | ||
if err != nil { | ||
|
@@ -155,14 +153,6 @@ | |
initHeight = 1 | ||
} | ||
|
||
nodeID, _, err := genutil.InitializeNodeValidatorFilesFromMnemonic(config, mnemonic, consensusKeyAlgo) | ||
if err != nil { | ||
return err | ||
} | ||
Comment on lines
-158
to
-161
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved this core logic to |
||
|
||
config.Moniker = args[0] | ||
|
||
genFile := config.GenesisFile() | ||
overwrite, err := cmd.Flags().GetBool(FlagOverwrite) | ||
if err != nil { | ||
return errors.New("failed to parse FlagOverwrite") | ||
|
@@ -171,65 +161,91 @@ | |
if err != nil { | ||
return errors.New("failed to parse FlagDefaultBondDenom") | ||
} | ||
return Init( | ||
chainID, | ||
config, | ||
mnemonic, | ||
initHeight, | ||
overwrite, | ||
defaultDenom, | ||
mm.DefaultGenesis(), | ||
cmd.ErrOrStderr(), | ||
) | ||
}, | ||
} | ||
|
||
// use os.Stat to check if the file exists | ||
_, err = os.Stat(genFile) | ||
if !overwrite && !os.IsNotExist(err) { | ||
return fmt.Errorf("genesis.json file already exists: %v", genFile) | ||
} | ||
cmd.Flags().BoolP(FlagOverwrite, "o", false, "overwrite the genesis.json file") | ||
cmd.Flags().Bool(FlagRecover, false, "provide seed phrase to recover existing key instead of creating") | ||
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") | ||
return cmd | ||
} | ||
|
||
// Overwrites the SDK default denom for side-effects | ||
if defaultDenom != "" { | ||
sdk.DefaultBondDenom = defaultDenom | ||
} | ||
appGenState := mm.DefaultGenesis() | ||
func Init( | ||
chainID string, | ||
config *cmtconfig.Config, | ||
mnemonic string, | ||
initHeight int64, | ||
overwrite bool, | ||
defaultDenom string, | ||
appGenesisState map[string]json.RawMessage, | ||
printDestination io.Writer, | ||
) error { | ||
nodeID, _, err := genutil.InitializeNodeValidatorFilesFromMnemonic(config, mnemonic, consensusKeyAlgo) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
appState, err := json.MarshalIndent(appGenState, "", " ") | ||
if err != nil { | ||
return errorsmod.Wrap(err, "Failed to marshal default genesis state") | ||
} | ||
genFile := config.GenesisFile() | ||
// use os.Stat to check if the file exists | ||
_, err = os.Stat(genFile) | ||
if !overwrite && !os.IsNotExist(err) { | ||
return fmt.Errorf("genesis.json file already exists: %v", genFile) | ||
} | ||
|
||
appGenesis := &types.AppGenesis{} | ||
if _, err = os.Stat(genFile); err != nil { | ||
if !os.IsNotExist(err) { | ||
return err | ||
} | ||
} else { | ||
appGenesis, err = types.AppGenesisFromFile(genFile) | ||
if err != nil { | ||
return errorsmod.Wrap(err, "Failed to read genesis doc from file") | ||
} | ||
} | ||
// Overwrites the SDK default denom for side-effects | ||
if defaultDenom != "" { | ||
sdk.DefaultBondDenom = defaultDenom | ||
} | ||
|
||
appGenesis.AppName = version.AppName | ||
appGenesis.AppVersion = version.Version | ||
appGenesis.ChainID = chainID | ||
appGenesis.AppState = appState | ||
appGenesis.InitialHeight = initHeight | ||
appGenesis.Consensus = &types.ConsensusGenesis{ | ||
Validators: nil, | ||
Params: cometbft.DefaultConsensusParams(consensusKeyAlgo), | ||
} | ||
appState, err := json.MarshalIndent(appGenesisState, "", " ") | ||
if err != nil { | ||
return errorsmod.Wrap(err, "Failed to marshal default genesis state") | ||
} | ||
|
||
if err = genutil.ExportGenesisFile(appGenesis, genFile); err != nil { | ||
return errorsmod.Wrap(err, "Failed to export genesis file") | ||
} | ||
appGenesis := &types.AppGenesis{} | ||
if _, err = os.Stat(genFile); err != nil { | ||
if !os.IsNotExist(err) { | ||
return err | ||
} | ||
} else { | ||
appGenesis, err = types.AppGenesisFromFile(genFile) | ||
if err != nil { | ||
return errorsmod.Wrap(err, "Failed to read genesis doc from file") | ||
} | ||
} | ||
|
||
toPrint := newPrintInfo(config.Moniker, chainID, nodeID, "", appState) | ||
appGenesis.AppName = version.AppName | ||
appGenesis.AppVersion = version.Version | ||
appGenesis.ChainID = chainID | ||
appGenesis.AppState = appState | ||
appGenesis.InitialHeight = initHeight | ||
appGenesis.Consensus = &types.ConsensusGenesis{ | ||
Validators: nil, | ||
Params: cometbft.DefaultConsensusParams(consensusKeyAlgo), | ||
} | ||
|
||
// Note: the config file was already creating before execution this command | ||
// by [SetupCommand], and it is being overwritten here. The only difference, | ||
// post default values cleanups, should be in the moniker, which is only setup | ||
// correctly here | ||
cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config) | ||
return displayInfo(cmd.ErrOrStderr(), toPrint) | ||
}, | ||
if err = genutil.ExportGenesisFile(appGenesis, genFile); err != nil { | ||
return errorsmod.Wrap(err, "Failed to export genesis file") | ||
} | ||
|
||
cmd.Flags().BoolP(FlagOverwrite, "o", false, "overwrite the genesis.json file") | ||
cmd.Flags().Bool(FlagRecover, false, "provide seed phrase to recover existing key instead of creating") | ||
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") | ||
return cmd | ||
toPrint := newPrintInfo(config.Moniker, chainID, nodeID, "", appState) | ||
|
||
// Note: the config file was already creating before execution this command | ||
// by [SetupCommand], and it is being overwritten here. The only difference, | ||
// post default values cleanups, should be in the moniker, which is only setup | ||
// correctly here | ||
cmtconfig.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config) | ||
return displayInfo(printDestination, toPrint) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We run ValidateGenesis in
initChain
and hence we don't use it here (i.e. this was unused)