-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
tx.go
308 lines (259 loc) · 10.8 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
package cli
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
"time"
ics23 "github.com/confio/ics23/go"
"github.com/pkg/errors"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/light"
"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"
"github.com/cosmos/cosmos-sdk/version"
clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/core/02-client/types"
commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/core/23-commitment/types"
"github.com/cosmos/cosmos-sdk/x/ibc/light-clients/07-tendermint/types"
)
const (
flagTrustLevel = "trust-level"
flagConsensusParams = "consensus-params"
flagProofSpecs = "proof-specs"
flagUpgradePath = "upgrade-path"
flagAllowUpdateAfterExpiry = "allow_update_after_expiry"
flagAllowUpdateAfterMisbehaviour = "allow_update_after_misbehaviour"
)
// NewCreateClientCmd defines the command to create a new IBC Client as defined
// in https://github.com/cosmos/ics/tree/master/spec/ics-002-client-semantics#create
func NewCreateClientCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create [client-id] [path/to/consensus_state.json] [trusting_period] [unbonding_period] [max_clock_drift]",
Short: "create new tendermint client",
Long: `Create a new tendermint IBC client.
- 'trust-level' flag can be a fraction (eg: '1/3') or 'default'
- 'consensus-params' flag can be a JSON input, a path to a .json file. The params must match the consensus parameters of the chain this light client represents.
- 'proof-specs' flag can be JSON input, a path to a .json file or 'default'
- 'upgrade-path' flag is a string specifying the upgrade path for this chain where a future upgraded client will be stored. The path represents a keypath for the store with each key separated by a '/'. Any slash within a key must be escaped.
e.g. 'upgrade/upgradedClient'`,
Example: fmt.Sprintf("%s tx ibc %s create [client-id] [path/to/consensus_state.json] [trusting_period] [unbonding_period] [max_clock_drift] --trust-level default --consensus-params [path/to/consensus-params.json] --proof-specs [path/to/proof-specs.json] --upgrade-path upgrade/upgradedClient --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 := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.ReadTxCommandFlags(clientCtx, cmd.Flags())
if err != nil {
return err
}
clientID := args[0]
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)
legacyAmino := codec.NewLegacyAmino()
var 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.New("neither JSON input nor path to .json file were provided for consensus header")
}
if err := cdc.UnmarshalJSON(contents, header); err != nil {
return errors.Wrap(err, "error unmarshalling consensus header file")
}
}
var (
trustLevel types.Fraction
consensusParams *abci.ConsensusParams
specs []*ics23.ProofSpec
)
lvl, _ := cmd.Flags().GetString(flagTrustLevel)
if lvl == "default" {
trustLevel = types.NewFractionFromTm(light.DefaultTrustLevel)
} else {
trustLevel, err = parseFraction(lvl)
if err != nil {
return err
}
}
trustingPeriod, err := time.ParseDuration(args[2])
if err != nil {
return err
}
ubdPeriod, err := time.ParseDuration(args[3])
if err != nil {
return err
}
maxClockDrift, err := time.ParseDuration(args[4])
if err != nil {
return err
}
cp, _ := cmd.Flags().GetString(flagConsensusParams)
if cp != "" {
if err := legacyAmino.UnmarshalJSON([]byte(cp), &consensusParams); err != nil {
// check for file path if JSON input not provided
contents, err := ioutil.ReadFile(cp)
if err != nil {
return errors.New("neither JSON input nor path to .json file was provided for consensus params flag")
}
// TODO migrate to use JSONMarshaler (implement MarshalJSONArray
// or wrap lists of proto.Message in some other message)
if err := legacyAmino.UnmarshalJSON(contents, &consensusParams); err != nil {
return errors.Wrap(err, "error unmarshalling consensus params file")
}
}
}
spc, _ := cmd.Flags().GetString(flagProofSpecs)
if spc == "default" {
specs = commitmenttypes.GetSDKSpecs()
// TODO migrate to use JSONMarshaler (implement MarshalJSONArray
// or wrap lists of proto.Message in some other message)
} else if err := legacyAmino.UnmarshalJSON([]byte(spc), &specs); err != nil {
// check for file path if JSON input not provided
contents, err := ioutil.ReadFile(spc)
if err != nil {
return errors.New("neither JSON input nor path to .json file was provided for proof specs flag")
}
// TODO migrate to use JSONMarshaler (implement MarshalJSONArray
// or wrap lists of proto.Message in some other message)
if err := legacyAmino.UnmarshalJSON(contents, &specs); err != nil {
return errors.Wrap(err, "error unmarshalling proof specs file")
}
}
allowUpdateAfterExpiry, _ := cmd.Flags().GetBool(flagAllowUpdateAfterExpiry)
allowUpdateAfterMisbehaviour, _ := cmd.Flags().GetBool(flagAllowUpdateAfterMisbehaviour)
upgradePath, _ := cmd.Flags().GetString(flagUpgradePath)
// validate header
if err := header.ValidateBasic(); err != nil {
return err
}
height := header.GetHeight().(clienttypes.Height)
clientState := types.NewClientState(
header.GetHeader().GetChainID(), trustLevel, trustingPeriod, ubdPeriod, maxClockDrift,
height, consensusParams, specs, upgradePath, allowUpdateAfterExpiry, allowUpdateAfterMisbehaviour,
)
consensusState := header.ConsensusState()
msg, err := clienttypes.NewMsgCreateClient(
clientID, 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().String(flagTrustLevel, "default", "light client trust level fraction for header updates")
cmd.Flags().String(flagProofSpecs, "default", "proof specs format to be used for verification")
cmd.Flags().Bool(flagAllowUpdateAfterExpiry, false, "allow governance proposal to update client after expiry")
cmd.Flags().Bool(flagAllowUpdateAfterMisbehaviour, false, "allow governance proposal to update client after misbehaviour")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewUpdateClientCmd defines the command to update a client as defined in
// https://github.com/cosmos/ics/tree/master/spec/ics-002-client-semantics#update
func NewUpdateClientCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "update [client-id] [path/to/header.json]",
Short: "update existing client with a header",
Long: "update existing tendermint client with a tendermint header",
Example: fmt.Sprintf(
"$ %s tx ibc %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 := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.ReadTxCommandFlags(clientCtx, cmd.Flags())
if err != nil {
return err
}
clientID := args[0]
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)
var 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.New("neither JSON input nor path to .json file 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)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewSubmitMisbehaviourCmd defines the command to submit a misbehaviour to invalidate
// previous state roots and prevent future updates as defined in
// https://github.com/cosmos/ics/tree/master/spec/ics-002-client-semantics#misbehaviour
func NewSubmitMisbehaviourCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "misbehaviour [path/to/misbehaviour.json]",
Short: "submit a client misbehaviour",
Long: "submit a client misbehaviour to invalidate to invalidate previous state roots and prevent future updates",
Example: fmt.Sprintf(
"$ %s tx ibc %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 := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.ReadTxCommandFlags(clientCtx, cmd.Flags())
if err != nil {
return err
}
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)
var 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.New("neither JSON input nor path to .json file 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)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func parseFraction(fraction string) (types.Fraction, error) {
fr := strings.Split(fraction, "/")
if len(fr) != 2 || fr[0] == fraction {
return types.Fraction{}, fmt.Errorf("fraction must have format 'numerator/denominator' got %s", fraction)
}
numerator, err := strconv.ParseInt(fr[0], 10, 64)
if err != nil {
return types.Fraction{}, fmt.Errorf("invalid trust-level numerator: %w", err)
}
denominator, err := strconv.ParseInt(fr[1], 10, 64)
if err != nil {
return types.Fraction{}, fmt.Errorf("invalid trust-level denominator: %w", err)
}
return types.Fraction{
Numerator: numerator,
Denominator: denominator,
}, nil
}