Skip to content

Commit

Permalink
changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed Sep 13, 2024
1 parent 4a55e5a commit 2fb2738
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
3 changes: 0 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

<<<<<<< HEAD
* (x/bank) [#21460](https://github.com/cosmos/cosmos-sdk/pull/21460) Added `Sender` attribute in `MsgMultiSend` event.
=======
* (genutil) [#21701](https://github.com/cosmos/cosmos-sdk/pull/21701) Improved error messages for genesis validation.
>>>>>>> 9c5cea08c (feat(x/genutil): add better error messages for genesis validation (#21701))

### Bug Fixes

Expand Down
12 changes: 2 additions & 10 deletions x/genutil/client/cli/validate_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,20 @@ func ValidateGenesisCmd(mbm module.BasicManager) *cobra.Command {

var genState map[string]json.RawMessage
if err = json.Unmarshal(appGenesis.AppState, &genState); err != nil {
<<<<<<< HEAD
return fmt.Errorf("error unmarshalling genesis doc %s: %s", genesis, err.Error())
}

if err = mbm.ValidateGenesis(cdc, clientCtx.TxConfig, genState); err != nil {
return fmt.Errorf("error validating genesis file %s: %s", genesis, err.Error())
=======
if strings.Contains(err.Error(), "unexpected end of JSON input") {
return fmt.Errorf("app_state is missing in the genesis file: %s", err.Error())
}
return fmt.Errorf("error unmarshalling genesis doc %s: %w", genesis, err)
}

if genMM != nil {
if err = genMM.ValidateGenesis(genState); err != nil {
if mbm != nil {
if err = mbm.ValidateGenesis(cdc, clientCtx.TxConfig, genState); err != nil {
errStr := fmt.Sprintf("error validating genesis file %s: %s", genesis, err.Error())
if errors.Is(err, io.EOF) {
errStr = fmt.Sprintf("%s: section is missing in the app_state", errStr)
}
return fmt.Errorf("%s", errStr)
}
>>>>>>> 9c5cea08c (feat(x/genutil): add better error messages for genesis validation (#21701))
}

fmt.Fprintf(cmd.OutOrStdout(), "File at %s is a valid genesis file\n", genesis)
Expand Down
39 changes: 24 additions & 15 deletions x/genutil/client/cli/validate_genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package cli_test

import (
"encoding/json"
"os"
"testing"

"github.com/stretchr/testify/require"

appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/x/staking"

"github.com/cosmos/cosmos-sdk/client"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/types/module"
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
)

Expand All @@ -39,18 +35,17 @@ var v037Exported = `{
}`

func TestValidateGenesis(t *testing.T) {
cdc := testutilmod.MakeTestEncodingConfig(codectestutil.CodecOptions{}, genutil.AppModule{}).Codec
testCases := []struct {
name string
genesis string
expErrStr string
genMM *module.Manager
genMM module.BasicManager
}{
{
"invalid json",
`{"app_state": {x,}}`,
"error at offset 16: invalid character",
module.NewManagerFromMap(nil),
module.NewBasicManager(),
},
{
"invalid: missing module config in app_state",
Expand All @@ -61,15 +56,13 @@ func TestValidateGenesis(t *testing.T) {
return string(bz)
}(),
"section is missing in the app_state",
module.NewManagerFromMap(map[string]appmodulev2.AppModule{
"custommod": staking.NewAppModule(cdc, nil, nil, nil),
}),
module.NewBasicManager(mockModule{}),
},
{
"exported 0.37 genesis file",
v037Exported,
"make sure that you have correctly migrated all CometBFT consensus params",
module.NewManagerFromMap(nil),
module.NewBasicManager(),
},
{
"valid 0.50 genesis file",
Expand All @@ -80,7 +73,7 @@ func TestValidateGenesis(t *testing.T) {
return string(bz)
}(),
"",
module.NewManagerFromMap(nil),
module.NewBasicManager(),
},
}

Expand All @@ -89,7 +82,7 @@ func TestValidateGenesis(t *testing.T) {

t.Run(tc.name, func(t *testing.T) {
genesisFile := testutil.WriteToNewTempFile(t, tc.genesis)
_, err := clitestutil.ExecTestCLICmd(client.Context{}, cli.ValidateGenesisCmd(tc.genMM), []string{genesisFile.Name()})
_, err := clitestutil.ExecTestCLICmd(client.Context{}, cli.ValidateGenesisCmd(nil), []string{genesisFile.Name()})
if tc.expErrStr != "" {
require.Contains(t, err.Error(), tc.expErrStr)
} else {
Expand All @@ -98,3 +91,19 @@ func TestValidateGenesis(t *testing.T) {
})
}
}

type mockModule struct {
module.AppModuleBasic
}

func (mockModule) Name() string {
return "mock"
}

func (mockModule) DefaultGenesis(codec.JSONCodec) json.RawMessage {
return json.RawMessage(`{"foo": "bar"}`)
}

func (mockModule) ValidateGenesis(codec.JSONCodec, client.TxEncodingConfig, json.RawMessage) error {
return nil
}

0 comments on commit 2fb2738

Please sign in to comment.