-
Notifications
You must be signed in to change notification settings - Fork 580
/
msgs.go
281 lines (252 loc) · 8.9 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
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"
host "github.com/cosmos/ibc-go/v2/modules/core/24-host"
"github.com/cosmos/ibc-go/v2/modules/core/exported"
)
// message types for the IBC client
const (
TypeMsgCreateClient string = "create_client"
TypeMsgUpdateClient string = "update_client"
TypeMsgUpgradeClient string = "upgrade_client"
TypeMsgSubmitMisbehaviour string = "submit_misbehaviour"
)
var (
_ sdk.Msg = &MsgCreateClient{}
_ sdk.Msg = &MsgUpdateClient{}
_ sdk.Msg = &MsgSubmitMisbehaviour{}
_ sdk.Msg = &MsgUpgradeClient{}
_ codectypes.UnpackInterfacesMessage = MsgCreateClient{}
_ codectypes.UnpackInterfacesMessage = MsgUpdateClient{}
_ codectypes.UnpackInterfacesMessage = MsgSubmitMisbehaviour{}
_ codectypes.UnpackInterfacesMessage = MsgUpgradeClient{}
)
// NewMsgCreateClient creates a new MsgCreateClient instance
//nolint:interfacer
func NewMsgCreateClient(
clientState exported.ClientState, consensusState exported.ConsensusState, signer string,
) (*MsgCreateClient, error) {
anyClientState, err := PackClientState(clientState)
if err != nil {
return nil, err
}
anyConsensusState, err := PackConsensusState(consensusState)
if err != nil {
return nil, err
}
return &MsgCreateClient{
ClientState: anyClientState,
ConsensusState: anyConsensusState,
Signer: signer,
}, nil
}
// ValidateBasic implements sdk.Msg
func (msg MsgCreateClient) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}
clientState, err := UnpackClientState(msg.ClientState)
if err != nil {
return err
}
if err := clientState.Validate(); err != nil {
return err
}
if clientState.ClientType() == exported.Localhost {
return sdkerrors.Wrap(ErrInvalidClient, "localhost client can only be created on chain initialization")
}
consensusState, err := UnpackConsensusState(msg.ConsensusState)
if err != nil {
return err
}
if clientState.ClientType() != consensusState.ClientType() {
return sdkerrors.Wrap(ErrInvalidClientType, "client type for client state and consensus state do not match")
}
if err := ValidateClientType(clientState.ClientType()); err != nil {
return sdkerrors.Wrap(err, "client type does not meet naming constraints")
}
return consensusState.ValidateBasic()
}
// GetSigners implements sdk.Msg
func (msg MsgCreateClient) GetSigners() []sdk.AccAddress {
accAddr, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{accAddr}
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (msg MsgCreateClient) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var clientState exported.ClientState
err := unpacker.UnpackAny(msg.ClientState, &clientState)
if err != nil {
return err
}
var consensusState exported.ConsensusState
return unpacker.UnpackAny(msg.ConsensusState, &consensusState)
}
// NewMsgUpdateClient creates a new MsgUpdateClient instance
//nolint:interfacer
func NewMsgUpdateClient(id string, header exported.Header, signer string) (*MsgUpdateClient, error) {
anyHeader, err := PackHeader(header)
if err != nil {
return nil, err
}
return &MsgUpdateClient{
ClientId: id,
Header: anyHeader,
Signer: signer,
}, nil
}
// ValidateBasic implements sdk.Msg
func (msg MsgUpdateClient) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}
header, err := UnpackHeader(msg.Header)
if err != nil {
return err
}
if err := header.ValidateBasic(); err != nil {
return err
}
if msg.ClientId == exported.Localhost {
return sdkerrors.Wrap(ErrInvalidClient, "localhost client is only updated on ABCI BeginBlock")
}
return host.ClientIdentifierValidator(msg.ClientId)
}
// GetSigners implements sdk.Msg
func (msg MsgUpdateClient) GetSigners() []sdk.AccAddress {
accAddr, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{accAddr}
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (msg MsgUpdateClient) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var header exported.Header
return unpacker.UnpackAny(msg.Header, &header)
}
// NewMsgUpgradeClient creates a new MsgUpgradeClient instance
// nolint: interfacer
func NewMsgUpgradeClient(clientID string, clientState exported.ClientState, consState exported.ConsensusState,
proofUpgradeClient, proofUpgradeConsState []byte, signer string) (*MsgUpgradeClient, error) {
anyClient, err := PackClientState(clientState)
if err != nil {
return nil, err
}
anyConsState, err := PackConsensusState(consState)
if err != nil {
return nil, err
}
return &MsgUpgradeClient{
ClientId: clientID,
ClientState: anyClient,
ConsensusState: anyConsState,
ProofUpgradeClient: proofUpgradeClient,
ProofUpgradeConsensusState: proofUpgradeConsState,
Signer: signer,
}, nil
}
// ValidateBasic implements sdk.Msg
func (msg MsgUpgradeClient) ValidateBasic() error {
// will not validate client state as committed client may not form a valid client state.
// client implementations are responsible for ensuring final upgraded client is valid.
clientState, err := UnpackClientState(msg.ClientState)
if err != nil {
return err
}
// will not validate consensus state here since the trusted kernel may not form a valid consenus state.
// client implementations are responsible for ensuring client can submit new headers against this consensus state.
consensusState, err := UnpackConsensusState(msg.ConsensusState)
if err != nil {
return err
}
if clientState.ClientType() != consensusState.ClientType() {
return sdkerrors.Wrapf(ErrInvalidUpgradeClient, "consensus state's client-type does not match client. expected: %s, got: %s",
clientState.ClientType(), consensusState.ClientType())
}
if len(msg.ProofUpgradeClient) == 0 {
return sdkerrors.Wrap(ErrInvalidUpgradeClient, "proof of upgrade client cannot be empty")
}
if len(msg.ProofUpgradeConsensusState) == 0 {
return sdkerrors.Wrap(ErrInvalidUpgradeClient, "proof of upgrade consensus state cannot be empty")
}
_, err = sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}
return host.ClientIdentifierValidator(msg.ClientId)
}
// GetSigners implements sdk.Msg
func (msg MsgUpgradeClient) GetSigners() []sdk.AccAddress {
accAddr, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{accAddr}
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (msg MsgUpgradeClient) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var (
clientState exported.ClientState
consState exported.ConsensusState
)
if err := unpacker.UnpackAny(msg.ClientState, &clientState); err != nil {
return err
}
return unpacker.UnpackAny(msg.ConsensusState, &consState)
}
// NewMsgSubmitMisbehaviour creates a new MsgSubmitMisbehaviour instance.
//nolint:interfacer
func NewMsgSubmitMisbehaviour(clientID string, misbehaviour exported.Misbehaviour, signer string) (*MsgSubmitMisbehaviour, error) {
anyMisbehaviour, err := PackMisbehaviour(misbehaviour)
if err != nil {
return nil, err
}
return &MsgSubmitMisbehaviour{
ClientId: clientID,
Misbehaviour: anyMisbehaviour,
Signer: signer,
}, nil
}
// ValidateBasic performs basic (non-state-dependant) validation on a MsgSubmitMisbehaviour.
func (msg MsgSubmitMisbehaviour) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}
misbehaviour, err := UnpackMisbehaviour(msg.Misbehaviour)
if err != nil {
return err
}
if err := misbehaviour.ValidateBasic(); err != nil {
return err
}
if misbehaviour.GetClientID() != msg.ClientId {
return sdkerrors.Wrapf(
ErrInvalidMisbehaviour,
"misbehaviour client-id doesn't match client-id from message (%s ≠ %s)",
misbehaviour.GetClientID(), msg.ClientId,
)
}
return host.ClientIdentifierValidator(msg.ClientId)
}
// GetSigners returns the single expected signer for a MsgSubmitMisbehaviour.
func (msg MsgSubmitMisbehaviour) GetSigners() []sdk.AccAddress {
accAddr, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{accAddr}
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (msg MsgSubmitMisbehaviour) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var misbehaviour exported.Misbehaviour
return unpacker.UnpackAny(msg.Misbehaviour, &misbehaviour)
}