-
Notifications
You must be signed in to change notification settings - Fork 111
/
hyperspace_commander.go
396 lines (353 loc) · 11.7 KB
/
hyperspace_commander.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
// Package hyperspace provides an interface to the hyperspace relayer running in a Docker container.
package hyperspace
import (
"context"
"fmt"
"path"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
types23 "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types"
"github.com/misko9/go-substrate-rpc-client/v4/signature"
"github.com/pelletier/go-toml/v2"
"github.com/strangelove-ventures/interchaintest/v7/chain/polkadot"
"github.com/strangelove-ventures/interchaintest/v7/ibc"
"go.uber.org/zap"
)
// hyperspaceCommander satisfies relayer.RelayerCommander.
type hyperspaceCommander struct {
log *zap.Logger
paths map[string]*pathConfiguration
extraStartFlags []string
}
// pathConfiguration represents the concept of a "path" which is implemented at the interchain test level rather
// than the hyperspace level.
type pathConfiguration struct {
chainA, chainB pathChainConfig
}
// pathChainConfig holds all values that will be required when interacting with a path.
type pathChainConfig struct {
chainID string
}
func (hyperspaceCommander) Name() string {
return "hyperspace"
}
func (hyperspaceCommander) DockerUser() string {
return "1000:1000" // docker run -it --rm --entrypoint echo ghcr.io/cosmos/relayer "$(id -u):$(id -g)"
}
func (c *hyperspaceCommander) AddChainConfiguration(containerFilePath, homeDir string) []string {
fmt.Println("[hyperspace] AddChainConfiguration ", containerFilePath, homeDir)
//c.chainConfigPaths = append(c.chainConfigPaths, containerFilePath)
return []string{
"hyperspace",
"-h",
}
}
// Hyperspace doesn't not have this functionality
func (hyperspaceCommander) AddKey(chainID, keyName, coinType, homeDir string) []string {
panic("[AddKey] Do not call me")
}
func (c *hyperspaceCommander) CreateChannel(pathName string, opts ibc.CreateChannelOptions, homeDir string) []string {
fmt.Println("[hyperspace] CreateChannel", pathName, homeDir)
_, ok := c.paths[pathName]
if !ok {
panic(fmt.Sprintf("path %s not found", pathName))
}
return []string{
"hyperspace",
"create-channel",
"--config-a",
configPath(homeDir, c.paths[pathName].chainA.chainID),
"--config-b",
configPath(homeDir, c.paths[pathName].chainB.chainID),
"--config-core",
path.Join(homeDir, "core.config"),
"--delay-period",
"0",
"--port-id",
opts.SourcePortName,
"--order",
"unordered",
"--version",
opts.Version,
}
}
func (c *hyperspaceCommander) CreateClient(srcChainID, dstChainID, pathName string, opts ibc.CreateClientOptions, homeDir string) []string {
panic("[CreateClient] do not call me")
}
func (c *hyperspaceCommander) CreateClients(pathName string, opts ibc.CreateClientOptions, homeDir string) []string {
fmt.Println("[hyperspace] CreateClients", pathName, opts, homeDir)
_, ok := c.paths[pathName]
if !ok {
panic(fmt.Sprintf("path %s not found", pathName))
}
return []string{
"hyperspace",
"create-clients",
"--config-a",
configPath(homeDir, c.paths[pathName].chainA.chainID),
"--config-b",
configPath(homeDir, c.paths[pathName].chainB.chainID),
"--config-core",
path.Join(homeDir, "core.config"),
"--delay-period",
"0",
"--port-id",
"transfer",
"--order",
"unordered",
}
}
func (c *hyperspaceCommander) CreateConnections(pathName, homeDir string) []string {
fmt.Println("[hyperspace] CreateConnections", pathName, homeDir)
_, ok := c.paths[pathName]
if !ok {
panic(fmt.Sprintf("path %s not found", pathName))
}
return []string{
"hyperspace",
"create-connection",
"--config-a",
configPath(homeDir, c.paths[pathName].chainA.chainID),
"--config-b",
configPath(homeDir, c.paths[pathName].chainB.chainID),
"--config-core",
path.Join(homeDir, "core.config"),
"--delay-period",
"0",
"--port-id",
"transfer",
"--order",
"unordered",
}
}
// Hyperspace doesn't not have this functionality
func (hyperspaceCommander) FlushAcknowledgements(pathName, channelID, homeDir string) []string {
panic("[FlushAcknowledgements] Do not call me")
}
// Hyperspace doesn't not have this functionality
func (hyperspaceCommander) FlushPackets(pathName, channelID, homeDir string) []string {
panic("[FlushPackets] Do not call me")
}
// GeneratePath establishes an in memory path representation. The concept does not exist in hyperspace.
func (c *hyperspaceCommander) GeneratePath(srcChainID, dstChainID, pathName, homeDir string) []string {
if c.paths == nil {
c.paths = map[string]*pathConfiguration{}
}
c.paths[pathName] = &pathConfiguration{
chainA: pathChainConfig{
chainID: srcChainID,
},
chainB: pathChainConfig{
chainID: dstChainID,
},
}
return []string{"true"}
}
// Hyperspace does not have paths, just two configs
func (hyperspaceCommander) UpdatePath(pathName, homeDir string, opts ibc.PathUpdateOptions) []string {
panic("[UpdatePath] Do not call me")
}
// Prints chain config which is populated by hyperspace
// Ideally, there should be a command from hyperspace to get this output
func (hyperspaceCommander) GetChannels(chainID, homeDir string) []string {
fmt.Println("[hyperspace] Get Channels")
configFilePath := path.Join(homeDir, chainID+".config")
return []string{
"cat",
configFilePath,
}
}
// Prints chain config which is populated by hyperspace
// Ideally, there should be a command from hyperspace to get this output
func (hyperspaceCommander) GetConnections(chainID, homeDir string) []string {
fmt.Println("[hyperspace] Get Connections")
configFilePath := path.Join(homeDir, chainID+".config")
return []string{
"cat",
configFilePath,
}
}
// Prints chain config which is populated by hyperspace
// Ideally, there should be a command from hyperspace to get this output
func (hyperspaceCommander) GetClients(chainID, homeDir string) []string {
fmt.Println("[hyperspace] Get Clients")
configFilePath := path.Join(homeDir, chainID+".config")
return []string{
"cat",
configFilePath,
}
}
// Hyperspace does not have link cmd, call create clients, create connection, and create channel
func (hyperspaceCommander) LinkPath(pathName, homeDir string, channelOpts ibc.CreateChannelOptions, clientOpt ibc.CreateClientOptions) []string {
panic("[LinkPath] Do not use me")
}
// There is no hyperspace call to restore the key, so this can't return an executable.
// HyperspaceRelayer's RestoreKey will restore the key in the chain's config file
func (hyperspaceCommander) RestoreKey(chainID, bech32Prefix, coinType, mnemonic, homeDir string) []string {
panic("[RestoreKey] Do not use me")
}
// hyperspace can only start 1 path
func (c *hyperspaceCommander) StartRelayer(homeDir string, pathNames ...string) []string {
fmt.Println("[hyperspace] StartRelayer", homeDir, pathNames)
if len(pathNames) != 1 {
panic("Hyperspace's StartRelayer list of paths can only have 1 path")
}
pathName := pathNames[0]
_, ok := c.paths[pathName]
if !ok {
panic(fmt.Sprintf("path %s not found", pathName))
}
return []string{
"hyperspace",
"relay",
"--config-a",
configPath(homeDir, c.paths[pathName].chainA.chainID),
"--config-b",
configPath(homeDir, c.paths[pathName].chainB.chainID),
"--config-core",
path.Join(homeDir, "core.config"),
"--delay-period",
"0",
"--port-id",
"transfer",
"--order",
"unordered",
"--version",
"ics20-1",
}
}
// Hyperspace doesn't not have this functionality
func (hyperspaceCommander) UpdateClients(pathName, homeDir string) []string {
panic("[UpdateClients] Do not use me")
}
func (hyperspaceCommander) ConfigContent(ctx context.Context, cfg ibc.ChainConfig, keyName, rpcAddr, grpcAddr string) ([]byte, error) {
fmt.Println("[hyperspace] ConfigContent", cfg, keyName, rpcAddr, grpcAddr)
HyperspaceRelayerChainConfig := ChainConfigToHyperspaceRelayerChainConfig(cfg, keyName, rpcAddr, grpcAddr)
bytes, err := toml.Marshal(HyperspaceRelayerChainConfig)
if err != nil {
return nil, err
}
return bytes, nil
}
func (hyperspaceCommander) DefaultContainerImage() string {
return HyperspaceDefaultContainerImage
}
func (hyperspaceCommander) DefaultContainerVersion() string {
return HyperspaceDefaultContainerVersion
}
// There is no hyperspace call to add key, so there is no stdout to parse.
// DockerRelayer's RestoreKey will restore the key in the chain's config file
func (hyperspaceCommander) ParseAddKeyOutput(stdout, stderr string) (ibc.Wallet, error) {
panic("[ParseAddKeyOutput] Do not call me")
}
// There is no hyperspace call to restore the key, so there is no stdout to parse.
// DockerRelayer's RestoreKey will restore the key in the chain's config file
func (hyperspaceCommander) ParseRestoreKeyOutput(stdout, stderr string) string {
panic("[ParseRestoreKeyOutput] Do not call me")
}
type ChannelsOutput struct {
Channels [][]string `toml:"channel_whitelist"`
}
// Parses output of chain config which is populated by hyperspace
// Ideally, there should be a command from hyperspace to get this output
func (hyperspaceCommander) ParseGetChannelsOutput(stdout, stderr string) ([]ibc.ChannelOutput, error) {
var cfg ChannelsOutput
err := toml.Unmarshal([]byte(stdout), &cfg)
if err != nil {
return nil, err
}
outputs := make([]ibc.ChannelOutput, 0)
for _, channel := range cfg.Channels {
outputs = append(outputs, ibc.ChannelOutput{
State: "",
Ordering: "",
Counterparty: ibc.ChannelCounterparty{ // TODO: retrieve from hyperspace
PortID: "",
ChannelID: "",
},
ConnectionHops: []string{},
Version: "",
PortID: channel[1],
ChannelID: channel[0],
})
}
return outputs, nil
}
type ConnectionsOutput struct {
ConnectionID string `toml:"connection_id"`
ClientID string `toml:"client_id"`
}
// Parses output of chain config which is populated by hyperspace
// Ideally, there should be a command from hyperspace to get this output
// Only supports 1 connection and limited info
func (hyperspaceCommander) ParseGetConnectionsOutput(stdout, stderr string) (ibc.ConnectionOutputs, error) {
var cfg ConnectionsOutput
err := toml.Unmarshal([]byte(stdout), &cfg)
if err != nil {
return nil, err
}
return ibc.ConnectionOutputs{
&ibc.ConnectionOutput{
ID: cfg.ConnectionID,
ClientID: cfg.ClientID,
Versions: []*ibcexported.Version{
{
Identifier: "",
Features: []string{},
},
},
State: "",
Counterparty: &ibcexported.Counterparty{
ClientId: "",
ConnectionId: "",
Prefix: types23.MerklePrefix{
KeyPrefix: []byte{},
},
},
DelayPeriod: "0",
},
}, nil
}
type ClientOutput struct {
ChainID string `toml:"chain_id"`
ClientID string `toml:"client_id"`
}
// Parses output of chain config which is populated by hyperspace
// Ideally, there should be a command from hyperspace to get this output
// Only supports 1 client
func (hyperspaceCommander) ParseGetClientsOutput(stdout, stderr string) (ibc.ClientOutputs, error) {
var cfg ClientOutput
err := toml.Unmarshal([]byte(stdout), &cfg)
if err != nil {
return nil, err
}
return ibc.ClientOutputs{
&ibc.ClientOutput{
ClientID: cfg.ClientID,
ClientState: ibc.ClientState{
ChainID: cfg.ChainID,
},
},
}, nil
}
func (hyperspaceCommander) Init(homeDir string) []string {
fmt.Println("[hyperspace] Init", homeDir)
// Return hyperspace help to ensure hyperspace binary is accessible
return []string{
"hyperspace",
"-h",
}
}
func (hyperspaceCommander) CreateWallet(keyName, address, mnemonic string) ibc.Wallet {
kp, err := signature.KeyringPairFromSecret(mnemonic, polkadot.Ss58Format)
if err != nil {
return NewWallet("", "", "")
}
return NewWallet("", kp.Address, mnemonic)
}
func (hyperspaceCommander) Flush(pathName, channelID, homeDir string) []string {
panic("flush implemented in hyperspace not the commander")
}
func configPath(homeDir, chainID string) string {
chainConfigFile := chainID + ".config"
return path.Join(homeDir, chainConfigFile)
}