diff --git a/x/auth/tx/builder.go b/x/auth/tx/builder.go index 857d34a08a5..d29e805ad70 100644 --- a/x/auth/tx/builder.go +++ b/x/auth/tx/builder.go @@ -60,6 +60,10 @@ func newBuilder(pubkeyCodec types.PublicKeyCodec) *builder { } func (t *builder) GetMsgs() []sdk.Msg { + if t.tx == nil || t.tx.Body == nil { + return nil + } + anys := t.tx.Body.Messages res := make([]sdk.Msg, len(anys)) for i, any := range anys { diff --git a/x/auth/tx/builder_test.go b/x/auth/tx/builder_test.go index c115f474ac8..f1a7b1140f1 100644 --- a/x/auth/tx/builder_test.go +++ b/x/auth/tx/builder_test.go @@ -110,6 +110,11 @@ func TestTxBuilder(t *testing.T) { require.Equal(t, len(msgs), len(tx.GetMsgs())) require.Equal(t, 1, len(tx.GetPubKeys())) require.Equal(t, pubkey.Bytes(), tx.GetPubKeys()[0].Bytes()) + + tx = &builder{} + require.NotPanics(t, func() { + _ = tx.GetMsgs() + }) } func TestBuilderValidateBasic(t *testing.T) { diff --git a/x/genutil/types/codec.go b/x/genutil/types/codec.go deleted file mode 100644 index 57e2115d072..00000000000 --- a/x/genutil/types/codec.go +++ /dev/null @@ -1,23 +0,0 @@ -package types - -import ( - "github.com/cosmos/cosmos-sdk/codec" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" - sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" -) - -// ModuleCdc defines a generic sealed codec to be used throughout this module -var ModuleCdc *codec.Codec - -// TODO: abstract genesis transactions registration back to staking -// required for genesis transactions -func init() { - ModuleCdc = codec.New() - stakingtypes.RegisterCodec(ModuleCdc) - authtypes.RegisterCodec(ModuleCdc) - sdk.RegisterCodec(ModuleCdc) - cryptocodec.RegisterCrypto(ModuleCdc) - ModuleCdc.Seal() -} diff --git a/x/genutil/types/genesis_state.go b/x/genutil/types/genesis_state.go index 034f6737795..2e801da76a4 100644 --- a/x/genutil/types/genesis_state.go +++ b/x/genutil/types/genesis_state.go @@ -38,10 +38,14 @@ func DefaultGenesisState() GenesisState { // NewGenesisStateFromTx creates a new GenesisState object // from auth transactions -func NewGenesisStateFromTx(genTxs []sdk.Tx) GenesisState { +func NewGenesisStateFromTx(txJSONEncoder sdk.TxEncoder, genTxs []sdk.Tx) GenesisState { genTxsBz := make([]json.RawMessage, len(genTxs)) for i, genTx := range genTxs { - genTxsBz[i] = ModuleCdc.MustMarshalJSON(genTx) + var err error + genTxsBz[i], err = txJSONEncoder(genTx) + if err != nil { + panic(err) + } } return NewGenesisState(genTxsBz) } diff --git a/x/genutil/types/genesis_state_test.go b/x/genutil/types/genesis_state_test.go index 3be955e610d..2da26fa0b77 100644 --- a/x/genutil/types/genesis_state_test.go +++ b/x/genutil/types/genesis_state_test.go @@ -9,7 +9,6 @@ import ( "github.com/tendermint/tendermint/crypto/ed25519" "github.com/cosmos/cosmos-sdk/simapp" - "github.com/cosmos/cosmos-sdk/simapp/params" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/genutil/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" @@ -42,13 +41,13 @@ func TestValidateGenesisMultipleMessages(t *testing.T) { msg2 := stakingtypes.NewMsgCreateValidator(sdk.ValAddress(pk2.Address()), pk2, sdk.NewInt64Coin(sdk.DefaultBondDenom, 50), desc, comm, sdk.OneInt()) - txGen := params.MakeEncodingConfig().TxConfig + txGen := simapp.MakeEncodingConfig().TxConfig txBuilder := txGen.NewTxBuilder() err := txBuilder.SetMsgs(msg1, msg2) require.NoError(t, err) tx := txBuilder.GetTx() - genesisState := types.NewGenesisStateFromTx([]sdk.Tx{tx}) + genesisState := types.NewGenesisStateFromTx(txGen.TxJSONEncoder(), []sdk.Tx{tx}) err = types.ValidateGenesis(genesisState, simapp.MakeEncodingConfig().TxConfig.TxJSONDecoder()) require.Error(t, err) @@ -59,13 +58,13 @@ func TestValidateGenesisBadMessage(t *testing.T) { msg1 := stakingtypes.NewMsgEditValidator(sdk.ValAddress(pk1.Address()), desc, nil, nil) - txGen := params.MakeEncodingConfig().TxConfig + txGen := simapp.MakeEncodingConfig().TxConfig txBuilder := txGen.NewTxBuilder() err := txBuilder.SetMsgs(msg1) require.NoError(t, err) tx := txBuilder.GetTx() - genesisState := types.NewGenesisStateFromTx([]sdk.Tx{tx}) + genesisState := types.NewGenesisStateFromTx(txGen.TxJSONEncoder(), []sdk.Tx{tx}) err = types.ValidateGenesis(genesisState, simapp.MakeEncodingConfig().TxConfig.TxJSONDecoder()) require.Error(t, err)