Skip to content

Commit

Permalink
Merge PR #5499: Cleanup genesis state validation + Genutil
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez committed Mar 10, 2020
1 parent 6a9f7a5 commit 9c64b02
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 42 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [v0.37.78] - TBD

### Bug Fixes

* (x/genutil) [\#5499](https://github.com/cosmos/cosmos-sdk/pull/) Ensure `DefaultGenesis` returns valid and non-nil default genesis state.

## [v0.37.7] - 2020-02-10

### Improvements
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"

"github.com/gorilla/mux"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -42,10 +43,10 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage {
// module validate genesis
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
8 changes: 4 additions & 4 deletions x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package bank

import (
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/spf13/cobra"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/client/context"
Expand Down Expand Up @@ -40,10 +40,10 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage {
// module validate genesis
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
8 changes: 4 additions & 4 deletions x/distribution/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package distribution

import (
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/spf13/cobra"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/client/context"
Expand Down Expand Up @@ -43,10 +43,10 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage {
// module validate genesis
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
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
}
10 changes: 5 additions & 5 deletions x/genutil/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package genutil

import (
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/spf13/cobra"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/client/context"
Expand Down Expand Up @@ -33,16 +33,16 @@ func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {}

// default genesis state
func (AppModuleBasic) DefaultGenesis() json.RawMessage {
return ModuleCdc.MustMarshalJSON(GenesisState{})
return ModuleCdc.MustMarshalJSON(types.DefaultGenesisState())
}

// module validate genesis
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
12 changes: 6 additions & 6 deletions x/gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package gov

import (
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/spf13/cobra"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/client/context"
Expand Down Expand Up @@ -39,7 +39,7 @@ var _ module.AppModuleBasic = AppModuleBasic{}

// module name
func (AppModuleBasic) Name() string {
return types.ModuleName
return ModuleName
}

// register module codec
Expand All @@ -49,16 +49,16 @@ func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {

// default genesis state
func (AppModuleBasic) DefaultGenesis() json.RawMessage {
return types.ModuleCdc.MustMarshalJSON(DefaultGenesisState())
return ModuleCdc.MustMarshalJSON(DefaultGenesisState())
}

// module validate genesis
func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error {
var data GenesisState
err := types.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
8 changes: 4 additions & 4 deletions x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package mint

import (
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/spf13/cobra"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/client/context"
Expand Down Expand Up @@ -42,10 +42,10 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage {
// module validate genesis
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
8 changes: 4 additions & 4 deletions x/slashing/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package slashing

import (
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/spf13/cobra"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/client/context"
Expand Down Expand Up @@ -45,10 +45,10 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage {
// module validate genesis
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
8 changes: 4 additions & 4 deletions x/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package staking

import (
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

abci "github.com/tendermint/tendermint/abci/types"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto"
Expand Down Expand Up @@ -49,10 +49,10 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage {
// module validate genesis
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
8 changes: 4 additions & 4 deletions x/supply/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package supply

import (
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/spf13/cobra"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/client/context"
Expand Down Expand Up @@ -43,10 +43,10 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage {
// module validate genesis
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

0 comments on commit 9c64b02

Please sign in to comment.