-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split ica module.go into ibc_module.go (#453)
Splits IBCModule logic into separate ibc_module.go file and updates app.go registration
- Loading branch information
1 parent
3271da8
commit 0db5c95
Showing
3 changed files
with
164 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters