diff --git a/CHANGELOG.md b/CHANGELOG.md index e26d15caed31..63226c53c871 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -248,6 +248,7 @@ to detail this new feature and how state transitions occur. ### Bug Fixes +* (x/genutil) [\#5499](https://github.com/cosmos/cosmos-sdk/pull/) Ensure `DefaultGenesis` returns valid and non-nil default genesis state. * (client) [\#5303](https://github.com/cosmos/cosmos-sdk/issues/5303) Fix ignored error in tx generate only mode. * (cli) [\#4763](https://github.com/cosmos/cosmos-sdk/issues/4763) Fix flag `--min-self-delegation` for staking `EditValidator` * (keys) Fix ledger custom coin type support bug diff --git a/x/auth/module.go b/x/auth/module.go index 510f4304d7c5..d3cf501e4f04 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -2,6 +2,7 @@ package auth import ( "encoding/json" + "fmt" "math/rand" "github.com/gorilla/mux" @@ -47,10 +48,10 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage { // ValidateGenesis performs genesis state validation for the auth module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data types.GenesisState - err := types.ModuleCdc.UnmarshalJSON(bz, &data) - if err != nil { - return err + if err := types.ModuleCdc.UnmarshalJSON(bz, &data); err != nil { + return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) } + return types.ValidateGenesis(data) } diff --git a/x/bank/module.go b/x/bank/module.go index 0430d18f1550..114962382758 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -2,6 +2,7 @@ package bank import ( "encoding/json" + "fmt" "math/rand" "github.com/gorilla/mux" @@ -45,10 +46,10 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage { // ValidateGenesis performs genesis state validation for the bank module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState - err := ModuleCdc.UnmarshalJSON(bz, &data) - if err != nil { - return err + if err := ModuleCdc.UnmarshalJSON(bz, &data); err != nil { + return fmt.Errorf("failed to unmarshal %s genesis state: %w", ModuleName, err) } + return ValidateGenesis(data) } diff --git a/x/crisis/module.go b/x/crisis/module.go index 4318c516cea8..1885f69ade9f 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -2,6 +2,7 @@ package crisis import ( "encoding/json" + "fmt" "github.com/gorilla/mux" "github.com/spf13/cobra" @@ -45,8 +46,9 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage { func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data types.GenesisState if err := types.ModuleCdc.UnmarshalJSON(bz, &data); err != nil { - return err + return fmt.Errorf("failed to unmarshal %s genesis state: %w", ModuleName, err) } + return types.ValidateGenesis(data) } diff --git a/x/distribution/module.go b/x/distribution/module.go index c0452ebb3c6b..0b143d6478b8 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -2,6 +2,7 @@ package distribution import ( "encoding/json" + "fmt" "math/rand" "github.com/gorilla/mux" @@ -49,10 +50,10 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage { // ValidateGenesis performs genesis state validation for the distribution module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState - err := ModuleCdc.UnmarshalJSON(bz, &data) - if err != nil { - return err + if err := ModuleCdc.UnmarshalJSON(bz, &data); err != nil { + return fmt.Errorf("failed to unmarshal %s genesis state: %w", ModuleName, err) } + return ValidateGenesis(data) } diff --git a/x/evidence/module.go b/x/evidence/module.go index aa4129469dab..30b427a95c1d 100644 --- a/x/evidence/module.go +++ b/x/evidence/module.go @@ -58,8 +58,7 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage { // ValidateGenesis performs genesis state validation for the evidence module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var gs GenesisState - err := ModuleCdc.UnmarshalJSON(bz, &gs) - if err != nil { + if err := ModuleCdc.UnmarshalJSON(bz, &gs); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", ModuleName, err) } diff --git a/x/genutil/client/cli/validate_genesis.go b/x/genutil/client/cli/validate_genesis.go index f4baa5dc9df3..6be33bdabf09 100644 --- a/x/genutil/client/cli/validate_genesis.go +++ b/x/genutil/client/cli/validate_genesis.go @@ -38,7 +38,7 @@ func ValidateGenesisCmd(ctx *server.Context, cdc *codec.Codec, mbm module.BasicM var genState map[string]json.RawMessage if err = cdc.UnmarshalJSON(genDoc.AppState, &genState); err != nil { - return fmt.Errorf("error unmarshaling genesis doc %s: %s", genesis, err.Error()) + return fmt.Errorf("error unmarshalling genesis doc %s: %s", genesis, err.Error()) } if err = mbm.ValidateGenesis(genState); err != nil { diff --git a/x/genutil/genesis.go b/x/genutil/genesis.go index c1a06a8b9aff..c8564bdc4e2d 100644 --- a/x/genutil/genesis.go +++ b/x/genutil/genesis.go @@ -9,12 +9,15 @@ import ( ) // InitGenesis - initialize accounts and deliver genesis transactions -func InitGenesis(ctx sdk.Context, cdc *codec.Codec, stakingKeeper types.StakingKeeper, - deliverTx deliverTxfn, genesisState GenesisState) []abci.ValidatorUpdate { +func InitGenesis( + ctx sdk.Context, cdc *codec.Codec, stakingKeeper types.StakingKeeper, + deliverTx deliverTxfn, genesisState GenesisState, +) []abci.ValidatorUpdate { var validators []abci.ValidatorUpdate if len(genesisState.GenTxs) > 0 { validators = DeliverGenTxs(ctx, cdc, genesisState.GenTxs, stakingKeeper, deliverTx) } + return validators } diff --git a/x/genutil/module.go b/x/genutil/module.go index 8d8f296ae1c9..31ca1b463523 100644 --- a/x/genutil/module.go +++ b/x/genutil/module.go @@ -2,6 +2,7 @@ package genutil import ( "encoding/json" + "fmt" "github.com/gorilla/mux" "github.com/spf13/cobra" @@ -34,16 +35,16 @@ func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {} // DefaultGenesis returns default genesis state as raw bytes for the genutil // module. func (AppModuleBasic) DefaultGenesis() json.RawMessage { - return ModuleCdc.MustMarshalJSON(GenesisState{}) + return ModuleCdc.MustMarshalJSON(types.DefaultGenesisState()) } // ValidateGenesis performs genesis state validation for the genutil module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState - err := ModuleCdc.UnmarshalJSON(bz, &data) - if err != nil { - return err + if err := ModuleCdc.UnmarshalJSON(bz, &data); err != nil { + return fmt.Errorf("failed to unmarshal %s genesis state: %w", ModuleName, err) } + return ValidateGenesis(data) } diff --git a/x/genutil/types/genesis_state.go b/x/genutil/types/genesis_state.go index e568bb42076a..1303ee82ca4b 100644 --- a/x/genutil/types/genesis_state.go +++ b/x/genutil/types/genesis_state.go @@ -25,6 +25,13 @@ func NewGenesisState(genTxs []json.RawMessage) GenesisState { } } +// DefaultGenesisState returns the genutil module's default genesis state. +func DefaultGenesisState() GenesisState { + return GenesisState{ + GenTxs: []json.RawMessage{}, + } +} + // NewGenesisStateFromStdTx creates a new GenesisState object // from auth transactions func NewGenesisStateFromStdTx(genTxs []authtypes.StdTx) GenesisState { diff --git a/x/gov/module.go b/x/gov/module.go index 1ef3a8c0f5cf..7ad5ecbc0ace 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -4,6 +4,7 @@ package gov import ( "encoding/json" + "fmt" "math/rand" "github.com/gorilla/mux" @@ -60,10 +61,10 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage { // ValidateGenesis performs genesis state validation for the gov module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState - err := ModuleCdc.UnmarshalJSON(bz, &data) - if err != nil { - return err + if err := ModuleCdc.UnmarshalJSON(bz, &data); err != nil { + return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) } + return ValidateGenesis(data) } diff --git a/x/mint/module.go b/x/mint/module.go index f0e36e1693d7..95b171251ee1 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -2,6 +2,7 @@ package mint import ( "encoding/json" + "fmt" "math/rand" "github.com/gorilla/mux" @@ -47,10 +48,10 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage { // ValidateGenesis performs genesis state validation for the mint module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState - err := ModuleCdc.UnmarshalJSON(bz, &data) - if err != nil { - return err + if err := ModuleCdc.UnmarshalJSON(bz, &data); err != nil { + return fmt.Errorf("failed to unmarshal %s genesis state: %w", ModuleName, err) } + return ValidateGenesis(data) } diff --git a/x/slashing/module.go b/x/slashing/module.go index 47d15cf89937..e99b421574fe 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -2,6 +2,7 @@ package slashing import ( "encoding/json" + "fmt" "math/rand" "github.com/gorilla/mux" @@ -51,10 +52,10 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage { // ValidateGenesis performs genesis state validation for the slashing module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState - err := ModuleCdc.UnmarshalJSON(bz, &data) - if err != nil { - return err + if err := ModuleCdc.UnmarshalJSON(bz, &data); err != nil { + return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) } + return ValidateGenesis(data) } diff --git a/x/staking/module.go b/x/staking/module.go index 69a8cb940504..0f39343af843 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -2,6 +2,7 @@ package staking import ( "encoding/json" + "fmt" "math/rand" "github.com/gorilla/mux" @@ -54,10 +55,10 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage { // ValidateGenesis performs genesis state validation for the staking module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState - err := ModuleCdc.UnmarshalJSON(bz, &data) - if err != nil { - return err + if err := ModuleCdc.UnmarshalJSON(bz, &data); err != nil { + return fmt.Errorf("failed to unmarshal %s genesis state: %w", ModuleName, err) } + return ValidateGenesis(data) } diff --git a/x/supply/module.go b/x/supply/module.go index 307b77865b36..f9e45322a99c 100644 --- a/x/supply/module.go +++ b/x/supply/module.go @@ -2,6 +2,7 @@ package supply import ( "encoding/json" + "fmt" "math/rand" "github.com/gorilla/mux" @@ -48,10 +49,10 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage { // ValidateGenesis performs genesis state validation for the supply module. func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { var data GenesisState - err := ModuleCdc.UnmarshalJSON(bz, &data) - if err != nil { - return err + if err := ModuleCdc.UnmarshalJSON(bz, &data); err != nil { + return fmt.Errorf("failed to unmarshal %s genesis state: %w", ModuleName, err) } + return ValidateGenesis(data) }