ibc: MsgTransfer amino JSON, commits from @fedekunze (#8437)

This commit is contained in:
colin axnér 2021-01-26 13:03:21 +01:00 committed by GitHub
parent 50b4be9110
commit 09b8f7783a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 7 deletions

View File

@ -36,6 +36,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
## [Unreleased]
### State Machine Breaking
* (x/ibc) [\#8266](https://github.com/cosmos/cosmos-sdk/issues/8266) Add amino JSON for IBC messages in order to support Ledger text signing.
### Improvements
* (x/ibc) [\#8404](https://github.com/cosmos/cosmos-sdk/pull/8404) Reorder IBC `ChanOpenAck` and `ChanOpenConfirm` handler execution to perform core handler first, followed by application callbacks.

View File

@ -186,8 +186,7 @@ func (s *IntegrationTestSuite) TestBroadcastIBCTxRequest() {
res, err := rest.PostRequest(fmt.Sprintf("%s/txs", val.APIAddress), "application/json", []byte(req))
s.Require().NoError(err)
// Make sure the error message is correct.
s.Require().Contains(string(res), "this transaction cannot be broadcasted via legacy REST endpoints")
s.Require().NotContains(string(res), "this transaction cannot be broadcasted via legacy REST endpoints", string(res))
}
// Helper function to test querying txs. We will use it to query StdTx and service `Msg`s.

View File

@ -46,7 +46,9 @@ func (AppModuleBasic) Name() string {
}
// RegisterLegacyAminoCodec implements AppModuleBasic interface
func (AppModuleBasic) RegisterLegacyAminoCodec(*codec.LegacyAmino) {}
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
types.RegisterLegacyAminoCodec(cdc)
}
// RegisterInterfaces registers module concrete types into protobuf Any.
func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {

View File

@ -7,6 +7,12 @@ import (
"github.com/cosmos/cosmos-sdk/types/msgservice"
)
// RegisterLegacyAminoCodec registers the necessary x/ibc transfer interfaces and concrete types
// on the provided LegacyAmino codec. These types are used for Amino JSON serialization.
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgTransfer{}, "cosmos-sdk/MsgTransfer", nil)
}
// RegisterInterfaces register the ibc transfer module interfaces to protobuf
// Any.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
@ -16,10 +22,20 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
}
var (
amino = codec.NewLegacyAmino()
// ModuleCdc references the global x/ibc-transfer 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-transfer and
// The actual codec used for serialization should be provided to x/ibc transfer and
// defined at the application level.
ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
// AminoCdc is a amino codec created to support amino json compatible msgs.
AminoCdc = codec.NewAminoCodec(amino)
)
func init() {
RegisterLegacyAminoCodec(amino)
amino.Seal()
}

View File

@ -70,10 +70,9 @@ func (msg MsgTransfer) ValidateBasic() error {
return ValidateIBCDenom(msg.Token.Denom)
}
// GetSignBytes implements sdk.Msg. The function will panic since it is used
// for amino transaction verification which IBC does not support.
// GetSignBytes implements sdk.Msg.
func (msg MsgTransfer) GetSignBytes() []byte {
panic("IBC messages do not support amino")
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// GetSigners implements sdk.Msg

View File

@ -1,6 +1,7 @@
package types
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
@ -51,6 +52,15 @@ func TestMsgTransferType(t *testing.T) {
require.Equal(t, "transfer", msg.Type())
}
func TestMsgTransferGetSignBytes(t *testing.T) {
msg := NewMsgTransfer(validPort, validChannel, coin, addr1, addr2, timeoutHeight, 0)
expected := fmt.Sprintf(`{"type":"cosmos-sdk/MsgTransfer","value":{"receiver":"%s","sender":"%s","source_channel":"testchannel","source_port":"testportid","timeout_height":{"revision_height":"10"},"token":{"amount":"100","denom":"atom"}}}`, addr2, addr1)
require.NotPanics(t, func() {
res := msg.GetSignBytes()
require.Equal(t, expected, string(res))
})
}
// TestMsgTransferValidation tests ValidateBasic for MsgTransfer
func TestMsgTransferValidation(t *testing.T) {
testCases := []struct {