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

Cleanup genesis state validation + Genutil #5499

Merged
merged 3 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auth

import (
"encoding/json"
"fmt"
"math/rand"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -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)
}

Expand Down
7 changes: 4 additions & 3 deletions x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bank

import (
"encoding/json"
"fmt"
"math/rand"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 3 additions & 1 deletion x/crisis/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package crisis

import (
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -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)
}

Expand Down
7 changes: 4 additions & 3 deletions x/distribution/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package distribution

import (
"encoding/json"
"fmt"
"math/rand"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -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)
}

Expand Down
3 changes: 1 addition & 2 deletions x/evidence/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion x/genutil/client/cli/validate_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions x/genutil/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 5 additions & 4 deletions x/genutil/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package genutil

import (
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -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)
}

Expand Down
7 changes: 7 additions & 0 deletions x/genutil/types/genesis_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions x/gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package gov

import (
"encoding/json"
"fmt"
"math/rand"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -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)
}

Expand Down
7 changes: 4 additions & 3 deletions x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mint

import (
"encoding/json"
"fmt"
"math/rand"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -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)
}

Expand Down
7 changes: 4 additions & 3 deletions x/slashing/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package slashing

import (
"encoding/json"
"fmt"
"math/rand"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -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)
}

Expand Down
7 changes: 4 additions & 3 deletions x/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package staking

import (
"encoding/json"
"fmt"
"math/rand"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -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)
}

Expand Down
7 changes: 4 additions & 3 deletions x/supply/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package supply

import (
"encoding/json"
"fmt"
"math/rand"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -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)
}

Expand Down