Skip to content

Commit 843b459

Browse files
feat: adding Pack/Unpack acknowledgement helper fns (#895)
* feat: adding Pack/Unpack acknowledgement helper fns * chore: changelog * fix: docs * Update modules/core/04-channel/types/codec.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
1 parent 8f62a47 commit 843b459

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
7676
* [\#432](https://github.com/cosmos/ibc-go/pull/432) Introduce `MockIBCApp` struct to the mock module. Allows the mock module to be reused to perform custom logic on each IBC App interface function. This might be useful when testing out IBC applications written as middleware.
7777
* [\#380](https://github.com/cosmos/ibc-go/pull/380) Adding the Interchain Accounts module v1
7878
* [\#679](https://github.com/cosmos/ibc-go/pull/679) New CLI command `query ibc-transfer denom-hash <denom trace>` to get the denom hash for a denom trace; this might be useful for debug
79+
* (channel) [\#895](https://github.com/cosmos/ibc-go/pull/895) Adding UnpackAcknowledgement and PackAcknowledgement helper functions to pack or unpack an Acknowledgement to and from a proto Any type
80+
7981

8082
### Bug Fixes
8183

modules/core/04-channel/types/codec.go

+34
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"github.com/cosmos/cosmos-sdk/codec"
55
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
66
sdk "github.com/cosmos/cosmos-sdk/types"
7+
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
78
"github.com/cosmos/cosmos-sdk/types/msgservice"
9+
proto "github.com/gogo/protobuf/proto"
810

911
"github.com/cosmos/ibc-go/v3/modules/core/exported"
1012
)
@@ -50,3 +52,35 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
5052
// The actual codec used for serialization should be provided to x/ibc/core/04-channel and
5153
// defined at the application level.
5254
var SubModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
55+
56+
// UnpackAcknowledgement unpacks an Any into an Acknowledgement. It returns an error if the
57+
// Any can't be unpacked into an Acknowledgement.
58+
func UnpackAcknowledgement(any *codectypes.Any) (exported.Acknowledgement, error) {
59+
if any == nil {
60+
return nil, sdkerrors.Wrap(sdkerrors.ErrUnpackAny, "protobuf Any message cannot be nil")
61+
}
62+
63+
ack, ok := any.GetCachedValue().(exported.Acknowledgement)
64+
if !ok {
65+
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnpackAny, "cannot unpack Any into Acknowledgement %T", any)
66+
}
67+
68+
return ack, nil
69+
}
70+
71+
// PackAcknowledgement constructs a new Any packed with the given acknowledgement value. It returns
72+
// an error if the acknowledgement can't be casted to a protobuf message or if the concrete
73+
// implemention is not registered to the protobuf codec.
74+
func PackAcknowledgement(acknowledgement exported.Acknowledgement) (*codectypes.Any, error) {
75+
msg, ok := acknowledgement.(proto.Message)
76+
if !ok {
77+
return nil, sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", acknowledgement)
78+
}
79+
80+
anyAcknowledgement, err := codectypes.NewAnyWithValue(msg)
81+
if err != nil {
82+
return nil, sdkerrors.Wrap(sdkerrors.ErrPackAny, err.Error())
83+
}
84+
85+
return anyAcknowledgement, nil
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package types_test
2+
3+
import (
4+
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
5+
6+
"github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
7+
"github.com/cosmos/ibc-go/v3/modules/core/exported"
8+
ibcmock "github.com/cosmos/ibc-go/v3/testing/mock"
9+
)
10+
11+
type caseAny struct {
12+
name string
13+
any *codectypes.Any
14+
expPass bool
15+
}
16+
17+
func (suite *TypesTestSuite) TestPackAcknowledgement() {
18+
19+
testCases := []struct {
20+
name string
21+
acknowledgement exported.Acknowledgement
22+
expPass bool
23+
}{
24+
{
25+
"success",
26+
&ibcmock.MockAcknowledgement,
27+
true,
28+
},
29+
{
30+
"nil",
31+
nil,
32+
false,
33+
},
34+
}
35+
36+
testCasesAny := []caseAny{}
37+
38+
for _, tc := range testCases {
39+
ackAny, err := types.PackAcknowledgement(tc.acknowledgement)
40+
if tc.expPass {
41+
suite.Require().NoError(err, tc.name)
42+
} else {
43+
suite.Require().Error(err, tc.name)
44+
}
45+
46+
testCasesAny = append(testCasesAny, caseAny{tc.name, ackAny, tc.expPass})
47+
}
48+
49+
for i, tc := range testCasesAny {
50+
cs, err := types.UnpackAcknowledgement(tc.any)
51+
if tc.expPass {
52+
suite.Require().NoError(err, tc.name)
53+
suite.Require().Equal(testCases[i].acknowledgement, cs, tc.name)
54+
} else {
55+
suite.Require().Error(err, tc.name)
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)