github.com/cosmos/cosmos-sdk@v0.50.10/x/genutil/client/cli/validate_genesis.go (about) 1 package cli 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "io" 8 "strings" 9 10 "github.com/spf13/cobra" 11 12 "github.com/cosmos/cosmos-sdk/client" 13 "github.com/cosmos/cosmos-sdk/server" 14 "github.com/cosmos/cosmos-sdk/types/module" 15 "github.com/cosmos/cosmos-sdk/x/genutil/types" 16 ) 17 18 const chainUpgradeGuide = "https://github.com/cosmos/cosmos-sdk/blob/main/UPGRADING.md" 19 20 // ValidateGenesisCmd takes a genesis file, and makes sure that it is valid. 21 func ValidateGenesisCmd(mbm module.BasicManager) *cobra.Command { 22 return &cobra.Command{ 23 Use: "validate [file]", 24 Aliases: []string{"validate-genesis"}, 25 Args: cobra.RangeArgs(0, 1), 26 Short: "Validates the genesis file at the default location or at the location passed as an arg", 27 RunE: func(cmd *cobra.Command, args []string) (err error) { 28 serverCtx := server.GetServerContextFromCmd(cmd) 29 clientCtx := client.GetClientContextFromCmd(cmd) 30 31 cdc := clientCtx.Codec 32 33 // Load default if passed no args, otherwise load passed file 34 var genesis string 35 if len(args) == 0 { 36 genesis = serverCtx.Config.GenesisFile() 37 } else { 38 genesis = args[0] 39 } 40 41 appGenesis, err := types.AppGenesisFromFile(genesis) 42 if err != nil { 43 return enrichUnmarshalError(err) 44 } 45 46 if err := appGenesis.ValidateAndComplete(); err != nil { 47 return fmt.Errorf("make sure that you have correctly migrated all CometBFT consensus params. Refer the UPGRADING.md (%s): %w", chainUpgradeGuide, err) 48 } 49 50 var genState map[string]json.RawMessage 51 if err := json.Unmarshal(appGenesis.AppState, &genState); err != nil { 52 if strings.Contains(err.Error(), "unexpected end of JSON input") { 53 return fmt.Errorf("app_state is missing in the genesis file: %s", err.Error()) 54 } 55 return fmt.Errorf("error unmarshalling genesis doc %s: %w", genesis, err) 56 } 57 58 if err = mbm.ValidateGenesis(cdc, clientCtx.TxConfig, genState); err != nil { 59 errStr := fmt.Sprintf("error validating genesis file %s: %s", genesis, err.Error()) 60 if errors.Is(err, io.EOF) { 61 errStr = fmt.Sprintf("%s: section is missing in the app_state", errStr) 62 } 63 return fmt.Errorf("%s", errStr) 64 } 65 66 fmt.Fprintf(cmd.OutOrStdout(), "File at %s is a valid genesis file\n", genesis) 67 return nil 68 }, 69 } 70 } 71 72 func enrichUnmarshalError(err error) error { 73 var syntaxErr *json.SyntaxError 74 if errors.As(err, &syntaxErr) { 75 return fmt.Errorf("error at offset %d: %s", syntaxErr.Offset, syntaxErr.Error()) 76 } 77 return err 78 }