RiccardoMontagnin

Multiple packets handling

Jun 15th, 2021
736
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // OnRecvPacket implements the IBCModule interface
  2. func (am AppModule) OnRecvPacket(
  3.   ctx sdk.Context,
  4.   packet channeltypes.Packet,
  5. ) (*sdk.Result, []byte, error) {
  6.   var ack channeltypes.Acknowledgement
  7.   var err error
  8.  
  9.   // Try handling the chain link packet data
  10.   ack, err = am.handleLinkChainAccountPacketData(ctx, packet)
  11.   if types.ErrInvalidPacketData.Is(err) {
  12.     // Try handling the oracle request packet data
  13.     ack, err = am.handleOracleRequestPacketData(ctx, packet)
  14.   }
  15.  
  16.   // If packet data is still invalid, return an error
  17.   if types.ErrInvalidPacketData.Is(err) {
  18.     return nil, nil, err
  19.   }
  20.  
  21.   // ...
  22. }
  23.  
  24. // handleLinkChainAccountPacketData tries handling a LinkChainAccountPacketData packet.
  25. // Returns ErrInvalidPacketData if the given packet data is not of such type.
  26. func (am AppModule) handleLinkChainAccountPacketData(
  27.   ctx sdk.Context, packet channeltypes.Packet,
  28. ) (channeltypes.Acknowledgement, error) {
  29.   var packetData types.LinkChainAccountPacketData
  30.   if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &packetData); err != nil {
  31.     return channeltypes.Acknowledgement{}, sdkerrors.Wrapf(types.ErrInvalidPacketData, "%T", packet)
  32.   }
  33.   // ...
  34. }
  35.  
  36. // handleOracleRequestPacketData tries handling a OracleResponsePacketData packet.
  37. // Returns ErrInvalidPacketData if the given packet data is not of such type.
  38. func (am AppModule) handleOracleRequestPacketData(
  39.   ctx sdk.Context, packet channeltypes.Packet,
  40. ) (channeltypes.Acknowledgement, error) {
  41.   var data oracletypes.OracleResponsePacketData
  42.   if err := oracletypes.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil {
  43.     return channeltypes.Acknowledgement{}, sdkerrors.Wrapf(types.ErrInvalidPacketData, "%T", packet)
  44.   }
  45.  
  46.   // ...
  47. }
RAW Paste Data