forked from cosmos/interchain-security
-
Notifications
You must be signed in to change notification settings - Fork 1
/
actions.go
3403 lines (2953 loc) · 106 KB
/
actions.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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"math"
"os"
"os/exec"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
"github.com/tidwall/gjson"
"golang.org/x/mod/semver"
sdk "github.com/cosmos/cosmos-sdk/types"
e2e "github.com/cosmos/interchain-security/v6/tests/e2e/testlib"
"github.com/cosmos/interchain-security/v6/x/ccv/provider/client"
"github.com/cosmos/interchain-security/v6/x/ccv/provider/types"
ccvtypes "github.com/cosmos/interchain-security/v6/x/ccv/types"
)
const (
done = "done!!!!!!!!"
VLatest = "latest"
V400 = "v4.0.0"
V330 = "v3.3.0"
V300 = "v3.0.0"
)
// type aliases
type (
AssignConsumerPubKeyAction = e2e.AssignConsumerPubKeyAction
)
type SendTokensAction struct {
Chain ChainID
From ValidatorID
To ValidatorID
Amount uint
}
type TxResponse struct {
TxHash string `json:"txhash"`
Code int `json:"code"`
RawLog string `json:"raw_log"`
Events []sdk.Event `json:"events"`
}
func (tr Chain) sendTokens(
action SendTokensAction,
verbose bool,
) {
fromValCfg := tr.testConfig.validatorConfigs[action.From]
toValCfg := tr.testConfig.validatorConfigs[action.To]
fromAddress := fromValCfg.DelAddress
toAddress := toValCfg.DelAddress
if action.Chain != ChainID("provi") {
// use binary with Bech32Prefix set to ConsumerAccountPrefix
if fromValCfg.UseConsumerKey {
fromAddress = fromValCfg.ConsumerDelAddress
} else {
// use the same address as on the provider but with different prefix
fromAddress = fromValCfg.DelAddressOnConsumer
}
if toValCfg.UseConsumerKey {
toAddress = toValCfg.ConsumerDelAddress
} else {
// use the same address as on the provider but with different prefix
toAddress = toValCfg.DelAddressOnConsumer
}
}
binaryName := tr.testConfig.chainConfigs[action.Chain].BinaryName
cmd := tr.target.ExecCommand(binaryName,
"tx", "bank", "send",
fromAddress,
toAddress,
fmt.Sprint(action.Amount)+`stake`,
`--chain-id`, string(tr.testConfig.chainConfigs[action.Chain].ChainId),
`--home`, tr.getValidatorHome(action.Chain, action.From),
`--node`, tr.getValidatorNode(action.Chain, action.From),
`--keyring-backend`, `test`,
`-y`,
)
if verbose {
fmt.Println("sendTokens cmd:", cmd.String())
}
bz, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err, "\n", string(bz))
}
// wait for inclusion in a block -> '--broadcast-mode block' is deprecated
tr.waitBlocks(action.Chain, 2, 30*time.Second)
}
type StartChainAction struct {
Chain ChainID
Validators []StartChainValidator
// Genesis changes specific to this action, appended to genesis changes defined in chain config
GenesisChanges string
IsConsumer bool
}
type StartChainValidator struct {
Id ValidatorID
Allocation uint
Stake uint
}
func (tr *Chain) startChain(
action StartChainAction,
verbose bool,
) {
chainConfig := tr.testConfig.chainConfigs[action.Chain]
type jsonValAttrs struct {
Mnemonic string `json:"mnemonic"`
Allocation string `json:"allocation"`
Stake string `json:"stake"`
ValId string `json:"val_id"`
PrivValidatorKey string `json:"priv_validator_key"`
NodeKey string `json:"node_key"`
IpSuffix string `json:"ip_suffix"`
ConsumerMnemonic string `json:"consumer_mnemonic"`
ConsumerPrivValidatorKey string `json:"consumer_priv_validator_key"`
StartWithConsumerKey bool `json:"start_with_consumer_key"`
}
var validators []jsonValAttrs
for _, val := range action.Validators {
validators = append(validators, jsonValAttrs{
Mnemonic: tr.testConfig.validatorConfigs[val.Id].Mnemonic,
NodeKey: tr.testConfig.validatorConfigs[val.Id].NodeKey,
ValId: fmt.Sprint(val.Id),
PrivValidatorKey: tr.testConfig.validatorConfigs[val.Id].PrivValidatorKey,
Allocation: fmt.Sprint(val.Allocation) + "stake",
Stake: fmt.Sprint(val.Stake) + "stake",
IpSuffix: tr.testConfig.validatorConfigs[val.Id].IpSuffix,
ConsumerMnemonic: tr.testConfig.validatorConfigs[val.Id].ConsumerMnemonic,
ConsumerPrivValidatorKey: tr.testConfig.validatorConfigs[val.Id].ConsumerPrivValidatorKey,
// if true node will be started with consumer key for each consumer chain
StartWithConsumerKey: tr.testConfig.validatorConfigs[val.Id].UseConsumerKey,
})
}
vals, err := json.Marshal(validators)
if err != nil {
log.Fatal(err)
}
// Concat genesis changes defined in chain config, with any custom genesis changes for this chain instantiation
var genesisChanges string
if action.GenesisChanges != "" {
genesisChanges = chainConfig.GenesisChanges + " | " + action.GenesisChanges
} else {
genesisChanges = chainConfig.GenesisChanges
}
var cometmockArg string
if tr.testConfig.useCometmock {
cometmockArg = "true"
} else {
cometmockArg = "false"
}
chainHome := string(action.Chain)
startChainScript := tr.target.GetTestScriptPath(action.IsConsumer, "start-chain.sh")
cmd := tr.target.ExecCommand("/bin/bash",
startChainScript, chainConfig.BinaryName, string(vals),
string(chainConfig.ChainId), chainConfig.IpPrefix, genesisChanges,
fmt.Sprint(action.IsConsumer),
// override config/config.toml for each node on chain
// usually timeout_commit and peer_gossip_sleep_duration are changed to vary the test run duration
// lower timeout_commit means the blocks are produced faster making the test run shorter
// with short timeout_commit (eg. timeout_commit = 1s) some nodes may miss blocks causing the test run to fail
tr.testConfig.tendermintConfigOverride,
cometmockArg,
chainHome,
)
cmdReader, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
cmd.Stderr = cmd.Stdout
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(cmdReader)
for scanner.Scan() {
out := scanner.Text()
if verbose {
fmt.Println("startChain: " + out)
}
if out == done {
break
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
tr.addChainToRelayer(AddChainToRelayerAction{
Chain: action.Chain,
Validator: action.Validators[0].Id,
IsConsumer: action.IsConsumer,
}, verbose)
// store the fact that we started the chain
tr.testConfig.runningChains[action.Chain] = true
fmt.Println("Started chain", action.Chain)
if tr.testConfig.timeOffset != 0 {
// advance time for this chain so that it is in sync with the rest of the network
tr.AdvanceTimeForChain(action.Chain, tr.testConfig.timeOffset)
}
}
type SubmitTextProposalAction struct {
Chain ChainID
From ValidatorID
Deposit uint
Title string
Description string
}
func (tr Chain) submitTextProposal(
action SubmitTextProposalAction,
verbose bool,
) {
// TEXT PROPOSAL
bz, err := tr.target.ExecCommand(
tr.testConfig.chainConfigs[action.Chain].BinaryName,
"tx", "gov", "submit-legacy-proposal",
`--title`, action.Title,
`--description`, action.Description,
`--deposit`, fmt.Sprint(action.Deposit)+`stake`,
`--from`, `validator`+fmt.Sprint(action.From),
`--chain-id`, string(tr.testConfig.chainConfigs[action.Chain].ChainId),
`--home`, tr.getValidatorHome(action.Chain, action.From),
`--node`, tr.getValidatorNode(action.Chain, action.From),
`--keyring-backend`, `test`,
`-y`,
).CombinedOutput()
if err != nil {
log.Fatal(err, "\n", string(bz))
}
// wait for inclusion in a block -> '--broadcast-mode block' is deprecated
tr.waitBlocks(action.Chain, 1, 10*time.Second)
}
type UpdateConsumerChainAction struct {
Chain ChainID
From ValidatorID
ConsumerChain ChainID
NewOwner string
InitParams *InitializationParameters
PowerShapingParams *PowerShapingParameters
}
type InitializationParameters struct {
InitialHeight clienttypes.Height
SpawnTime uint
DistributionChannel string
}
type PowerShapingParameters struct {
TopN uint32
ValidatorsPowerCap uint32
ValidatorSetCap uint32
Allowlist []string
Denylist []string
MinStake uint64
AllowInactiveVals bool
}
func (tr Chain) updateConsumerChain(action UpdateConsumerChainAction, verbose bool) {
var initParams *types.ConsumerInitializationParameters
if action.InitParams != nil {
spawnTime := tr.testConfig.containerConfig.Now.Add(time.Duration(action.InitParams.SpawnTime) * time.Millisecond)
params := ccvtypes.DefaultParams()
initParams = &types.ConsumerInitializationParameters{
InitialHeight: action.InitParams.InitialHeight,
GenesisHash: []byte("gen_hash"),
BinaryHash: []byte("bin_hash"),
SpawnTime: spawnTime,
UnbondingPeriod: params.UnbondingPeriod,
CcvTimeoutPeriod: params.CcvTimeoutPeriod,
TransferTimeoutPeriod: params.TransferTimeoutPeriod,
ConsumerRedistributionFraction: params.ConsumerRedistributionFraction,
BlocksPerDistributionTransmission: params.BlocksPerDistributionTransmission,
HistoricalEntries: params.HistoricalEntries,
DistributionTransmissionChannel: action.InitParams.DistributionChannel,
}
}
powerShapingParams := types.PowerShapingParameters{
Top_N: action.PowerShapingParams.TopN,
ValidatorsPowerCap: action.PowerShapingParams.ValidatorsPowerCap,
ValidatorSetCap: action.PowerShapingParams.ValidatorSetCap,
Allowlist: action.PowerShapingParams.Allowlist,
Denylist: action.PowerShapingParams.Denylist,
MinStake: action.PowerShapingParams.MinStake,
AllowInactiveVals: action.PowerShapingParams.AllowInactiveVals,
}
consumerId := tr.testConfig.chainConfigs[action.ConsumerChain].ConsumerId
msg := types.MsgUpdateConsumer{
ConsumerId: string(consumerId),
NewOwnerAddress: action.NewOwner,
InitializationParameters: initParams,
PowerShapingParameters: &powerShapingParams,
}
tr.UpdateConsumer(action.Chain, action.From, msg, verbose)
}
type CreateConsumerChainAction struct {
Chain ChainID
From ValidatorID
ConsumerChain ChainID
InitParams *InitializationParameters
PowerShapingParams *PowerShapingParameters
}
// createConsumerChain creates and initializes a consumer chain
func (tr Chain) createConsumerChain(action CreateConsumerChainAction, verbose bool) {
consumerChainCfg := tr.testConfig.chainConfigs[action.ConsumerChain]
providerChainCfg := tr.testConfig.chainConfigs[action.Chain]
if consumerChainCfg.ConsumerId != "" {
log.Fatalf("consumer chain already created for '%s'", action.ConsumerChain)
}
var initParams *types.ConsumerInitializationParameters
if action.InitParams != nil {
spawnTime := tr.testConfig.containerConfig.Now.Add(time.Duration(action.InitParams.SpawnTime) * time.Millisecond)
params := ccvtypes.DefaultParams()
initParams = &types.ConsumerInitializationParameters{
InitialHeight: action.InitParams.InitialHeight,
GenesisHash: []byte("gen_hash"),
BinaryHash: []byte("bin_hash"),
SpawnTime: spawnTime,
UnbondingPeriod: params.UnbondingPeriod,
CcvTimeoutPeriod: params.CcvTimeoutPeriod,
TransferTimeoutPeriod: params.TransferTimeoutPeriod,
ConsumerRedistributionFraction: params.ConsumerRedistributionFraction,
BlocksPerDistributionTransmission: params.BlocksPerDistributionTransmission,
HistoricalEntries: params.HistoricalEntries,
DistributionTransmissionChannel: action.InitParams.DistributionChannel,
}
}
powerShapingParams := &types.PowerShapingParameters{
Top_N: action.PowerShapingParams.TopN,
ValidatorsPowerCap: action.PowerShapingParams.ValidatorsPowerCap,
ValidatorSetCap: action.PowerShapingParams.ValidatorSetCap,
Allowlist: action.PowerShapingParams.Allowlist,
Denylist: action.PowerShapingParams.Denylist,
MinStake: action.PowerShapingParams.MinStake,
AllowInactiveVals: action.PowerShapingParams.AllowInactiveVals,
}
metadata := types.ConsumerMetadata{
Name: "chain name of " + string(consumerChainCfg.ChainId),
Description: "no description",
Metadata: "no metadata",
}
// create consumer to get a consumer-id
consumerId := tr.CreateConsumer(providerChainCfg.ChainId, consumerChainCfg.ChainId, action.From, metadata, initParams, powerShapingParams)
if verbose {
fmt.Println("Created consumer chain", string(consumerChainCfg.ChainId), " with consumer-id", string(consumerId))
}
// Set the new created consumer-id on the chain's config
consumerChainCfg.ConsumerId = consumerId
tr.testConfig.chainConfigs[action.ConsumerChain] = consumerChainCfg
}
type RemoveConsumerChainAction struct {
Chain ChainID
From ValidatorID
ConsumerChain ChainID
}
func (tr Chain) removeConsumerChain(action RemoveConsumerChainAction, verbose bool) {
consumerId := tr.testConfig.chainConfigs[action.ConsumerChain].ConsumerId
if consumerId == "" {
log.Fatal("failed removing consumer chain. no consumer-id found for chain: ",
action.ConsumerChain)
}
// Send consumer chain removal
cmd := tr.target.ExecCommand(
tr.testConfig.chainConfigs[action.Chain].BinaryName,
"tx", "provider", "remove-consumer", string(consumerId),
`--from`, `validator`+fmt.Sprint(action.From),
`--chain-id`, string(tr.testConfig.chainConfigs[action.Chain].ChainId),
`--home`, tr.getValidatorHome(action.Chain, action.From),
`--gas`, `900000`,
`--node`, tr.getValidatorNode(action.Chain, action.From),
`--keyring-backend`, `test`,
"--output", "json",
`-y`,
)
bz, err := cmd.CombinedOutput()
if err != nil {
log.Println("command failed:", cmd)
log.Fatalf("remove consumer failed error: %s, output: %s", err.Error(), string(bz))
}
// Check transaction
txResponse := &TxResponse{}
err = json.Unmarshal(bz, txResponse)
if err != nil {
log.Fatalf("unmarshalling tx response on remove-consumer: %s, json: %s", err.Error(), string(bz))
}
if txResponse.Code != 0 {
log.Fatalf("sending remove-consumer transaction failed with error code %d, Log:'%s'", txResponse.Code, txResponse.RawLog)
}
if verbose {
fmt.Println("running 'remove-consumer' returned: ", txResponse)
}
tr.waitBlocks(action.Chain, 2, 10*time.Second)
}
type SubmitConsumerAdditionProposalAction struct {
PreCCV bool
Chain ChainID
From ValidatorID
Deposit uint
ConsumerChain ChainID
SpawnTime uint
InitialHeight clienttypes.Height
DistributionChannel string
TopN uint32
ValidatorsPowerCap uint32
ValidatorSetCap uint32
Allowlist []string
Denylist []string
MinStake uint64
AllowInactiveVals bool
}
func (tr Chain) UpdateConsumer(providerChain ChainID, validator ValidatorID, update types.MsgUpdateConsumer, verbose bool) {
content, err := json.Marshal(update)
if err != nil {
log.Fatal("failed marshalling MsgUpdateConsumer: ", err.Error())
}
jsonFile := "/update-consumer.json"
bz, err := tr.target.ExecCommand(
"/bin/bash", "-c", fmt.Sprintf(`echo '%s' > %s`, content, jsonFile),
).CombinedOutput()
if err != nil {
log.Fatal(err, "\n", string(bz))
}
// Send consumer chain update
cmd := tr.target.ExecCommand(
tr.testConfig.chainConfigs[providerChain].BinaryName,
"tx", "provider", "update-consumer", jsonFile,
`--from`, `validator`+fmt.Sprint(validator),
`--chain-id`, string(tr.testConfig.chainConfigs[providerChain].ChainId),
`--home`, tr.getValidatorHome(providerChain, validator),
`--gas`, `900000`,
`--node`, tr.getValidatorNode(providerChain, validator),
`--keyring-backend`, `test`,
"--output", "json",
`-y`,
)
bz, err = cmd.CombinedOutput()
if err != nil {
fmt.Println("command failed: ", cmd)
log.Fatalf("update consumer failed error: %s, output: %s", err, string(bz))
}
// Check transaction
txResponse := &TxResponse{}
err = json.Unmarshal(bz, txResponse)
if err != nil {
log.Fatalf("unmarshalling tx response on update-consumer: %s, json: %s", err.Error(), string(bz))
}
if txResponse.Code != 0 {
log.Fatalf("sending update-consumer transaction failed with error code %d, Log:'%s'", txResponse.Code, txResponse.RawLog)
}
if verbose {
fmt.Println("running 'update-consumer' returned: ", txResponse)
}
tr.waitBlocks(providerChain, 2, 10*time.Second)
}
// CreateConsumer creates a consumer chain and returns its consumer-id
func (tr Chain) CreateConsumer(providerChain, consumerChain ChainID, validator ValidatorID, metadata types.ConsumerMetadata, initParams *types.ConsumerInitializationParameters, powerShapingParams *types.PowerShapingParameters) ConsumerID {
msg := types.MsgCreateConsumer{
ChainId: string(consumerChain),
Metadata: metadata,
InitializationParameters: initParams,
PowerShapingParameters: powerShapingParams,
}
content, err := json.Marshal(msg)
if err != nil {
log.Fatalf("failed marshalling MsgCreateConsumer: %s", err.Error())
}
jsonFile := "/create-consumer.json"
bz, err := tr.target.ExecCommand(
"/bin/bash", "-c", fmt.Sprintf(`echo '%s' > %s`, content, jsonFile),
).CombinedOutput()
if err != nil {
log.Fatal(err, "\n", string(bz))
}
// Send consumer chain creation
cmd := tr.target.ExecCommand(
tr.testConfig.chainConfigs[providerChain].BinaryName,
"tx", "provider", "create-consumer", jsonFile,
`--from`, `validator`+fmt.Sprint(validator),
`--chain-id`, string(tr.testConfig.chainConfigs[providerChain].ChainId),
`--home`, tr.getValidatorHome(providerChain, validator),
`--gas`, `900000`,
`--node`, tr.getValidatorNode(providerChain, validator),
`--keyring-backend`, `test`,
"--output", "json",
`-y`,
)
bz, err = cmd.CombinedOutput()
if err != nil {
log.Fatal("create consumer failed ", "error: ", err, "output: ", string(bz))
}
txResponse := &TxResponse{}
err = json.Unmarshal(bz, txResponse)
if err != nil {
log.Fatalf("unmarshalling tx response on create-consumer: %s, json: %s", err.Error(), string(bz))
}
if txResponse.Code != 0 {
log.Fatalf("sending transaction failed with error code %d, Log:'%s'", txResponse.Code, txResponse.RawLog)
}
// TODO: introduce waitForTx (see issue #2198)
tr.waitBlocks(providerChain, 2, 10*time.Second)
// Get Consumer ID from transaction
cmd = tr.target.ExecCommand(
tr.testConfig.chainConfigs[providerChain].BinaryName,
"query", "tx", txResponse.TxHash,
`--node`, tr.getValidatorNode(providerChain, validator),
"--output", "json",
)
bz, err = cmd.CombinedOutput()
if err != nil {
log.Fatalf("not able to query tx containing creation-consumer: tx: %s, err: %s, out: %s",
txResponse.TxHash, err.Error(), string(bz))
}
err = json.Unmarshal(bz, txResponse)
if err != nil {
log.Fatalf("unmarshalling tx containing create-consumer: %s, json: %s", err.Error(), string(bz))
}
consumerId := ""
for _, event := range txResponse.Events {
if event.Type != "create_consumer" {
continue
}
attr, exists := event.GetAttribute("consumer_id")
if !exists {
log.Fatalf("no event with consumer_id found in tx content of create_consumer: %v", event)
}
consumerId = attr.Value
}
if consumerId == "" {
log.Fatalf("no consumer-id found in consumer creation transaction events for chain '%s'. events: %v", consumerChain, txResponse.Events)
}
return ConsumerID(consumerId)
}
// submitConsumerAdditionProposal initializes a consumer chain and submits a governance proposal
func (tr Chain) submitConsumerAdditionProposal(
action SubmitConsumerAdditionProposalAction,
verbose bool,
) {
params := ccvtypes.DefaultParams()
spawnTime := tr.testConfig.containerConfig.Now.Add(time.Duration(action.SpawnTime) * time.Millisecond)
consumerChainCfg := tr.testConfig.chainConfigs[action.ConsumerChain]
providerChainCfg := tr.testConfig.chainConfigs[action.Chain]
Metadata := types.ConsumerMetadata{
Name: "chain " + string(action.Chain),
Description: "no description",
Metadata: "no metadata",
}
initializationParameters := types.ConsumerInitializationParameters{
InitialHeight: action.InitialHeight,
GenesisHash: []byte("gen_hash"),
BinaryHash: []byte("bin_hash"),
SpawnTime: spawnTime,
UnbondingPeriod: params.UnbondingPeriod,
CcvTimeoutPeriod: params.CcvTimeoutPeriod,
TransferTimeoutPeriod: params.TransferTimeoutPeriod,
ConsumerRedistributionFraction: params.ConsumerRedistributionFraction,
BlocksPerDistributionTransmission: params.BlocksPerDistributionTransmission,
HistoricalEntries: params.HistoricalEntries,
DistributionTransmissionChannel: action.DistributionChannel,
}
consumerId := tr.CreateConsumer(providerChainCfg.ChainId, consumerChainCfg.ChainId, action.From, Metadata, nil, nil)
authority := "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn"
// Set the new created consumer-id on the chain's config
consumerChainCfg.ConsumerId = consumerId
tr.testConfig.chainConfigs[action.ConsumerChain] = consumerChainCfg
// Update consumer to change owner to governance before submitting the proposal
update := &types.MsgUpdateConsumer{
ConsumerId: string(consumerId),
NewOwnerAddress: authority,
}
// For the MsgUpdateConsumer sent in the proposal
powerShapingParameters := types.PowerShapingParameters{
Top_N: 0,
ValidatorsPowerCap: action.ValidatorsPowerCap,
ValidatorSetCap: action.ValidatorSetCap,
Allowlist: action.Allowlist,
Denylist: action.Denylist,
MinStake: action.MinStake,
AllowInactiveVals: action.AllowInactiveVals,
}
update.PowerShapingParameters = &powerShapingParameters
tr.UpdateConsumer(action.Chain, action.From, *update, verbose)
// - set PowerShaping params TopN > 0 for consumer chain
update.PowerShapingParameters.Top_N = action.TopN
update.Owner = authority
update.NewOwnerAddress = ""
update.InitializationParameters = &initializationParameters
update.InitializationParameters.SpawnTime = spawnTime
update.Metadata = &Metadata
// Generate proposal content
title := "Propose the addition of a new chain"
description := "description of the consumer modification proposal"
summary := "Gonna be a great chain"
expedited := false
metadata := "ipfs://CID"
deposit := fmt.Sprintf("%dstake", action.Deposit)
jsonStr := e2e.GenerateGovProposalContent(title, summary, metadata, deposit, description, expedited, update)
// #nosec G204 -- bypass unsafe quoting warning (no production code)
proposalFile := "/update-consumer-proposal.json"
bz, err := tr.target.ExecCommand(
"/bin/bash", "-c", fmt.Sprintf(`echo '%s' > %s`, jsonStr, proposalFile),
).CombinedOutput()
if err != nil {
log.Fatal(err, "\n", string(bz))
}
// CONSUMER ADDITION PROPOSAL
cmd := tr.target.ExecCommand(
tr.testConfig.chainConfigs[action.Chain].BinaryName,
"tx", "gov", "submit-proposal", proposalFile,
`--from`, `validator`+fmt.Sprint(action.From),
`--chain-id`, string(providerChainCfg.ChainId),
`--home`, tr.getValidatorHome(providerChainCfg.ChainId, action.From),
`--gas`, `900000`,
`--node`, tr.getValidatorNode(providerChainCfg.ChainId, action.From),
`--keyring-backend`, `test`,
`-o json`,
`-y`,
)
if verbose {
fmt.Println("submitConsumerAdditionProposal cmd:", cmd.String())
fmt.Println("submitConsumerAdditionProposal json:", jsonStr)
}
bz, err = cmd.CombinedOutput()
if err != nil {
log.Fatal("executing submit-proposal failed:", err, "\n", string(bz))
}
txResponse := &TxResponse{}
err = json.Unmarshal(bz, txResponse)
if err != nil {
log.Fatalf("failed unmarshalling tx response on submit consumer update: %s, json: %s", err.Error(), string(bz))
}
if txResponse.Code != 0 {
log.Fatalf("gov submit consumer update transaction failed with error code %d, Log:'%s'", txResponse.Code, txResponse.RawLog)
}
if verbose {
fmt.Println("submitConsumerAdditionProposal output:", string(bz))
}
// wait for inclusion in a block -> '--broadcast-mode block' is deprecated
tr.waitBlocks(providerChainCfg.ChainId, 2, 10*time.Second)
}
func (tr Chain) submitConsumerAdditionLegacyProposal(
action SubmitConsumerAdditionProposalAction,
verbose bool,
) {
spawnTime := tr.testConfig.containerConfig.Now.Add(time.Duration(action.SpawnTime) * time.Millisecond)
params := ccvtypes.DefaultParams()
prop := client.ConsumerAdditionProposalJSON{
Title: "Propose the addition of a new chain",
Summary: "Gonna be a great chain",
ChainId: string(tr.testConfig.chainConfigs[action.ConsumerChain].ChainId),
InitialHeight: action.InitialHeight,
GenesisHash: []byte("gen_hash"),
BinaryHash: []byte("bin_hash"),
SpawnTime: spawnTime,
ConsumerRedistributionFraction: params.ConsumerRedistributionFraction,
BlocksPerDistributionTransmission: params.BlocksPerDistributionTransmission,
HistoricalEntries: params.HistoricalEntries,
CcvTimeoutPeriod: params.CcvTimeoutPeriod,
TransferTimeoutPeriod: params.TransferTimeoutPeriod,
UnbondingPeriod: params.UnbondingPeriod,
Deposit: fmt.Sprint(action.Deposit) + `stake`,
DistributionTransmissionChannel: action.DistributionChannel,
TopN: action.TopN,
ValidatorsPowerCap: action.ValidatorsPowerCap,
ValidatorSetCap: action.ValidatorSetCap,
Allowlist: action.Allowlist,
Denylist: action.Denylist,
MinStake: action.MinStake,
AllowInactiveVals: action.AllowInactiveVals,
}
bz, err := json.Marshal(prop)
if err != nil {
log.Fatal(err)
}
jsonStr := string(bz)
if strings.Contains(jsonStr, "'") {
log.Fatal("prop json contains single quote")
}
//#nosec G204 -- bypass unsafe quoting warning (no production code)
cmd := tr.target.ExecCommand(
"/bin/bash", "-c", fmt.Sprintf(`echo '%s' > %s`, jsonStr, "/temp-proposal.json"))
bz, err = cmd.CombinedOutput()
if verbose {
log.Println("submitConsumerAdditionProposal cmd: ", cmd.String())
}
if err != nil {
log.Fatal(err, "\n", string(bz))
}
// CONSUMER ADDITION PROPOSAL
cmd = tr.target.ExecCommand(
tr.testConfig.chainConfigs[action.Chain].BinaryName,
"tx", "gov", "submit-legacy-proposal", "consumer-addition", "/temp-proposal.json",
`--from`, `validator`+fmt.Sprint(action.From),
`--chain-id`, string(tr.testConfig.chainConfigs[action.Chain].ChainId),
`--home`, tr.getValidatorHome(action.Chain, action.From),
`--gas`, `900000`,
`--node`, tr.getValidatorNode(action.Chain, action.From),
`--keyring-backend`, `test`,
`-y`,
)
if verbose {
fmt.Println("submitConsumerAdditionProposal cmd:", cmd.String())
fmt.Println("submitConsumerAdditionProposal json:", jsonStr)
}
bz, err = cmd.CombinedOutput()
if err != nil {
log.Fatal(err, "\n", string(bz))
}
if verbose {
fmt.Println("submitConsumerAdditionProposal output:", string(bz))
}
// wait for inclusion in a block -> '--broadcast-mode block' is deprecated
tr.waitBlocks(ChainID("provi"), 2, 10*time.Second)
}
type SubmitConsumerRemovalProposalAction struct {
Chain ChainID
From ValidatorID
Deposit uint
ConsumerChain ChainID
}
func (tr Chain) submitConsumerRemovalProposal(
action SubmitConsumerRemovalProposalAction,
verbose bool,
) {
consumerId := string(tr.testConfig.chainConfigs[action.ConsumerChain].ConsumerId)
title := fmt.Sprintf("Stop the %v chain", action.ConsumerChain)
description := "stop consumer chain"
summary := "It was a great chain"
expedited := false
metadata := "ipfs://CID"
deposit := fmt.Sprintf("%dstake", action.Deposit)
authority := "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn"
msg := types.MsgRemoveConsumer{
ConsumerId: consumerId,
Owner: authority,
}
jsonStr := e2e.GenerateGovProposalContent(title, summary, metadata, deposit, description, expedited, &msg)
// #nosec G204 -- bypass unsafe quoting warning (no production code)
proposalFile := "/remove-consumer-proposal.json"
bz, err := tr.target.ExecCommand(
"/bin/bash", "-c", fmt.Sprintf(`echo '%s' > %s`, jsonStr, proposalFile),
).CombinedOutput()
if err != nil {
log.Fatal(err, "\n", string(bz))
}
// CONSUMER REMOVAL PROPOSAL
cmd := tr.target.ExecCommand(
tr.testConfig.chainConfigs[action.Chain].BinaryName,
"tx", "gov", "submit-proposal", proposalFile,
`--from`, `validator`+fmt.Sprint(action.From),
`--chain-id`, string(tr.testConfig.chainConfigs[action.Chain].ChainId),
`--home`, tr.getValidatorHome(action.Chain, action.From),
`--gas`, `900000`,
`--node`, tr.getValidatorNode(action.Chain, action.From),
`--keyring-backend`, `test`,
`-y`,
)
if verbose {
fmt.Println("submitConsumerRemovalProposal cmd:", cmd.String())
fmt.Println("submitConsumerRemovalProposal json:", jsonStr)
}
bz, err = cmd.CombinedOutput()
if err != nil {
log.Fatal("submit consumer removal proposal failed:", err, "\n", string(bz))
}
if verbose {
fmt.Println("submitConsumerRemovalProposal output:", string(bz))
}
// wait for inclusion in a block -> '--broadcast-mode block' is deprecated
tr.waitBlocks(ChainID("provi"), 2, 20*time.Second)
}
func (tr Chain) submitConsumerRemovalLegacyProposal(
action SubmitConsumerRemovalProposalAction,
verbose bool,
) {
prop := client.ConsumerRemovalProposalJSON{
Title: fmt.Sprintf("Stop the %v chain", action.ConsumerChain),
Summary: "It was a great chain",
ChainId: string(tr.testConfig.chainConfigs[action.ConsumerChain].ChainId),
Deposit: fmt.Sprint(action.Deposit) + `stake`,
}
bz, err := json.Marshal(prop)
if err != nil {
log.Fatal(err)
}
jsonStr := string(bz)
if strings.Contains(jsonStr, "'") {
log.Fatal("prop json contains single quote")
}
bz, err = tr.target.ExecCommand(
"/bin/bash", "-c", fmt.Sprintf(`echo '%s' > %s`, jsonStr, "/temp-proposal.json")).CombinedOutput()
if err != nil {
log.Fatal(err, "\n", string(bz))
}
bz, err = tr.target.ExecCommand(
tr.testConfig.chainConfigs[action.Chain].BinaryName,
"tx", "gov", "submit-legacy-proposal", "consumer-removal",
"/temp-proposal.json",
`--from`, `validator`+fmt.Sprint(action.From),
`--chain-id`, string(tr.testConfig.chainConfigs[action.Chain].ChainId),
`--home`, tr.getValidatorHome(action.Chain, action.From),
`--node`, tr.getValidatorNode(action.Chain, action.From),
`--gas`, "900000",
`--keyring-backend`, `test`,
`-y`,
).CombinedOutput()
if err != nil {
log.Fatal(err, "\n", string(bz))
}
// wait for inclusion in a block -> '--broadcast-mode block' is deprecated
tr.waitBlocks(ChainID("provi"), 2, 20*time.Second)
}
type SubmitConsumerModificationProposalAction struct {
Chain ChainID
From ValidatorID
Deposit uint
ConsumerChain ChainID
TopN uint32
ValidatorsPowerCap uint32
ValidatorSetCap uint32
Allowlist []string
Denylist []string
AllowInactiveVals bool
MinStake uint64
NewOwner string
}
func (tr Chain) submitConsumerModificationProposal(
action SubmitConsumerModificationProposalAction,
verbose bool,
) {
consumerId := string(tr.testConfig.chainConfigs[action.ConsumerChain].ConsumerId)
title := "Propose the modification of the PSS parameters of a chain"
description := "description of the consumer modification proposal"
summary := "summary of a modification proposal"
expedited := false
metadata := "ipfs://CID"
deposit := fmt.Sprintf("%dstake", action.Deposit)
authority := "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn"
msg := types.MsgUpdateConsumer{
Owner: authority,
ConsumerId: consumerId,
NewOwnerAddress: action.NewOwner,
PowerShapingParameters: &types.PowerShapingParameters{
Top_N: action.TopN,
ValidatorsPowerCap: action.ValidatorsPowerCap,
ValidatorSetCap: action.ValidatorSetCap,
Allowlist: action.Allowlist,
Denylist: action.Denylist,
},
}
jsonStr := e2e.GenerateGovProposalContent(title, summary, metadata, deposit, description, expedited, &msg)
// #nosec G204 -- bypass unsafe quoting warning (no production code)
proposalFile := "/update-consumer-proposal.json"
bz, err := tr.target.ExecCommand(
"/bin/bash", "-c", fmt.Sprintf(`echo '%s' > %s`, jsonStr, proposalFile),
).CombinedOutput()
if err != nil {
log.Fatal(err, "\n", string(bz))
}
// CONSUMER MODIFICATION PROPOSAL
cmd := tr.target.ExecCommand(
tr.testConfig.chainConfigs[action.Chain].BinaryName,
"tx", "gov", "submit-proposal", proposalFile,
`--from`, `validator`+fmt.Sprint(action.From),
`--chain-id`, string(tr.testConfig.chainConfigs[action.Chain].ChainId),
`--home`, tr.getValidatorHome(action.Chain, action.From),
`--gas`, `900000`,
`--node`, tr.getValidatorNode(action.Chain, action.From),
`--keyring-backend`, `test`,
`-y`,
)
if verbose {
fmt.Println("submitConsumerModificationProposal cmd:", cmd.String())
fmt.Println("submitConsumerModificationProposal json:", jsonStr)
}
bz, err = cmd.CombinedOutput()
if err != nil {
log.Fatal("submit consumer modification proposal failed:", err, "\n", string(bz))
}
if verbose {