-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
169 lines (139 loc) · 5.65 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
package cli
import (
"fmt"
"io/ioutil"
"strconv"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/creatachain/creata-sdk/client"
"github.com/creatachain/creata-sdk/client/flags"
"github.com/creatachain/creata-sdk/client/tx"
"github.com/creatachain/creata-sdk/codec"
"github.com/creatachain/creata-sdk/version"
clienttypes "github.com/creatachain/creata-sdk/x/icp/core/02-client/types"
"github.com/creatachain/creata-sdk/x/icp/light-clients/06-solomachine/types"
)
const (
flagAllowUpdateAfterProposal = "allow_update_after_proposal"
)
// NewCreateClientCmd defines the command to create a new solo machine client.
func NewCreateClientCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create [sequence] [path/to/consensus_state.json]",
Short: "create new solo machine client",
Long: `create a new solo machine client with the specified identifier and public key
- ConsensusState json example: {"public_key":{"@type":"/creata.crypto.secp256k1.PubKey","key":"A/3SXL2ONYaOkxpdR5P8tHTlSlPv1AwQwSFxKRee5JQW"},"diversifier":"diversifier","timestamp":"10"}`,
Example: fmt.Sprintf("%s tx icp %s create [sequence] [path/to/consensus_state] --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
}
sequence, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return err
}
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)
// attempt to unmarshal consensus state argument
consensusState := &types.ConsensusState{}
if err := cdc.UnmarshalJSON([]byte(args[1]), consensusState); err != nil {
// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(args[1])
if err != nil {
return errors.Wrap(err, "neither JSON input nor path to .json file for consensus state were provided")
}
if err := cdc.UnmarshalJSON(contents, consensusState); err != nil {
return errors.Wrap(err, "error unmarshalling consensus state file")
}
}
allowUpdateAfterProposal, _ := cmd.Flags().GetBool(flagAllowUpdateAfterProposal)
clientState := types.NewClientState(sequence, consensusState, allowUpdateAfterProposal)
msg, err := clienttypes.NewMsgCreateClient(clientState, consensusState, clientCtx.GetFromAddress())
if err != nil {
return err
}
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().Bool(flagAllowUpdateAfterProposal, false, "allow governance proposal to update client")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewUpdateClientCmd defines the command to update a solo machine client.
func NewUpdateClientCmd() *cobra.Command {
return &cobra.Command{
Use: "update [client-id] [path/to/header.json]",
Short: "update existing client with a header",
Long: "update existing client with a solo machine header",
Example: fmt.Sprintf("%s tx icp %s update [client-id] [path/to/header.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)
header := &types.Header{}
if err := cdc.UnmarshalJSON([]byte(args[1]), header); err != nil {
// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(args[1])
if err != nil {
return errors.Wrap(err, "neither JSON input nor path to .json file for header were provided")
}
if err := cdc.UnmarshalJSON(contents, header); err != nil {
return errors.Wrap(err, "error unmarshalling header file")
}
}
msg, err := clienttypes.NewMsgUpdateClient(clientID, header, clientCtx.GetFromAddress())
if err != nil {
return err
}
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
}
// NewSubmitMisbehaviourCmd defines the command to submit a misbehaviour to prevent
// future updates.
func NewSubmitMisbehaviourCmd() *cobra.Command {
return &cobra.Command{
Use: "misbehaviour [path/to/misbehaviour.json]",
Short: "submit a client misbehaviour",
Long: "submit a client misbehaviour to prevent future updates",
Example: fmt.Sprintf("%s tx icp %s misbehaviour [path/to/misbehaviour.json] --from node0 --home ../node0/<app>cli --chain-id $CID", version.AppName, types.SubModuleName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)
m := &types.Misbehaviour{}
if err := cdc.UnmarshalJSON([]byte(args[0]), m); err != nil {
// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(args[0])
if err != nil {
return errors.Wrap(err, "neither JSON input nor path to .json file for misbehaviour were provided")
}
if err := cdc.UnmarshalJSON(contents, m); err != nil {
return errors.Wrap(err, "error unmarshalling misbehaviour file")
}
}
msg, err := clienttypes.NewMsgSubmitMisbehaviour(m.ClientId, m, clientCtx.GetFromAddress())
if err != nil {
return err
}
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
}