Skip to content

Commit 0db5c95

Browse files
authored
split ica module.go into ibc_module.go (#453)
Splits IBCModule logic into separate ibc_module.go file and updates app.go registration
1 parent 3271da8 commit 0db5c95

File tree

3 files changed

+164
-138
lines changed

3 files changed

+164
-138
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package interchain_accounts
2+
3+
import (
4+
"fmt"
5+
6+
sdk "github.com/cosmos/cosmos-sdk/types"
7+
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
8+
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
9+
10+
"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"
12+
channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types"
13+
porttypes "github.com/cosmos/ibc-go/v2/modules/core/05-port/types"
14+
ibcexported "github.com/cosmos/ibc-go/v2/modules/core/exported"
15+
)
16+
17+
// IBCModule implements the ICS26 callbacks for interchain accounts given the
18+
// interchain account keeper and underlying application.
19+
type IBCModule struct {
20+
keeper keeper.Keeper
21+
app porttypes.IBCModule
22+
}
23+
24+
// NewIBCModule creates a new IBCModule given the keeper and underlying application
25+
func NewIBCModule(k keeper.Keeper, app porttypes.IBCModule) IBCModule {
26+
return IBCModule{
27+
keeper: k,
28+
app: app,
29+
}
30+
}
31+
32+
// Implement IBCModule callbacks
33+
func (im IBCModule) OnChanOpenInit(
34+
ctx sdk.Context,
35+
order channeltypes.Order,
36+
connectionHops []string,
37+
portID string,
38+
channelID string,
39+
chanCap *capabilitytypes.Capability,
40+
counterparty channeltypes.Counterparty,
41+
version string,
42+
) error {
43+
return im.keeper.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version)
44+
}
45+
46+
func (im IBCModule) OnChanOpenTry(
47+
ctx sdk.Context,
48+
order channeltypes.Order,
49+
connectionHops []string,
50+
portID,
51+
channelID string,
52+
chanCap *capabilitytypes.Capability,
53+
counterparty channeltypes.Counterparty,
54+
version,
55+
counterpartyVersion string,
56+
) error {
57+
return im.keeper.OnChanOpenTry(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version, counterpartyVersion)
58+
}
59+
60+
func (im IBCModule) OnChanOpenAck(
61+
ctx sdk.Context,
62+
portID,
63+
channelID string,
64+
counterpartyVersion string,
65+
) error {
66+
return im.keeper.OnChanOpenAck(ctx, portID, channelID, counterpartyVersion)
67+
}
68+
69+
func (im IBCModule) OnChanOpenConfirm(
70+
ctx sdk.Context,
71+
portID,
72+
channelID string,
73+
) error {
74+
return im.keeper.OnChanOpenConfirm(ctx, portID, channelID)
75+
}
76+
77+
func (im IBCModule) OnChanCloseInit(
78+
ctx sdk.Context,
79+
portID,
80+
channelID string,
81+
) error {
82+
// Disallow user-initiated channel closing for interchain account channels
83+
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "user cannot close channel")
84+
}
85+
86+
func (im IBCModule) OnChanCloseConfirm(
87+
ctx sdk.Context,
88+
portID,
89+
channelID string,
90+
) error {
91+
return nil
92+
}
93+
94+
func (im IBCModule) OnRecvPacket(
95+
ctx sdk.Context,
96+
packet channeltypes.Packet,
97+
_ sdk.AccAddress,
98+
) ibcexported.Acknowledgement {
99+
ack := channeltypes.NewResultAcknowledgement([]byte{byte(1)})
100+
101+
var data types.IBCAccountPacketData
102+
if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil {
103+
ack = channeltypes.NewErrorAcknowledgement(fmt.Sprintf("cannot unmarshal ICS-27 interchain account packet data: %s", err.Error()))
104+
}
105+
106+
// only attempt the application logic if the packet data
107+
// was successfully decoded
108+
if ack.Success() {
109+
err := im.keeper.OnRecvPacket(ctx, packet)
110+
if err != nil {
111+
ack = channeltypes.NewErrorAcknowledgement(err.Error())
112+
}
113+
}
114+
115+
// NOTE: acknowledgement will be written synchronously during IBC handler execution.
116+
return ack
117+
}
118+
119+
func (im IBCModule) OnAcknowledgementPacket(
120+
ctx sdk.Context,
121+
packet channeltypes.Packet,
122+
acknowledgement []byte,
123+
_ sdk.AccAddress,
124+
) error {
125+
var ack channeltypes.Acknowledgement
126+
127+
if err := types.ModuleCdc.UnmarshalJSON(acknowledgement, &ack); err != nil {
128+
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-27 interchain account packet acknowledgment: %v", err)
129+
}
130+
var data types.IBCAccountPacketData
131+
if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil {
132+
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-27 interchain account packet data: %s", err.Error())
133+
}
134+
135+
if err := im.keeper.OnAcknowledgementPacket(ctx, packet, data, ack); err != nil {
136+
return err
137+
}
138+
139+
return nil
140+
}
141+
142+
func (im IBCModule) OnTimeoutPacket(
143+
ctx sdk.Context,
144+
packet channeltypes.Packet,
145+
_ sdk.AccAddress,
146+
) error {
147+
// TODO
148+
return nil
149+
}
150+
151+
// NegotiateAppVersion implements the IBCModule interface
152+
func (im IBCModule) NegotiateAppVersion(
153+
ctx sdk.Context,
154+
order channeltypes.Order,
155+
connectionID string,
156+
portID string,
157+
counterparty channeltypes.Counterparty,
158+
proposedVersion string,
159+
) (string, error) {
160+
return im.keeper.NegotiateAppVersion(ctx, order, connectionID, portID, counterparty, proposedVersion)
161+
}

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

+1-137
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package interchain_accounts
22

33
import (
44
"encoding/json"
5-
"fmt"
65

76
"github.com/gorilla/mux"
87
"github.com/spf13/cobra"
@@ -11,23 +10,19 @@ import (
1110
"github.com/cosmos/cosmos-sdk/codec"
1211
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
1312
sdk "github.com/cosmos/cosmos-sdk/types"
14-
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
1513
"github.com/cosmos/cosmos-sdk/types/module"
16-
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
1714
"github.com/grpc-ecosystem/grpc-gateway/runtime"
1815
abci "github.com/tendermint/tendermint/abci/types"
1916

2017
"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/client/cli"
2118
"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/keeper"
2219
"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types"
23-
channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types"
2420
porttypes "github.com/cosmos/ibc-go/v2/modules/core/05-port/types"
25-
ibcexported "github.com/cosmos/ibc-go/v2/modules/core/exported"
2621
)
2722

2823
var (
2924
_ module.AppModule = AppModule{}
30-
_ porttypes.IBCModule = AppModule{}
25+
_ porttypes.IBCModule = IBCModule{}
3126
_ module.AppModuleBasic = AppModuleBasic{}
3227
)
3328

@@ -135,134 +130,3 @@ func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
135130
func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate {
136131
return []abci.ValidatorUpdate{}
137132
}
138-
139-
// Implement IBCModule callbacks
140-
func (am AppModule) OnChanOpenInit(
141-
ctx sdk.Context,
142-
order channeltypes.Order,
143-
connectionHops []string,
144-
portID string,
145-
channelID string,
146-
chanCap *capabilitytypes.Capability,
147-
counterparty channeltypes.Counterparty,
148-
version string,
149-
) error {
150-
return am.keeper.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version)
151-
}
152-
153-
func (am AppModule) OnChanOpenTry(
154-
ctx sdk.Context,
155-
order channeltypes.Order,
156-
connectionHops []string,
157-
portID,
158-
channelID string,
159-
chanCap *capabilitytypes.Capability,
160-
counterparty channeltypes.Counterparty,
161-
version,
162-
counterpartyVersion string,
163-
) error {
164-
return am.keeper.OnChanOpenTry(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version, counterpartyVersion)
165-
}
166-
167-
func (am AppModule) OnChanOpenAck(
168-
ctx sdk.Context,
169-
portID,
170-
channelID string,
171-
counterpartyVersion string,
172-
) error {
173-
return am.keeper.OnChanOpenAck(ctx, portID, channelID, counterpartyVersion)
174-
}
175-
176-
func (am AppModule) OnChanOpenConfirm(
177-
ctx sdk.Context,
178-
portID,
179-
channelID string,
180-
) error {
181-
return am.keeper.OnChanOpenConfirm(ctx, portID, channelID)
182-
}
183-
184-
func (am AppModule) OnChanCloseInit(
185-
ctx sdk.Context,
186-
portID,
187-
channelID string,
188-
) error {
189-
// Disallow user-initiated channel closing for interchain account channels
190-
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "user cannot close channel")
191-
}
192-
193-
func (am AppModule) OnChanCloseConfirm(
194-
ctx sdk.Context,
195-
portID,
196-
channelID string,
197-
) error {
198-
return nil
199-
}
200-
201-
func (am AppModule) OnRecvPacket(
202-
ctx sdk.Context,
203-
packet channeltypes.Packet,
204-
_ sdk.AccAddress,
205-
) ibcexported.Acknowledgement {
206-
ack := channeltypes.NewResultAcknowledgement([]byte{byte(1)})
207-
208-
var data types.IBCAccountPacketData
209-
if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil {
210-
ack = channeltypes.NewErrorAcknowledgement(fmt.Sprintf("cannot unmarshal ICS-27 interchain account packet data: %s", err.Error()))
211-
}
212-
213-
// only attempt the application logic if the packet data
214-
// was successfully decoded
215-
if ack.Success() {
216-
err := am.keeper.OnRecvPacket(ctx, packet)
217-
if err != nil {
218-
ack = channeltypes.NewErrorAcknowledgement(err.Error())
219-
}
220-
}
221-
222-
// NOTE: acknowledgement will be written synchronously during IBC handler execution.
223-
return ack
224-
}
225-
226-
func (am AppModule) OnAcknowledgementPacket(
227-
ctx sdk.Context,
228-
packet channeltypes.Packet,
229-
acknowledgement []byte,
230-
_ sdk.AccAddress,
231-
) error {
232-
var ack channeltypes.Acknowledgement
233-
234-
if err := types.ModuleCdc.UnmarshalJSON(acknowledgement, &ack); err != nil {
235-
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-27 interchain account packet acknowledgment: %v", err)
236-
}
237-
var data types.IBCAccountPacketData
238-
if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil {
239-
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-27 interchain account packet data: %s", err.Error())
240-
}
241-
242-
if err := am.keeper.OnAcknowledgementPacket(ctx, packet, data, ack); err != nil {
243-
return err
244-
}
245-
246-
return nil
247-
}
248-
249-
func (am AppModule) OnTimeoutPacket(
250-
ctx sdk.Context,
251-
packet channeltypes.Packet,
252-
_ sdk.AccAddress,
253-
) error {
254-
// TODO
255-
return nil
256-
}
257-
258-
// NegotiateAppVersion implements the IBCModule interface
259-
func (am AppModule) NegotiateAppVersion(
260-
ctx sdk.Context,
261-
order channeltypes.Order,
262-
connectionID string,
263-
portID string,
264-
counterparty channeltypes.Counterparty,
265-
proposedVersion string,
266-
) (string, error) {
267-
return am.keeper.NegotiateAppVersion(ctx, order, connectionID, portID, counterparty, proposedVersion)
268-
}

testing/simapp/app.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ func NewSimApp(
338338
app.AccountKeeper, scopedICAKeeper, app.MsgServiceRouter(), app,
339339
)
340340
icaModule := ica.NewAppModule(app.ICAKeeper)
341+
icaIBCModule := ica.NewIBCModule(app.ICAKeeper, nil)
341342

342343
// NOTE: the IBC mock keeper and application module is used only for testing core IBC. Do
343344
// note replicate if you do not need to test core IBC or light clients.
@@ -346,7 +347,7 @@ func NewSimApp(
346347
// Create static IBC router, add transfer route, then set and seal it
347348
ibcRouter := porttypes.NewRouter()
348349
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferModule)
349-
ibcRouter.AddRoute(icatypes.ModuleName, icaModule)
350+
ibcRouter.AddRoute(icatypes.ModuleName, icaIBCModule)
350351
ibcRouter.AddRoute(ibcmock.ModuleName, mockModule)
351352
app.IBCKeeper.SetRouter(ibcRouter)
352353

0 commit comments

Comments
 (0)