-
Notifications
You must be signed in to change notification settings - Fork 580
/
msgs.go
291 lines (272 loc) · 10.2 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
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/ibc-go/v5/modules/core/02-client/types"
commitmenttypes "github.com/cosmos/ibc-go/v5/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/v5/modules/core/24-host"
"github.com/cosmos/ibc-go/v5/modules/core/exported"
)
var (
_ sdk.Msg = &MsgConnectionOpenInit{}
_ sdk.Msg = &MsgConnectionOpenConfirm{}
_ sdk.Msg = &MsgConnectionOpenAck{}
_ sdk.Msg = &MsgConnectionOpenTry{}
_ codectypes.UnpackInterfacesMessage = MsgConnectionOpenTry{}
_ codectypes.UnpackInterfacesMessage = MsgConnectionOpenAck{}
)
// 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 string,
) *MsgConnectionOpenInit {
// counterparty must have the same delay period
counterparty := NewCounterparty(counterpartyClientID, "", counterpartyPrefix)
return &MsgConnectionOpenInit{
ClientId: clientID,
Counterparty: counterparty,
Version: version,
DelayPeriod: delayPeriod,
Signer: signer,
}
}
// 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()
}
// 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}
}
// NewMsgConnectionOpenTry creates a new MsgConnectionOpenTry instance
//
//nolint:interfacer
func NewMsgConnectionOpenTry(
clientID, counterpartyConnectionID, counterpartyClientID string,
counterpartyClient exported.ClientState,
counterpartyPrefix commitmenttypes.MerklePrefix,
counterpartyVersions []*Version, delayPeriod uint64,
proofInit, proofClient, proofConsensus []byte,
proofHeight, consensusHeight clienttypes.Height, signer string,
) *MsgConnectionOpenTry {
counterparty := NewCounterparty(counterpartyClientID, counterpartyConnectionID, counterpartyPrefix)
csAny, _ := clienttypes.PackClientState(counterpartyClient)
return &MsgConnectionOpenTry{
ClientId: clientID,
ClientState: csAny,
Counterparty: counterparty,
CounterpartyVersions: counterpartyVersions,
DelayPeriod: delayPeriod,
ProofInit: proofInit,
ProofClient: proofClient,
ProofConsensus: proofConsensus,
ProofHeight: proofHeight,
ConsensusHeight: consensusHeight,
Signer: signer,
}
}
// ValidateBasic implements sdk.Msg
func (msg MsgConnectionOpenTry) ValidateBasic() error {
if msg.PreviousConnectionId != "" {
return sdkerrors.Wrap(ErrInvalidConnectionIdentifier, "previous connection identifier must be empty, this field has been deprecated as crossing hellos are no longer supported")
}
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 {
return unpacker.UnpackAny(msg.ClientState, new(exported.ClientState))
}
// 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}
}
// 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 string,
) *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,
}
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (msg MsgConnectionOpenAck) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
return unpacker.UnpackAny(msg.ClientState, new(exported.ClientState))
}
// 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
}
// 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}
}
// NewMsgConnectionOpenConfirm creates a new MsgConnectionOpenConfirm instance
//
//nolint:interfacer
func NewMsgConnectionOpenConfirm(
connectionID string, proofAck []byte, proofHeight clienttypes.Height,
signer string,
) *MsgConnectionOpenConfirm {
return &MsgConnectionOpenConfirm{
ConnectionId: connectionID,
ProofAck: proofAck,
ProofHeight: proofHeight,
Signer: signer,
}
}
// 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
}
// 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}
}