-
Notifications
You must be signed in to change notification settings - Fork 597
/
tx.go
452 lines (381 loc) · 15.9 KB
/
tx.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package cli
import (
"fmt"
"os"
"strconv"
"github.com/spf13/cobra"
upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/cosmos/cosmos-sdk/version"
govcli "github.com/cosmos/cosmos-sdk/x/gov/client/cli"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
)
const FlagAuthority = "authority"
// newCreateClientCmd defines the command to create a new IBC light client.
func newCreateClientCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create [path/to/client_state.json] [path/to/consensus_state.json]",
Short: "create new IBC client",
Long: `create a new IBC client with the specified client state and consensus state
- ClientState JSON example: {"@type":"/ibc.lightclients.solomachine.v1.ClientState","sequence":"1","frozen_sequence":"0","consensus_state":{"public_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AtK50+5pJOoaa04qqAqrnyAqsYrwrR/INnA6UPIaYZlp"},"diversifier":"testing","timestamp":"10"},"allow_update_after_proposal":false}
- ConsensusState JSON example: {"@type":"/ibc.lightclients.solomachine.v1.ConsensusState","public_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AtK50+5pJOoaa04qqAqrnyAqsYrwrR/INnA6UPIaYZlp"},"diversifier":"testing","timestamp":"10"}`,
Example: fmt.Sprintf("%s tx ibc %s create [path/to/client_state.json] [path/to/consensus_state.json] --from node0 --home ../node0/<app>cli --chain-id $CID", version.AppName, types.SubModuleName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)
// attempt to unmarshal client state argument
var clientState exported.ClientState
clientContentOrFileName := args[0]
if err := cdc.UnmarshalInterfaceJSON([]byte(clientContentOrFileName), &clientState); err != nil {
// check for file path if JSON input is not provided
contents, err := os.ReadFile(clientContentOrFileName)
if err != nil {
return fmt.Errorf("neither JSON input nor path to .json file for client state were provided: %w", err)
}
if err := cdc.UnmarshalInterfaceJSON(contents, &clientState); err != nil {
return fmt.Errorf("error unmarshalling client state file: %w", err)
}
}
// attempt to unmarshal consensus state argument
var consensusState exported.ConsensusState
consensusContentOrFileName := args[1]
if err := cdc.UnmarshalInterfaceJSON([]byte(consensusContentOrFileName), &consensusState); err != nil {
// check for file path if JSON input is not provided
contents, err := os.ReadFile(consensusContentOrFileName)
if err != nil {
return fmt.Errorf("neither JSON input nor path to .json file for consensus state were provided: %w", err)
}
if err := cdc.UnmarshalInterfaceJSON(contents, &consensusState); err != nil {
return fmt.Errorf("error unmarshalling consensus state file: %w", err)
}
}
msg, err := types.NewMsgCreateClient(clientState, consensusState, clientCtx.GetFromAddress().String())
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// newUpdateClientCmd defines the command to update an IBC client.
func newUpdateClientCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "update [client-id] [path/to/client_msg.json]",
Short: "update existing client with a client message",
Long: "update existing client with a client message, for example a header, misbehaviour or batch update",
Example: fmt.Sprintf("%s tx ibc %s update [client-id] [path/to/client_msg.json] --from node0 --home ../node0/<app>cli --chain-id $CID", version.AppName, types.SubModuleName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
clientID := args[0]
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)
var clientMsg exported.ClientMessage
clientMsgContentOrFileName := args[1]
if err := cdc.UnmarshalInterfaceJSON([]byte(clientMsgContentOrFileName), &clientMsg); err != nil {
// check for file path if JSON input is not provided
contents, err := os.ReadFile(clientMsgContentOrFileName)
if err != nil {
return fmt.Errorf("neither JSON input nor path to .json file for header were provided: %w", err)
}
if err := cdc.UnmarshalInterfaceJSON(contents, &clientMsg); err != nil {
return fmt.Errorf("error unmarshalling header file: %w", err)
}
}
msg, err := types.NewMsgUpdateClient(clientID, clientMsg, clientCtx.GetFromAddress().String())
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// newSubmitMisbehaviourCmd defines the command to submit a misbehaviour to prevent
// future updates.
// Deprecated: NewSubmitMisbehaviourCmd is deprecated and will be removed in a future release.
// Please use NewUpdateClientCmd instead.
func newSubmitMisbehaviourCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "misbehaviour [clientID] [path/to/misbehaviour.json]",
Short: "submit a client misbehaviour",
Long: "submit a client misbehaviour to prevent future updates",
Example: fmt.Sprintf("%s tx ibc %s misbehaviour [clientID] [path/to/misbehaviour.json] --from node0 --home ../node0/<app>cli --chain-id $CID", version.AppName, types.SubModuleName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)
var misbehaviour exported.ClientMessage
clientID := args[0]
misbehaviourContentOrFileName := args[1]
if err := cdc.UnmarshalInterfaceJSON([]byte(misbehaviourContentOrFileName), &misbehaviour); err != nil {
// check for file path if JSON input is not provided
contents, err := os.ReadFile(misbehaviourContentOrFileName)
if err != nil {
return fmt.Errorf("neither JSON input nor path to .json file for misbehaviour were provided: %w", err)
}
if err := cdc.UnmarshalInterfaceJSON(contents, &misbehaviour); err != nil {
return fmt.Errorf("error unmarshalling misbehaviour file: %w", err)
}
}
msg, err := types.NewMsgSubmitMisbehaviour(clientID, misbehaviour, clientCtx.GetFromAddress().String())
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// newUpgradeClientCmd defines the command to upgrade an IBC light client.
func newUpgradeClientCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "upgrade [client-identifier] [path/to/client_state.json] [path/to/consensus_state.json] [upgrade-client-proof] [upgrade-consensus-state-proof]",
Short: "upgrade an IBC client",
Long: `upgrade the IBC client associated with the provided client identifier while providing proof committed by the counterparty chain to the new client and consensus states
- ClientState JSON example: {"@type":"/ibc.lightclients.solomachine.v1.ClientState","sequence":"1","frozen_sequence":"0","consensus_state":{"public_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AtK50+5pJOoaa04qqAqrnyAqsYrwrR/INnA6UPIaYZlp"},"diversifier":"testing","timestamp":"10"},"allow_update_after_proposal":false}
- ConsensusState JSON example: {"@type":"/ibc.lightclients.solomachine.v1.ConsensusState","public_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AtK50+5pJOoaa04qqAqrnyAqsYrwrR/INnA6UPIaYZlp"},"diversifier":"testing","timestamp":"10"}`,
Example: fmt.Sprintf("%s tx ibc %s upgrade [client-identifier] [path/to/client_state.json] [path/to/consensus_state.json] [client-state-proof] [consensus-state-proof] --from node0 --home ../node0/<app>cli --chain-id $CID", version.AppName, types.SubModuleName),
Args: cobra.ExactArgs(5),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)
clientID := args[0]
// attempt to unmarshal client state argument
var clientState exported.ClientState
clientContentOrFileName := args[1]
if err := cdc.UnmarshalInterfaceJSON([]byte(clientContentOrFileName), &clientState); err != nil {
// check for file path if JSON input is not provided
contents, err := os.ReadFile(clientContentOrFileName)
if err != nil {
return fmt.Errorf("neither JSON input nor path to .json file for client state were provided: %w", err)
}
if err := cdc.UnmarshalInterfaceJSON(contents, &clientState); err != nil {
return fmt.Errorf("error unmarshalling client state file: %w", err)
}
}
// attempt to unmarshal consensus state argument
var consensusState exported.ConsensusState
consensusContentOrFileName := args[2]
if err := cdc.UnmarshalInterfaceJSON([]byte(consensusContentOrFileName), &consensusState); err != nil {
// check for file path if JSON input is not provided
contents, err := os.ReadFile(consensusContentOrFileName)
if err != nil {
return fmt.Errorf("neither JSON input nor path to .json file for consensus state were provided: %w", err)
}
if err := cdc.UnmarshalInterfaceJSON(contents, &consensusState); err != nil {
return fmt.Errorf("error unmarshalling consensus state file: %w", err)
}
}
upgradeClientProof := []byte(args[3])
upgradeConsensusProof := []byte(args[4])
msg, err := types.NewMsgUpgradeClient(clientID, clientState, consensusState, upgradeClientProof, upgradeConsensusProof, clientCtx.GetFromAddress().String())
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// newSubmitRecoverClientProposalCmd defines the command to recover an IBC light client.
func newSubmitRecoverClientProposalCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "recover-client [subject-client-id] [substitute-client-id] [flags]",
Args: cobra.ExactArgs(2),
Short: "recover an IBC client",
Long: `Submit a recover IBC client proposal along with an initial deposit
Please specify a subject client identifier you want to recover
Please specify the substitute client the subject client will be recovered to.`,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
proposal, err := govcli.ReadGovPropFlags(clientCtx, cmd.Flags())
if err != nil {
return err
}
subjectClientID, substituteClientID := args[0], args[1]
authority, _ := cmd.Flags().GetString(FlagAuthority)
if authority != "" {
if _, err = sdk.AccAddressFromBech32(authority); err != nil {
return fmt.Errorf("invalid authority address: %w", err)
}
} else {
authority = sdk.AccAddress(address.Module(govtypes.ModuleName)).String()
}
msg := types.NewMsgRecoverClient(authority, subjectClientID, substituteClientID)
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("error validating %T: %w", types.MsgRecoverClient{}, err)
}
if err := proposal.SetMsgs([]sdk.Msg{msg}); err != nil {
return fmt.Errorf("failed to create recover client proposal message: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), proposal)
},
}
cmd.Flags().String(FlagAuthority, "", "The address of the client module authority (defaults to gov)")
flags.AddTxFlagsToCmd(cmd)
govcli.AddGovPropFlagsToCmd(cmd)
err := cmd.MarkFlagRequired(govcli.FlagTitle)
if err != nil {
panic(err)
}
return cmd
}
// newScheduleIBCUpgradeProposalCmd defines the command for submitting an IBC software upgrade proposal.
func newScheduleIBCUpgradeProposalCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "schedule-ibc-upgrade [name] [height] [path/to/upgraded_client_state.json] [flags]",
Args: cobra.ExactArgs(3),
Short: "Submit an IBC software upgrade proposal",
Long: `Please specify a unique name and height for the upgrade to take effect.
The client state specified is the upgraded client state representing the upgraded chain
Example Upgraded Client State JSON:
{
"@type":"/ibc.lightclients.tendermint.v1.ClientState",
"chain_id":"testchain1",
"unbonding_period":"1814400s",
"latest_height":{
"revision_number":"0",
"revision_height":"2"
},
"proof_specs":[
{
"leaf_spec":{
"hash":"SHA256",
"prehash_key":"NO_HASH",
"prehash_value":"SHA256",
"length":"VAR_PROTO",
"prefix":"AA=="
},
"inner_spec":{
"child_order":[
0,
1
],
"child_size":33,
"min_prefix_length":4,
"max_prefix_length":12,
"empty_child":null,
"hash":"SHA256"
},
"max_depth":0,
"min_depth":0
},
{
"leaf_spec":{
"hash":"SHA256",
"prehash_key":"NO_HASH",
"prehash_value":"SHA256",
"length":"VAR_PROTO",
"prefix":"AA=="
},
"inner_spec":{
"child_order":[
0,
1
],
"child_size":32,
"min_prefix_length":1,
"max_prefix_length":1,
"empty_child":null,
"hash":"SHA256"
},
"max_depth":0,
"min_depth":0
}
],
"upgrade_path":[
"upgrade",
"upgradedIBCState"
]
}
`,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
proposal, err := govcli.ReadGovPropFlags(clientCtx, cmd.Flags())
if err != nil {
return err
}
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)
name := args[0]
height, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return err
}
plan := upgradetypes.Plan{
Name: name,
Height: height,
}
// attempt to unmarshal client state argument
var clientState exported.ClientState
clientContentOrFileName := args[2]
if err := cdc.UnmarshalInterfaceJSON([]byte(clientContentOrFileName), &clientState); err != nil {
// check for file path if JSON input is not provided
contents, err := os.ReadFile(clientContentOrFileName)
if err != nil {
return fmt.Errorf("neither JSON input nor path to .json file for client state were provided: %w", err)
}
if err := cdc.UnmarshalInterfaceJSON(contents, &clientState); err != nil {
return fmt.Errorf("error unmarshalling client state file: %w", err)
}
}
authority, _ := cmd.Flags().GetString(FlagAuthority)
if authority != "" {
if _, err = sdk.AccAddressFromBech32(authority); err != nil {
return fmt.Errorf("invalid authority address: %w", err)
}
} else {
authority = sdk.AccAddress(address.Module(govtypes.ModuleName)).String()
}
msg, err := types.NewMsgIBCSoftwareUpgrade(authority, plan, clientState)
if err != nil {
return fmt.Errorf("error in %T: %w", types.MsgIBCSoftwareUpgrade{}, err)
}
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("error validating %T: %w", types.MsgIBCSoftwareUpgrade{}, err)
}
if err := proposal.SetMsgs([]sdk.Msg{msg}); err != nil {
return fmt.Errorf("failed to create proposal message for scheduling an IBC software upgrade: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), proposal)
},
}
cmd.Flags().String(FlagAuthority, "", "The address of the client module authority (defaults to gov)")
flags.AddTxFlagsToCmd(cmd)
govcli.AddGovPropFlagsToCmd(cmd)
err := cmd.MarkFlagRequired(govcli.FlagTitle)
if err != nil {
panic(err)
}
return cmd
}