diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fdd0a915115..3cf3e8e366e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,11 +65,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/bank) [\#8614](https://github.com/cosmos/cosmos-sdk/issues/8614) Add `Name` and `Symbol` fields to denom metadata * (x/auth) [\#8522](https://github.com/cosmos/cosmos-sdk/pull/8522) Allow to query all stored accounts +* (x/ibc) [\#7949](https://github.com/cosmos/cosmos-sdk/issues/7949) Standardized channel `Acknowledgement` moved to its own file. Codec registration redundancy removed. * (crypto/types) [\#8600](https://github.com/cosmos/cosmos-sdk/pull/8600) `CompactBitArray`: optimize the `NumTrueBitsBefore` method and add an `Equal` method. * (store/cachekv), (x/bank/types) [\#8719](https://github.com/cosmos/cosmos-sdk/pull/8719) algorithmically fix pathologically slow code - - ### Bug Fixes * (keyring) [#\8635](https://github.com/cosmos/cosmos-sdk/issues/8635) Remove hardcoded default passphrase value on `NewMnemonic` diff --git a/x/ibc/core/04-channel/types/acknowledgement.go b/x/ibc/core/04-channel/types/acknowledgement.go new file mode 100644 index 000000000000..a3f677ab4410 --- /dev/null +++ b/x/ibc/core/04-channel/types/acknowledgement.go @@ -0,0 +1,50 @@ +package types + +import ( + "strings" + + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + +// NewResultAcknowledgement returns a new instance of Acknowledgement using an Acknowledgement_Result +// type in the Response field. +func NewResultAcknowledgement(result []byte) Acknowledgement { + return Acknowledgement{ + Response: &Acknowledgement_Result{ + Result: result, + }, + } +} + +// NewErrorAcknowledgement returns a new instance of Acknowledgement using an Acknowledgement_Error +// type in the Response field. +func NewErrorAcknowledgement(err string) Acknowledgement { + return Acknowledgement{ + Response: &Acknowledgement_Error{ + Error: err, + }, + } +} + +// GetBytes is a helper for serialising acknowledgements +func (ack Acknowledgement) GetBytes() []byte { + return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&ack)) +} + +// ValidateBasic performs a basic validation of the acknowledgement +func (ack Acknowledgement) ValidateBasic() error { + switch resp := ack.Response.(type) { + case *Acknowledgement_Result: + if len(resp.Result) == 0 { + return sdkerrors.Wrap(ErrInvalidAcknowledgement, "acknowledgement result cannot be empty") + } + case *Acknowledgement_Error: + if strings.TrimSpace(resp.Error) == "" { + return sdkerrors.Wrap(ErrInvalidAcknowledgement, "acknowledgement error cannot be empty") + } + default: + return sdkerrors.Wrapf(ErrInvalidAcknowledgement, "unsupported acknowledgement response field type %T", resp) + } + return nil +} diff --git a/x/ibc/core/04-channel/types/acknowledgement_test.go b/x/ibc/core/04-channel/types/acknowledgement_test.go new file mode 100644 index 000000000000..58074dd7bde5 --- /dev/null +++ b/x/ibc/core/04-channel/types/acknowledgement_test.go @@ -0,0 +1,63 @@ +package types_test + +import "github.com/cosmos/cosmos-sdk/x/ibc/core/04-channel/types" + +// tests acknowledgement.ValidateBasic and acknowledgement.GetBytes +func (suite TypesTestSuite) TestAcknowledgement() { + testCases := []struct { + name string + ack types.Acknowledgement + expPass bool + }{ + { + "valid successful ack", + types.NewResultAcknowledgement([]byte("success")), + true, + }, + { + "valid failed ack", + types.NewErrorAcknowledgement("error"), + true, + }, + { + "empty successful ack", + types.NewResultAcknowledgement([]byte{}), + false, + }, + { + "empty faied ack", + types.NewErrorAcknowledgement(" "), + false, + }, + { + "nil response", + types.Acknowledgement{ + Response: nil, + }, + false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() + + err := tc.ack.ValidateBasic() + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + + // expect all acks to be able to be marshaled + suite.NotPanics(func() { + bz := tc.ack.GetBytes() + suite.Require().NotNil(bz) + }) + }) + } + +} diff --git a/x/ibc/core/04-channel/types/channel.go b/x/ibc/core/04-channel/types/channel.go index 8513a8123df8..b808a220b303 100644 --- a/x/ibc/core/04-channel/types/channel.go +++ b/x/ibc/core/04-channel/types/channel.go @@ -1,9 +1,6 @@ package types import ( - "strings" - - sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" host "github.com/cosmos/cosmos-sdk/x/ibc/core/24-host" "github.com/cosmos/cosmos-sdk/x/ibc/core/exported" @@ -128,45 +125,3 @@ func (ic IdentifiedChannel) ValidateBasic() error { channel := NewChannel(ic.State, ic.Ordering, ic.Counterparty, ic.ConnectionHops, ic.Version) return channel.ValidateBasic() } - -// NewResultAcknowledgement returns a new instance of Acknowledgement using an Acknowledgement_Result -// type in the Response field. -func NewResultAcknowledgement(result []byte) Acknowledgement { - return Acknowledgement{ - Response: &Acknowledgement_Result{ - Result: result, - }, - } -} - -// NewErrorAcknowledgement returns a new instance of Acknowledgement using an Acknowledgement_Error -// type in the Response field. -func NewErrorAcknowledgement(err string) Acknowledgement { - return Acknowledgement{ - Response: &Acknowledgement_Error{ - Error: err, - }, - } -} - -// GetBytes is a helper for serialising acknowledgements -func (ack Acknowledgement) GetBytes() []byte { - return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&ack)) -} - -// ValidateBasic performs a basic validation of the acknowledgement -func (ack Acknowledgement) ValidateBasic() error { - switch resp := ack.Response.(type) { - case *Acknowledgement_Result: - if len(resp.Result) == 0 { - return sdkerrors.Wrap(ErrInvalidAcknowledgement, "acknowledgement result cannot be empty") - } - case *Acknowledgement_Error: - if strings.TrimSpace(resp.Error) == "" { - return sdkerrors.Wrap(ErrInvalidAcknowledgement, "acknowledgement error cannot be empty") - } - default: - return sdkerrors.Wrapf(ErrInvalidAcknowledgement, "unsupported acknowledgement response field type %T", resp) - } - return nil -} diff --git a/x/ibc/core/04-channel/types/channel_test.go b/x/ibc/core/04-channel/types/channel_test.go index 30fee4443b2e..a53304972ef5 100644 --- a/x/ibc/core/04-channel/types/channel_test.go +++ b/x/ibc/core/04-channel/types/channel_test.go @@ -57,63 +57,3 @@ func TestCounterpartyValidateBasic(t *testing.T) { } } } - -// tests acknowledgement.ValidateBasic and acknowledgement.GetBytes -func (suite TypesTestSuite) TestAcknowledgement() { - testCases := []struct { - name string - ack types.Acknowledgement - expPass bool - }{ - { - "valid successful ack", - types.NewResultAcknowledgement([]byte("success")), - true, - }, - { - "valid failed ack", - types.NewErrorAcknowledgement("error"), - true, - }, - { - "empty successful ack", - types.NewResultAcknowledgement([]byte{}), - false, - }, - { - "empty faied ack", - types.NewErrorAcknowledgement(" "), - false, - }, - { - "nil response", - types.Acknowledgement{ - Response: nil, - }, - false, - }, - } - - for _, tc := range testCases { - tc := tc - - suite.Run(tc.name, func() { - suite.SetupTest() - - err := tc.ack.ValidateBasic() - - if tc.expPass { - suite.Require().NoError(err) - } else { - suite.Require().Error(err) - } - - // expect all acks to be able to be marshaled - suite.NotPanics(func() { - bz := tc.ack.GetBytes() - suite.Require().NotNil(bz) - }) - }) - } - -} diff --git a/x/ibc/core/04-channel/types/codec.go b/x/ibc/core/04-channel/types/codec.go index a74f0a7fc9c0..cfe0110264cb 100644 --- a/x/ibc/core/04-channel/types/codec.go +++ b/x/ibc/core/04-channel/types/codec.go @@ -14,25 +14,16 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { registry.RegisterInterface( "ibc.core.channel.v1.ChannelI", (*exported.ChannelI)(nil), + &Channel{}, ) registry.RegisterInterface( "ibc.core.channel.v1.CounterpartyChannelI", (*exported.CounterpartyChannelI)(nil), + &Counterparty{}, ) registry.RegisterInterface( "ibc.core.channel.v1.PacketI", (*exported.PacketI)(nil), - ) - registry.RegisterImplementations( - (*exported.ChannelI)(nil), - &Channel{}, - ) - registry.RegisterImplementations( - (*exported.CounterpartyChannelI)(nil), - &Counterparty{}, - ) - registry.RegisterImplementations( - (*exported.PacketI)(nil), &Packet{}, ) registry.RegisterImplementations(