Merge PR #5499: Cleanup genesis state validation + Genutil

This commit is contained in:
Alexander Bezobchuk 2020-01-09 15:14:23 -05:00 committed by GitHub
parent d7bdfe3110
commit 3df3887597
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 55 additions and 34 deletions

View File

@ -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

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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)
}

View File

@ -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 {

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}