github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/core/keeper/msg_server.go (about)

     1  package keeper
     2  
     3  import (
     4  	"context"
     5  
     6  	//"github.com/armon/go-metrics"
     7  	//"github.com/cosmos/cosmos-sdk/telemetry"
     8  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  	sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors"
    10  	clienttypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/02-client/types"
    11  	connectiontypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/03-connection/types"
    12  	channeltypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/04-channel/types"
    13  	porttypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/05-port/types"
    14  )
    15  
    16  var _ clienttypes.MsgServer = Keeper{}
    17  var _ connectiontypes.MsgServer = Keeper{}
    18  var _ channeltypes.MsgServer = Keeper{}
    19  
    20  // CreateClient defines a rpc handler method for MsgCreateClient.
    21  func (k Keeper) CreateClient(goCtx context.Context, msg *clienttypes.MsgCreateClient) (*clienttypes.MsgCreateClientResponse, error) {
    22  	ctx := sdk.UnwrapSDKContext(goCtx)
    23  
    24  	clientState, err := clienttypes.UnpackClientState(msg.ClientState)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	consensusState, err := clienttypes.UnpackConsensusState(msg.ConsensusState)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	clientID, err := k.ClientKeeper.CreateClient(ctx, clientState, consensusState)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	ctx.EventManager().EmitEvents(sdk.Events{
    40  		sdk.NewEvent(
    41  			clienttypes.EventTypeCreateClient,
    42  			sdk.NewAttribute(clienttypes.AttributeKeyClientID, clientID),
    43  			sdk.NewAttribute(clienttypes.AttributeKeyClientType, clientState.ClientType()),
    44  			sdk.NewAttribute(clienttypes.AttributeKeyConsensusHeight, clientState.GetLatestHeight().String()),
    45  		),
    46  		sdk.NewEvent(
    47  			sdk.EventTypeMessage,
    48  			sdk.NewAttribute(sdk.AttributeKeyModule, clienttypes.AttributeValueCategory),
    49  		),
    50  	})
    51  
    52  	return &clienttypes.MsgCreateClientResponse{}, nil
    53  }
    54  
    55  // UpdateClient defines a rpc handler method for MsgUpdateClient.
    56  func (k Keeper) UpdateClient(goCtx context.Context, msg *clienttypes.MsgUpdateClient) (*clienttypes.MsgUpdateClientResponse, error) {
    57  	ctx := sdk.UnwrapSDKContext(goCtx)
    58  
    59  	header, err := clienttypes.UnpackHeader(msg.Header)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	if err = k.ClientKeeper.UpdateClient(ctx, msg.ClientId, header); err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	ctx.EventManager().EmitEvent(
    69  		sdk.NewEvent(
    70  			sdk.EventTypeMessage,
    71  			sdk.NewAttribute(sdk.AttributeKeyModule, clienttypes.AttributeValueCategory),
    72  		),
    73  	)
    74  
    75  	return &clienttypes.MsgUpdateClientResponse{}, nil
    76  }
    77  
    78  // UpgradeClient defines a rpc handler method for MsgUpgradeClient.
    79  func (k Keeper) UpgradeClient(goCtx context.Context, msg *clienttypes.MsgUpgradeClient) (*clienttypes.MsgUpgradeClientResponse, error) {
    80  	ctx := sdk.UnwrapSDKContext(goCtx)
    81  
    82  	upgradedClient, err := clienttypes.UnpackClientState(msg.ClientState)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	upgradedConsState, err := clienttypes.UnpackConsensusState(msg.ConsensusState)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	if err = k.ClientKeeper.UpgradeClient(ctx, msg.ClientId, upgradedClient, upgradedConsState,
    92  		msg.ProofUpgradeClient, msg.ProofUpgradeConsensusState); err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	ctx.EventManager().EmitEvent(
    97  		sdk.NewEvent(
    98  			sdk.EventTypeMessage,
    99  			sdk.NewAttribute(sdk.AttributeKeyModule, clienttypes.AttributeValueCategory),
   100  		),
   101  	)
   102  
   103  	return &clienttypes.MsgUpgradeClientResponse{}, nil
   104  }
   105  
   106  // SubmitMisbehaviour defines a rpc handler method for MsgSubmitMisbehaviour.
   107  func (k Keeper) SubmitMisbehaviour(goCtx context.Context, msg *clienttypes.MsgSubmitMisbehaviour) (*clienttypes.MsgSubmitMisbehaviourResponse, error) {
   108  	ctx := sdk.UnwrapSDKContext(goCtx)
   109  
   110  	misbehaviour, err := clienttypes.UnpackMisbehaviour(msg.Misbehaviour)
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	if err := k.ClientKeeper.CheckMisbehaviourAndUpdateState(ctx, misbehaviour); err != nil {
   116  		return nil, sdkerrors.Wrap(err, "failed to process misbehaviour for IBC client")
   117  	}
   118  
   119  	ctx.EventManager().EmitEvent(
   120  		sdk.NewEvent(
   121  			clienttypes.EventTypeSubmitMisbehaviour,
   122  			sdk.NewAttribute(clienttypes.AttributeKeyClientID, msg.ClientId),
   123  			sdk.NewAttribute(clienttypes.AttributeKeyClientType, misbehaviour.ClientType()),
   124  		),
   125  	)
   126  
   127  	return &clienttypes.MsgSubmitMisbehaviourResponse{}, nil
   128  }
   129  
   130  // ConnectionOpenInit defines a rpc handler method for MsgConnectionOpenInit.
   131  func (k Keeper) ConnectionOpenInit(goCtx context.Context, msg *connectiontypes.MsgConnectionOpenInit) (*connectiontypes.MsgConnectionOpenInitResponse, error) {
   132  	ctx := sdk.UnwrapSDKContext(goCtx)
   133  
   134  	connectionID, err := k.ConnectionKeeper.ConnOpenInit(ctx, msg.ClientId, msg.Counterparty, msg.Version, msg.DelayPeriod)
   135  	if err != nil {
   136  		return nil, sdkerrors.Wrap(err, "connection handshake open init failed")
   137  	}
   138  
   139  	ctx.EventManager().EmitEvents(sdk.Events{
   140  		sdk.NewEvent(
   141  			connectiontypes.EventTypeConnectionOpenInit,
   142  			sdk.NewAttribute(connectiontypes.AttributeKeyConnectionID, connectionID),
   143  			sdk.NewAttribute(connectiontypes.AttributeKeyClientID, msg.ClientId),
   144  			sdk.NewAttribute(connectiontypes.AttributeKeyCounterpartyClientID, msg.Counterparty.ClientId),
   145  			sdk.NewAttribute(connectiontypes.AttributeKeyCounterpartyConnectionID, msg.Counterparty.ConnectionId),
   146  		),
   147  		sdk.NewEvent(
   148  			sdk.EventTypeMessage,
   149  			sdk.NewAttribute(sdk.AttributeKeyModule, connectiontypes.AttributeValueCategory),
   150  		),
   151  	})
   152  
   153  	return &connectiontypes.MsgConnectionOpenInitResponse{}, nil
   154  }
   155  
   156  // ConnectionOpenTry defines a rpc handler method for MsgConnectionOpenTry.
   157  func (k Keeper) ConnectionOpenTry(goCtx context.Context, msg *connectiontypes.MsgConnectionOpenTry) (*connectiontypes.MsgConnectionOpenTryResponse, error) {
   158  	ctx := sdk.UnwrapSDKContext(goCtx)
   159  
   160  	targetClient, err := clienttypes.UnpackClientState(msg.ClientState)
   161  	if err != nil {
   162  		return nil, sdkerrors.Wrapf(err, "client in msg is not exported.ClientState. invalid client: %v.", targetClient)
   163  	}
   164  
   165  	connectionID, err := k.ConnectionKeeper.ConnOpenTry(
   166  		ctx, msg.PreviousConnectionId, msg.Counterparty, msg.DelayPeriod, msg.ClientId, targetClient,
   167  		connectiontypes.ProtoVersionsToExported(msg.CounterpartyVersions), msg.ProofInit, msg.ProofClient, msg.ProofConsensus,
   168  		msg.ProofHeight, msg.ConsensusHeight,
   169  	)
   170  	if err != nil {
   171  		return nil, sdkerrors.Wrap(err, "connection handshake open try failed")
   172  	}
   173  
   174  	ctx.EventManager().EmitEvents(sdk.Events{
   175  		sdk.NewEvent(
   176  			connectiontypes.EventTypeConnectionOpenTry,
   177  			sdk.NewAttribute(connectiontypes.AttributeKeyConnectionID, connectionID),
   178  			sdk.NewAttribute(connectiontypes.AttributeKeyClientID, msg.ClientId),
   179  			sdk.NewAttribute(connectiontypes.AttributeKeyCounterpartyClientID, msg.Counterparty.ClientId),
   180  			sdk.NewAttribute(connectiontypes.AttributeKeyCounterpartyConnectionID, msg.Counterparty.ConnectionId),
   181  		),
   182  		sdk.NewEvent(
   183  			sdk.EventTypeMessage,
   184  			sdk.NewAttribute(sdk.AttributeKeyModule, connectiontypes.AttributeValueCategory),
   185  		),
   186  	})
   187  
   188  	return &connectiontypes.MsgConnectionOpenTryResponse{}, nil
   189  }
   190  
   191  // ConnectionOpenAck defines a rpc handler method for MsgConnectionOpenAck.
   192  func (k Keeper) ConnectionOpenAck(goCtx context.Context, msg *connectiontypes.MsgConnectionOpenAck) (*connectiontypes.MsgConnectionOpenAckResponse, error) {
   193  	ctx := sdk.UnwrapSDKContext(goCtx)
   194  	targetClient, err := clienttypes.UnpackClientState(msg.ClientState)
   195  	if err != nil {
   196  		return nil, sdkerrors.Wrapf(err, "client in msg is not exported.ClientState. invalid client: %v", targetClient)
   197  	}
   198  
   199  	if err := k.ConnectionKeeper.ConnOpenAck(
   200  		ctx, msg.ConnectionId, targetClient, msg.Version, msg.CounterpartyConnectionId,
   201  		msg.ProofTry, msg.ProofClient, msg.ProofConsensus,
   202  		msg.ProofHeight, msg.ConsensusHeight,
   203  	); err != nil {
   204  		return nil, sdkerrors.Wrap(err, "connection handshake open ack failed")
   205  	}
   206  
   207  	connectionEnd, _ := k.ConnectionKeeper.GetConnection(ctx, msg.ConnectionId)
   208  
   209  	ctx.EventManager().EmitEvents(sdk.Events{
   210  		sdk.NewEvent(
   211  			connectiontypes.EventTypeConnectionOpenAck,
   212  			sdk.NewAttribute(connectiontypes.AttributeKeyConnectionID, msg.ConnectionId),
   213  			sdk.NewAttribute(connectiontypes.AttributeKeyClientID, connectionEnd.ClientId),
   214  			sdk.NewAttribute(connectiontypes.AttributeKeyCounterpartyClientID, connectionEnd.Counterparty.ClientId),
   215  			sdk.NewAttribute(connectiontypes.AttributeKeyCounterpartyConnectionID, connectionEnd.Counterparty.ConnectionId),
   216  		),
   217  		sdk.NewEvent(
   218  			sdk.EventTypeMessage,
   219  			sdk.NewAttribute(sdk.AttributeKeyModule, connectiontypes.AttributeValueCategory),
   220  		),
   221  	})
   222  
   223  	return &connectiontypes.MsgConnectionOpenAckResponse{}, nil
   224  }
   225  
   226  // ConnectionOpenConfirm defines a rpc handler method for MsgConnectionOpenConfirm.
   227  func (k Keeper) ConnectionOpenConfirm(goCtx context.Context, msg *connectiontypes.MsgConnectionOpenConfirm) (*connectiontypes.MsgConnectionOpenConfirmResponse, error) {
   228  	ctx := sdk.UnwrapSDKContext(goCtx)
   229  
   230  	if err := k.ConnectionKeeper.ConnOpenConfirm(
   231  		ctx, msg.ConnectionId, msg.ProofAck, msg.ProofHeight,
   232  	); err != nil {
   233  		return nil, sdkerrors.Wrap(err, "connection handshake open confirm failed")
   234  	}
   235  
   236  	connectionEnd, _ := k.ConnectionKeeper.GetConnection(ctx, msg.ConnectionId)
   237  
   238  	ctx.EventManager().EmitEvents(sdk.Events{
   239  		sdk.NewEvent(
   240  			connectiontypes.EventTypeConnectionOpenConfirm,
   241  			sdk.NewAttribute(connectiontypes.AttributeKeyConnectionID, msg.ConnectionId),
   242  			sdk.NewAttribute(connectiontypes.AttributeKeyClientID, connectionEnd.ClientId),
   243  			sdk.NewAttribute(connectiontypes.AttributeKeyCounterpartyClientID, connectionEnd.Counterparty.ClientId),
   244  			sdk.NewAttribute(connectiontypes.AttributeKeyCounterpartyConnectionID, connectionEnd.Counterparty.ConnectionId),
   245  		),
   246  		sdk.NewEvent(
   247  			sdk.EventTypeMessage,
   248  			sdk.NewAttribute(sdk.AttributeKeyModule, connectiontypes.AttributeValueCategory),
   249  		),
   250  	})
   251  
   252  	return &connectiontypes.MsgConnectionOpenConfirmResponse{}, nil
   253  }
   254  
   255  // ChannelOpenInit defines a rpc handler method for MsgChannelOpenInit.
   256  func (k Keeper) ChannelOpenInit(goCtx context.Context, msg *channeltypes.MsgChannelOpenInit) (*channeltypes.MsgChannelOpenInitResponse, error) {
   257  	ctx := sdk.UnwrapSDKContext(goCtx)
   258  
   259  	// Lookup module by port capability
   260  	module, portCap, err := k.PortKeeper.LookupModuleByPort(ctx, msg.PortId)
   261  	if err != nil {
   262  		return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id")
   263  	}
   264  
   265  	channelID, cap, err := k.ChannelKeeper.ChanOpenInit(
   266  		ctx, msg.Channel.Ordering, msg.Channel.ConnectionHops, msg.PortId,
   267  		portCap, msg.Channel.Counterparty, msg.Channel.Version,
   268  	)
   269  	if err != nil {
   270  		return nil, sdkerrors.Wrap(err, "channel handshake open init failed")
   271  	}
   272  
   273  	// Retrieve callbacks from router
   274  	cbs, ok := k.Router.GetRoute(module)
   275  	if !ok {
   276  		return nil, sdkerrors.Wrapf(porttypes.ErrInvalidRoute, "route not found to module: %s", module)
   277  	}
   278  
   279  	if _, err = cbs.OnChanOpenInit(ctx, msg.Channel.Ordering, msg.Channel.ConnectionHops, msg.PortId, channelID, cap, msg.Channel.Counterparty, msg.Channel.Version); err != nil {
   280  		return nil, sdkerrors.Wrap(err, "channel open init callback failed")
   281  	}
   282  
   283  	ctx.EventManager().EmitEvents(sdk.Events{
   284  		sdk.NewEvent(
   285  			sdk.EventTypeMessage,
   286  			sdk.NewAttribute(sdk.AttributeKeyModule, channeltypes.AttributeValueCategory),
   287  		),
   288  	})
   289  
   290  	return &channeltypes.MsgChannelOpenInitResponse{}, nil
   291  }
   292  
   293  // ChannelOpenTry defines a rpc handler method for MsgChannelOpenTry.
   294  func (k Keeper) ChannelOpenTry(goCtx context.Context, msg *channeltypes.MsgChannelOpenTry) (*channeltypes.MsgChannelOpenTryResponse, error) {
   295  	ctx := sdk.UnwrapSDKContext(goCtx)
   296  	// Lookup module by port capability
   297  	module, portCap, err := k.PortKeeper.LookupModuleByPort(ctx, msg.PortId)
   298  	if err != nil {
   299  		return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id")
   300  	}
   301  
   302  	channelID, cap, err := k.ChannelKeeper.ChanOpenTryV2(ctx, msg.Channel.Ordering, msg.Channel.ConnectionHops, msg.PortId, msg.PreviousChannelId,
   303  		portCap, msg.Channel.Counterparty, msg.Channel.Version, msg.CounterpartyVersion, msg.ProofInit, msg.ProofHeight,
   304  	)
   305  	if err != nil {
   306  		return nil, sdkerrors.Wrap(err, "channel handshake open try failed")
   307  	}
   308  
   309  	// Retrieve callbacks from router
   310  	cbs, ok := k.Router.GetRoute(module)
   311  	if !ok {
   312  		return nil, sdkerrors.Wrapf(porttypes.ErrInvalidRoute, "route not found to module: %s", module)
   313  	}
   314  
   315  	if _, err = cbs.OnChanOpenTry(ctx, msg.Channel.Ordering, msg.Channel.ConnectionHops, msg.PortId, channelID, cap, msg.Channel.Counterparty, msg.Channel.Version, msg.CounterpartyVersion); err != nil {
   316  		return nil, sdkerrors.Wrap(err, "channel open try callback failed")
   317  	}
   318  
   319  	ctx.EventManager().EmitEvents(sdk.Events{
   320  		sdk.NewEvent(
   321  			sdk.EventTypeMessage,
   322  			sdk.NewAttribute(sdk.AttributeKeyModule, channeltypes.AttributeValueCategory),
   323  		),
   324  	})
   325  
   326  	return &channeltypes.MsgChannelOpenTryResponse{}, nil
   327  }
   328  
   329  // ChannelOpenAck defines a rpc handler method for MsgChannelOpenAck.
   330  func (k Keeper) ChannelOpenAck(goCtx context.Context, msg *channeltypes.MsgChannelOpenAck) (*channeltypes.MsgChannelOpenAckResponse, error) {
   331  	ctx := sdk.UnwrapSDKContext(goCtx)
   332  
   333  	// Lookup module by channel capability
   334  	module, cap, err := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.PortId, msg.ChannelId)
   335  	if err != nil {
   336  		return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id")
   337  	}
   338  
   339  	// Retrieve callbacks from router
   340  	cbs, ok := k.Router.GetRoute(module)
   341  	if !ok {
   342  		return nil, sdkerrors.Wrapf(porttypes.ErrInvalidRoute, "route not found to module: %s", module)
   343  	}
   344  
   345  	err = k.ChannelKeeper.ChanOpenAck(
   346  		ctx, msg.PortId, msg.ChannelId, cap, msg.CounterpartyVersion, msg.CounterpartyChannelId, msg.ProofTry, msg.ProofHeight,
   347  	)
   348  	if err != nil {
   349  		return nil, sdkerrors.Wrap(err, "channel handshake open ack failed")
   350  	}
   351  
   352  	if err = cbs.OnChanOpenAck(ctx, msg.PortId, msg.ChannelId, msg.CounterpartyChannelId, msg.CounterpartyVersion); err != nil {
   353  		return nil, sdkerrors.Wrap(err, "channel open ack callback failed")
   354  	}
   355  
   356  	ctx.EventManager().EmitEvents(sdk.Events{
   357  		sdk.NewEvent(
   358  			sdk.EventTypeMessage,
   359  			sdk.NewAttribute(sdk.AttributeKeyModule, channeltypes.AttributeValueCategory),
   360  		),
   361  	})
   362  
   363  	return &channeltypes.MsgChannelOpenAckResponse{}, nil
   364  }
   365  
   366  // ChannelOpenConfirm defines a rpc handler method for MsgChannelOpenConfirm.
   367  func (k Keeper) ChannelOpenConfirm(goCtx context.Context, msg *channeltypes.MsgChannelOpenConfirm) (*channeltypes.MsgChannelOpenConfirmResponse, error) {
   368  	ctx := sdk.UnwrapSDKContext(goCtx)
   369  
   370  	// Lookup module by channel capability
   371  	module, cap, err := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.PortId, msg.ChannelId)
   372  	if err != nil {
   373  		return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id")
   374  	}
   375  
   376  	// Retrieve callbacks from router
   377  	cbs, ok := k.Router.GetRoute(module)
   378  	if !ok {
   379  		return nil, sdkerrors.Wrapf(porttypes.ErrInvalidRoute, "route not found to module: %s", module)
   380  	}
   381  
   382  	err = k.ChannelKeeper.ChanOpenConfirm(ctx, msg.PortId, msg.ChannelId, cap, msg.ProofAck, msg.ProofHeight)
   383  	if err != nil {
   384  		return nil, sdkerrors.Wrap(err, "channel handshake open confirm failed")
   385  	}
   386  
   387  	if err = cbs.OnChanOpenConfirm(ctx, msg.PortId, msg.ChannelId); err != nil {
   388  		return nil, sdkerrors.Wrap(err, "channel open confirm callback failed")
   389  	}
   390  
   391  	ctx.EventManager().EmitEvents(sdk.Events{
   392  		sdk.NewEvent(
   393  			sdk.EventTypeMessage,
   394  			sdk.NewAttribute(sdk.AttributeKeyModule, channeltypes.AttributeValueCategory),
   395  		),
   396  	})
   397  
   398  	return &channeltypes.MsgChannelOpenConfirmResponse{}, nil
   399  }
   400  
   401  // ChannelCloseInit defines a rpc handler method for MsgChannelCloseInit.
   402  func (k Keeper) ChannelCloseInit(goCtx context.Context, msg *channeltypes.MsgChannelCloseInit) (*channeltypes.MsgChannelCloseInitResponse, error) {
   403  	ctx := sdk.UnwrapSDKContext(goCtx)
   404  	// Lookup module by channel capability
   405  	module, cap, err := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.PortId, msg.ChannelId)
   406  	if err != nil {
   407  		return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id")
   408  	}
   409  
   410  	// Retrieve callbacks from router
   411  	cbs, ok := k.Router.GetRoute(module)
   412  	if !ok {
   413  		return nil, sdkerrors.Wrapf(porttypes.ErrInvalidRoute, "route not found to module: %s", module)
   414  	}
   415  
   416  	if err = cbs.OnChanCloseInit(ctx, msg.PortId, msg.ChannelId); err != nil {
   417  		return nil, sdkerrors.Wrap(err, "channel close init callback failed")
   418  	}
   419  
   420  	err = k.ChannelKeeper.ChanCloseInit(ctx, msg.PortId, msg.ChannelId, cap)
   421  	if err != nil {
   422  		return nil, sdkerrors.Wrap(err, "channel handshake close init failed")
   423  	}
   424  
   425  	ctx.EventManager().EmitEvents(sdk.Events{
   426  		sdk.NewEvent(
   427  			sdk.EventTypeMessage,
   428  			sdk.NewAttribute(sdk.AttributeKeyModule, channeltypes.AttributeValueCategory),
   429  		),
   430  	})
   431  
   432  	return &channeltypes.MsgChannelCloseInitResponse{}, nil
   433  }
   434  
   435  // ChannelCloseConfirm defines a rpc handler method for MsgChannelCloseConfirm.
   436  func (k Keeper) ChannelCloseConfirm(goCtx context.Context, msg *channeltypes.MsgChannelCloseConfirm) (*channeltypes.MsgChannelCloseConfirmResponse, error) {
   437  	ctx := sdk.UnwrapSDKContext(goCtx)
   438  
   439  	// Lookup module by channel capability
   440  	module, cap, err := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.PortId, msg.ChannelId)
   441  	if err != nil {
   442  		return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id")
   443  	}
   444  
   445  	// Retrieve callbacks from router
   446  	cbs, ok := k.Router.GetRoute(module)
   447  	if !ok {
   448  		return nil, sdkerrors.Wrapf(porttypes.ErrInvalidRoute, "route not found to module: %s", module)
   449  	}
   450  
   451  	if err = cbs.OnChanCloseConfirm(ctx, msg.PortId, msg.ChannelId); err != nil {
   452  		return nil, sdkerrors.Wrap(err, "channel close confirm callback failed")
   453  	}
   454  
   455  	err = k.ChannelKeeper.ChanCloseConfirm(ctx, msg.PortId, msg.ChannelId, cap, msg.ProofInit, msg.ProofHeight)
   456  	if err != nil {
   457  		return nil, sdkerrors.Wrap(err, "channel handshake close confirm failed")
   458  	}
   459  
   460  	ctx.EventManager().EmitEvents(sdk.Events{
   461  		sdk.NewEvent(
   462  			sdk.EventTypeMessage,
   463  			sdk.NewAttribute(sdk.AttributeKeyModule, channeltypes.AttributeValueCategory),
   464  		),
   465  	})
   466  
   467  	return &channeltypes.MsgChannelCloseConfirmResponse{}, nil
   468  }
   469  
   470  // RecvPacket defines a rpc handler method for MsgRecvPacket.
   471  func (k Keeper) RecvPacket(goCtx context.Context, msg *channeltypes.MsgRecvPacket) (*channeltypes.MsgRecvPacketResponse, error) {
   472  	ctx := sdk.UnwrapSDKContext(goCtx)
   473  
   474  	relayer, err := sdk.AccAddressFromBech32(msg.Signer)
   475  	if err != nil {
   476  		return nil, sdkerrors.Wrap(err, "Invalid address for msg Signer")
   477  	}
   478  
   479  	// Lookup module by channel capability
   480  	module, cap, err := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.Packet.DestinationPort, msg.Packet.DestinationChannel)
   481  	if err != nil {
   482  		return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id")
   483  	}
   484  
   485  	// Retrieve callbacks from router
   486  	cbs, ok := k.Router.GetRoute(module)
   487  	if !ok {
   488  		return nil, sdkerrors.Wrapf(porttypes.ErrInvalidRoute, "route not found to module: %s", module)
   489  	}
   490  
   491  	// Perform TAO verification
   492  	//
   493  	// If the packet was already received, perform a no-op
   494  	// Use a cached context to prevent accidental state changes
   495  	cacheCtx, writeFn := ctx.CacheContext()
   496  	err = k.ChannelKeeper.RecvPacket(cacheCtx, cap, msg.Packet, msg.ProofCommitment, msg.ProofHeight)
   497  
   498  	// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
   499  	ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
   500  
   501  	switch err {
   502  	case nil:
   503  		writeFn()
   504  	case channeltypes.ErrNoOpMsg:
   505  		return &channeltypes.MsgRecvPacketResponse{}, nil // no-op
   506  	default:
   507  		return nil, sdkerrors.Wrap(err, "receive packet verification failed")
   508  	}
   509  
   510  	// Perform application logic callback
   511  	//
   512  	// Cache context so that we may discard state changes from callback if the acknowledgement is unsuccessful.
   513  	cacheCtx, writeFn = ctx.CacheContext()
   514  	ack := cbs.OnRecvPacket(cacheCtx, msg.Packet, relayer)
   515  	// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
   516  	// Events from callback are emitted regardless of acknowledgement success
   517  	ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
   518  	if ack == nil || ack.Success() {
   519  		// write application state changes for asynchronous and successful acknowledgements
   520  		writeFn()
   521  	}
   522  
   523  	// Set packet acknowledgement only if the acknowledgement is not nil.
   524  	// NOTE: IBC applications modules may call the WriteAcknowledgement asynchronously if the
   525  	// acknowledgement is nil.
   526  	if ack != nil {
   527  		if err := k.ChannelKeeper.WriteAcknowledgement(ctx, cap, msg.Packet, ack); err != nil {
   528  			return nil, err
   529  		}
   530  	}
   531  
   532  	return &channeltypes.MsgRecvPacketResponse{}, nil
   533  }
   534  
   535  // Timeout defines a rpc handler method for MsgTimeout.
   536  func (k Keeper) Timeout(goCtx context.Context, msg *channeltypes.MsgTimeout) (*channeltypes.MsgTimeoutResponse, error) {
   537  	ctx := sdk.UnwrapSDKContext(goCtx)
   538  
   539  	relayer, err := sdk.AccAddressFromBech32(msg.Signer)
   540  	if err != nil {
   541  		return nil, sdkerrors.Wrap(err, "Invalid address for msg Signer")
   542  	}
   543  
   544  	// Lookup module by channel capability
   545  	module, cap, err := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.Packet.SourcePort, msg.Packet.SourceChannel)
   546  	if err != nil {
   547  		return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id")
   548  	}
   549  
   550  	// Retrieve callbacks from router
   551  	cbs, ok := k.Router.GetRoute(module)
   552  	if !ok {
   553  		return nil, sdkerrors.Wrapf(porttypes.ErrInvalidRoute, "route not found to module: %s", module)
   554  	}
   555  
   556  	// Perform TAO verification
   557  	//
   558  	// If the timeout was already received, perform a no-op
   559  	// Use a cached context to prevent accidental state changes
   560  	cacheCtx, writeFn := ctx.CacheContext()
   561  	err = k.ChannelKeeper.TimeoutPacket(cacheCtx, msg.Packet, msg.ProofUnreceived, msg.ProofHeight, msg.NextSequenceRecv)
   562  
   563  	// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
   564  	ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
   565  
   566  	switch err {
   567  	case nil:
   568  		writeFn()
   569  	case channeltypes.ErrNoOpMsg:
   570  		return &channeltypes.MsgTimeoutResponse{}, nil // no-op
   571  	default:
   572  		return nil, sdkerrors.Wrap(err, "timeout packet verification failed")
   573  	}
   574  
   575  	// Perform application logic callback
   576  	err = cbs.OnTimeoutPacket(ctx, msg.Packet, relayer)
   577  	if err != nil {
   578  		return nil, sdkerrors.Wrap(err, "timeout packet callback failed")
   579  	}
   580  
   581  	// Delete packet commitment
   582  	if err = k.ChannelKeeper.TimeoutExecuted(ctx, cap, msg.Packet); err != nil {
   583  		return nil, err
   584  	}
   585  
   586  	return &channeltypes.MsgTimeoutResponse{}, nil
   587  }
   588  
   589  // TimeoutOnClose defines a rpc handler method for MsgTimeoutOnClose.
   590  func (k Keeper) TimeoutOnClose(goCtx context.Context, msg *channeltypes.MsgTimeoutOnClose) (*channeltypes.MsgTimeoutOnCloseResponse, error) {
   591  	ctx := sdk.UnwrapSDKContext(goCtx)
   592  
   593  	relayer, err := sdk.AccAddressFromBech32(msg.Signer)
   594  	if err != nil {
   595  		return nil, sdkerrors.Wrap(err, "Invalid address for msg Signer")
   596  	}
   597  
   598  	// Lookup module by channel capability
   599  	module, cap, err := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.Packet.SourcePort, msg.Packet.SourceChannel)
   600  	if err != nil {
   601  		return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id")
   602  	}
   603  
   604  	// Retrieve callbacks from router
   605  	cbs, ok := k.Router.GetRoute(module)
   606  	if !ok {
   607  		return nil, sdkerrors.Wrapf(porttypes.ErrInvalidRoute, "route not found to module: %s", module)
   608  	}
   609  
   610  	// Perform TAO verification
   611  	//
   612  	// If the timeout was already received, perform a no-op
   613  	// Use a cached context to prevent accidental state changes
   614  	cacheCtx, writeFn := ctx.CacheContext()
   615  	err = k.ChannelKeeper.TimeoutOnClose(cacheCtx, cap, msg.Packet, msg.ProofUnreceived, msg.ProofClose, msg.ProofHeight, msg.NextSequenceRecv)
   616  
   617  	// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
   618  	ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
   619  
   620  	switch err {
   621  	case nil:
   622  		writeFn()
   623  	case channeltypes.ErrNoOpMsg:
   624  		return &channeltypes.MsgTimeoutOnCloseResponse{}, nil // no-op
   625  	default:
   626  		return nil, sdkerrors.Wrap(err, "timeout on close packet verification failed")
   627  	}
   628  
   629  	// Perform application logic callback
   630  	//
   631  	// NOTE: MsgTimeout and MsgTimeoutOnClose use the same "OnTimeoutPacket"
   632  	// application logic callback.
   633  	err = cbs.OnTimeoutPacket(ctx, msg.Packet, relayer)
   634  	if err != nil {
   635  		return nil, sdkerrors.Wrap(err, "timeout packet callback failed")
   636  	}
   637  
   638  	// Delete packet commitment
   639  	if err = k.ChannelKeeper.TimeoutExecuted(ctx, cap, msg.Packet); err != nil {
   640  		return nil, err
   641  	}
   642  
   643  	return &channeltypes.MsgTimeoutOnCloseResponse{}, nil
   644  }
   645  
   646  // Acknowledgement defines a rpc handler method for MsgAcknowledgement.
   647  func (k Keeper) Acknowledgement(goCtx context.Context, msg *channeltypes.MsgAcknowledgement) (*channeltypes.MsgAcknowledgementResponse, error) {
   648  	ctx := sdk.UnwrapSDKContext(goCtx)
   649  
   650  	relayer, err := sdk.AccAddressFromBech32(msg.Signer)
   651  	if err != nil {
   652  		return nil, sdkerrors.Wrap(err, "Invalid address for msg Signer")
   653  	}
   654  
   655  	// Lookup module by channel capability
   656  	module, cap, err := k.ChannelKeeper.LookupModuleByChannel(ctx, msg.Packet.SourcePort, msg.Packet.SourceChannel)
   657  	if err != nil {
   658  		return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id")
   659  	}
   660  
   661  	// Retrieve callbacks from router
   662  	cbs, ok := k.Router.GetRoute(module)
   663  	if !ok {
   664  		return nil, sdkerrors.Wrapf(porttypes.ErrInvalidRoute, "route not found to module: %s", module)
   665  	}
   666  
   667  	// Perform TAO verification
   668  	//
   669  	// If the acknowledgement was already received, perform a no-op
   670  	// Use a cached context to prevent accidental state changes
   671  	cacheCtx, writeFn := ctx.CacheContext()
   672  	err = k.ChannelKeeper.AcknowledgePacket(cacheCtx, cap, msg.Packet, msg.Acknowledgement, msg.ProofAcked, msg.ProofHeight)
   673  
   674  	// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
   675  	ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
   676  
   677  	switch err {
   678  	case nil:
   679  		writeFn()
   680  	case channeltypes.ErrNoOpMsg:
   681  		return &channeltypes.MsgAcknowledgementResponse{}, nil // no-op
   682  	default:
   683  		return nil, sdkerrors.Wrap(err, "acknowledge packet verification failed")
   684  	}
   685  
   686  	// Perform application logic callback
   687  	err = cbs.OnAcknowledgementPacket(ctx, msg.Packet, msg.Acknowledgement, relayer)
   688  	if err != nil {
   689  		return nil, sdkerrors.Wrap(err, "acknowledge packet callback failed")
   690  	}
   691  
   692  	return &channeltypes.MsgAcknowledgementResponse{}, nil
   693  }
   694  
   695  ////