Skip to content

Commit cd2f81d

Browse files
colin-axnerseantkingdamiannolan
authored
ICA Controller Side Middleware (#417)
* initial draft for ica middleware * add capability handling, fix build and tests * split module.go into ibc_module.go * add middleware handshake tests * fix app.go wiring and various tests * godoc self nits * remove unnecessary error * update comment * fix testing build * split channel keeper interface splits ChannelKeeper interface into ics4Wrapper and core ChannelKeeper * fix tests * remove comments * add callback for timeouts * Apply suggestions from code review Co-authored-by: Sean King <seantking@users.noreply.github.com> Co-authored-by: Damian Nolan <damiannolan@gmail.com> * fix test and update testing README apply test fix pointed out by Sean. Update testing README to reflect how to test with the mock module for middleware * add OnRecvPacket test Add test cases for OnRecvPacket, reused structure in relay_test.go * add failing test case for NegotiateAppVersion Co-authored-by: Sean King <seantking@users.noreply.github.com> Co-authored-by: Damian Nolan <damiannolan@gmail.com>
1 parent 05d434e commit cd2f81d

File tree

12 files changed

+656
-329
lines changed

12 files changed

+656
-329
lines changed

modules/apps/27-interchain-accounts/ibc_module.go

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
package interchain_accounts
22

33
import (
4-
"fmt"
5-
64
sdk "github.com/cosmos/cosmos-sdk/types"
75
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
86
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
97

108
"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/keeper"
11-
"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types"
129
channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types"
1310
porttypes "github.com/cosmos/ibc-go/v2/modules/core/05-port/types"
1411
ibcexported "github.com/cosmos/ibc-go/v2/modules/core/exported"
1512
)
1613

17-
// IBCModule implements the ICS26 callbacks for interchain accounts given the
14+
// IBCModule implements the ICS26 interface for interchain accounts given the
1815
// interchain account keeper and underlying application.
1916
type IBCModule struct {
2017
keeper keeper.Keeper
@@ -29,7 +26,13 @@ func NewIBCModule(k keeper.Keeper, app porttypes.IBCModule) IBCModule {
2926
}
3027
}
3128

32-
// OnChanOpenInit implements the IBCModule interface
29+
// OnChanOpenInit implements the IBCModule interface. Interchain Accounts is
30+
// implemented to act as middleware for connected authentication modules on
31+
// the controller side. The connected modules may not change the controller side portID or
32+
// version. They will be allowed to perform custom logic without changing
33+
// the parameters stored within a channel struct.
34+
//
35+
// Controller Chain
3336
func (im IBCModule) OnChanOpenInit(
3437
ctx sdk.Context,
3538
order channeltypes.Order,
@@ -40,10 +43,18 @@ func (im IBCModule) OnChanOpenInit(
4043
counterparty channeltypes.Counterparty,
4144
version string,
4245
) error {
43-
return im.keeper.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version)
46+
if err := im.keeper.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version); err != nil {
47+
return err
48+
}
49+
50+
// call underlying app's OnChanOpenInit callback with the appVersion
51+
return im.app.OnChanOpenInit(ctx, order, connectionHops, portID, channelID,
52+
chanCap, counterparty, version)
4453
}
4554

4655
// OnChanOpenTry implements the IBCModule interface
56+
//
57+
// Host Chain
4758
func (im IBCModule) OnChanOpenTry(
4859
ctx sdk.Context,
4960
order channeltypes.Order,
@@ -58,17 +69,30 @@ func (im IBCModule) OnChanOpenTry(
5869
return im.keeper.OnChanOpenTry(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version, counterpartyVersion)
5970
}
6071

61-
// OnChanOpenAck implements the IBCModule interface
72+
// OnChanOpenAck implements the IBCModule interface. Interchain Accounts is
73+
// implemented to act as middleware for connected authentication modules on
74+
// the controller side. The connected modules may not change the portID or
75+
// version. They will be allowed to perform custom logic without changing
76+
// the parameters stored within a channel struct.
77+
//
78+
// Controller Chain
6279
func (im IBCModule) OnChanOpenAck(
6380
ctx sdk.Context,
6481
portID,
6582
channelID string,
6683
counterpartyVersion string,
6784
) error {
68-
return im.keeper.OnChanOpenAck(ctx, portID, channelID, counterpartyVersion)
85+
if err := im.keeper.OnChanOpenAck(ctx, portID, channelID, counterpartyVersion); err != nil {
86+
return err
87+
}
88+
89+
// call underlying app's OnChanOpenAck callback with the counterparty app version.
90+
return im.app.OnChanOpenAck(ctx, portID, channelID, counterpartyVersion)
6991
}
7092

71-
// OnChanOpenConfirm implements the IBCModule interface
93+
// OnChanOpenAck implements the IBCModule interface
94+
//
95+
// Host Chain
7296
func (im IBCModule) OnChanOpenConfirm(
7397
ctx sdk.Context,
7498
portID,
@@ -97,18 +121,15 @@ func (im IBCModule) OnChanCloseConfirm(
97121
}
98122

99123
// OnRecvPacket implements the IBCModule interface
124+
//
125+
// Host Chain
100126
func (im IBCModule) OnRecvPacket(
101127
ctx sdk.Context,
102128
packet channeltypes.Packet,
103129
_ sdk.AccAddress,
104130
) ibcexported.Acknowledgement {
105131
ack := channeltypes.NewResultAcknowledgement([]byte{byte(1)})
106132

107-
var data types.InterchainAccountPacketData
108-
if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil {
109-
ack = channeltypes.NewErrorAcknowledgement(fmt.Sprintf("cannot unmarshal ICS-27 interchain account packet data: %s", err.Error()))
110-
}
111-
112133
// only attempt the application logic if the packet data
113134
// was successfully decoded
114135
if ack.Success() {
@@ -123,36 +144,31 @@ func (im IBCModule) OnRecvPacket(
123144
}
124145

125146
// OnAcknowledgementPacket implements the IBCModule interface
147+
//
148+
// Controller Chain
126149
func (im IBCModule) OnAcknowledgementPacket(
127150
ctx sdk.Context,
128151
packet channeltypes.Packet,
129152
acknowledgement []byte,
130-
_ sdk.AccAddress,
153+
relayer sdk.AccAddress,
131154
) error {
132-
var ack channeltypes.Acknowledgement
133-
134-
if err := types.ModuleCdc.UnmarshalJSON(acknowledgement, &ack); err != nil {
135-
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-27 interchain account packet acknowledgment: %v", err)
136-
}
137-
var data types.InterchainAccountPacketData
138-
if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil {
139-
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-27 interchain account packet data: %s", err.Error())
140-
}
141-
142-
if err := im.keeper.OnAcknowledgementPacket(ctx, packet, data, ack); err != nil {
143-
return err
144-
}
145-
146-
return nil
155+
// call underlying app's OnAcknowledgementPacket callback.
156+
return im.app.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer)
147157
}
148158

149159
// OnTimeoutPacket implements the IBCModule interface
160+
//
161+
// Controller Chain
150162
func (im IBCModule) OnTimeoutPacket(
151163
ctx sdk.Context,
152164
packet channeltypes.Packet,
153-
_ sdk.AccAddress,
165+
relayer sdk.AccAddress,
154166
) error {
155-
return im.keeper.OnTimeoutPacket(ctx, packet)
167+
if err := im.keeper.OnTimeoutPacket(ctx, packet); err != nil {
168+
return err
169+
}
170+
171+
return im.app.OnTimeoutPacket(ctx, packet, relayer)
156172
}
157173

158174
// NegotiateAppVersion implements the IBCModule interface

0 commit comments

Comments
 (0)