/
chain.go
831 lines (718 loc) · 22.4 KB
/
chain.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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
package relayer
import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"path"
"strconv"
"strings"
"sync"
"time"
retry "github.com/avast/retry-go"
sdkCtx "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
tx "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/crypto/hd"
keys "github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authTypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/cosmos/go-bip39"
clienttypes "github.com/cosmos/ibc-go/v2/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v2/modules/core/03-connection/types"
ibcexported "github.com/cosmos/ibc-go/v2/modules/core/exported"
ibctmtypes "github.com/cosmos/ibc-go/v2/modules/light-clients/07-tendermint/types"
"github.com/gogo/protobuf/proto"
"github.com/tendermint/tendermint/libs/log"
provtypes "github.com/tendermint/tendermint/light/provider"
prov "github.com/tendermint/tendermint/light/provider/http"
rpcclient "github.com/tendermint/tendermint/rpc/client"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
libclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
"golang.org/x/sync/errgroup"
)
var (
rtyAttNum = uint(5)
rtyAtt = retry.Attempts(rtyAttNum)
rtyDel = retry.Delay(time.Millisecond * 400)
rtyErr = retry.LastErrorOnly(true)
)
// Chain represents the necessary data for connecting to and indentifying a chain and its counterparites
type Chain struct {
Key string `yaml:"key" json:"key"`
ChainID string `yaml:"chain-id" json:"chain-id"`
RPCAddr string `yaml:"rpc-addr" json:"rpc-addr"`
AccountPrefix string `yaml:"account-prefix" json:"account-prefix"`
GasAdjustment float64 `yaml:"gas-adjustment" json:"gas-adjustment"`
GasPrices string `yaml:"gas-prices" json:"gas-prices"`
TrustingPeriod string `yaml:"trusting-period" json:"trusting-period"`
// TODO: make these private
HomePath string `yaml:"-" json:"-"`
PathEnd *PathEnd `yaml:"-" json:"-"`
Keybase keys.Keyring `yaml:"-" json:"-"`
Client rpcclient.Client `yaml:"-" json:"-"`
Encoding params.EncodingConfig `yaml:"-" json:"-"`
Provider provtypes.Provider `yaml:"-" json:"-"`
address sdk.AccAddress
logger log.Logger
timeout time.Duration
debug bool
// stores faucet addresses that have been used reciently
faucetAddrs map[string]time.Time
}
// ValidatePaths takes two chains and validates their paths
func ValidatePaths(src, dst *Chain) error {
if err := src.PathEnd.ValidateFull(); err != nil {
return src.ErrCantSetPath(err)
}
if err := dst.PathEnd.ValidateFull(); err != nil {
return dst.ErrCantSetPath(err)
}
return nil
}
// ValidateClientPaths takes two chains and validates their clients
func ValidateClientPaths(src, dst *Chain) error {
if err := src.PathEnd.Vclient(); err != nil {
return err
}
if err := dst.PathEnd.Vclient(); err != nil {
return err
}
return nil
}
// ValidateConnectionPaths takes two chains and validates the connections
// and underlying client identifiers
func ValidateConnectionPaths(src, dst *Chain) error {
if err := src.PathEnd.Vclient(); err != nil {
return err
}
if err := dst.PathEnd.Vclient(); err != nil {
return err
}
if err := src.PathEnd.Vconn(); err != nil {
return err
}
if err := dst.PathEnd.Vconn(); err != nil {
return err
}
return nil
}
// ValidateChannelParams takes two chains and validates their respective channel params
func ValidateChannelParams(src, dst *Chain) error {
if err := src.PathEnd.ValidateBasic(); err != nil {
return err
}
if err := dst.PathEnd.ValidateBasic(); err != nil {
return err
}
//nolint:staticcheck
if strings.ToUpper(src.PathEnd.Order) != strings.ToUpper(dst.PathEnd.Order) {
return fmt.Errorf("src and dst path ends must have same ORDER. got src: %s, dst: %s",
src.PathEnd.Order, dst.PathEnd.Order)
}
return nil
}
// ListenRPCEmitJSON listens for tx and block events from a chain and outputs them as JSON to stdout
func (c *Chain) ListenRPCEmitJSON(tx, block, data bool) func() {
doneChan := make(chan struct{})
go c.listenLoop(doneChan, tx, block, data)
return func() { doneChan <- struct{}{} }
}
func (c *Chain) listenLoop(doneChan chan struct{}, tx, block, data bool) {
// Subscribe to source chain
if err := c.Start(); err != nil {
c.Error(err)
return
}
srcTxEvents, srcTxCancel, err := c.Subscribe(txEvents)
if err != nil {
c.Error(err)
return
}
defer srcTxCancel()
srcBlockEvents, srcBlockCancel, err := c.Subscribe(blEvents)
if err != nil {
c.Error(err)
return
}
defer srcBlockCancel()
// Listen to channels and take appropriate action
var byt []byte
var mar interface{}
for {
select {
case srcMsg := <-srcTxEvents:
switch {
case tx:
continue
case data:
mar = srcMsg
default:
mar = srcMsg.Events
}
if byt, err = json.Marshal(mar); err != nil {
c.Error(err)
}
fmt.Println(string(byt))
case srcMsg := <-srcBlockEvents:
switch {
case block:
continue
case data:
mar = srcMsg
default:
mar = srcMsg.Events
}
if byt, err = json.Marshal(mar); err != nil {
c.Error(err)
}
fmt.Println(string(byt))
case <-doneChan:
close(doneChan)
return
}
}
}
// Init initializes the pieces of a chain that aren't set when it parses a config
// NOTE: All validation of the chain should happen here.
func (c *Chain) Init(homePath string, timeout time.Duration, logger log.Logger, debug bool) error {
keybase, err := keys.New(c.ChainID, "test", keysDir(homePath, c.ChainID), nil)
if err != nil {
return err
}
client, err := newRPCClient(c.RPCAddr, timeout)
if err != nil {
return err
}
liteprovider, err := prov.New(c.ChainID, c.RPCAddr)
if err != nil {
return err
}
_, err = time.ParseDuration(c.TrustingPeriod)
if err != nil {
return fmt.Errorf("failed to parse trusting period (%s) for chain %s", c.TrustingPeriod, c.ChainID)
}
_, err = sdk.ParseDecCoins(c.GasPrices)
if err != nil {
return fmt.Errorf("failed to parse gas prices (%s) for chain %s", c.GasPrices, c.ChainID)
}
encodingConfig := c.MakeEncodingConfig()
c.Keybase = keybase
c.Client = client
c.HomePath = homePath
c.Encoding = encodingConfig
c.logger = logger
c.timeout = timeout
c.debug = debug
c.Provider = liteprovider
c.faucetAddrs = make(map[string]time.Time)
if c.logger == nil {
c.logger = defaultChainLogger()
}
return nil
}
func defaultChainLogger() log.Logger {
return log.NewTMLogger(log.NewSyncWriter(os.Stdout))
}
// KeyExists returns true if there is a specified key in chain's keybase
func (c *Chain) KeyExists(name string) bool {
k, err := c.Keybase.Key(name)
if err != nil {
return false
}
return k.GetName() == name
}
// GetSelfVersion returns the version of the given chain
func (c *Chain) GetSelfVersion() uint64 {
return clienttypes.ParseChainID(c.ChainID)
}
// GetTrustingPeriod returns the trusting period for the chain
func (c *Chain) GetTrustingPeriod() time.Duration {
tp, _ := time.ParseDuration(c.TrustingPeriod)
return tp
}
func newRPCClient(addr string, timeout time.Duration) (*rpchttp.HTTP, error) {
httpClient, err := libclient.DefaultHTTPClient(addr)
if err != nil {
return nil, err
}
httpClient.Timeout = timeout
rpcClient, err := rpchttp.NewWithClient(addr, "/websocket", httpClient)
if err != nil {
return nil, err
}
return rpcClient, nil
}
// SendMsg wraps the msg in a stdtx, signs and sends it
func (c *Chain) SendMsg(datagram sdk.Msg) (*sdk.TxResponse, bool, error) {
return c.SendMsgs([]sdk.Msg{datagram})
}
// SendMsgs wraps the msgs in a StdTx, signs and sends it. An error is returned if there
// was an issue sending the transaction. A successfully sent, but failed transaction will
// not return an error. If a transaction is successfully sent, the result of the execution
// of that transaction will be logged. A boolean indicating if a transaction was successfully
// sent and executed successfully is returned.
func (c *Chain) SendMsgs(msgs []sdk.Msg) (*sdk.TxResponse, bool, error) {
// Instantiate the client context
ctx := c.CLIContext(0)
// Query account details
txf, err := prepareFactory(ctx, c.TxFactory(0))
if err != nil {
return nil, false, err
}
// TODO: Make this work with new CalculateGas method
// https://github.com/cosmos/cosmos-sdk/blob/5725659684fc93790a63981c653feee33ecf3225/client/tx/tx.go#L297
// If users pass gas adjustment, then calculate gas
_, adjusted, err := CalculateGas(ctx.QueryWithData, txf, msgs...)
if err != nil {
return nil, false, err
}
// Set the gas amount on the transaction factory
txf = txf.WithGas(adjusted)
// Build the transaction builder
txb, err := tx.BuildUnsignedTx(txf, msgs...)
if err != nil {
return nil, false, err
}
// Attach the signature to the transaction
// c.LogFailedTx(nil, err, msgs)
// Force encoding in the chain specific address
for _, msg := range msgs {
c.Encoding.Marshaler.MustMarshalJSON(msg)
}
err = tx.Sign(txf, c.Key, txb, false)
if err != nil {
return nil, false, err
}
// Generate the transaction bytes
txBytes, err := ctx.TxConfig.TxEncoder()(txb.GetTx())
if err != nil {
return nil, false, err
}
// Broadcast those bytes
res, err := ctx.BroadcastTx(txBytes)
if err != nil {
return nil, false, err
}
// transaction was executed, log the success or failure using the tx response code
// NOTE: error is nil, logic should use the returned error to determine if the
// transaction was successfully executed.
if res.Code != 0 {
c.LogFailedTx(res, err, msgs)
return res, false, nil
}
c.LogSuccessTx(res, msgs)
return res, true, nil
}
func prepareFactory(clientCtx sdkCtx.Context, txf tx.Factory) (tx.Factory, error) {
from := clientCtx.GetFromAddress()
if err := txf.AccountRetriever().EnsureExists(clientCtx, from); err != nil {
return txf, err
}
initNum, initSeq := txf.AccountNumber(), txf.Sequence()
if initNum == 0 || initSeq == 0 {
num, seq, err := txf.AccountRetriever().GetAccountNumberSequence(clientCtx, from)
if err != nil {
return txf, err
}
if initNum == 0 {
txf = txf.WithAccountNumber(num)
}
if initSeq == 0 {
txf = txf.WithSequence(seq)
}
}
return txf, nil
}
// protoTxProvider is a type which can provide a proto transaction. It is a
// workaround to get access to the wrapper TxBuilder's method GetProtoTx().
type protoTxProvider interface {
GetProtoTx() *txtypes.Tx
}
// BuildSimTx creates an unsigned tx with an empty single signature and returns
// the encoded transaction or an error if the unsigned transaction cannot be
// built.
func BuildSimTx(txf tx.Factory, msgs ...sdk.Msg) ([]byte, error) {
txb, err := tx.BuildUnsignedTx(txf, msgs...)
if err != nil {
return nil, err
}
// Create an empty signature literal as the ante handler will populate with a
// sentinel pubkey.
sig := signing.SignatureV2{
PubKey: &secp256k1.PubKey{},
Data: &signing.SingleSignatureData{
SignMode: txf.SignMode(),
},
Sequence: txf.Sequence(),
}
if err := txb.SetSignatures(sig); err != nil {
return nil, err
}
protoProvider, ok := txb.(protoTxProvider)
if !ok {
return nil, fmt.Errorf("cannot simulate amino tx")
}
simReq := txtypes.SimulateRequest{Tx: protoProvider.GetProtoTx()}
return simReq.Marshal()
}
// CalculateGas simulates the execution of a transaction and returns the
// simulation response obtained by the query and the adjusted gas amount.
func CalculateGas(
queryFunc func(string, []byte) ([]byte, int64, error), txf tx.Factory, msgs ...sdk.Msg,
) (txtypes.SimulateResponse, uint64, error) {
txBytes, err := BuildSimTx(txf, msgs...)
if err != nil {
return txtypes.SimulateResponse{}, 0, err
}
bz, _, err := queryFunc("/cosmos.tx.v1beta1.Service/Simulate", txBytes)
if err != nil {
return txtypes.SimulateResponse{}, 0, err
}
var simRes txtypes.SimulateResponse
if err := simRes.Unmarshal(bz); err != nil {
return txtypes.SimulateResponse{}, 0, err
}
return simRes, uint64(txf.GasAdjustment() * float64(simRes.GasInfo.GasUsed)), nil
}
// CLIContext returns an instance of client.Context derived from Chain
func (c *Chain) CLIContext(height int64) sdkCtx.Context {
addr, err := c.GetAddress()
if err != nil {
panic(err)
}
return sdkCtx.Context{}.
WithChainID(c.ChainID).
WithCodec(c.Encoding.Marshaler).
WithInterfaceRegistry(c.Encoding.InterfaceRegistry).
WithTxConfig(c.Encoding.TxConfig).
WithLegacyAmino(c.Encoding.Amino).
WithInput(os.Stdin).
WithNodeURI(c.RPCAddr).
WithClient(c.Client).
WithAccountRetriever(authTypes.AccountRetriever{}).
WithBroadcastMode(flags.BroadcastBlock).
WithKeyring(c.Keybase).
WithOutputFormat("json").
WithFrom(c.Key).
WithFromName(c.Key).
WithFromAddress(addr).
WithSkipConfirmation(true).
WithNodeURI(c.RPCAddr).
WithHeight(height)
}
// TxFactory returns an instance of tx.Factory derived from
func (c *Chain) TxFactory(height int64) tx.Factory {
ctx := c.CLIContext(height)
return tx.Factory{}.
WithAccountRetriever(ctx.AccountRetriever).
WithChainID(c.ChainID).
WithTxConfig(ctx.TxConfig).
WithGasAdjustment(c.GasAdjustment).
WithGasPrices(c.GasPrices).
WithKeybase(c.Keybase).
WithSignMode(signing.SignMode_SIGN_MODE_DIRECT)
}
// Log takes a string and logs the data
func (c *Chain) Log(s string) {
c.logger.Info(s)
}
// Error takes an error, wraps it in the chainID and logs the error
func (c *Chain) Error(err error) {
c.logger.Error(fmt.Sprintf("%s: err(%s)", c.ChainID, err.Error()))
}
// Start the client service
func (c *Chain) Start() error {
return c.Client.Start()
}
// Subscribe returns channel of events given a query
func (c *Chain) Subscribe(query string) (<-chan ctypes.ResultEvent, context.CancelFunc, error) {
suffix, err := GenerateRandomString(8)
if err != nil {
return nil, nil, err
}
subscriber := fmt.Sprintf("%s-subscriber-%s", c.ChainID, suffix)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
eventChan, err := c.Client.Subscribe(ctx, subscriber, query, 1000)
return eventChan, cancel, err
}
// KeysDir returns the path to the keys for this chain
func keysDir(home, chainID string) string {
return path.Join(home, "keys", chainID)
}
// GetAddress returns the sdk.AccAddress associated with the configured key
func (c *Chain) GetAddress() (sdk.AccAddress, error) {
done := c.UseSDKContext()
defer done()
if c.address != nil {
return c.address, nil
}
// Signing key for c chain
srcAddr, err := c.Keybase.Key(c.Key)
if err != nil {
return nil, err
}
return srcAddr.GetAddress(), nil
}
// MustGetAddress used for brevity
func (c *Chain) MustGetAddress() string {
srcAddr, err := c.GetAddress()
if err != nil {
panic(err)
}
done := c.UseSDKContext()
defer done()
return srcAddr.String()
}
var sdkContextMutex sync.Mutex
// UseSDKContext uses a custom Bech32 account prefix and returns a restore func
// CONTRACT: When using this function, caller must ensure that lock contention
// doesn't cause program to hang. This function is only for use in codec calls
func (c *Chain) UseSDKContext() func() {
// Ensure we're the only one using the global context,
// lock context to begin function
sdkContextMutex.Lock()
// Mutate the sdkConf
sdkConf := sdk.GetConfig()
sdkConf.SetBech32PrefixForAccount(c.AccountPrefix, c.AccountPrefix+"pub")
sdkConf.SetBech32PrefixForValidator(c.AccountPrefix+"valoper", c.AccountPrefix+"valoperpub")
sdkConf.SetBech32PrefixForConsensusNode(c.AccountPrefix+"valcons", c.AccountPrefix+"valconspub")
// Return the unlock function, caller must lock and ensure that lock is released
// before any other function needs to use c.UseSDKContext
return sdkContextMutex.Unlock
}
func (c *Chain) String() string {
out, _ := json.Marshal(c)
return string(out)
}
// Update returns a new chain with updated values
func (c *Chain) Update(key, value string) (out *Chain, err error) {
out = c
switch key {
case "key":
out.Key = value
case "chain-id":
out.ChainID = value
case "rpc-addr":
if _, err = rpchttp.New(value, "/websocket"); err != nil {
return
}
out.RPCAddr = value
case "gas-adjustment":
adj, err := strconv.ParseFloat(value, 64)
if err != nil {
return nil, err
}
out.GasAdjustment = adj
case "gas-prices":
_, err = sdk.ParseDecCoins(value)
if err != nil {
return nil, err
}
out.GasPrices = value
case "account-prefix":
out.AccountPrefix = value
case "trusting-period":
if _, err = time.ParseDuration(value); err != nil {
return
}
out.TrustingPeriod = value
default:
return out, fmt.Errorf("key %s not found", key)
}
return out, err
}
// Print fmt.Printlns the json or yaml representation of whatever is passed in
// CONTRACT: The cmd calling this function needs to have the "json" and "indent" flags set
// TODO: better "text" printing here would be a nice to have
// TODO: fix indenting all over the code base
func (c *Chain) Print(toPrint proto.Message, text, indent bool) error {
var (
out []byte
err error
)
switch {
case indent && text:
return fmt.Errorf("must pass either indent or text, not both")
case text:
// TODO: This isn't really a good option,
out = []byte(fmt.Sprintf("%v", toPrint))
default:
out, err = c.Encoding.Marshaler.MarshalJSON(toPrint)
}
if err != nil {
return err
}
fmt.Println(string(out))
return nil
}
// SendAndPrint sends a transaction and prints according to the passed args
func (c *Chain) SendAndPrint(txs []sdk.Msg, text, indent bool) (err error) {
if c.debug {
for _, msg := range txs {
if err = c.Print(msg, text, indent); err != nil {
return err
}
}
}
// SendAndPrint sends the transaction with printing options from the CLI
res, _, err := c.SendMsgs(txs)
if err != nil {
return err
}
return c.Print(res, text, indent)
}
// Chains is a collection of Chain
type Chains []*Chain
// Get returns the configuration for a given chain
func (c Chains) Get(chainID string) (*Chain, error) {
for _, chain := range c {
if chainID == chain.ChainID {
addr, _ := chain.GetAddress()
chain.address = addr
return chain, nil
}
}
return &Chain{}, fmt.Errorf("chain with ID %s is not configured", chainID)
}
// MustGet returns the chain and panics on any error
func (c Chains) MustGet(chainID string) *Chain {
out, err := c.Get(chainID)
if err != nil {
panic(err)
}
return out
}
// Gets returns a map chainIDs to their chains
func (c Chains) Gets(chainIDs ...string) (map[string]*Chain, error) {
out := make(map[string]*Chain)
for _, cid := range chainIDs {
chain, err := c.Get(cid)
if err != nil {
return out, err
}
out[cid] = chain
}
return out, nil
}
// GetRPCPort returns the port configured for the chain
func (c *Chain) GetRPCPort() string {
u, _ := url.Parse(c.RPCAddr)
return u.Port()
}
// CreateTestKey creates a key for test chain
func (c *Chain) CreateTestKey() error {
if c.KeyExists(c.Key) {
return fmt.Errorf("key %s exists for chain %s", c.ChainID, c.Key)
}
mnemonic, err := CreateMnemonic()
if err != nil {
return err
}
_, err = c.Keybase.NewAccount(c.Key, mnemonic, "", hd.CreateHDPath(118, 0, 0).String(), hd.Secp256k1)
return err
}
// CreateMnemonic creates a new mnemonic
func CreateMnemonic() (string, error) {
entropySeed, err := bip39.NewEntropy(256)
if err != nil {
return "", err
}
mnemonic, err := bip39.NewMnemonic(entropySeed)
if err != nil {
return "", err
}
return mnemonic, nil
}
// GetTimeout returns the chain's configured timeout
func (c *Chain) GetTimeout() time.Duration {
return c.timeout
}
// StatusErr returns err unless the chain is ready to go
func (c *Chain) StatusErr() error {
stat, err := c.Client.Status(context.Background())
switch {
case err != nil:
return err
case stat.SyncInfo.LatestBlockHeight < 3:
return fmt.Errorf("haven't produced any blocks yet")
default:
return nil
}
}
// GenerateConnHandshakeProof generates all the proofs needed to prove the existence of the
// connection state on this chain. A counterparty should use these generated proofs.
func (c *Chain) GenerateConnHandshakeProof(height uint64) (clientState ibcexported.ClientState,
clientStateProof []byte, consensusProof []byte, connectionProof []byte,
connectionProofHeight clienttypes.Height, err error) {
var (
clientStateRes *clienttypes.QueryClientStateResponse
consensusStateRes *clienttypes.QueryConsensusStateResponse
connectionStateRes *connectiontypes.QueryConnectionResponse
eg = new(errgroup.Group)
)
// query for the client state for the proof and get the height to query the consensus state at.
clientStateRes, err = c.QueryClientStateResponse(int64(height))
if err != nil {
return nil, nil, nil, nil, clienttypes.Height{}, err
}
clientState, err = clienttypes.UnpackClientState(clientStateRes.ClientState)
if err != nil {
return nil, nil, nil, nil, clienttypes.Height{}, err
}
eg.Go(func() error {
consensusStateRes, err = c.QueryClientConsensusState(int64(height), clientState.GetLatestHeight())
return err
})
eg.Go(func() error {
connectionStateRes, err = c.QueryConnection(int64(height))
return err
})
if err := eg.Wait(); err != nil {
return nil, nil, nil, nil, clienttypes.Height{}, err
}
return clientState, clientStateRes.Proof, consensusStateRes.Proof, connectionStateRes.Proof,
connectionStateRes.ProofHeight, nil
}
// UpgradeChain submits and upgrade proposal using a zero'd out client state with an updated unbonding period.
func (c *Chain) UpgradeChain(dst *Chain, plan *upgradetypes.Plan, deposit sdk.Coin,
unbondingPeriod time.Duration) error {
height, err := dst.QueryLatestHeight()
if err != nil {
return err
}
clientState, err := dst.QueryClientState(height)
if err != nil {
return err
}
upgradedClientState := clientState.ZeroCustomFields().(*ibctmtypes.ClientState)
upgradedClientState.LatestHeight.RevisionHeight = uint64(plan.Height + 1)
upgradedClientState.UnbondingPeriod = unbondingPeriod
// TODO: make cli args for title and description
upgradeProposal, err := clienttypes.NewUpgradeProposal("upgrade",
"upgrade the chain's software and unbonding period", *plan, upgradedClientState)
if err != nil {
return err
}
addr, err := c.GetAddress()
if err != nil {
return err
}
msg, err := govtypes.NewMsgSubmitProposal(upgradeProposal, sdk.NewCoins(deposit), addr)
if err != nil {
return err
}
_, _, err = c.SendMsg(msg)
if err != nil {
return err
}
return nil
}