-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
msgs.go
358 lines (325 loc) · 12.1 KB
/
msgs.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
package types
import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/core/02-client/types"
commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/core/23-commitment/types"
host "github.com/cosmos/cosmos-sdk/x/ibc/core/24-host"
"github.com/cosmos/cosmos-sdk/x/ibc/core/exported"
)
var _ sdk.Msg = &MsgConnectionOpenInit{}
// NewMsgConnectionOpenInit creates a new MsgConnectionOpenInit instance. It sets the
// counterparty connection identifier to be empty.
//nolint:interfacer
func NewMsgConnectionOpenInit(
clientID, counterpartyClientID string,
counterpartyPrefix commitmenttypes.MerklePrefix,
version *Version, delayPeriod uint64, signer sdk.AccAddress,
) *MsgConnectionOpenInit {
// counterparty must have the same delay period
counterparty := NewCounterparty(counterpartyClientID, "", counterpartyPrefix)
return &MsgConnectionOpenInit{
ClientId: clientID,
Counterparty: counterparty,
Version: version,
DelayPeriod: delayPeriod,
Signer: signer.String(),
}
}
// Route implements sdk.Msg
func (msg MsgConnectionOpenInit) Route() string {
return host.RouterKey
}
// Type implements sdk.Msg
func (msg MsgConnectionOpenInit) Type() string {
return "connection_open_init"
}
// ValidateBasic implements sdk.Msg.
func (msg MsgConnectionOpenInit) ValidateBasic() error {
if err := host.ClientIdentifierValidator(msg.ClientId); err != nil {
return sdkerrors.Wrap(err, "invalid client ID")
}
if msg.Counterparty.ConnectionId != "" {
return sdkerrors.Wrap(ErrInvalidCounterparty, "counterparty connection identifier must be empty")
}
// NOTE: Version can be nil on MsgConnectionOpenInit
if msg.Version != nil {
if err := ValidateVersion(msg.Version); err != nil {
return sdkerrors.Wrap(err, "basic validation of the provided version failed")
}
}
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}
return msg.Counterparty.ValidateBasic()
}
// GetSignBytes implements sdk.Msg. The function will panic since it is used
// for amino transaction verification which IBC does not support.
func (msg MsgConnectionOpenInit) GetSignBytes() []byte {
panic("IBC messages do not support amino")
}
// GetSigners implements sdk.Msg
func (msg MsgConnectionOpenInit) GetSigners() []sdk.AccAddress {
accAddr, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{accAddr}
}
var _ sdk.Msg = &MsgConnectionOpenTry{}
// NewMsgConnectionOpenTry creates a new MsgConnectionOpenTry instance
//nolint:interfacer
func NewMsgConnectionOpenTry(
previousConnectionID, clientID, counterpartyConnectionID,
counterpartyClientID string, counterpartyClient exported.ClientState,
counterpartyPrefix commitmenttypes.MerklePrefix,
counterpartyVersions []*Version, delayPeriod uint64,
proofInit, proofClient, proofConsensus []byte,
proofHeight, consensusHeight clienttypes.Height, signer sdk.AccAddress,
) *MsgConnectionOpenTry {
counterparty := NewCounterparty(counterpartyClientID, counterpartyConnectionID, counterpartyPrefix)
csAny, _ := clienttypes.PackClientState(counterpartyClient)
return &MsgConnectionOpenTry{
PreviousConnectionId: previousConnectionID,
ClientId: clientID,
ClientState: csAny,
Counterparty: counterparty,
CounterpartyVersions: counterpartyVersions,
DelayPeriod: delayPeriod,
ProofInit: proofInit,
ProofClient: proofClient,
ProofConsensus: proofConsensus,
ProofHeight: proofHeight,
ConsensusHeight: consensusHeight,
Signer: signer.String(),
}
}
// Route implements sdk.Msg
func (msg MsgConnectionOpenTry) Route() string {
return host.RouterKey
}
// Type implements sdk.Msg
func (msg MsgConnectionOpenTry) Type() string {
return "connection_open_try"
}
// ValidateBasic implements sdk.Msg
func (msg MsgConnectionOpenTry) ValidateBasic() error {
// an empty connection identifier indicates that a connection identifier should be generated
if msg.PreviousConnectionId != "" {
if !IsValidConnectionID(msg.PreviousConnectionId) {
return sdkerrors.Wrap(ErrInvalidConnectionIdentifier, "invalid previous connection ID")
}
}
if err := host.ClientIdentifierValidator(msg.ClientId); err != nil {
return sdkerrors.Wrap(err, "invalid client ID")
}
// counterparty validate basic allows empty counterparty connection identifiers
if err := host.ConnectionIdentifierValidator(msg.Counterparty.ConnectionId); err != nil {
return sdkerrors.Wrap(err, "invalid counterparty connection ID")
}
if msg.ClientState == nil {
return sdkerrors.Wrap(clienttypes.ErrInvalidClient, "counterparty client is nil")
}
clientState, err := clienttypes.UnpackClientState(msg.ClientState)
if err != nil {
return sdkerrors.Wrapf(clienttypes.ErrInvalidClient, "unpack err: %v", err)
}
if err := clientState.Validate(); err != nil {
return sdkerrors.Wrap(err, "counterparty client is invalid")
}
if len(msg.CounterpartyVersions) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidVersion, "empty counterparty versions")
}
for i, version := range msg.CounterpartyVersions {
if err := ValidateVersion(version); err != nil {
return sdkerrors.Wrapf(err, "basic validation failed on version with index %d", i)
}
}
if len(msg.ProofInit) == 0 {
return sdkerrors.Wrap(commitmenttypes.ErrInvalidProof, "cannot submit an empty proof init")
}
if len(msg.ProofClient) == 0 {
return sdkerrors.Wrap(commitmenttypes.ErrInvalidProof, "cannot submit empty proof client")
}
if len(msg.ProofConsensus) == 0 {
return sdkerrors.Wrap(commitmenttypes.ErrInvalidProof, "cannot submit an empty proof of consensus state")
}
if msg.ProofHeight.IsZero() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidHeight, "proof height must be non-zero")
}
if msg.ConsensusHeight.IsZero() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidHeight, "consensus height must be non-zero")
}
_, err = sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}
return msg.Counterparty.ValidateBasic()
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (msg MsgConnectionOpenTry) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var clientState exported.ClientState
err := unpacker.UnpackAny(msg.ClientState, &clientState)
if err != nil {
return err
}
return nil
}
// GetSignBytes implements sdk.Msg. The function will panic since it is used
// for amino transaction verification which IBC does not support.
func (msg MsgConnectionOpenTry) GetSignBytes() []byte {
panic("IBC messages do not support amino")
}
// GetSigners implements sdk.Msg
func (msg MsgConnectionOpenTry) GetSigners() []sdk.AccAddress {
accAddr, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{accAddr}
}
var _ sdk.Msg = &MsgConnectionOpenAck{}
// NewMsgConnectionOpenAck creates a new MsgConnectionOpenAck instance
//nolint:interfacer
func NewMsgConnectionOpenAck(
connectionID, counterpartyConnectionID string, counterpartyClient exported.ClientState,
proofTry, proofClient, proofConsensus []byte,
proofHeight, consensusHeight clienttypes.Height,
version *Version,
signer sdk.AccAddress,
) *MsgConnectionOpenAck {
csAny, _ := clienttypes.PackClientState(counterpartyClient)
return &MsgConnectionOpenAck{
ConnectionId: connectionID,
CounterpartyConnectionId: counterpartyConnectionID,
ClientState: csAny,
ProofTry: proofTry,
ProofClient: proofClient,
ProofConsensus: proofConsensus,
ProofHeight: proofHeight,
ConsensusHeight: consensusHeight,
Version: version,
Signer: signer.String(),
}
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (msg MsgConnectionOpenAck) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
return unpacker.UnpackAny(msg.ClientState, new(exported.ClientState))
}
// Route implements sdk.Msg
func (msg MsgConnectionOpenAck) Route() string {
return host.RouterKey
}
// Type implements sdk.Msg
func (msg MsgConnectionOpenAck) Type() string {
return "connection_open_ack"
}
// ValidateBasic implements sdk.Msg
func (msg MsgConnectionOpenAck) ValidateBasic() error {
if !IsValidConnectionID(msg.ConnectionId) {
return ErrInvalidConnectionIdentifier
}
if err := host.ConnectionIdentifierValidator(msg.CounterpartyConnectionId); err != nil {
return sdkerrors.Wrap(err, "invalid counterparty connection ID")
}
if err := ValidateVersion(msg.Version); err != nil {
return err
}
if msg.ClientState == nil {
return sdkerrors.Wrap(clienttypes.ErrInvalidClient, "counterparty client is nil")
}
clientState, err := clienttypes.UnpackClientState(msg.ClientState)
if err != nil {
return sdkerrors.Wrapf(clienttypes.ErrInvalidClient, "unpack err: %v", err)
}
if err := clientState.Validate(); err != nil {
return sdkerrors.Wrap(err, "counterparty client is invalid")
}
if len(msg.ProofTry) == 0 {
return sdkerrors.Wrap(commitmenttypes.ErrInvalidProof, "cannot submit an empty proof try")
}
if len(msg.ProofClient) == 0 {
return sdkerrors.Wrap(commitmenttypes.ErrInvalidProof, "cannot submit empty proof client")
}
if len(msg.ProofConsensus) == 0 {
return sdkerrors.Wrap(commitmenttypes.ErrInvalidProof, "cannot submit an empty proof of consensus state")
}
if msg.ProofHeight.IsZero() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidHeight, "proof height must be non-zero")
}
if msg.ConsensusHeight.IsZero() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidHeight, "consensus height must be non-zero")
}
_, err = sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}
return nil
}
// GetSignBytes implements sdk.Msg. The function will panic since it is used
// for amino transaction verification which IBC does not support.
func (msg MsgConnectionOpenAck) GetSignBytes() []byte {
panic("IBC messages do not support amino")
}
// GetSigners implements sdk.Msg
func (msg MsgConnectionOpenAck) GetSigners() []sdk.AccAddress {
accAddr, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{accAddr}
}
var _ sdk.Msg = &MsgConnectionOpenConfirm{}
// NewMsgConnectionOpenConfirm creates a new MsgConnectionOpenConfirm instance
//nolint:interfacer
func NewMsgConnectionOpenConfirm(
connectionID string, proofAck []byte, proofHeight clienttypes.Height,
signer sdk.AccAddress,
) *MsgConnectionOpenConfirm {
return &MsgConnectionOpenConfirm{
ConnectionId: connectionID,
ProofAck: proofAck,
ProofHeight: proofHeight,
Signer: signer.String(),
}
}
// Route implements sdk.Msg
func (msg MsgConnectionOpenConfirm) Route() string {
return host.RouterKey
}
// Type implements sdk.Msg
func (msg MsgConnectionOpenConfirm) Type() string {
return "connection_open_confirm"
}
// ValidateBasic implements sdk.Msg
func (msg MsgConnectionOpenConfirm) ValidateBasic() error {
if !IsValidConnectionID(msg.ConnectionId) {
return ErrInvalidConnectionIdentifier
}
if len(msg.ProofAck) == 0 {
return sdkerrors.Wrap(commitmenttypes.ErrInvalidProof, "cannot submit an empty proof ack")
}
if msg.ProofHeight.IsZero() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidHeight, "proof height must be non-zero")
}
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}
return nil
}
// GetSignBytes implements sdk.Msg. The function will panic since it is used
// for amino transaction verification which IBC does not support.
func (msg MsgConnectionOpenConfirm) GetSignBytes() []byte {
panic("IBC messages do not support amino")
}
// GetSigners implements sdk.Msg
func (msg MsgConnectionOpenConfirm) GetSigners() []sdk.AccAddress {
accAddr, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{accAddr}
}