-
Notifications
You must be signed in to change notification settings - Fork 8
/
ibc.go
371 lines (310 loc) · 11.1 KB
/
ibc.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package types
/*** taken from wasmvm:types/msg.go (so this compiles with easyjson) ***/
var (
_ ToMsg = IBCMsg{}
_ ToMsg = TransferMsg{}
_ ToMsg = SendPacketMsg{}
_ ToMsg = CloseChannelMsg{}
)
type IBCMsg struct {
Transfer *TransferMsg `json:"transfer,omitempty"`
SendPacket *SendPacketMsg `json:"send_packet,omitempty"`
CloseChannel *CloseChannelMsg `json:"close_channel,omitempty"`
}
func (m IBCMsg) ToMsg() CosmosMsg {
return CosmosMsg{IBC: &m}
}
type TransferMsg struct {
ChannelID string `json:"channel_id"`
ToAddress string `json:"to_address"`
Amount Coin `json:"amount"`
Timeout IBCTimeout `json:"timeout"`
}
func (m TransferMsg) ToMsg() CosmosMsg {
return CosmosMsg{IBC: &IBCMsg{Transfer: &m}}
}
type SendPacketMsg struct {
ChannelID string `json:"channel_id"`
Data []byte `json:"data"`
Timeout IBCTimeout `json:"timeout"`
}
func (m SendPacketMsg) ToMsg() CosmosMsg {
return CosmosMsg{IBC: &IBCMsg{SendPacket: &m}}
}
type CloseChannelMsg struct {
ChannelID string `json:"channel_id"`
}
func (m CloseChannelMsg) ToMsg() CosmosMsg {
return CosmosMsg{IBC: &IBCMsg{CloseChannel: &m}}
}
/*** taken from wasmvm:types/queries.go (so this compiles with easyjson) ***/
var (
_ ToQuery = IBCQuery{}
_ ToQuery = PortIDQuery{}
_ ToQuery = ListChannelsQuery{}
_ ToQuery = ChannelQuery{}
)
// IBCQuery defines a query request from the contract into the chain.
// This is the counterpart of [IbcQuery](https://github.com/CosmWasm/cosmwasm/blob/v0.14.0-beta1/packages/std/src/ibc.rs#L61-L83).
type IBCQuery struct {
PortID *PortIDQuery `json:"port_id,omitempty"`
ListChannels *ListChannelsQuery `json:"list_channels,omitempty"`
Channel *ChannelQuery `json:"channel,omitempty"`
}
func (m IBCQuery) ToQuery() QueryRequest {
return QueryRequest{IBC: &m}
}
type PortIDQuery struct{}
func (m PortIDQuery) ToQuery() QueryRequest {
return QueryRequest{IBC: &IBCQuery{PortID: &m}}
}
type PortIDResponse struct {
PortID string `json:"port_id"`
}
// ListChannelsQuery is an IBCQuery that lists all channels that are bound to a given port.
// If `PortID` is unset, this list all channels bound to the contract's port.
// Returns a `ListChannelsResponse`.
// This is the counterpart of [IbcQuery::ListChannels](https://github.com/CosmWasm/cosmwasm/blob/v0.14.0-beta1/packages/std/src/ibc.rs#L70-L73).
type ListChannelsQuery struct {
// optional argument
PortID string `json:"port_id,omitempty"`
}
func (m ListChannelsQuery) ToQuery() QueryRequest {
return QueryRequest{IBC: &IBCQuery{ListChannels: &m}}
}
type ListChannelsResponse struct {
Channels []IBCChannel `json:"channels,emptyslice"`
}
type ChannelQuery struct {
// optional argument
PortID string `json:"port_id,omitempty"`
ChannelID string `json:"channel_id"`
}
func (m ChannelQuery) ToQuery() QueryRequest {
return QueryRequest{IBC: &IBCQuery{Channel: &m}}
}
type ChannelResponse struct {
// may be empty if there is no matching channel
Channel *IBCChannel `json:"channel,omitempty"`
}
/**** Below from wasmvm:types/ibc.go *****/
type IBCEndpoint struct {
PortID string `json:"port_id"`
ChannelID string `json:"channel_id"`
}
type IBCChannel struct {
Endpoint IBCEndpoint `json:"endpoint"`
CounterpartyEndpoint IBCEndpoint `json:"counterparty_endpoint"`
Order IBCOrder `json:"order"`
Version string `json:"version"`
ConnectionID string `json:"connection_id"`
}
type IBCChannelOpenMsg struct {
OpenInit *IBCOpenInit `json:"open_init,omitempty"`
OpenTry *IBCOpenTry `json:"open_try,omitempty"`
}
// GetChannel returns the IBCChannel in this message.
func (msg IBCChannelOpenMsg) GetChannel() IBCChannel {
if msg.OpenInit != nil {
return msg.OpenInit.Channel
}
return msg.OpenTry.Channel
}
// GetCounterVersion checks if the message has a counterparty version and
// returns it if so.
func (msg IBCChannelOpenMsg) GetCounterVersion() (ver string, ok bool) {
if msg.OpenTry != nil {
return msg.OpenTry.CounterpartyVersion, true
}
return "", false
}
type IBCOpenInit struct {
Channel IBCChannel `json:"channel"`
}
func (m *IBCOpenInit) ToMsg() IBCChannelOpenMsg {
return IBCChannelOpenMsg{
OpenInit: m,
}
}
type IBCOpenTry struct {
Channel IBCChannel `json:"channel"`
CounterpartyVersion string `json:"counterparty_version"`
}
func (m *IBCOpenTry) ToMsg() IBCChannelOpenMsg {
return IBCChannelOpenMsg{
OpenTry: m,
}
}
type IBCChannelConnectMsg struct {
OpenAck *IBCOpenAck `json:"open_ack,omitempty"`
OpenConfirm *IBCOpenConfirm `json:"open_confirm,omitempty"`
}
// GetChannel returns the IBCChannel in this message.
func (msg IBCChannelConnectMsg) GetChannel() IBCChannel {
if msg.OpenAck != nil {
return msg.OpenAck.Channel
}
return msg.OpenConfirm.Channel
}
// GetCounterVersion checks if the message has a counterparty version and
// returns it if so.
func (msg IBCChannelConnectMsg) GetCounterVersion() (ver string, ok bool) {
if msg.OpenAck != nil {
return msg.OpenAck.CounterpartyVersion, true
}
return "", false
}
type IBCOpenAck struct {
Channel IBCChannel `json:"channel"`
CounterpartyVersion string `json:"counterparty_version"`
}
func (m *IBCOpenAck) ToMsg() IBCChannelConnectMsg {
return IBCChannelConnectMsg{
OpenAck: m,
}
}
type IBCOpenConfirm struct {
Channel IBCChannel `json:"channel"`
}
func (m *IBCOpenConfirm) ToMsg() IBCChannelConnectMsg {
return IBCChannelConnectMsg{
OpenConfirm: m,
}
}
type IBCChannelCloseMsg struct {
CloseInit *IBCCloseInit `json:"close_init,omitempty"`
CloseConfirm *IBCCloseConfirm `json:"close_confirm,omitempty"`
}
// GetChannel returns the IBCChannel in this message.
func (msg IBCChannelCloseMsg) GetChannel() IBCChannel {
if msg.CloseInit != nil {
return msg.CloseInit.Channel
}
return msg.CloseConfirm.Channel
}
type IBCCloseInit struct {
Channel IBCChannel `json:"channel"`
}
func (m *IBCCloseInit) ToMsg() IBCChannelCloseMsg {
return IBCChannelCloseMsg{
CloseInit: m,
}
}
type IBCCloseConfirm struct {
Channel IBCChannel `json:"channel"`
}
func (m *IBCCloseConfirm) ToMsg() IBCChannelCloseMsg {
return IBCChannelCloseMsg{
CloseConfirm: m,
}
}
type IBCPacketReceiveMsg struct {
Packet IBCPacket `json:"packet"`
}
type IBCPacketAckMsg struct {
Acknowledgement IBCAcknowledgement `json:"acknowledgement"`
OriginalPacket IBCPacket `json:"original_packet"`
}
type IBCPacketTimeoutMsg struct {
Packet IBCPacket `json:"packet"`
}
// TODO: test what the sdk Order.String() represents and how to parse back
// Proto files: https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/ibc/core/channel/v1/channel.proto#L69-L80
// Auto-gen code: https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/x/ibc/core/04-channel/types/channel.pb.go#L70-L101
type IBCOrder = string
// These are the only two valid values for IbcOrder
const Unordered = "ORDER_UNORDERED"
const Ordered = "ORDER_ORDERED"
// IBCTimeoutBlock Height is a monotonically increasing data type
// that can be compared against another Height for the purposes of updating and
// freezing clients.
// Ordering is (revision_number, timeout_height)
type IBCTimeoutBlock struct {
// the version that the client is currently on
// (eg. after reseting the chain this could increment 1 as height drops to 0)
Revision uint64 `json:"revision"`
// block height after which the packet times out.
// the height within the given revision
Height uint64 `json:"height"`
}
func (t IBCTimeoutBlock) IsZero() bool {
return t.Revision == 0 && t.Height == 0
}
// IBCTimeout is the timeout for an IBC packet. At least one of block and timestamp is required.
type IBCTimeout struct {
Block *IBCTimeoutBlock `json:"block"`
// Nanoseconds since UNIX epoch
Timestamp uint64 `json:"timestamp,string,omitempty"`
}
type IBCAcknowledgement struct {
Data []byte `json:"data"`
}
type IBCPacket struct {
Data []byte `json:"data"`
Src IBCEndpoint `json:"src"`
Dest IBCEndpoint `json:"dest"`
Sequence uint64 `json:"sequence"`
Timeout IBCTimeout `json:"timeout"`
}
// IBCChannelOpenResult is the raw response from the ibc_channel_open call.
// This is mirrors Rust's ContractResult<()>.
// We just check if Err == "" to see if this is success (no other data on success)
type IBCChannelOpenResult struct {
Ok *struct{} `json:"ok,omitempty"`
Err string `json:"error,omitempty"`
}
// This is the return value for the majority of the ibc handlers.
// That are able to dispatch messages / events on their own,
// but have no meaningful return value to the calling code.
//
// Callbacks that have return values (like ibc_receive_packet)
// or that cannot redispatch messages (like ibc_channel_open)
// will use other Response types
type IBCBasicResult struct {
Ok *IBCBasicResponse `json:"ok,omitempty"`
Err string `json:"error,omitempty"`
}
// IBCBasicResponse defines the return value on a successful processing.
// This is the counterpart of [IbcBasicResponse](https://github.com/CosmWasm/cosmwasm/blob/v0.14.0-beta1/packages/std/src/ibc.rs#L194-L216).
type IBCBasicResponse struct {
// Messages comes directly from the contract and is its request for action.
// If the ReplyOn value matches the result, the runtime will invoke this
// contract's `reply` entry point after execution. Otherwise, this is all
// "fire and forget".
Messages []SubMsg `json:"messages"`
// attributes for a log event to return over abci interface
Attributes []EventAttribute `json:"attributes"`
// custom events (separate from the main one that contains the attributes
// above)
Events []Event `json:"events"`
}
// This is the return value for the majority of the ibc handlers.
// That are able to dispatch messages / events on their own,
// but have no meaningful return value to the calling code.
//
// Callbacks that have return values (like receive_packet)
// or that cannot redispatch messages (like the handshake callbacks)
// will use other Response types
type IBCReceiveResult struct {
Ok *IBCReceiveResponse `json:"ok,omitempty"`
Err string `json:"error,omitempty"`
}
// IBCReceiveResponse defines the return value on packet response processing.
// This "success" case should be returned even in application-level errors,
// Where the Acknowledgement bytes contain an encoded error message to be returned to
// the calling chain. (Returning IBCReceiveResult::Err will abort processing of this packet
// and not inform the calling chain).
// This is the counterpart of (IbcReceiveResponse)(https://github.com/CosmWasm/cosmwasm/blob/v0.15.0/packages/std/src/ibc.rs#L247-L267).
type IBCReceiveResponse struct {
// binary encoded data to be returned to calling chain as the acknowledgement
Acknowledgement []byte `json:"acknowledgement"`
// Messages comes directly from the contract and is it's request for action.
// If the ReplyOn value matches the result, the runtime will invoke this
// contract's `reply` entry point after execution. Otherwise, this is all
// "fire and forget".
Messages []SubMsg `json:"messages"`
Attributes []EventAttribute `json:"attributes"`
// custom events (separate from the main one that contains the attributes
// above)
Events []Event `json:"events"`
}