-
Notifications
You must be signed in to change notification settings - Fork 119
/
ibc-diag.go
330 lines (279 loc) · 7.6 KB
/
ibc-diag.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package cmd
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/types/query"
chtypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"
"github.com/spf13/cobra"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
abcitypes "github.com/tendermint/tendermint/abci/types"
coretypes "github.com/tendermint/tendermint/rpc/core/types"
)
var (
srcNode string
dstNode string
srcChannel string
dstChannel string
transferPort = "transfer"
)
func NewIBCDiagCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "ibc-diag",
Short: "IBC diagnostics commands",
}
cmd.AddCommand(
NewGetStuckTransfersCmd(),
)
return cmd
}
func NewGetStuckTransfersCmd() *cobra.Command {
command := &cobra.Command{
Use: "stuck-txs",
Short: "get stuck IBC transfers",
Long: `get stuck IBC transfers
Example: Getting stuck transfers between sifchain and terra
sifnoded ibc-diag stuck-txs \
--src-node http://rpc.sifchain.finance:80 \
--dst-node http://public-node.terra.dev:26657 \
--src-channel channel-18 \
--dst-channel channel-7
Use the regular IBC commands to find the src and dst channels of a connection
`,
Run: func(cmd *cobra.Command, args []string) {
getStuckTransfers(cmd)
},
}
command.Flags().StringVar(&srcNode, "src-node", srcNode, "rpc endpoint of source node")
command.Flags().StringVar(&dstNode, "dst-node", dstNode, "rpc endpoint of destination node")
command.Flags().StringVar(&srcChannel, "src-channel", dstChannel, "source channel id")
command.Flags().StringVar(&dstChannel, "dst-channel", dstChannel, "destination channel id")
_ = command.MarkFlagRequired("src-node")
_ = command.MarkFlagRequired("dst-node")
_ = command.MarkFlagRequired("src-channel")
_ = command.MarkFlagRequired("dst-channel")
return command
}
func getStuckTransfers(cmd *cobra.Command) {
commitments, err := getCommittedPackets(cmd, srcNode, transferPort, srcChannel)
if err != nil {
panic(err)
}
unreceived, err := getUnreceivedPackets(
cmd,
dstNode,
commitments,
transferPort,
dstChannel)
if err != nil {
panic(err)
}
transfers, err := getTransfers(srcNode, srcChannel, unreceived)
if err != nil {
panic(err)
}
// print results in csv format
fmt.Println("packet_sequence, tx_hash, amount, denom, receiver, sender")
for _, t := range transfers {
fmt.Printf("%s, %s, %s, %s, %s, %s\n",
t.PacketSequence,
t.TxHash,
t.PacketData.Amount,
t.PacketData.Denom,
t.PacketData.Receiver,
t.PacketData.Sender,
)
}
}
// getCommmittedPackets returns the list of packets that were sent on a given
// channel/port but for which there is still a PacketCommitment in the
// underlying DB . A packet that still has a PacketCommitment is a packet whose
// receipt was never acknowledged and which hasn't yet timed out.
func getCommittedPackets(
cmd *cobra.Command,
nodeURI string,
portID string,
channelID string) ([]uint64, error) {
clientCtx, err := getClientContext(cmd, nodeURI)
if err != nil {
return nil, err
}
queryClient := chtypes.NewQueryClient(clientCtx)
packets := []uint64{}
page := uint64(1)
limit := uint64(100)
for {
pageReq := &query.PageRequest{
Offset: (page - 1) * limit,
Limit: limit,
}
req := &chtypes.QueryPacketCommitmentsRequest{
PortId: portID,
ChannelId: channelID,
Pagination: pageReq,
}
res, err := queryClient.PacketCommitments(context.Background(), req)
if err != nil {
return nil, err
}
packetSequences := make([]uint64, len(res.Commitments))
for i, p := range res.Commitments {
packetSequences[i] = p.Sequence
}
packets = append(packets, packetSequences...)
if len(res.Commitments) < int(limit) {
break
} else {
page++
}
}
return packets, nil
}
// getUnreceivedPackets takes a list of packets and returns the subset that
// hasn't been received on the destination channel/port.
func getUnreceivedPackets(
cmd *cobra.Command,
nodeURI string,
committedPackets []uint64,
portID string,
channelID string) ([]uint64, error) {
clientCtx, err := getClientContext(cmd, nodeURI)
if err != nil {
panic(err)
}
queryClient := chtypes.NewQueryClient(clientCtx)
req := &chtypes.QueryUnreceivedPacketsRequest{
PortId: portID,
ChannelId: channelID,
PacketCommitmentSequences: committedPackets,
}
res, err := queryClient.UnreceivedPackets(context.Background(), req)
if err != nil {
return nil, err
}
return res.Sequences, nil
}
// getTransfers takes a list of packet sequences and returns the corresponding
// list of Transfers which contain the actual transfer data.
func getTransfers(nodeURI string, channelID string, packets []uint64) ([]*Transfer, error) {
c, err := rpchttp.New(nodeURI, "/websocket")
if err != nil {
return nil, err
}
transfers := []*Transfer{}
for _, seq := range packets {
ev, err := getTransfer(c, channelID, seq)
if err != nil {
return nil, err
}
transfers = append(transfers, ev)
}
return transfers, nil
}
// getTransfer fetches transfer data corresponding to a given packet.
func getTransfer(client *rpchttp.HTTP, channelID string, packetSequence uint64) (*Transfer, error) {
query := fmt.Sprintf("send_packet.packet_sequence=%d AND send_packet.packet_src_channel='%s'", packetSequence, channelID)
page := 1
perPage := 100
res, err := client.TxSearch(
context.Background(),
query,
false,
&page,
&perPage,
"asc",
)
if err != nil {
return nil, err
}
filter := func(eventType string) bool {
return eventType == "send_packet"
}
filteredEvents := FilterEvents(res.Txs, filter)
if len(filteredEvents) == 0 {
return nil, fmt.Errorf("pruned send_packet (sequence %d)", packetSequence)
}
if len(filteredEvents) > 1 {
return nil, fmt.Errorf("multiple events (%d) for %s", len(filteredEvents), query)
}
ev := filteredEvents[0]
var fungibleTokenPacket FungibleTokenPacketData
err = json.Unmarshal(
[]byte(ev.GetAttribute("packet_data")),
&fungibleTokenPacket,
)
if err != nil {
return nil, err
}
t := &Transfer{
PacketSequence: ev.GetAttribute("packet_sequence"),
TxHash: ev.TxHash,
PacketData: fungibleTokenPacket,
}
return t, nil
}
func getClientContext(cmd *cobra.Command, nodeURI string) (*client.Context, error) {
ctx, err := client.GetClientQueryContext(cmd)
if err != nil {
return nil, err
}
ctx = ctx.WithNodeURI(nodeURI)
srcClient, err := client.NewClientFromNode(nodeURI)
if err != nil {
return nil, err
}
ctx = ctx.WithClient(srcClient)
return &ctx, nil
}
type Transfer struct {
TxHash string
PacketSequence string
PacketData FungibleTokenPacketData
}
type FungibleTokenPacketData struct {
Denom string
Amount string
Sender string
Receiver string
}
func FilterEvents(
txs []*coretypes.ResultTx,
typeFilter func(string) bool,
) []*EventInfo {
infos := []*EventInfo{}
for _, tx := range txs {
txHash := hex.EncodeToString(tx.Tx.Hash())
for _, ev := range tx.TxResult.Events {
if typeFilter(ev.Type) {
attributes := []string{}
for _, attr := range ev.Attributes {
attributes = append(attributes, attr.String())
}
info := &EventInfo{
Type: ev.Type,
TxHash: txHash,
Attributes: attributes,
RealAttributes: ev.Attributes,
}
infos = append(infos, info)
}
}
}
return infos
}
type EventInfo struct {
Type string
TxHash string
Attributes []string
RealAttributes []abcitypes.EventAttribute
}
func (ev *EventInfo) GetAttribute(key string) string {
for _, attr := range ev.RealAttributes {
if string(attr.Key) == key {
return string(attr.Value)
}
}
return ""
}