Migrate pieces of x/genutil missed in #6734 (#6860)

* Migrate pieces of x/genutil missed in 6734

* Fix tests

* Fix lint

* Add test
This commit is contained in:
Aaron Craelius 2020-07-27 11:41:20 -04:00 committed by GitHub
parent 359e9a031c
commit bb8aabd4e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 30 deletions

View File

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

View File

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

View File

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

View File

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

View File

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