-
Notifications
You must be signed in to change notification settings - Fork 2
/
message.go
94 lines (83 loc) · 2.96 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package ibc
import (
"context"
"strings"
codec "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibcchanneltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
jsoniter "github.com/json-iterator/go"
"github.com/bro-n-bro/spacebox-crawler/types"
"github.com/bro-n-bro/spacebox/broker/model"
)
type icaHostData struct {
Data []byte `json:"data"`
}
// HandleMessageRecursive implements types.RecursiveMessagesHandler.
// Handles ibc types messages.
// For MsgTransfer message types returns slice of messages to be handled recursively.
func (m *Module) HandleMessageRecursive(
ctx context.Context,
index int,
cosmosMsg sdk.Msg,
tx *types.Tx,
) ([]*codec.Any, error) {
switch msg := cosmosMsg.(type) {
case *ibctransfertypes.MsgTransfer:
return nil, m.broker.PublishTransferMessage(ctx, model.TransferMessage{
SourceChannel: msg.SourceChannel,
Coin: m.tbM.MapCoin(types.NewCoinFromSDK(msg.Token)),
Sender: msg.Sender,
Receiver: msg.Receiver,
Height: tx.Height,
MsgIndex: int64(index),
TxHash: tx.TxHash,
})
case *ibcchanneltypes.MsgAcknowledgement:
return nil, m.broker.PublishAcknowledgementMessage(ctx, model.AcknowledgementMessage{
SourcePort: msg.Packet.SourcePort,
SourceChannel: msg.Packet.SourceChannel,
DestinationPort: msg.Packet.DestinationPort,
DestinationChannel: msg.Packet.DestinationChannel,
Data: msg.Packet.Data,
TimeoutTimestamp: msg.Packet.TimeoutTimestamp,
Signer: msg.Signer,
Height: tx.Height,
MsgIndex: int64(index),
TxHash: tx.TxHash,
})
case *ibcchanneltypes.MsgRecvPacket:
if err := m.broker.PublishReceivePacketMessage(ctx, model.RecvPacketMessage{
SourcePort: msg.Packet.SourcePort,
SourceChannel: msg.Packet.SourceChannel,
DestinationPort: msg.Packet.DestinationPort,
DestinationChannel: msg.Packet.DestinationChannel,
Signer: msg.Signer,
Data: msg.Packet.Data,
TimeoutTimestamp: msg.Packet.TimeoutTimestamp,
Height: tx.Height,
MsgIndex: int64(index),
TxHash: tx.TxHash,
}); err != nil {
return nil, err
}
if msg.Packet.DestinationPort == "icahost" {
hostData := icaHostData{}
if err := jsoniter.Unmarshal(msg.Packet.Data, &hostData); err != nil {
return nil, err
}
cosmosTx := icatypes.CosmosTx{}
if err := m.cdc.Unmarshal(hostData.Data, &cosmosTx); err != nil {
// skip unsupported messages
if strings.HasPrefix(err.Error(), "no concrete type registered for type URL") {
m.log.Warn().Err(err).Msg("error while unpacking message")
return nil, nil
}
return nil, err
}
return cosmosTx.Messages, nil
}
}
return nil, nil
}