forked from strangelove-ventures/interchaintest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interchain.go
702 lines (587 loc) · 22.7 KB
/
interchain.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
package interchaintest
import (
"context"
"fmt"
"math"
"github.com/docker/docker/client"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
sdkmath "cosmossdk.io/math"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/strangelove-ventures/interchaintest/v8/testreporter"
)
// Interchain represents a full IBC network, encompassing a collection of
// one or more chains, one or more relayer instances, and initial account configuration.
type Interchain struct {
log *zap.Logger
// Map of chain reference to chain ID.
chains map[ibc.Chain]string
// Map of relayer reference to user-supplied instance name.
relayers map[ibc.Relayer]string
// Key: relayer and path name; Value: the two chains being linked.
links map[relayerPath]interchainLink
// Key: relayer and path name; Value: the provider and consumer chain link.
providerConsumerLinks map[relayerPath]providerConsumerLink
// Set to true after Build is called once.
built bool
// Map of relayer-chain pairs to address and mnemonic, set during Build().
// Not yet exposed through any exported API.
relayerWallets map[relayerChain]ibc.Wallet
// Map of chain to additional genesis wallets to include at chain start.
AdditionalGenesisWallets map[ibc.Chain][]ibc.WalletAmount
// Set during Build and cleaned up in the Close method.
cs *chainSet
}
type interchainLink struct {
chains [2]ibc.Chain
// If set, these options will be used when creating the client in the path link step.
// If a zero value initialization is used, e.g. CreateClientOptions{},
// then the default values will be used via ibc.DefaultClientOpts.
createClientOpts ibc.CreateClientOptions
// If set, these options will be used when creating the channel in the path link step.
// If a zero value initialization is used, e.g. CreateChannelOptions{},
// then the default values will be used via ibc.DefaultChannelOpts.
createChannelOpts ibc.CreateChannelOptions
}
type providerConsumerLink struct {
provider, consumer ibc.Chain
// If set, these options will be used when creating the client in the path link step.
// If a zero value initialization is used, e.g. CreateClientOptions{},
// then the default values will be used via ibc.DefaultClientOpts.
createClientOpts ibc.CreateClientOptions
// If set, these options will be used when creating the channel in the path link step.
// If a zero value initialization is used, e.g. CreateChannelOptions{},
// then the default values will be used via ibc.DefaultChannelOpts.
createChannelOpts ibc.CreateChannelOptions
}
// NewInterchain returns a new Interchain.
//
// Typical usage involves multiple calls to AddChain, one or more calls to AddRelayer,
// one or more calls to AddLink, and then finally a single call to Build.
func NewInterchain() *Interchain {
return &Interchain{
log: zap.NewNop(),
chains: make(map[ibc.Chain]string),
relayers: make(map[ibc.Relayer]string),
links: make(map[relayerPath]interchainLink),
providerConsumerLinks: make(map[relayerPath]providerConsumerLink),
}
}
// relayerPath is a tuple of a relayer and a path name.
type relayerPath struct {
Relayer ibc.Relayer
Path string
}
// AddChain adds the given chain to the Interchain,
// using the chain ID reported by the chain's config.
// If the given chain already exists,
// or if another chain with the same configured chain ID exists, AddChain panics.
func (ic *Interchain) AddChain(chain ibc.Chain, additionalGenesisWallets ...ibc.WalletAmount) *Interchain {
if chain == nil {
panic(fmt.Errorf("cannot add nil chain"))
}
newID := chain.Config().ChainID
newName := chain.Config().Name
for c, id := range ic.chains {
if c == chain {
panic(fmt.Errorf("chain %v was already added", c))
}
if id == newID {
panic(fmt.Errorf("a chain with ID %s already exists", id))
}
if c.Config().Name == newName {
panic(fmt.Errorf("a chain with name %s already exists", newName))
}
}
ic.chains[chain] = newID
if len(additionalGenesisWallets) == 0 {
return ic
}
if ic.AdditionalGenesisWallets == nil {
ic.AdditionalGenesisWallets = make(map[ibc.Chain][]ibc.WalletAmount)
}
ic.AdditionalGenesisWallets[chain] = additionalGenesisWallets
return ic
}
// AddRelayer adds the given relayer with the given name to the Interchain.
func (ic *Interchain) AddRelayer(relayer ibc.Relayer, name string) *Interchain {
if relayer == nil {
panic(fmt.Errorf("cannot add nil relayer"))
}
for r, n := range ic.relayers {
if r == relayer {
panic(fmt.Errorf("relayer %v was already added", r))
}
if n == name {
panic(fmt.Errorf("a relayer with name %s already exists", n))
}
}
ic.relayers[relayer] = name
return ic
}
// AddLink adds the given link to the Interchain.
// If any validation fails, AddLink panics.
func (ic *Interchain) AddProviderConsumerLink(link ProviderConsumerLink) *Interchain {
if _, exists := ic.chains[link.Provider]; !exists {
cfg := link.Provider.Config()
panic(fmt.Errorf("chain with name=%s and id=%s was never added to Interchain", cfg.Name, cfg.ChainID))
}
if _, exists := ic.chains[link.Consumer]; !exists {
cfg := link.Consumer.Config()
panic(fmt.Errorf("chain with name=%s and id=%s was never added to Interchain", cfg.Name, cfg.ChainID))
}
if _, exists := ic.relayers[link.Relayer]; !exists {
panic(fmt.Errorf("relayer %v was never added to Interchain", link.Relayer))
}
if link.Provider == link.Consumer {
panic(fmt.Errorf("chains must be different (both were %v)", link.Provider))
}
key := relayerPath{
Relayer: link.Relayer,
Path: link.Path,
}
if _, exists := ic.providerConsumerLinks[key]; exists {
panic(fmt.Errorf("relayer %q already has a path named %q", key.Relayer, key.Path))
}
ic.providerConsumerLinks[key] = providerConsumerLink{
provider: link.Provider,
consumer: link.Consumer,
createChannelOpts: link.CreateChannelOpts,
createClientOpts: link.CreateClientOpts,
}
return ic
}
// InterchainLink describes a link between two chains,
// by specifying the chain names, the relayer name,
// and the name of the path to create.
type InterchainLink struct {
// Chains involved.
Chain1, Chain2 ibc.Chain
// Relayer to use for link.
Relayer ibc.Relayer
// Name of path to create.
Path string
// If set, these options will be used when creating the client in the path link step.
// If a zero value initialization is used, e.g. CreateClientOptions{},
// then the default values will be used via ibc.DefaultClientOpts.
CreateClientOpts ibc.CreateClientOptions
// If set, these options will be used when creating the channel in the path link step.
// If a zero value initialization is used, e.g. CreateChannelOptions{},
// then the default values will be used via ibc.DefaultChannelOpts.
CreateChannelOpts ibc.CreateChannelOptions
}
type ProviderConsumerLink struct {
Provider, Consumer ibc.Chain
// Relayer to use for link.
Relayer ibc.Relayer
// Name of path to create.
Path string
// If set, these options will be used when creating the client in the path link step.
// If a zero value initialization is used, e.g. CreateClientOptions{},
// then the default values will be used via ibc.DefaultClientOpts.
CreateClientOpts ibc.CreateClientOptions
// If set, these options will be used when creating the channel in the path link step.
// If a zero value initialization is used, e.g. CreateChannelOptions{},
// then the default values will be used via ibc.DefaultChannelOpts.
CreateChannelOpts ibc.CreateChannelOptions
}
// AddLink adds the given link to the Interchain.
// If any validation fails, AddLink panics.
func (ic *Interchain) AddLink(link InterchainLink) *Interchain {
if _, exists := ic.chains[link.Chain1]; !exists {
cfg := link.Chain1.Config()
panic(fmt.Errorf("chain with name=%s and id=%s was never added to Interchain", cfg.Name, cfg.ChainID))
}
if _, exists := ic.chains[link.Chain2]; !exists {
cfg := link.Chain2.Config()
panic(fmt.Errorf("chain with name=%s and id=%s was never added to Interchain", cfg.Name, cfg.ChainID))
}
if _, exists := ic.relayers[link.Relayer]; !exists {
panic(fmt.Errorf("relayer %v was never added to Interchain", link.Relayer))
}
if link.Chain1 == link.Chain2 {
panic(fmt.Errorf("chains must be different (both were %v)", link.Chain1))
}
key := relayerPath{
Relayer: link.Relayer,
Path: link.Path,
}
if _, exists := ic.links[key]; exists {
panic(fmt.Errorf("relayer %q already has a path named %q", key.Relayer, key.Path))
}
ic.links[key] = interchainLink{
chains: [2]ibc.Chain{link.Chain1, link.Chain2},
createChannelOpts: link.CreateChannelOpts,
createClientOpts: link.CreateClientOpts,
}
return ic
}
// InterchainBuildOptions describes configuration for (*Interchain).Build.
type InterchainBuildOptions struct {
TestName string
Client *client.Client
NetworkID string
// If set, ic.Build does not create paths or links in the relayer,
// but it does still configure keys and wallets for declared relayer-chain links.
// This is useful for tests that need lower-level access to configuring relayers.
SkipPathCreation bool
// Optional. Git sha for test invocation. Once Go 1.18 supported,
// may be deprecated in favor of runtime/debug.ReadBuildInfo.
GitSha string
// If set, saves block history to a sqlite3 database to aid debugging.
BlockDatabaseFile string
}
// Build starts all the chains and configures the relayers associated with the Interchain.
// It is the caller's responsibility to directly call StartRelayer on the relayer implementations.
//
// Calling Build more than once will cause a panic.
func (ic *Interchain) Build(ctx context.Context, rep *testreporter.RelayerExecReporter, opts InterchainBuildOptions) error {
if ic.built {
panic(fmt.Errorf("Interchain.Build called more than once"))
}
ic.built = true
chains := make([]ibc.Chain, 0, len(ic.chains))
for chain := range ic.chains {
chains = append(chains, chain)
}
ic.cs = newChainSet(ic.log, chains)
// Consumer chains need to have the same number of validators as their provider.
// Consumer also needs reference to its provider chain.
for _, providerConsumerLink := range ic.providerConsumerLinks {
provider, consumer := providerConsumerLink.provider.(*cosmos.CosmosChain), providerConsumerLink.consumer.(*cosmos.CosmosChain)
consumer.NumValidators = provider.NumValidators
consumer.Provider = provider
provider.Consumers = append(provider.Consumers, consumer)
}
// Initialize the chains (pull docker images, etc.).
if err := ic.cs.Initialize(ctx, opts.TestName, opts.Client, opts.NetworkID); err != nil {
return fmt.Errorf("failed to initialize chains: %w", err)
}
ic.log.Info("Chains initialized")
err := ic.generateRelayerWallets(ctx) // Build the relayer wallet mapping.
if err != nil {
return err
}
ic.log.Info("Relayer wallets generated")
walletAmounts, err := ic.genesisWalletAmounts(ctx)
if err != nil {
// Error already wrapped with appropriate detail.
return err
}
ic.log.Info("Received genesis wallet amounts")
if err := ic.cs.Start(ctx, opts.TestName, walletAmounts); err != nil {
return fmt.Errorf("failed to start chains: %w", err)
}
if err := ic.cs.TrackBlocks(ctx, opts.TestName, opts.BlockDatabaseFile, opts.GitSha); err != nil {
return fmt.Errorf("failed to track blocks: %w", err)
}
// If any configured chain is an instance of Penumbra we need to initialize new pclientd instances for the
// newly created faucet account.
for c := range ic.chains {
err = CreatePenumbraClient(ctx, c, FaucetAccountKeyName)
if err != nil {
return err
}
}
if err := ic.configureRelayerKeys(ctx, rep); err != nil {
// Error already wrapped with appropriate detail.
return err
}
// Some tests may want to configure the relayer from a lower level,
// but still have wallets configured.
if opts.SkipPathCreation {
return nil
}
// For every relayer link, teach the relayer about the link and create the link.
for rp, link := range ic.links {
c0 := link.chains[0]
c1 := link.chains[1]
if err := rp.Relayer.GeneratePath(ctx, rep, c0.Config().ChainID, c1.Config().ChainID, rp.Path); err != nil {
return fmt.Errorf(
"failed to generate path %s on relayer %s between chains %s and %s: %w",
rp.Path, rp.Relayer, ic.chains[c0], ic.chains[c1], err,
)
}
}
// For every provider consumer link, teach the relayer about the link and create the link.
for rp, link := range ic.providerConsumerLinks {
p := link.provider
c := link.consumer
if err := rp.Relayer.GeneratePath(ctx, rep, c.Config().ChainID, p.Config().ChainID, rp.Path); err != nil {
return fmt.Errorf(
"failed to generate path %s on relayer %s between chains %s and %s: %w",
rp.Path, rp.Relayer, ic.chains[p], ic.chains[c], err,
)
}
}
var eg errgroup.Group
// Now link the paths in parallel
// Creates clients, connections, and channels for each link/path.
for rp, link := range ic.providerConsumerLinks {
p := link.provider
c := link.consumer
eg.Go(func() error {
// If the user specifies a zero value CreateClientOptions struct then we fall back to the default
// client options.
if link.createClientOpts == (ibc.CreateClientOptions{}) {
link.createClientOpts = ibc.DefaultClientOpts()
}
// Check that the client creation options are valid and fully specified.
if err := link.createClientOpts.Validate(); err != nil {
return err
}
// If the user specifies a zero value CreateChannelOptions struct then we fall back to the default
// channel options for an ics20 fungible token transfer channel.
if link.createChannelOpts == (ibc.CreateChannelOptions{}) {
link.createChannelOpts = ibc.DefaultChannelOpts()
}
// Check that the channel creation options are valid and fully specified.
if err := link.createChannelOpts.Validate(); err != nil {
return err
}
consumerClients, err := rp.Relayer.GetClients(ctx, rep, c.Config().ChainID)
if err != nil {
return fmt.Errorf(
"failed to fetch consumer clients while linking path %s on relayer %s between chains %s and %s: %w",
rp.Path, rp.Relayer, ic.chains[p], ic.chains[c], err,
)
}
var consumerClient *ibc.ClientOutput
for _, client := range consumerClients {
if client.ClientState.ChainID == p.Config().ChainID {
consumerClient = client
break
}
}
if consumerClient == nil {
return fmt.Errorf(
"consumer chain %s does not have a client tracking the provider chain %s for path %s on relayer %s",
ic.chains[c], ic.chains[p], rp.Path, rp.Relayer,
)
}
consumerClientID := consumerClients[0].ClientID
providerClients, err := rp.Relayer.GetClients(ctx, rep, p.Config().ChainID)
if err != nil {
return fmt.Errorf(
"failed to fetch provider clients while linking path %s on relayer %s between chains %s and %s: %w",
rp.Path, rp.Relayer, ic.chains[p], ic.chains[c], err,
)
}
var providerClient *ibc.ClientOutput
for _, client := range providerClients {
if client.ClientState.ChainID == c.Config().ChainID {
providerClient = client
break
}
}
if providerClient == nil {
return fmt.Errorf(
"provider chain %s does not have a client tracking the consumer chain %s for path %s on relayer %s",
ic.chains[p], ic.chains[c], rp.Path, rp.Relayer,
)
}
providerClientID := providerClients[0].ClientID
// Update relayer config with client IDs
if err := rp.Relayer.UpdatePath(ctx, rep, rp.Path, ibc.PathUpdateOptions{
SrcClientID: &consumerClientID,
DstClientID: &providerClientID,
}); err != nil {
return fmt.Errorf(
"failed to update path %s on relayer %s between chains %s and %s: %w",
rp.Path, rp.Relayer, ic.chains[p], ic.chains[c], err,
)
}
// Connection handshake
if err := rp.Relayer.CreateConnections(ctx, rep, rp.Path); err != nil {
return fmt.Errorf(
"failed to create connections on path %s on relayer %s between chains %s and %s: %w",
rp.Path, rp.Relayer, ic.chains[p], ic.chains[c], err,
)
}
// Create the provider/consumer channel for relaying val set updates
if err := rp.Relayer.CreateChannel(ctx, rep, rp.Path, ibc.CreateChannelOptions{
SourcePortName: "consumer",
DestPortName: "provider",
Order: ibc.Ordered,
Version: "1",
}); err != nil {
return fmt.Errorf(
"failed to create ccv channels on path %s on relayer %s between chains %s and %s: %w",
rp.Path, rp.Relayer, ic.chains[p], ic.chains[c], err,
)
}
return nil
})
}
// Now link the paths in parallel
// Creates clients, connections, and channels for each link/path.
for rp, link := range ic.links {
c0 := link.chains[0]
c1 := link.chains[1]
eg.Go(func() error {
// If the user specifies a zero value CreateClientOptions struct then we fall back to the default
// client options.
if link.createClientOpts == (ibc.CreateClientOptions{}) {
link.createClientOpts = ibc.DefaultClientOpts()
}
// Check that the client creation options are valid and fully specified.
if err := link.createClientOpts.Validate(); err != nil {
return err
}
// If the user specifies a zero value CreateChannelOptions struct then we fall back to the default
// channel options for an ics20 fungible token transfer channel.
if link.createChannelOpts == (ibc.CreateChannelOptions{}) {
link.createChannelOpts = ibc.DefaultChannelOpts()
}
// Check that the channel creation options are valid and fully specified.
if err := link.createChannelOpts.Validate(); err != nil {
return err
}
if err := rp.Relayer.LinkPath(ctx, rep, rp.Path, link.createChannelOpts, link.createClientOpts); err != nil {
return fmt.Errorf(
"failed to link path %s on relayer %s between chains %s and %s: %w",
rp.Path, rp.Relayer, ic.chains[c0], ic.chains[c1], err,
)
}
return nil
})
}
return eg.Wait()
}
// WithLog sets the logger on the interchain object.
// Usually the default nop logger is fine, but sometimes it can be helpful
// to see more verbose logs, typically by passing zaptest.NewLogger(t).
func (ic *Interchain) WithLog(log *zap.Logger) *Interchain {
ic.log = log
return ic
}
// Close cleans up any resources created during Build,
// and returns any relevant errors.
func (ic *Interchain) Close() error {
return ic.cs.Close()
}
func (ic *Interchain) genesisWalletAmounts(ctx context.Context) (map[ibc.Chain][]ibc.WalletAmount, error) {
// Faucet addresses are created separately because they need to be explicitly added to the chains.
ic.log.Info("Creating faucet accounts")
faucetAddresses, err := ic.cs.CreateCommonAccount(ctx, FaucetAccountKeyName)
if err != nil {
return nil, fmt.Errorf("failed to create faucet accounts: %w", err)
}
// Wallet amounts for genesis.
walletAmounts := make(map[ibc.Chain][]ibc.WalletAmount, len(ic.cs.chains))
// Add faucet for each chain first.
for c := range ic.chains {
decimalPow := int64(math.Pow10(int(*c.Config().CoinDecimals)))
// The values are nil at this point, so it is safe to directly assign the slice.
walletAmounts[c] = []ibc.WalletAmount{
{
Address: faucetAddresses[c],
Denom: c.Config().Denom,
Amount: sdkmath.NewInt(100_000_000).MulRaw(decimalPow), // Faucet wallet gets 100M units of scaled denom.
},
}
if ic.AdditionalGenesisWallets != nil {
walletAmounts[c] = append(walletAmounts[c], ic.AdditionalGenesisWallets[c]...)
}
}
// Then add all defined relayer wallets.
for rc, wallet := range ic.relayerWallets {
c := rc.C
decimalPow := int64(math.Pow10(int(*c.Config().CoinDecimals)))
walletAmounts[c] = append(walletAmounts[c], ibc.WalletAmount{
Address: wallet.FormattedAddress(),
Denom: c.Config().Denom,
Amount: sdkmath.NewInt(1_000_000).MulRaw(decimalPow), // Every wallet gets 1M units of scaled denom.
})
}
return walletAmounts, nil
}
// generateRelayerWallets populates ic.relayerWallets.
func (ic *Interchain) generateRelayerWallets(ctx context.Context) error {
if ic.relayerWallets != nil {
panic(fmt.Errorf("cannot call generateRelayerWallets more than once"))
}
relayerChains := ic.relayerChains()
ic.relayerWallets = make(map[relayerChain]ibc.Wallet, len(relayerChains))
for r, chains := range relayerChains {
for _, c := range chains {
// Just an ephemeral unique name, only for the local use of the keyring.
accountName := ic.relayers[r] + "-" + ic.chains[c]
newWallet, err := c.BuildRelayerWallet(ctx, accountName)
if err != nil {
return err
}
ic.relayerWallets[relayerChain{R: r, C: c}] = newWallet
}
}
return nil
}
// configureRelayerKeys adds the chain configuration for each relayer
// and adds the preconfigured key to the relayer for each relayer-chain.
func (ic *Interchain) configureRelayerKeys(ctx context.Context, rep *testreporter.RelayerExecReporter) error {
// Possible optimization: each relayer could be configured concurrently.
// But we are only testing with a single relayer so far, so we don't need this yet.
for r, chains := range ic.relayerChains() {
for _, c := range chains {
rpcAddr, grpcAddr := c.GetRPCAddress(), c.GetGRPCAddress()
if !r.UseDockerNetwork() {
rpcAddr, grpcAddr = c.GetHostRPCAddress(), c.GetHostGRPCAddress()
}
chainName := ic.chains[c]
if err := r.AddChainConfiguration(ctx,
rep,
c.Config(), chainName,
rpcAddr, grpcAddr,
); err != nil {
return fmt.Errorf("failed to configure relayer %s for chain %s: %w", ic.relayers[r], chainName, err)
}
if err := r.RestoreKey(ctx,
rep,
c.Config(), chainName,
ic.relayerWallets[relayerChain{R: r, C: c}].Mnemonic(),
); err != nil {
return fmt.Errorf("failed to restore key to relayer %s for chain %s: %w", ic.relayers[r], chainName, err)
}
}
}
return nil
}
// relayerChain is a tuple of a Relayer and a Chain.
type relayerChain struct {
R ibc.Relayer
C ibc.Chain
}
// relayerChains builds a mapping of relayers to the chains they connect to.
// The order of the chains is arbitrary.
func (ic *Interchain) relayerChains() map[ibc.Relayer][]ibc.Chain {
// First, collect a mapping of relayers to sets of chains,
// so we don't have to manually deduplicate entries.
uniq := make(map[ibc.Relayer]map[ibc.Chain]struct{}, len(ic.relayers))
for rp, link := range ic.links {
r := rp.Relayer
if uniq[r] == nil {
uniq[r] = make(map[ibc.Chain]struct{}, 2) // Adding at least 2 chains per relayer.
}
uniq[r][link.chains[0]] = struct{}{}
uniq[r][link.chains[1]] = struct{}{}
}
for rp, link := range ic.providerConsumerLinks {
r := rp.Relayer
if uniq[r] == nil {
uniq[r] = make(map[ibc.Chain]struct{}, 2) // Adding at least 2 chains per relayer.
}
uniq[r][link.provider] = struct{}{}
uniq[r][link.consumer] = struct{}{}
}
// Then convert the sets to slices.
out := make(map[ibc.Relayer][]ibc.Chain, len(uniq))
for r, chainSet := range uniq {
chains := make([]ibc.Chain, 0, len(chainSet))
for chain := range chainSet {
chains = append(chains, chain)
}
out[r] = chains
}
return out
}