From e4378e747dc40171573034a499d2e2a202de73a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?colin=20axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Fri, 23 Oct 2020 19:14:12 +0200 Subject: [PATCH] IBC: panic on GetSignBytes and remove SubModuleCdc (#7645) * panic on GetSignBytes and remove SubModuleCdc where possible * fix build Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> --- x/ibc/applications/transfer/types/msgs.go | 5 +- .../applications/transfer/types/msgs_test.go | 10 ---- x/ibc/core/02-client/types/codec.go | 10 ---- x/ibc/core/02-client/types/msgs.go | 21 ++++---- x/ibc/core/03-connection/types/msgs.go | 20 +++++--- x/ibc/core/04-channel/types/msgs.go | 50 +++++++++++-------- x/ibc/core/04-channel/types/msgs_test.go | 12 ----- x/ibc/core/23-commitment/types/codec.go | 10 ---- .../06-solomachine/types/codec.go | 9 ---- .../07-tendermint/types/codec.go | 10 ---- .../light-clients/09-localhost/types/codec.go | 8 --- 11 files changed, 57 insertions(+), 108 deletions(-) diff --git a/x/ibc/applications/transfer/types/msgs.go b/x/ibc/applications/transfer/types/msgs.go index f5d31e0794..25ff69e715 100644 --- a/x/ibc/applications/transfer/types/msgs.go +++ b/x/ibc/applications/transfer/types/msgs.go @@ -64,9 +64,10 @@ func (msg MsgTransfer) ValidateBasic() error { return ValidateIBCDenom(msg.Token.Denom) } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgTransfer) GetSignBytes() []byte { - return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg diff --git a/x/ibc/applications/transfer/types/msgs_test.go b/x/ibc/applications/transfer/types/msgs_test.go index be3320ba43..4c9eb80acd 100644 --- a/x/ibc/applications/transfer/types/msgs_test.go +++ b/x/ibc/applications/transfer/types/msgs_test.go @@ -1,7 +1,6 @@ package types import ( - "fmt" "testing" "github.com/stretchr/testify/require" @@ -85,15 +84,6 @@ func TestMsgTransferValidation(t *testing.T) { } } -// TestMsgTransferGetSignBytes tests GetSignBytes for MsgTransfer -func TestMsgTransferGetSignBytes(t *testing.T) { - msg := NewMsgTransfer(validPort, validChannel, coin, addr1, addr2, clienttypes.NewHeight(0, 110), 10) - res := msg.GetSignBytes() - - expected := fmt.Sprintf(`{"receiver":"cosmos1w3jhxarpv3j8yvs7f9y7g","sender":"%s","source_channel":"testchannel","source_port":"testportid","timeout_height":{"version_height":"110","version_number":"0"},"timeout_timestamp":"10","token":{"amount":"100","denom":"atom"}}`, addr1.String()) - require.Equal(t, expected, string(res)) -} - // TestMsgTransferGetSigners tests GetSigners for MsgTransfer func TestMsgTransferGetSigners(t *testing.T) { msg := NewMsgTransfer(validPort, validChannel, coin, addr1, addr2, timeoutHeight, 0) diff --git a/x/ibc/core/02-client/types/codec.go b/x/ibc/core/02-client/types/codec.go index 5678c6bfd4..76402a1116 100644 --- a/x/ibc/core/02-client/types/codec.go +++ b/x/ibc/core/02-client/types/codec.go @@ -3,7 +3,6 @@ package types import ( proto "github.com/gogo/protobuf/proto" - "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" @@ -44,15 +43,6 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { ) } -var ( - // SubModuleCdc references the global x/ibc/core/02-client module codec. Note, the codec should - // ONLY be used in certain instances of tests and for JSON encoding. - // - // The actual codec used for serialization should be provided to x/ibc/core/02-client and - // defined at the application level. - SubModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) -) - // PackClientState constructs a new Any packed with the given client state value. It returns // an error if the client state can't be casted to a protobuf message or if the concrete // implemention is not registered to the protobuf codec. diff --git a/x/ibc/core/02-client/types/msgs.go b/x/ibc/core/02-client/types/msgs.go index 07738bfc27..7d3e4af219 100644 --- a/x/ibc/core/02-client/types/msgs.go +++ b/x/ibc/core/02-client/types/msgs.go @@ -87,9 +87,10 @@ func (msg MsgCreateClient) ValidateBasic() error { return host.ClientIdentifierValidator(msg.ClientId) } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgCreateClient) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg @@ -156,9 +157,10 @@ func (msg MsgUpdateClient) ValidateBasic() error { return host.ClientIdentifierValidator(msg.ClientId) } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgUpdateClient) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg @@ -233,9 +235,10 @@ func (msg MsgUpgradeClient) ValidateBasic() error { return host.ClientIdentifierValidator(msg.ClientId) } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgUpgradeClient) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg @@ -299,10 +302,10 @@ func (msg MsgSubmitMisbehaviour) ValidateBasic() error { return host.ClientIdentifierValidator(msg.ClientId) } -// GetSignBytes returns the raw bytes a signer is expected to sign when submitting -// a MsgSubmitMisbehaviour message. +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgSubmitMisbehaviour) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners returns the single expected signer for a MsgSubmitMisbehaviour. diff --git a/x/ibc/core/03-connection/types/msgs.go b/x/ibc/core/03-connection/types/msgs.go index 7974cf0244..a461dd1cca 100644 --- a/x/ibc/core/03-connection/types/msgs.go +++ b/x/ibc/core/03-connection/types/msgs.go @@ -58,9 +58,10 @@ func (msg MsgConnectionOpenInit) ValidateBasic() error { return msg.Counterparty.ValidateBasic() } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgConnectionOpenInit) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg @@ -172,9 +173,10 @@ func (msg MsgConnectionOpenTry) UnpackInterfaces(unpacker codectypes.AnyUnpacker return nil } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgConnectionOpenTry) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg @@ -268,9 +270,10 @@ func (msg MsgConnectionOpenAck) ValidateBasic() error { return nil } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgConnectionOpenAck) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg @@ -325,9 +328,10 @@ func (msg MsgConnectionOpenConfirm) ValidateBasic() error { return nil } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgConnectionOpenConfirm) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg diff --git a/x/ibc/core/04-channel/types/msgs.go b/x/ibc/core/04-channel/types/msgs.go index 2fb0a2b183..ebd9730e01 100644 --- a/x/ibc/core/04-channel/types/msgs.go +++ b/x/ibc/core/04-channel/types/msgs.go @@ -50,9 +50,10 @@ func (msg MsgChannelOpenInit) ValidateBasic() error { return msg.Channel.ValidateBasic() } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgChannelOpenInit) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg @@ -118,9 +119,10 @@ func (msg MsgChannelOpenTry) ValidateBasic() error { return msg.Channel.ValidateBasic() } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgChannelOpenTry) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg @@ -182,9 +184,10 @@ func (msg MsgChannelOpenAck) ValidateBasic() error { return nil } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgChannelOpenAck) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg @@ -241,9 +244,10 @@ func (msg MsgChannelOpenConfirm) ValidateBasic() error { return nil } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgChannelOpenConfirm) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg @@ -291,9 +295,10 @@ func (msg MsgChannelCloseInit) ValidateBasic() error { return nil } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgChannelCloseInit) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg @@ -350,9 +355,10 @@ func (msg MsgChannelCloseConfirm) ValidateBasic() error { return nil } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgChannelCloseConfirm) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg @@ -400,9 +406,10 @@ func (msg MsgRecvPacket) ValidateBasic() error { return msg.Packet.ValidateBasic() } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgRecvPacket) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetDataSignBytes returns the base64-encoded bytes used for the @@ -463,9 +470,10 @@ func (msg MsgTimeout) ValidateBasic() error { return msg.Packet.ValidateBasic() } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgTimeout) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg @@ -522,9 +530,10 @@ func (msg MsgTimeoutOnClose) ValidateBasic() error { return msg.Packet.ValidateBasic() } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgTimeoutOnClose) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg @@ -576,9 +585,10 @@ func (msg MsgAcknowledgement) ValidateBasic() error { return msg.Packet.ValidateBasic() } -// GetSignBytes implements sdk.Msg +// GetSignBytes implements sdk.Msg. The function will panic since it is used +// for amino transaction verification which IBC does not support. func (msg MsgAcknowledgement) GetSignBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&msg)) + panic("IBC messages do not support amino") } // GetSigners implements sdk.Msg diff --git a/x/ibc/core/04-channel/types/msgs_test.go b/x/ibc/core/04-channel/types/msgs_test.go index 3edebb031b..c7c1bbb4ba 100644 --- a/x/ibc/core/04-channel/types/msgs_test.go +++ b/x/ibc/core/04-channel/types/msgs_test.go @@ -346,18 +346,6 @@ func (suite *TypesTestSuite) TestMsgRecvPacketValidateBasic() { } } -func (suite *TypesTestSuite) TestMsgRecvPacketGetSignBytes() { - msg := types.NewMsgRecvPacket(packet, suite.proof, height, addr) - res := msg.GetSignBytes() - - expected := fmt.Sprintf( - `{"packet":{"data":%s,"destination_channel":"testcpchannel","destination_port":"testcpport","sequence":"1","source_channel":"testchannel","source_port":"testportid","timeout_height":{"version_height":"100","version_number":"0"},"timeout_timestamp":"100"},"proof":"Co0BCi4KCmljczIzOmlhdmwSA0tFWRobChkKA0tFWRIFVkFMVUUaCwgBGAEgASoDAAICClsKDGljczIzOnNpbXBsZRIMaWF2bFN0b3JlS2V5Gj0KOwoMaWF2bFN0b3JlS2V5EiAcIiDXSHQRSvh/Wa07MYpTK0B4XtbaXtzxBED76xk0WhoJCAEYASABKgEA","proof_height":{"version_height":"1","version_number":"0"},"signer":"%s"}`, - string(msg.GetDataSignBytes()), - addr.String(), - ) - suite.Equal(expected, string(res)) -} - func (suite *TypesTestSuite) TestMsgRecvPacketGetSigners() { msg := types.NewMsgRecvPacket(packet, suite.proof, height, addr) res := msg.GetSigners() diff --git a/x/ibc/core/23-commitment/types/codec.go b/x/ibc/core/23-commitment/types/codec.go index 4d7312cd77..1195c7c26d 100644 --- a/x/ibc/core/23-commitment/types/codec.go +++ b/x/ibc/core/23-commitment/types/codec.go @@ -1,7 +1,6 @@ package types import ( - "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/x/ibc/core/exported" ) @@ -42,12 +41,3 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { &MerkleProof{}, ) } - -var ( - // SubModuleCdc references the global x/ibc/core/23-commitmentl module codec. Note, the codec should - // ONLY be used in certain instances of tests and for JSON encoding. - // - // The actual codec used for serialization should be provided to x/ibc/core/23-commitmentl and - // defined at the application level. - SubModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) -) diff --git a/x/ibc/light-clients/06-solomachine/types/codec.go b/x/ibc/light-clients/06-solomachine/types/codec.go index 6907a93514..313a910ca9 100644 --- a/x/ibc/light-clients/06-solomachine/types/codec.go +++ b/x/ibc/light-clients/06-solomachine/types/codec.go @@ -30,15 +30,6 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { ) } -var ( - // SubModuleCdc references the global x/ibc/light-clients/06-solomachine module codec. Note, the codec - // should ONLY be used in certain instances of tests and for JSON encoding. - // - // The actual codec used for serialization should be provided to x/ibc/light-clients/06-solomachine and - // defined at the application level. - SubModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) -) - func UnmarshalSignatureData(cdc codec.BinaryMarshaler, data []byte) (signing.SignatureData, error) { protoSigData := &signing.SignatureDescriptor_Data{} if err := cdc.UnmarshalBinaryBare(data, protoSigData); err != nil { diff --git a/x/ibc/light-clients/07-tendermint/types/codec.go b/x/ibc/light-clients/07-tendermint/types/codec.go index c46aa0d795..f0ff8b2f56 100644 --- a/x/ibc/light-clients/07-tendermint/types/codec.go +++ b/x/ibc/light-clients/07-tendermint/types/codec.go @@ -1,7 +1,6 @@ package types import ( - "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/x/ibc/core/exported" ) @@ -30,12 +29,3 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { &Header{}, ) } - -var ( - // SubModuleCdc references the global x/ibc/light-clients/07-tendermint module codec. Note, the codec should - // ONLY be used in certain instances of tests and for JSON encoding. - // - // The actual codec used for serialization should be provided to x/ibc/light-clients/07-tendermint and - // defined at the application level. - SubModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) -) diff --git a/x/ibc/light-clients/09-localhost/types/codec.go b/x/ibc/light-clients/09-localhost/types/codec.go index 009989bf1a..b338dfb699 100644 --- a/x/ibc/light-clients/09-localhost/types/codec.go +++ b/x/ibc/light-clients/09-localhost/types/codec.go @@ -1,7 +1,6 @@ package types import ( - "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/x/ibc/core/exported" ) @@ -14,10 +13,3 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { &ClientState{}, ) } - -var ( - // SubModuleCdc references the global x/ibc/light-clients/09-localhost module codec. - // The actual codec used for serialization should be provided to x/ibc/light-clients/09-localhost and - // defined at the application level. - SubModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) -)