/
tx.go
335 lines (278 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
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
package cli
import (
"fmt"
"io/ioutil"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"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"
"github.com/cosmos/cosmos-sdk/x/ibc/core/03-connection/client/utils"
"github.com/cosmos/cosmos-sdk/x/ibc/core/03-connection/types"
host "github.com/cosmos/cosmos-sdk/x/ibc/core/24-host"
)
const (
flagVersionIdentifier = "version-identifier"
flagVersionFeatures = "version-features"
flagDelayPeriod = "delay-period"
)
// NewConnectionOpenInitCmd defines the command to initialize a connection on
// chain A with a given counterparty chain B
func NewConnectionOpenInitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "open-init [client-id] [counterparty-client-id] [path/to/counterparty_prefix.json]",
Short: "Initialize connection on chain A",
Long: `Initialize a connection on chain A with a given counterparty chain B.
- 'version-identifier' flag can be a single pre-selected version identifier to be used in the handshake.
- 'version-features' flag can be a list of features separated by commas to accompany the version identifier.`,
Example: fmt.Sprintf(
"%s tx %s %s open-init [client-id] [counterparty-client-id] [path/to/counterparty_prefix.json] --version-identifier=\"1.0\" --version-features=\"ORDER_UNORDERED\" --delay-period=500",
version.AppName, host.ModuleName, types.SubModuleName,
),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
clientID := args[0]
counterpartyClientID := args[1]
counterpartyPrefix, err := utils.ParsePrefix(clientCtx.LegacyAmino, args[2])
if err != nil {
return err
}
var version *types.Version
versionIdentifier, _ := cmd.Flags().GetString(flagVersionIdentifier)
if versionIdentifier != "" {
var features []string
versionFeatures, _ := cmd.Flags().GetString(flagVersionFeatures)
if versionFeatures != "" {
features = strings.Split(versionFeatures, ",")
}
version = types.NewVersion(versionIdentifier, features)
}
delayPeriod, err := cmd.Flags().GetUint64(flagDelayPeriod)
if err != nil {
return err
}
msg := types.NewMsgConnectionOpenInit(
clientID, counterpartyClientID,
counterpartyPrefix, version, delayPeriod, clientCtx.GetFromAddress(),
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
// NOTE: we should use empty default values since the user may not want to select a version
// at this step in the handshake.
cmd.Flags().String(flagVersionIdentifier, "", "version identifier to be used in the connection handshake version negotiation")
cmd.Flags().String(flagVersionFeatures, "", "version features list separated by commas without spaces. The features must function with the version identifier.")
cmd.Flags().Uint64(flagDelayPeriod, 0, "delay period that must pass before packet verification can pass against a consensus state")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewConnectionOpenTryCmd defines the command to relay a try open a connection on
// chain B
func NewConnectionOpenTryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: strings.TrimSpace(`open-try [connection-id] [client-id]
[counterparty-connection-id] [counterparty-client-id] [path/to/counterparty_prefix.json] [path/to/client_state.json]
[path/to/counterparty_version1.json,path/to/counterparty_version2.json...] [consensus-height] [proof-height] [path/to/proof_init.json] [path/to/proof_client.json] [path/to/proof_consensus.json]`),
Short: "initiate connection handshake between two chains",
Long: "Initialize a connection on chain A with a given counterparty chain B. Provide counterparty versions separated by commas",
Example: fmt.Sprintf(
`%s tx %s %s open-try connection-id] [client-id] \
[counterparty-connection-id] [counterparty-client-id] [path/to/counterparty_prefix.json] [path/to/client_state.json]\
[counterparty-versions] [consensus-height] [proof-height] [path/to/proof_init.json] [path/to/proof_client.json] [path/to/proof_consensus.json]`,
version.AppName, host.ModuleName, types.SubModuleName,
),
Args: cobra.ExactArgs(12),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
connectionID := args[0]
clientID := args[1]
counterpartyConnectionID := args[2]
counterpartyClientID := args[3]
counterpartyPrefix, err := utils.ParsePrefix(clientCtx.LegacyAmino, args[4])
if err != nil {
return err
}
counterpartyClient, err := utils.ParseClientState(clientCtx.LegacyAmino, args[5])
if err != nil {
return err
}
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)
versionsStr := strings.Split(args[6], ",")
counterpartyVersions := make([]*types.Version, len(versionsStr))
for _, ver := range versionsStr {
// attempt to unmarshal version
version := &types.Version{}
if err := cdc.UnmarshalJSON([]byte(ver), version); err != nil {
// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(ver)
if err != nil {
return errors.Wrap(err, "neither JSON input nor path to .json file for version were provided")
}
if err := cdc.UnmarshalJSON(contents, version); err != nil {
return errors.Wrap(err, "error unmarshalling version file")
}
}
}
consensusHeight, err := clienttypes.ParseHeight(args[7])
if err != nil {
return err
}
proofHeight, err := clienttypes.ParseHeight(args[8])
if err != nil {
return err
}
proofInit, err := utils.ParseProof(clientCtx.LegacyAmino, args[9])
if err != nil {
return err
}
proofClient, err := utils.ParseProof(clientCtx.LegacyAmino, args[10])
if err != nil {
return err
}
proofConsensus, err := utils.ParseProof(clientCtx.LegacyAmino, args[11])
if err != nil {
return err
}
delayPeriod, err := cmd.Flags().GetUint64(flagDelayPeriod)
if err != nil {
return err
}
msg := types.NewMsgConnectionOpenTry(
connectionID, clientID, counterpartyConnectionID, counterpartyClientID,
counterpartyClient, counterpartyPrefix, counterpartyVersions, delayPeriod,
proofInit, proofClient, proofConsensus, proofHeight,
consensusHeight, clientCtx.GetFromAddress(),
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().Uint64(flagDelayPeriod, 0, "delay period that must pass before packet verification can pass against a consensus state")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewConnectionOpenAckCmd defines the command to relay the acceptance of a
// connection open attempt from chain B to chain A
func NewConnectionOpenAckCmd() *cobra.Command {
cmd := &cobra.Command{
Use: `open-ack [connection-id] [counterparty-connection-id] [path/to/client_state.json] [consensus-height] [proof-height]
[path/to/proof_try.json] [path/to/proof_client.json] [path/to/proof_consensus.json] [version]`,
Short: "relay the acceptance of a connection open attempt",
Long: "Relay the acceptance of a connection open attempt from chain B to chain A",
Example: fmt.Sprintf(
`%s tx %s %s open-ack [connection-id] [counterparty-connection-id] [path/to/client_state.json] [consensus-height] [proof-height]
[path/to/proof_try.json] [path/to/proof_client.json] [path/to/proof_consensus.json] [version]`,
version.AppName, host.ModuleName, types.SubModuleName,
),
Args: cobra.ExactArgs(9),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
connectionID := args[0]
counterpartyConnectionID := args[1]
counterpartyClient, err := utils.ParseClientState(clientCtx.LegacyAmino, args[2])
if err != nil {
return err
}
consensusHeight, err := clienttypes.ParseHeight(args[3])
if err != nil {
return err
}
proofHeight, err := clienttypes.ParseHeight(args[4])
if err != nil {
return err
}
proofTry, err := utils.ParseProof(clientCtx.LegacyAmino, args[5])
if err != nil {
return err
}
proofClient, err := utils.ParseProof(clientCtx.LegacyAmino, args[6])
if err != nil {
return err
}
proofConsensus, err := utils.ParseProof(clientCtx.LegacyAmino, args[7])
if err != nil {
return err
}
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)
// attempt to unmarshal version
version := &types.Version{}
if err := cdc.UnmarshalJSON([]byte(args[8]), version); err != nil {
// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(args[8])
if err != nil {
return errors.Wrap(err, "neither JSON input nor path to .json file for version were provided")
}
if err := cdc.UnmarshalJSON(contents, version); err != nil {
return errors.Wrap(err, "error unmarshalling version file")
}
}
msg := types.NewMsgConnectionOpenAck(
connectionID, counterpartyConnectionID, counterpartyClient, proofTry, proofClient, proofConsensus, proofHeight,
consensusHeight, version, clientCtx.GetFromAddress(),
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewConnectionOpenConfirmCmd defines the command to initialize a connection on
// chain A with a given counterparty chain B
func NewConnectionOpenConfirmCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "open-confirm [connection-id] [proof-height] [path/to/proof_ack.json]",
Short: "confirm to chain B that connection is open on chain A",
Long: "Confirm to chain B that connection is open on chain A",
Example: fmt.Sprintf(
"%s tx %s %s open-confirm [connection-id] [proof-height] [path/to/proof_ack.json]",
version.AppName, host.ModuleName, types.SubModuleName,
),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
connectionID := args[0]
proofHeight, err := clienttypes.ParseHeight(args[1])
if err != nil {
return err
}
proofAck, err := utils.ParseProof(clientCtx.LegacyAmino, args[2])
if err != nil {
return err
}
msg := types.NewMsgConnectionOpenConfirm(
connectionID, proofAck, proofHeight, clientCtx.GetFromAddress(),
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}