From 458ecd1a8b45423939ef75d65f8c41f438c6c93c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Wed, 29 Sep 2021 12:45:30 +0200 Subject: [PATCH] split ica module.go into ibc_module.go Splits IBCModule logic into separate ibc_module.go file and updates app.go registration --- .../apps/27-interchain-accounts/ibc_module.go | 161 ++++++++++++++++++ modules/apps/27-interchain-accounts/module.go | 138 +-------------- testing/simapp/app.go | 3 +- 3 files changed, 164 insertions(+), 138 deletions(-) create mode 100644 modules/apps/27-interchain-accounts/ibc_module.go diff --git a/modules/apps/27-interchain-accounts/ibc_module.go b/modules/apps/27-interchain-accounts/ibc_module.go new file mode 100644 index 00000000000..80e798af826 --- /dev/null +++ b/modules/apps/27-interchain-accounts/ibc_module.go @@ -0,0 +1,161 @@ +package interchain_accounts + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/keeper" + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" + porttypes "github.com/cosmos/ibc-go/v2/modules/core/05-port/types" + ibcexported "github.com/cosmos/ibc-go/v2/modules/core/exported" +) + +// IBCModule implements the ICS26 callbacks for interchain accounts given the +// interchain account keeper and underlying application. +type IBCModule struct { + keeper keeper.Keeper + app porttypes.IBCModule +} + +// NewIBCModule creates a new IBCModule given the keeper and underlying application +func NewIBCModule(k keeper.Keeper, app porttypes.IBCModule) IBCModule { + return IBCModule{ + keeper: k, + app: app, + } +} + +// Implement IBCModule callbacks +func (im IBCModule) OnChanOpenInit( + ctx sdk.Context, + order channeltypes.Order, + connectionHops []string, + portID string, + channelID string, + chanCap *capabilitytypes.Capability, + counterparty channeltypes.Counterparty, + version string, +) error { + return im.keeper.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version) +} + +func (im IBCModule) OnChanOpenTry( + ctx sdk.Context, + order channeltypes.Order, + connectionHops []string, + portID, + channelID string, + chanCap *capabilitytypes.Capability, + counterparty channeltypes.Counterparty, + version, + counterpartyVersion string, +) error { + return im.keeper.OnChanOpenTry(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version, counterpartyVersion) +} + +func (im IBCModule) OnChanOpenAck( + ctx sdk.Context, + portID, + channelID string, + counterpartyVersion string, +) error { + return im.keeper.OnChanOpenAck(ctx, portID, channelID, counterpartyVersion) +} + +func (im IBCModule) OnChanOpenConfirm( + ctx sdk.Context, + portID, + channelID string, +) error { + return im.keeper.OnChanOpenConfirm(ctx, portID, channelID) +} + +func (im IBCModule) OnChanCloseInit( + ctx sdk.Context, + portID, + channelID string, +) error { + // Disallow user-initiated channel closing for interchain account channels + return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "user cannot close channel") +} + +func (im IBCModule) OnChanCloseConfirm( + ctx sdk.Context, + portID, + channelID string, +) error { + return nil +} + +func (im IBCModule) OnRecvPacket( + ctx sdk.Context, + packet channeltypes.Packet, + _ sdk.AccAddress, +) ibcexported.Acknowledgement { + ack := channeltypes.NewResultAcknowledgement([]byte{byte(1)}) + + var data types.IBCAccountPacketData + if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil { + ack = channeltypes.NewErrorAcknowledgement(fmt.Sprintf("cannot unmarshal ICS-27 interchain account packet data: %s", err.Error())) + } + + // only attempt the application logic if the packet data + // was successfully decoded + if ack.Success() { + err := im.keeper.OnRecvPacket(ctx, packet) + if err != nil { + ack = channeltypes.NewErrorAcknowledgement(err.Error()) + } + } + + // NOTE: acknowledgement will be written synchronously during IBC handler execution. + return ack +} + +func (im IBCModule) OnAcknowledgementPacket( + ctx sdk.Context, + packet channeltypes.Packet, + acknowledgement []byte, + _ sdk.AccAddress, +) error { + var ack channeltypes.Acknowledgement + + if err := types.ModuleCdc.UnmarshalJSON(acknowledgement, &ack); err != nil { + return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-27 interchain account packet acknowledgment: %v", err) + } + var data types.IBCAccountPacketData + if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil { + return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-27 interchain account packet data: %s", err.Error()) + } + + if err := im.keeper.OnAcknowledgementPacket(ctx, packet, data, ack); err != nil { + return err + } + + return nil +} + +func (im IBCModule) OnTimeoutPacket( + ctx sdk.Context, + packet channeltypes.Packet, + _ sdk.AccAddress, +) error { + // TODO + return nil +} + +// NegotiateAppVersion implements the IBCModule interface +func (im IBCModule) NegotiateAppVersion( + ctx sdk.Context, + order channeltypes.Order, + connectionID string, + portID string, + counterparty channeltypes.Counterparty, + proposedVersion string, +) (string, error) { + return im.keeper.NegotiateAppVersion(ctx, order, connectionID, portID, counterparty, proposedVersion) +} diff --git a/modules/apps/27-interchain-accounts/module.go b/modules/apps/27-interchain-accounts/module.go index 59a02825112..ab085b886dc 100644 --- a/modules/apps/27-interchain-accounts/module.go +++ b/modules/apps/27-interchain-accounts/module.go @@ -2,7 +2,6 @@ package interchain_accounts import ( "encoding/json" - "fmt" "github.com/gorilla/mux" "github.com/spf13/cobra" @@ -11,23 +10,19 @@ import ( "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/module" - capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" "github.com/grpc-ecosystem/grpc-gateway/runtime" abci "github.com/tendermint/tendermint/abci/types" "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/client/cli" "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/keeper" "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" - channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" porttypes "github.com/cosmos/ibc-go/v2/modules/core/05-port/types" - ibcexported "github.com/cosmos/ibc-go/v2/modules/core/exported" ) var ( _ module.AppModule = AppModule{} - _ porttypes.IBCModule = AppModule{} + _ porttypes.IBCModule = IBCModule{} _ module.AppModuleBasic = AppModuleBasic{} ) @@ -135,134 +130,3 @@ func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate { return []abci.ValidatorUpdate{} } - -// Implement IBCModule callbacks -func (am AppModule) OnChanOpenInit( - ctx sdk.Context, - order channeltypes.Order, - connectionHops []string, - portID string, - channelID string, - chanCap *capabilitytypes.Capability, - counterparty channeltypes.Counterparty, - version string, -) error { - return am.keeper.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version) -} - -func (am AppModule) OnChanOpenTry( - ctx sdk.Context, - order channeltypes.Order, - connectionHops []string, - portID, - channelID string, - chanCap *capabilitytypes.Capability, - counterparty channeltypes.Counterparty, - version, - counterpartyVersion string, -) error { - return am.keeper.OnChanOpenTry(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version, counterpartyVersion) -} - -func (am AppModule) OnChanOpenAck( - ctx sdk.Context, - portID, - channelID string, - counterpartyVersion string, -) error { - return am.keeper.OnChanOpenAck(ctx, portID, channelID, counterpartyVersion) -} - -func (am AppModule) OnChanOpenConfirm( - ctx sdk.Context, - portID, - channelID string, -) error { - return am.keeper.OnChanOpenConfirm(ctx, portID, channelID) -} - -func (am AppModule) OnChanCloseInit( - ctx sdk.Context, - portID, - channelID string, -) error { - // Disallow user-initiated channel closing for interchain account channels - return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "user cannot close channel") -} - -func (am AppModule) OnChanCloseConfirm( - ctx sdk.Context, - portID, - channelID string, -) error { - return nil -} - -func (am AppModule) OnRecvPacket( - ctx sdk.Context, - packet channeltypes.Packet, - _ sdk.AccAddress, -) ibcexported.Acknowledgement { - ack := channeltypes.NewResultAcknowledgement([]byte{byte(1)}) - - var data types.IBCAccountPacketData - if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil { - ack = channeltypes.NewErrorAcknowledgement(fmt.Sprintf("cannot unmarshal ICS-27 interchain account packet data: %s", err.Error())) - } - - // only attempt the application logic if the packet data - // was successfully decoded - if ack.Success() { - err := am.keeper.OnRecvPacket(ctx, packet) - if err != nil { - ack = channeltypes.NewErrorAcknowledgement(err.Error()) - } - } - - // NOTE: acknowledgement will be written synchronously during IBC handler execution. - return ack -} - -func (am AppModule) OnAcknowledgementPacket( - ctx sdk.Context, - packet channeltypes.Packet, - acknowledgement []byte, - _ sdk.AccAddress, -) error { - var ack channeltypes.Acknowledgement - - if err := types.ModuleCdc.UnmarshalJSON(acknowledgement, &ack); err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-27 interchain account packet acknowledgment: %v", err) - } - var data types.IBCAccountPacketData - if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-27 interchain account packet data: %s", err.Error()) - } - - if err := am.keeper.OnAcknowledgementPacket(ctx, packet, data, ack); err != nil { - return err - } - - return nil -} - -func (am AppModule) OnTimeoutPacket( - ctx sdk.Context, - packet channeltypes.Packet, - _ sdk.AccAddress, -) error { - // TODO - return nil -} - -// NegotiateAppVersion implements the IBCModule interface -func (am AppModule) NegotiateAppVersion( - ctx sdk.Context, - order channeltypes.Order, - connectionID string, - portID string, - counterparty channeltypes.Counterparty, - proposedVersion string, -) (string, error) { - return am.keeper.NegotiateAppVersion(ctx, order, connectionID, portID, counterparty, proposedVersion) -} diff --git a/testing/simapp/app.go b/testing/simapp/app.go index 211fe55c212..bb50b230aad 100644 --- a/testing/simapp/app.go +++ b/testing/simapp/app.go @@ -338,6 +338,7 @@ func NewSimApp( app.AccountKeeper, scopedICAKeeper, app.MsgServiceRouter(), app, ) icaModule := ica.NewAppModule(app.ICAKeeper) + icaIBCModule := ica.NewIBCModule(app.ICAKeeper, nil) // NOTE: the IBC mock keeper and application module is used only for testing core IBC. Do // note replicate if you do not need to test core IBC or light clients. @@ -346,7 +347,7 @@ func NewSimApp( // Create static IBC router, add transfer route, then set and seal it ibcRouter := porttypes.NewRouter() ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferModule) - ibcRouter.AddRoute(icatypes.ModuleName, icaModule) + ibcRouter.AddRoute(icatypes.ModuleName, icaIBCModule) ibcRouter.AddRoute(ibcmock.ModuleName, mockModule) app.IBCKeeper.SetRouter(ibcRouter)