-
Notifications
You must be signed in to change notification settings - Fork 30
/
query.go
240 lines (235 loc) · 6.01 KB
/
query.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
package core
import (
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
conntypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
chantypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
"golang.org/x/sync/errgroup"
)
// QueryClientStatePair returns a pair of connection responses
func QueryClientStatePair(
srcCtx, dstCtx QueryContext,
src, dst interface {
Chain
StateProver
},
prove bool,
) (srcCsRes, dstCsRes *clienttypes.QueryClientStateResponse, err error) {
var eg = new(errgroup.Group)
eg.Go(func() error {
var err error
srcCsRes, err = src.QueryClientState(srcCtx)
if err != nil {
return err
}
if prove {
path := host.FullClientStatePath(src.Path().ClientID)
var value []byte
value, err = src.Codec().Marshal(srcCsRes.ClientState)
if err != nil {
return err
}
srcCsRes.Proof, srcCsRes.ProofHeight, err = src.ProveState(srcCtx, path, value)
}
return err
})
eg.Go(func() error {
var err error
dstCsRes, err = dst.QueryClientState(dstCtx)
if err != nil {
return err
}
if prove {
path := host.FullClientStatePath(dst.Path().ClientID)
var value []byte
value, err = dst.Codec().Marshal(dstCsRes.ClientState)
if err != nil {
return err
}
dstCsRes.Proof, dstCsRes.ProofHeight, err = dst.ProveState(dstCtx, path, value)
}
return err
})
err = eg.Wait()
return
}
// QueryClientConsensusStatePair allows for the querying of multiple client states at the same time
func QueryClientConsensusStatePair(
srcCtx, dstCtx QueryContext,
src, dst interface {
Chain
StateProver
},
srcClientConsH,
dstClientConsH ibcexported.Height,
prove bool,
) (srcCsRes, dstCsRes *clienttypes.QueryConsensusStateResponse, err error) {
var eg = new(errgroup.Group)
eg.Go(func() error {
var err error
srcCsRes, err = src.QueryClientConsensusState(srcCtx, srcClientConsH)
if err != nil {
return err
}
if prove {
path := host.FullConsensusStatePath(src.Path().ClientID, srcClientConsH)
var value []byte
value, err = src.Codec().Marshal(srcCsRes.ConsensusState)
if err != nil {
return err
}
srcCsRes.Proof, srcCsRes.ProofHeight, err = src.ProveState(srcCtx, path, value)
}
return err
})
eg.Go(func() error {
var err error
dstCsRes, err = dst.QueryClientConsensusState(dstCtx, dstClientConsH)
if err != nil {
return err
}
if prove {
path := host.FullConsensusStatePath(dst.Path().ClientID, dstClientConsH)
var value []byte
value, err = dst.Codec().Marshal(dstCsRes.ConsensusState)
if err != nil {
return err
}
dstCsRes.Proof, dstCsRes.ProofHeight, err = dst.ProveState(dstCtx, path, value)
}
return err
})
err = eg.Wait()
return
}
// QueryConnectionPair returns a pair of connection responses
func QueryConnectionPair(
srcCtx, dstCtx QueryContext,
src, dst interface {
Chain
StateProver
},
prove bool,
) (srcConn, dstConn *conntypes.QueryConnectionResponse, err error) {
var eg = new(errgroup.Group)
eg.Go(func() error {
if src.Path().ConnectionID == "" {
srcConn = &conntypes.QueryConnectionResponse{
Connection: &conntypes.ConnectionEnd{
State: conntypes.UNINITIALIZED,
},
}
return nil
}
var err error
srcConn, err = src.QueryConnection(srcCtx)
if err != nil {
return err
} else if srcConn.Connection.State == conntypes.UNINITIALIZED {
return nil
}
if prove {
path := host.ConnectionPath(src.Path().ConnectionID)
var value []byte
value, err = src.Codec().Marshal(srcConn.Connection)
if err != nil {
return err
}
srcConn.Proof, srcConn.ProofHeight, err = src.ProveState(srcCtx, path, value)
}
return err
})
eg.Go(func() error {
if dst.Path().ConnectionID == "" {
dstConn = &conntypes.QueryConnectionResponse{
Connection: &conntypes.ConnectionEnd{
State: conntypes.UNINITIALIZED,
},
}
return nil
}
var err error
dstConn, err = dst.QueryConnection(dstCtx)
if err != nil {
return err
} else if dstConn.Connection.State == conntypes.UNINITIALIZED {
return nil
}
if prove {
path := host.ConnectionPath(dst.Path().ConnectionID)
var value []byte
value, err = dst.Codec().Marshal(dstConn.Connection)
if err != nil {
return err
}
dstConn.Proof, dstConn.ProofHeight, err = dst.ProveState(dstCtx, path, value)
}
return err
})
err = eg.Wait()
return
}
// QueryChannelPair returns a pair of channel responses
func QueryChannelPair(srcCtx, dstCtx QueryContext, src, dst interface {
Chain
StateProver
}, prove bool) (srcChan, dstChan *chantypes.QueryChannelResponse, err error) {
var eg = new(errgroup.Group)
eg.Go(func() error {
if src.Path().ChannelID == "" {
srcChan = &chantypes.QueryChannelResponse{
Channel: &chantypes.Channel{
State: chantypes.UNINITIALIZED,
},
}
return nil
}
var err error
srcChan, err = src.QueryChannel(srcCtx)
if err != nil {
return err
} else if srcChan.Channel.State == chantypes.UNINITIALIZED {
return nil
}
if prove {
path := host.ChannelPath(src.Path().PortID, src.Path().ChannelID)
var value []byte
value, err = src.Codec().Marshal(srcChan.Channel)
if err != nil {
return err
}
srcChan.Proof, srcChan.ProofHeight, err = src.ProveState(srcCtx, path, value)
}
return err
})
eg.Go(func() error {
if dst.Path().ChannelID == "" {
dstChan = &chantypes.QueryChannelResponse{
Channel: &chantypes.Channel{
State: chantypes.UNINITIALIZED,
},
}
return nil
}
var err error
dstChan, err = dst.QueryChannel(dstCtx)
if err != nil {
return err
} else if dstChan.Channel.State == chantypes.UNINITIALIZED {
return nil
}
if prove {
path := host.ChannelPath(dst.Path().PortID, dst.Path().ChannelID)
var value []byte
value, err = dst.Codec().Marshal(dstChan.Channel)
if err != nil {
return err
}
dstChan.Proof, dstChan.ProofHeight, err = dst.ProveState(dstCtx, path, value)
}
return err
})
err = eg.Wait()
return
}