Skip to content

Commit

Permalink
remove amino, enforce serialize and deserialize functions to only acc…
Browse files Browse the repository at this point in the history
…ept ProtoCodec (#725)
  • Loading branch information
colin-axner authored Jan 14, 2022
1 parent 8924ee6 commit e2ac503
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
6 changes: 2 additions & 4 deletions modules/apps/27-interchain-accounts/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ func (AppModuleBasic) Name() string {
return types.ModuleName
}

// RegisterLegacyAminoCodec implements AppModuleBasic interface
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
types.RegisterLegacyAminoCodec(cdc)
}
// RegisterLegacyAminoCodec implements AppModuleBasic.
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {}

// RegisterInterfaces registers module concrete types into protobuf Any
func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
Expand Down
23 changes: 14 additions & 9 deletions modules/apps/27-interchain-accounts/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

Expand All @@ -16,13 +17,6 @@ var (
ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
)

// RegisterLegacyAminoCodec registers the account interfaces and concrete types on the
// provided LegacyAmino codec. These types are used for Amino JSON serialization
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterInterface((*InterchainAccountI)(nil), nil)
cdc.RegisterConcrete(&InterchainAccount{}, "27-interchain-accounts/InterchainAccount", nil)
}

// RegisterInterfaces registers the concrete InterchainAccount implementation against the associated
// x/auth AccountI and GenesisAccount interfaces
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
Expand All @@ -32,8 +26,13 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {

// SerializeCosmosTx serializes a slice of sdk.Msg's using the CosmosTx type. The sdk.Msg's are
// packed into Any's and inserted into the Messages field of a CosmosTx. The proto marshaled CosmosTx
// bytes are returned.
// bytes are returned. Only the ProtoCodec is supported for serializing messages.
func SerializeCosmosTx(cdc codec.BinaryCodec, msgs []sdk.Msg) (bz []byte, err error) {
// only ProtoCodec is supported
if _, ok := cdc.(*codec.ProtoCodec); !ok {
return nil, sdkerrors.Wrap(ErrInvalidCodec, "only ProtoCodec is supported for receiving messages on the host chain")
}

msgAnys := make([]*codectypes.Any, len(msgs))

for i, msg := range msgs {
Expand All @@ -56,8 +55,14 @@ func SerializeCosmosTx(cdc codec.BinaryCodec, msgs []sdk.Msg) (bz []byte, err er
}

// DeserializeCosmosTx unmarshals and unpacks a slice of transaction bytes
// into a slice of sdk.Msg's.
// into a slice of sdk.Msg's. Only the ProtoCodec is supported for message
// deserialization.
func DeserializeCosmosTx(cdc codec.BinaryCodec, data []byte) ([]sdk.Msg, error) {
// only ProtoCodec is supported
if _, ok := cdc.(*codec.ProtoCodec); !ok {
return nil, sdkerrors.Wrap(ErrInvalidCodec, "only ProtoCodec is supported for receiving messages on the host chain")
}

var cosmosTx CosmosTx
if err := cdc.Unmarshal(data, &cosmosTx); err != nil {
return nil, err
Expand Down
26 changes: 24 additions & 2 deletions modules/apps/27-interchain-accounts/types/codec_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types_test

import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
Expand Down Expand Up @@ -42,8 +43,7 @@ func (mockSdkMsg) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{}
}

func (suite *TypesTestSuite) TestSerializeCosmosTx() {

func (suite *TypesTestSuite) TestSerializeAndDeserializeCosmosTx() {
testCases := []struct {
name string
msgs []sdk.Msg
Expand Down Expand Up @@ -127,4 +127,26 @@ func (suite *TypesTestSuite) TestSerializeCosmosTx() {
suite.Require().Error(err, tc.name)
}
}

// test deserializing unknown bytes
msgs, err := types.DeserializeCosmosTx(simapp.MakeTestEncodingConfig().Marshaler, []byte("invalid"))
suite.Require().Error(err)
suite.Require().Empty(msgs)
}

// unregistered bytes causes amino to panic.
// test that DeserializeCosmosTx gracefully returns an error on
// unsupported amino codec.
func (suite *TypesTestSuite) TestDeserializeAndSerializeCosmosTxWithAmino() {
cdc := codec.NewLegacyAmino()
marshaler := codec.NewAminoCodec(cdc)

msgs, err := types.SerializeCosmosTx(marshaler, []sdk.Msg{&banktypes.MsgSend{}})
suite.Require().Error(err)
suite.Require().Empty(msgs)

bz, err := types.DeserializeCosmosTx(marshaler, []byte{0x10, 0})
suite.Require().Error(err)
suite.Require().Empty(bz)

}
1 change: 1 addition & 0 deletions modules/apps/27-interchain-accounts/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ var (
ErrInvalidControllerPort = sdkerrors.Register(ModuleName, 14, "invalid controller port")
ErrInvalidHostPort = sdkerrors.Register(ModuleName, 15, "invalid host port")
ErrInvalidTimeoutTimestamp = sdkerrors.Register(ModuleName, 16, "timeout timestamp must be in the future")
ErrInvalidCodec = sdkerrors.Register(ModuleName, 17, "codec is not supported")
)

0 comments on commit e2ac503

Please sign in to comment.