github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/apps/27-interchain-accounts/host/ibc_module.go (about) 1 package host 2 3 import ( 4 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 5 sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors" 6 capabilitytypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/capability/types" 7 "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/apps/27-interchain-accounts/host/keeper" 8 "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/apps/27-interchain-accounts/host/types" 9 icatypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/apps/27-interchain-accounts/types" 10 channeltypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/04-channel/types" 11 ibcporttypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/05-port/types" 12 ibcexported "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/exported" 13 ) 14 15 var ( 16 _ ibcporttypes.IBCModule = IBCModule{} 17 ) 18 19 // IBCModule implements the ICS26 interface for interchain accounts host chains 20 type IBCModule struct { 21 keeper keeper.Keeper 22 } 23 24 // NewIBCModule creates a new IBCModule given the associated keeper 25 func NewIBCModule(k keeper.Keeper) IBCModule { 26 return IBCModule{ 27 keeper: k, 28 } 29 } 30 31 // OnChanOpenInit implements the IBCModule interface 32 func (im IBCModule) OnChanOpenInit( 33 ctx sdk.Context, 34 order channeltypes.Order, 35 connectionHops []string, 36 portID string, 37 channelID string, 38 chanCap *capabilitytypes.Capability, 39 counterparty channeltypes.Counterparty, 40 version string, 41 ) (string, error) { 42 return version, sdkerrors.Wrap(icatypes.ErrInvalidChannelFlow, "channel handshake must be initiated by controller chain") 43 } 44 45 // OnChanOpenTry implements the IBCModule interface 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 string, 55 counterpartyVersion string, 56 ) (string, error) { 57 if !im.keeper.IsHostEnabled(ctx) { 58 return "", types.ErrHostSubModuleDisabled 59 } 60 61 return im.keeper.OnChanOpenTry(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, counterpartyVersion) 62 } 63 64 // OnChanOpenAck implements the IBCModule interface 65 func (im IBCModule) OnChanOpenAck( 66 ctx sdk.Context, 67 portID, 68 channelID string, 69 counterpartyChannelID string, 70 counterpartyVersion string, 71 ) error { 72 return sdkerrors.Wrap(icatypes.ErrInvalidChannelFlow, "channel handshake must be initiated by controller chain") 73 } 74 75 // OnChanOpenAck implements the IBCModule interface 76 func (im IBCModule) OnChanOpenConfirm( 77 ctx sdk.Context, 78 portID, 79 channelID string, 80 ) error { 81 if !im.keeper.IsHostEnabled(ctx) { 82 return types.ErrHostSubModuleDisabled 83 } 84 85 return im.keeper.OnChanOpenConfirm(ctx, portID, channelID) 86 } 87 88 // OnChanCloseInit implements the IBCModule interface 89 func (im IBCModule) OnChanCloseInit( 90 ctx sdk.Context, 91 portID, 92 channelID string, 93 ) error { 94 // Disallow user-initiated channel closing for interchain account channels 95 return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "user cannot close channel") 96 } 97 98 // OnChanCloseConfirm implements the IBCModule interface 99 func (im IBCModule) OnChanCloseConfirm( 100 ctx sdk.Context, 101 portID, 102 channelID string, 103 ) error { 104 return im.keeper.OnChanCloseConfirm(ctx, portID, channelID) 105 } 106 107 // OnRecvPacket implements the IBCModule interface 108 func (im IBCModule) OnRecvPacket( 109 ctx sdk.Context, 110 packet channeltypes.Packet, 111 _ sdk.AccAddress, 112 ) ibcexported.Acknowledgement { 113 if !im.keeper.IsHostEnabled(ctx) { 114 return channeltypes.NewErrorAcknowledgementV4(types.ErrHostSubModuleDisabled) 115 } 116 117 txResponse, err := im.keeper.OnRecvPacket(ctx, packet) 118 ack := channeltypes.NewResultAcknowledgement(txResponse) 119 if err != nil { 120 ack = channeltypes.NewErrorAcknowledgementV4(err) 121 } 122 123 // Emit an event indicating a successful or failed acknowledgement. 124 keeper.EmitAcknowledgementEvent(ctx, packet, ack, err) 125 126 // NOTE: acknowledgement will be written synchronously during IBC handler execution. 127 return ack 128 } 129 130 // OnAcknowledgementPacket implements the IBCModule interface 131 func (im IBCModule) OnAcknowledgementPacket( 132 ctx sdk.Context, 133 packet channeltypes.Packet, 134 acknowledgement []byte, 135 relayer sdk.AccAddress, 136 ) error { 137 return sdkerrors.Wrap(icatypes.ErrInvalidChannelFlow, "cannot receive acknowledgement on a host channel end, a host chain does not send a packet over the channel") 138 } 139 140 // OnTimeoutPacket implements the IBCModule interface 141 func (im IBCModule) OnTimeoutPacket( 142 ctx sdk.Context, 143 packet channeltypes.Packet, 144 relayer sdk.AccAddress, 145 ) error { 146 return sdkerrors.Wrap(icatypes.ErrInvalidChannelFlow, "cannot cause a packet timeout on a host channel end, a host chain does not send a packet over the channel") 147 } 148 149 func (im IBCModule) NegotiateAppVersion(ctx sdk.Context, order channeltypes.Order, connectionID string, portID string, counterparty channeltypes.Counterparty, proposedVersion string) (version string, err error) { 150 return version, nil 151 }