-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
189 lines (153 loc) · 6.28 KB
/
utils.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
package utils
import (
"github.com/ci123chain/ci123chain/pkg/abci/codec"
"github.com/ci123chain/ci123chain/pkg/abci/types/pagination"
"github.com/ci123chain/ci123chain/pkg/client/context"
"github.com/ci123chain/ci123chain/pkg/ibc"
"github.com/ci123chain/ci123chain/pkg/ibc/core/channel/types"
ibcclient "github.com/ci123chain/ci123chain/pkg/ibc/core/client"
clienttypes "github.com/ci123chain/ci123chain/pkg/ibc/core/clients/types"
"github.com/ci123chain/ci123chain/pkg/ibc/core/host"
coretypes "github.com/ci123chain/ci123chain/pkg/ibc/core/types"
sdkerrors "github.com/pkg/errors"
)
// QueryChannel returns a channel end.
// If prove is true, it performs an ABCI store query in order to retrieve the merkle proof. Otherwise,
// it uses the gRPC query client.
func QueryChannel(
clientCtx context.Context, portID, channelID string, prove bool,
) (*types.QueryChannelResponse, error) {
return queryChannelABCI(clientCtx, portID, channelID, prove)
}
func queryChannelABCI(clientCtx context.Context, portID, channelID string, prove bool) (*types.QueryChannelResponse, error) {
key := host.ChannelKey(portID, channelID)
var value, proofBz []byte
var err error
var proofHeight clienttypes.Height
if prove {
value, proofBz, proofHeight, err = ibcclient.QueryTendermintProof(clientCtx, key)
} else {
value, proofHeight, err = ibcclient.QueryTendermint(clientCtx, key)
}
if err != nil {
return nil, err
}
// check if channel exists
if len(value) == 0 {
return nil, sdkerrors.Wrapf(types.ErrChannelNotFound, "portID (%s), channelID (%s)", portID, channelID)
}
var channel types.Channel
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)
if err := cdc.UnmarshalBinaryBare(value, &channel); err != nil {
return nil, err
}
return types.NewQueryChannelResponse(channel, proofBz, proofHeight), nil
}
// QueryPacketCommitment returns a packet commitment.
// If prove is true, it performs an ABCI store query in order to retrieve the merkle proof. Otherwise,
// it uses the gRPC query client.
func QueryPacketCommitment(
clientCtx context.Context, portID, channelID string,
sequence uint64, prove bool,
) (*types.QueryPacketCommitmentResponse, error) {
key := host.PacketCommitmentKey(portID, channelID, sequence)
value, proofBz, proofHeight, err := ibcclient.QueryTendermintProof(clientCtx, key)
if err != nil {
return nil, err
}
// check if packet commitment exists
if len(value) == 0 {
return nil, sdkerrors.Wrapf(types.ErrPacketCommitmentNotFound, "portID (%s), channelID (%s), sequence (%d)", portID, channelID, sequence)
}
return types.NewQueryPacketCommitmentResponse(value, proofBz, proofHeight), nil
}
// QueryPacketAcknowledgement returns the data about a packet acknowledgement.
// If prove is true, it performs an ABCI store query in order to retrieve the merkle proof. Otherwise,
// it uses the gRPC query client
func QueryPacketAcknowledgement(clientCtx context.Context, portID, channelID string, sequence uint64, prove bool) (*types.QueryPacketAcknowledgementResponse, error) {
key := host.PacketAcknowledgementKey(portID, channelID, sequence)
value, proofBz, proofHeight, err := ibcclient.QueryTendermintProof(clientCtx, key)
if err != nil {
return nil, err
}
if len(value) == 0 {
return nil, sdkerrors.Wrapf(types.ErrInvalidAcknowledgement, "portID (%s), channelID (%s), sequence (%d)", portID, channelID, sequence)
}
return types.NewQueryPacketAcknowledgementResponse(value, proofBz, proofHeight), nil
}
// QueryPacketReceipt returns data about a packet receipt.
// If prove is true, it performs an ABCI store query in order to retrieve the merkle proof. Otherwise,
// it uses the gRPC query client.
func QueryPacketReceipt(
clientCtx context.Context, portID, channelID string,
sequence uint64, prove bool,
) (*types.QueryPacketReceiptResponse, error) {
key := host.PacketReceiptKey(portID, channelID, sequence)
value, proofBz, proofHeight, err := ibcclient.QueryTendermintProof(clientCtx, key)
if err != nil {
return nil, err
}
return types.NewQueryPacketReceiptResponse(value != nil, proofBz, proofHeight), nil
}
// instead grpc request
func QueryChannels(clientCtx context.Context, offset, limit uint64,) (*types.QueryChannelsResponse, error) {
path := "/custom/" + ibc.ModuleName + "/" + coretypes.QueryChannels
req := &types.QueryChannelsRequest{
Pagination: &pagination.PageRequest{
Key: []byte(""),
Offset: offset,
Limit: limit,
CountTotal: true,
},
}
key := clientCtx.Cdc.MustMarshalJSON(req)
value, _, err := ibcclient.QueryABCI(clientCtx, path, key, false)
if err != nil {
return nil, err
}
// check if client exists
if len(value) == 0 {
return nil, sdkerrors.Wrap(types.ErrChannelNotFound, "channels not found")
}
clientStatesResp := types.MustUnmarshalQueryChannelsResp(types.ChannelCdc, value)
return &clientStatesResp, nil
}
// QueryPacketAcknowledgement returns the data about a packet acknowledgement.
// If prove is true, it performs an ABCI store query in order to retrieve the merkle proof. Otherwise,
// it uses the gRPC query client
func QueryPacketCommitments(clientCtx context.Context, portID, channelID string, offset, limit uint64) (*types.QueryPacketCommitmentsResponse, error) {
path := "/custom/" + ibc.ModuleName + "/" + coretypes.QueryPacketCommitments
req := &types.QueryPacketCommitmentsRequest{
PortId: portID,
ChannelId: channelID,
Pagination: &pagination.PageRequest{
Offset: offset,
Limit: limit,
CountTotal: true,
},
}
key := clientCtx.Cdc.MustMarshalJSON(req)
value, _, err := ibcclient.QueryABCI(clientCtx, path, key, false)
if err != nil {
return nil, err
}
var resp types.QueryPacketCommitmentsResponse
types.ChannelCdc.MustUnmarshalJSON(value, &resp)
return &resp, nil
}
func QueryUnreceivedPackets(clientCtx context.Context, portID, channelID string, seqs []uint64) (*types.QueryUnreceivedPacketsResponse, error) {
path := "/custom/" + ibc.ModuleName + "/" + coretypes.UnreceivedPacketd
req := &types.QueryUnreceivedPacketsRequest{
PortId: portID,
ChannelId: channelID,
PacketCommitmentSequences: seqs,
}
key := clientCtx.Cdc.MustMarshalJSON(req)
value, _, err := ibcclient.QueryABCI(clientCtx, path, key, false)
if err != nil {
return nil, err
}
var resp types.QueryUnreceivedPacketsResponse
types.ChannelCdc.MustUnmarshalJSON(value, &resp)
return &resp, nil
}