-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathkey_assignment_test.go
974 lines (852 loc) · 36.4 KB
/
key_assignment_test.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
package keeper_test
import (
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
cryptotestutil "github.com/cosmos/interchain-security/testutil/crypto"
testkeeper "github.com/cosmos/interchain-security/testutil/keeper"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
tmprotocrypto "github.com/tendermint/tendermint/proto/tendermint/crypto"
"math/rand"
providerkeeper "github.com/cosmos/interchain-security/x/ccv/provider/keeper"
"github.com/cosmos/interchain-security/x/ccv/utils"
"github.com/golang/mock/gomock"
)
type testAssignment struct {
chainID string
providerAddr sdk.ConsAddress
consumerAddr sdk.ConsAddress
consumerPubKey tmprotocrypto.PublicKey
pubKeyAndPower abci.ValidatorUpdate
vscID uint64
}
func TestValidatorConsumerPubKeyCRUD(t *testing.T) {
chainID := "consumer"
providerAddr := sdk.ConsAddress([]byte("providerAddr"))
consumerKey := cryptotestutil.NewCryptoIdentityFromIntSeed(1).TMProtoCryptoPublicKey()
keeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()
keeper.SetValidatorConsumerPubKey(ctx, chainID, providerAddr, consumerKey)
consumerPubKey, found := keeper.GetValidatorConsumerPubKey(ctx, chainID, providerAddr)
require.True(t, found, "consumer pubkey not found")
require.NotEmpty(t, consumerPubKey, "consumer pubkey is empty")
require.Equal(t, consumerPubKey, consumerKey)
keeper.DeleteValidatorConsumerPubKey(ctx, chainID, providerAddr)
consumerPubKey, found = keeper.GetValidatorConsumerPubKey(ctx, chainID, providerAddr)
require.False(t, found, "consumer pubkey was found")
require.Empty(t, consumerPubKey, "consumer pubkey was returned")
require.NotEqual(t, consumerPubKey, consumerKey)
}
func TestIterateValidatorConsumerPubKeys(t *testing.T) {
chainID := "consumer"
testAssignments := []testAssignment{
{
providerAddr: sdk.ConsAddress([]byte("validator-1")),
consumerPubKey: cryptotestutil.NewCryptoIdentityFromIntSeed(1).TMProtoCryptoPublicKey(),
},
{
providerAddr: sdk.ConsAddress([]byte("validator-2")),
consumerPubKey: cryptotestutil.NewCryptoIdentityFromIntSeed(2).TMProtoCryptoPublicKey(),
},
{
providerAddr: sdk.ConsAddress([]byte("validator-3")),
consumerPubKey: cryptotestutil.NewCryptoIdentityFromIntSeed(3).TMProtoCryptoPublicKey(),
},
}
keeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()
for _, assignment := range testAssignments {
keeper.SetValidatorConsumerPubKey(ctx, chainID, assignment.providerAddr, assignment.consumerPubKey)
}
result := []testAssignment{}
cbIterateAll := func(iteratorProviderAddr sdk.ConsAddress, consumerKey tmprotocrypto.PublicKey) (stop bool) {
result = append(result, testAssignment{
providerAddr: iteratorProviderAddr,
consumerPubKey: consumerKey,
})
return false
}
keeper.IterateValidatorConsumerPubKeys(ctx, chainID, cbIterateAll)
require.Len(t, result, len(testAssignments), "incorrect result len - should be %d, got %d", len(testAssignments), len(result))
for i, res := range result {
require.Equal(t, testAssignments[i], res, "mismatched consumer key assignment in case %d", i)
}
result = []testAssignment{}
cbIterateOne := func(iteratorProviderAddr sdk.ConsAddress, consumerKey tmprotocrypto.PublicKey) (stop bool) {
result = append(result, testAssignment{
providerAddr: iteratorProviderAddr,
consumerPubKey: consumerKey,
})
return true
}
keeper.IterateValidatorConsumerPubKeys(ctx, chainID, cbIterateOne)
require.Len(t, result, 1, "incorrect result len - should be 1, got %d", len(result))
require.Equal(t, testAssignments[0], result[0], "mismatched consumer key assignment in iterate one")
}
func TestIterateAllValidatorConsumerPubKeys(t *testing.T) {
providerAddr := sdk.ConsAddress([]byte("validator-1"))
testAssignments := []testAssignment{
{
chainID: "consumer-1",
providerAddr: providerAddr,
consumerPubKey: cryptotestutil.NewCryptoIdentityFromIntSeed(1).TMProtoCryptoPublicKey(),
},
{
chainID: "consumer-2",
providerAddr: providerAddr,
consumerPubKey: cryptotestutil.NewCryptoIdentityFromIntSeed(2).TMProtoCryptoPublicKey(),
},
{
chainID: "consumer-3",
providerAddr: providerAddr,
consumerPubKey: cryptotestutil.NewCryptoIdentityFromIntSeed(3).TMProtoCryptoPublicKey(),
},
}
keeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()
for _, assignment := range testAssignments {
keeper.SetValidatorConsumerPubKey(ctx, assignment.chainID, assignment.providerAddr, assignment.consumerPubKey)
}
result := []testAssignment{}
cbIterateAll := func(chainID string, iteratorProviderAddr sdk.ConsAddress, consumerKey tmprotocrypto.PublicKey) (stop bool) {
require.Equal(t, providerAddr, iteratorProviderAddr, "unexpected provider address in iterator - expecting just 1 provider address")
result = append(result, testAssignment{
chainID: chainID,
providerAddr: iteratorProviderAddr,
consumerPubKey: consumerKey,
})
return false
}
keeper.IterateAllValidatorConsumerPubKeys(ctx, cbIterateAll)
require.Len(t, result, len(testAssignments), "incorrect result len - should be %d, got %d", len(testAssignments), len(result))
for i, res := range result {
require.Equal(t, testAssignments[i], res, "mismatched consumer key assignment in case %d", i)
}
result = []testAssignment{}
cbIterateOne := func(chainID string, iteratorProviderAddr sdk.ConsAddress, consumerKey tmprotocrypto.PublicKey) (stop bool) {
require.Equal(t, providerAddr, iteratorProviderAddr, "unexpected provider address in iterator - expecting just 1 provider address")
result = append(result, testAssignment{
chainID: chainID,
providerAddr: iteratorProviderAddr,
consumerPubKey: consumerKey,
})
return false
}
keeper.IterateAllValidatorConsumerPubKeys(ctx, cbIterateOne)
require.Len(t, result, len(testAssignments), "incorrect result len - should be 1, got %d", len(result))
require.Equal(t, testAssignments[0], result[0], "mismatched consumer key assignment in iterate one")
}
func TestValidatorByConsumerAddrCRUD(t *testing.T) {
chainID := "consumer"
providerAddr := sdk.ConsAddress([]byte("providerAddr"))
consumerAddr := sdk.ConsAddress([]byte("consumerAddr"))
keeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()
keeper.SetValidatorByConsumerAddr(ctx, chainID, consumerAddr, providerAddr)
providerAddrResult, found := keeper.GetValidatorByConsumerAddr(ctx, chainID, consumerAddr)
require.True(t, found, "provider address not found")
require.NotEmpty(t, providerAddrResult, "provider address is empty")
require.Equal(t, providerAddr, providerAddrResult)
keeper.DeleteValidatorByConsumerAddr(ctx, chainID, consumerAddr)
providerAddrResult, found = keeper.GetValidatorByConsumerAddr(ctx, chainID, consumerAddr)
require.False(t, found, "provider address was found")
require.Empty(t, providerAddrResult, "provider address not empty")
require.NotEqual(t, providerAddr, providerAddrResult)
}
func TestIterateValidatorsByConsumerAddr(t *testing.T) {
chainID := "consumer"
testAssignments := []testAssignment{
{
providerAddr: sdk.ConsAddress([]byte("validator-1")),
consumerAddr: sdk.ConsAddress([]byte("consumer-1")),
},
{
providerAddr: sdk.ConsAddress([]byte("validator-2")),
consumerAddr: sdk.ConsAddress([]byte("consumer-2")),
},
{
providerAddr: sdk.ConsAddress([]byte("validator-3")),
consumerAddr: sdk.ConsAddress([]byte("consumer-3")),
},
}
keeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()
for _, assignment := range testAssignments {
keeper.SetValidatorByConsumerAddr(ctx, chainID, assignment.consumerAddr, assignment.providerAddr)
}
result := []testAssignment{}
cbIterateAll := func(consumerAddr sdk.ConsAddress, providerAddr sdk.ConsAddress) (stop bool) {
result = append(result, testAssignment{
providerAddr: providerAddr,
consumerAddr: consumerAddr,
})
return false // continue iteration
}
keeper.IterateValidatorsByConsumerAddr(ctx, chainID, cbIterateAll)
require.Len(t, result, len(testAssignments), "incorrect result len - should be %d, got %d", len(testAssignments), len(result))
for i, res := range result {
require.Equal(t, testAssignments[i], res, "mismatched consumer address assignment in case %d", i)
}
result = []testAssignment{}
cbIterateOne := func(consumerAddr sdk.ConsAddress, providerAddr sdk.ConsAddress) (stop bool) {
result = append(result, testAssignment{
providerAddr: providerAddr,
consumerAddr: consumerAddr,
})
return true // stop after first
}
keeper.IterateValidatorsByConsumerAddr(ctx, chainID, cbIterateOne)
require.Len(t, result, 1, "incorrect result len - should be 1, got %d", len(result))
require.Equal(t, testAssignments[0], result[0], "mismatched consumer address assignment in iterate one")
}
func TestIterateAllValidatorsByConsumerAddr(t *testing.T) {
providerAddr := sdk.ConsAddress([]byte("validator-1"))
testAssignments := []testAssignment{
{
chainID: "consumer-1",
providerAddr: providerAddr,
consumerAddr: sdk.ConsAddress([]byte("consumer-1")),
},
{
chainID: "consumer-2",
providerAddr: providerAddr,
consumerAddr: sdk.ConsAddress([]byte("consumer-2")),
},
{
chainID: "consumer-3",
providerAddr: providerAddr,
consumerAddr: sdk.ConsAddress([]byte("consumer-3")),
},
}
keeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()
for _, assignment := range testAssignments {
keeper.SetValidatorByConsumerAddr(ctx, assignment.chainID, assignment.consumerAddr, assignment.providerAddr)
}
result := []testAssignment{}
cbIterateAll := func(chainID string, consumerAddr sdk.ConsAddress, providerAddr sdk.ConsAddress) (stop bool) {
result = append(result, testAssignment{
chainID: chainID,
providerAddr: providerAddr,
consumerAddr: consumerAddr,
})
return false // continue iteration
}
keeper.IterateAllValidatorsByConsumerAddr(ctx, cbIterateAll)
require.Len(t, result, len(testAssignments), "incorrect result len - should be %d, got %d", len(testAssignments), len(result))
for i, res := range result {
require.Equal(t, testAssignments[i], res, "mismatched consumer address assignment in case %d", i)
}
result = []testAssignment{}
cbIterateOne := func(chainID string, consumerAddr sdk.ConsAddress, providerAddr sdk.ConsAddress) (stop bool) {
result = append(result, testAssignment{
chainID: chainID,
providerAddr: providerAddr,
consumerAddr: consumerAddr,
})
return true // stop after first
}
keeper.IterateAllValidatorsByConsumerAddr(ctx, cbIterateOne)
require.Len(t, result, 1, "incorrect result len - should be 1, got %d", len(result))
require.Equal(t, testAssignments[0], result[0], "mismatched consumer address assignment in iterate one")
}
func TestKeyAssignmentReplacementCRUD(t *testing.T) {
chainID := "consumer"
providerAddr := sdk.ConsAddress([]byte("providerAddr"))
expCPubKey := cryptotestutil.NewCryptoIdentityFromIntSeed(1).TMProtoCryptoPublicKey()
var expPower int64 = 100
keeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()
keeper.SetKeyAssignmentReplacement(ctx, chainID, providerAddr, expCPubKey, expPower)
cPubKey, power, found := keeper.GetKeyAssignmentReplacement(ctx, chainID, providerAddr)
require.True(t, found, "key assignment replacement not found")
require.Equal(t, expCPubKey, cPubKey, "previous consumer key not matching")
require.Equal(t, expPower, power, "power not matching")
keeper.DeleteKeyAssignmentReplacement(ctx, chainID, providerAddr)
_, _, found = keeper.GetKeyAssignmentReplacement(ctx, chainID, providerAddr)
require.False(t, found, "key assignment replacement found")
}
func TestIterateKeyAssignmentReplacements(t *testing.T) {
chainID := "consumer"
testAssignments := []testAssignment{
{
providerAddr: sdk.ConsAddress([]byte("validator-1")),
pubKeyAndPower: abci.ValidatorUpdate{
Power: 100,
PubKey: cryptotestutil.NewCryptoIdentityFromIntSeed(1).TMProtoCryptoPublicKey(),
},
},
{
providerAddr: sdk.ConsAddress([]byte("validator-2")),
pubKeyAndPower: abci.ValidatorUpdate{
Power: 100,
PubKey: cryptotestutil.NewCryptoIdentityFromIntSeed(2).TMProtoCryptoPublicKey(),
},
},
{
providerAddr: sdk.ConsAddress([]byte("validator-3")),
pubKeyAndPower: abci.ValidatorUpdate{
Power: 100,
PubKey: cryptotestutil.NewCryptoIdentityFromIntSeed(3).TMProtoCryptoPublicKey(),
},
},
}
keeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()
for _, assignment := range testAssignments {
keeper.SetKeyAssignmentReplacement(ctx, chainID, assignment.providerAddr, assignment.pubKeyAndPower.PubKey, assignment.pubKeyAndPower.Power)
}
result := []testAssignment{}
cbIterateAll := func(providerAddr sdk.ConsAddress, prevCKey tmprotocrypto.PublicKey, power int64) (stop bool) {
result = append(result, testAssignment{
providerAddr: providerAddr,
pubKeyAndPower: abci.ValidatorUpdate{PubKey: prevCKey, Power: power},
})
return false // continue iteration
}
keeper.IterateKeyAssignmentReplacements(ctx, chainID, cbIterateAll)
require.Len(t, result, len(testAssignments), "incorrect result len - should be %d, got %d", len(testAssignments), len(result))
for i, res := range result {
require.Equal(t, testAssignments[i], res, "mismatched key assignment replacement in case %d", i)
}
result = []testAssignment{}
cbIterateOne := func(providerAddr sdk.ConsAddress, prevCKey tmprotocrypto.PublicKey, power int64) (stop bool) {
result = append(result, testAssignment{
providerAddr: providerAddr,
pubKeyAndPower: abci.ValidatorUpdate{PubKey: prevCKey, Power: power},
})
return true // stop after first
}
keeper.IterateKeyAssignmentReplacements(ctx, chainID, cbIterateOne)
require.Len(t, result, 1, "incorrect result len - should be 1, got %d", len(result))
require.Equal(t, testAssignments[0], result[0], "mismatched key assignment replacement in iterate one")
}
func TestConsumerAddrsToPruneCRUD(t *testing.T) {
chainID := "consumer"
consumerAddr := sdk.ConsAddress([]byte("consumerAddr1"))
vscID := uint64(1)
keeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()
keeper.AppendConsumerAddrsToPrune(ctx, chainID, vscID, consumerAddr)
addrToPrune := keeper.GetConsumerAddrsToPrune(ctx, chainID, vscID)
require.NotEmpty(t, addrToPrune, "address to prune is empty")
require.Len(t, addrToPrune, 1, "address to prune is not len 1")
require.Equal(t, sdk.ConsAddress(addrToPrune[0]), consumerAddr)
keeper.DeleteConsumerAddrsToPrune(ctx, chainID, vscID)
addrToPrune = keeper.GetConsumerAddrsToPrune(ctx, chainID, vscID)
require.Empty(t, addrToPrune, "address to prune was returned")
}
func TestIterateAllConsumerAddrsToPrune(t *testing.T) {
testAssignments := []testAssignment{
{
chainID: "consumer-1",
providerAddr: sdk.ConsAddress([]byte("validator-1")),
consumerAddr: sdk.ConsAddress([]byte("validator-1-consumer-1-key")),
vscID: 1,
},
{
chainID: "consumer-1",
providerAddr: sdk.ConsAddress([]byte("validator-2")),
consumerAddr: sdk.ConsAddress([]byte("validator-2-consumer-1-key")),
vscID: 1,
},
{
chainID: "consumer-3",
providerAddr: sdk.ConsAddress([]byte("validator-1")),
consumerAddr: sdk.ConsAddress([]byte("validator-1-consumer-3-key")),
vscID: 2,
},
}
keeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()
for _, ta := range testAssignments {
keeper.AppendConsumerAddrsToPrune(ctx, ta.chainID, ta.vscID, ta.consumerAddr)
}
addrToPrune := keeper.GetConsumerAddrsToPrune(ctx, testAssignments[0].chainID, testAssignments[0].vscID)
require.NotEmpty(t, addrToPrune, "address to prune is empty")
require.Len(t, addrToPrune, 2, "address to prune is not len 2")
require.Equal(t, sdk.ConsAddress(addrToPrune[0]), testAssignments[0].consumerAddr)
require.Equal(t, sdk.ConsAddress(addrToPrune[1]), testAssignments[1].consumerAddr)
type iterResult struct {
vscID uint64
consumerAddrsToPrune [][]byte
}
results := []iterResult{}
cbIterateAll := func(chainID string, vscID uint64, consumerAddrsToPrune [][]byte) (stop bool) {
results = append(results, iterResult{
vscID: vscID,
consumerAddrsToPrune: consumerAddrsToPrune,
})
return false // continue iteration
}
keeper.IterateAllConsumerAddrsToPrune(ctx, cbIterateAll)
require.Len(t, results, 2, "incorrect results len - should be 2, got %d", len(results))
// 2 keys for vscID == 1
require.Equal(t, results[0].vscID, uint64(1), "mismatched vscID in iterate all")
vsc1Addrs := results[0].consumerAddrsToPrune
require.Len(t, vsc1Addrs, 2, "wrong len of addrs to prune")
require.Equal(t, testAssignments[0].consumerAddr, sdk.ConsAddress(vsc1Addrs[0]), "mismatched consumer address")
require.Equal(t, testAssignments[1].consumerAddr, sdk.ConsAddress(vsc1Addrs[1]), "mismatched consumer address")
// 1 key for vscID == 2
require.Equal(t, results[1].vscID, uint64(2), "mismatched vscID in iterate all")
vsc2Addrs := results[1].consumerAddrsToPrune
require.Len(t, vsc2Addrs, 1, "wrong len of addrs to prune")
require.Equal(t, testAssignments[2].consumerAddr, sdk.ConsAddress(vsc2Addrs[0]), "mismatched consumer address")
results = []iterResult{}
cbIterateOne := func(chainID string, vscID uint64, consumerAddrsToPrune [][]byte) (stop bool) {
results = append(results, iterResult{
vscID: vscID,
consumerAddrsToPrune: consumerAddrsToPrune,
})
return true // stop iteration
}
keeper.IterateAllConsumerAddrsToPrune(ctx, cbIterateOne)
require.Len(t, results, 1, "incorrect results len - should be 1, got %d", len(results))
// 2 keys for vscID == 1
require.Equal(t, results[0].vscID, uint64(1), "mismatched vscID in iterate")
vsc1Addrs = results[0].consumerAddrsToPrune
require.Len(t, vsc1Addrs, 2, "wrong len of addrs to prune")
require.Equal(t, testAssignments[0].consumerAddr, sdk.ConsAddress(vsc1Addrs[0]), "mismatched consumer address")
require.Equal(t, testAssignments[1].consumerAddr, sdk.ConsAddress(vsc1Addrs[1]), "mismatched consumer address")
}
// checkCorrectPruningProperty checks that the pruning property is correct for a given
// consumer chain. See AppendConsumerAddrsToPrune for a formulation of the property.
func checkCorrectPruningProperty(ctx sdk.Context, k providerkeeper.Keeper, chainID string) bool {
/*
For each consumer address cAddr in ValidatorByConsumerAddr,
- either there exists a provider address pAddr in ValidatorConsumerPubKey,
s.t. hash(ValidatorConsumerPubKey(pAddr)) = cAddr
- or there exists a vscID in ConsumerAddrsToPrune s.t. cAddr in ConsumerAddrsToPrune(vscID)
*/
willBePruned := map[string]bool{}
k.IterateConsumerAddrsToPrune(ctx, chainID, func(vscID uint64, consumerAddrsToPrune [][]byte) (stop bool) {
for _, cAddr := range consumerAddrsToPrune {
addr := sdk.ConsAddress(cAddr)
willBePruned[addr.String()] = true
}
return false
})
good := true
k.IterateAllValidatorsByConsumerAddr(ctx, func(chainID string, consumerAddr sdk.ConsAddress, providerAddr sdk.ConsAddress) (stop bool) {
if _, ok := willBePruned[consumerAddr.String()]; ok {
// Address will be pruned, everything is fine.
return false
}
// Try to find a validator who has this consumer address currently assigned
isCurrentlyAssigned := false
k.IterateValidatorConsumerPubKeys(ctx, chainID,
func(_ sdk.ConsAddress, consumerKey tmprotocrypto.PublicKey) (stop bool) {
if utils.TMCryptoPublicKeyToConsAddr(consumerKey).Equals(consumerAddr) {
isCurrentlyAssigned = true
return true // stop iterating early
}
return false
},
)
if !isCurrentlyAssigned {
// Will not be pruned, and is not currently assigned: violation
good = false
return true // breakout early
}
return false
})
return good
}
func TestAssignConsensusKeyForConsumerChain(t *testing.T) {
chainID := "chainID"
providerIdentities := []*cryptotestutil.CryptoIdentity{
cryptotestutil.NewCryptoIdentityFromIntSeed(0),
cryptotestutil.NewCryptoIdentityFromIntSeed(1),
}
consumerIdentities := []*cryptotestutil.CryptoIdentity{
cryptotestutil.NewCryptoIdentityFromIntSeed(2),
cryptotestutil.NewCryptoIdentityFromIntSeed(3),
}
testCases := []struct {
name string
// State-mutating mockSetup specific to this test case
mockSetup func(sdk.Context, providerkeeper.Keeper, testkeeper.MockedKeepers)
doActions func(sdk.Context, providerkeeper.Keeper)
}{
/*
0. Consumer registered: Assign PK0->CK0 and retrieve PK0->CK0
1. Consumer registered: Assign PK0->CK0, PK0->CK1 and retrieve PK0->CK1
2. Consumer registered: Assign PK0->CK0, PK1->CK0 and error
3. Consumer registered: Assign PK1->PK0 and error (TODO: see https://github.com/cosmos/interchain-security/issues/503)
4. Consumer not registered: Assign PK0->CK0 and retrieve PK0->CK0
5. Consumer not registered: Assign PK0->CK0, PK0->CK1 and retrieve PK0->CK1
6. Consumer not registered: Assign PK0->CK0, PK1->CK0 and error
7. Consumer not registered: Assign PK1->PK0 and error (TODO: see https://github.com/cosmos/interchain-security/issues/503)
*/
{
name: "0",
mockSetup: func(ctx sdk.Context, k providerkeeper.Keeper, mocks testkeeper.MockedKeepers) {
gomock.InOrder(
mocks.MockStakingKeeper.EXPECT().GetLastValidatorPower(
ctx, providerIdentities[0].SDKValAddress(),
).Return(int64(0)),
)
},
doActions: func(ctx sdk.Context, k providerkeeper.Keeper) {
k.SetConsumerClientId(ctx, chainID, "")
err := k.AssignConsumerKey(ctx, chainID,
providerIdentities[0].SDKStakingValidator(),
consumerIdentities[0].TMProtoCryptoPublicKey(),
)
require.NoError(t, err)
providerAddr, found := k.GetValidatorByConsumerAddr(ctx, chainID, consumerIdentities[0].SDKConsAddress())
require.True(t, found)
require.Equal(t, providerIdentities[0].SDKConsAddress(), providerAddr)
},
},
{
name: "1",
mockSetup: func(ctx sdk.Context, k providerkeeper.Keeper, mocks testkeeper.MockedKeepers) {
gomock.InOrder(
mocks.MockStakingKeeper.EXPECT().GetLastValidatorPower(
ctx, providerIdentities[0].SDKValAddress(),
).Return(int64(0)),
mocks.MockStakingKeeper.EXPECT().GetLastValidatorPower(
ctx, providerIdentities[0].SDKValAddress(),
).Return(int64(0)),
)
},
doActions: func(ctx sdk.Context, k providerkeeper.Keeper) {
k.SetConsumerClientId(ctx, chainID, "")
err := k.AssignConsumerKey(ctx, chainID,
providerIdentities[0].SDKStakingValidator(),
consumerIdentities[0].TMProtoCryptoPublicKey(),
)
require.NoError(t, err)
err = k.AssignConsumerKey(ctx, chainID,
providerIdentities[0].SDKStakingValidator(),
consumerIdentities[1].TMProtoCryptoPublicKey(),
)
require.NoError(t, err)
providerAddr, found := k.GetValidatorByConsumerAddr(ctx, chainID, consumerIdentities[1].SDKConsAddress())
require.True(t, found)
require.Equal(t, providerIdentities[0].SDKConsAddress(), providerAddr)
},
},
{
name: "2",
mockSetup: func(ctx sdk.Context, k providerkeeper.Keeper, mocks testkeeper.MockedKeepers) {
gomock.InOrder(
mocks.MockStakingKeeper.EXPECT().GetLastValidatorPower(
ctx, providerIdentities[0].SDKValAddress(),
).Return(int64(0)),
)
},
doActions: func(ctx sdk.Context, k providerkeeper.Keeper) {
k.SetConsumerClientId(ctx, chainID, "")
err := k.AssignConsumerKey(ctx, chainID,
providerIdentities[0].SDKStakingValidator(),
consumerIdentities[0].TMProtoCryptoPublicKey(),
)
require.NoError(t, err)
err = k.AssignConsumerKey(ctx, chainID,
providerIdentities[1].SDKStakingValidator(),
consumerIdentities[0].TMProtoCryptoPublicKey(),
)
require.Error(t, err)
providerAddr, found := k.GetValidatorByConsumerAddr(ctx, chainID, consumerIdentities[0].SDKConsAddress())
require.True(t, found)
require.Equal(t, providerIdentities[0].SDKConsAddress(), providerAddr)
},
},
// (TODO: see https://github.com/cosmos/interchain-security/issues/503)
// {
// name: "3",
// mockSetup: func(ctx sdk.Context, k providerkeeper.Keeper, mocks testkeeper.MockedKeepers) {
// },
// doActions: func(ctx sdk.Context, k providerkeeper.Keeper) {
// },
// },
{
name: "4",
mockSetup: func(ctx sdk.Context, k providerkeeper.Keeper, mocks testkeeper.MockedKeepers) {
},
doActions: func(ctx sdk.Context, k providerkeeper.Keeper) {
err := k.AssignConsumerKey(ctx, chainID,
providerIdentities[0].SDKStakingValidator(),
consumerIdentities[0].TMProtoCryptoPublicKey(),
)
require.NoError(t, err)
providerAddr, found := k.GetValidatorByConsumerAddr(ctx, chainID, consumerIdentities[0].SDKConsAddress())
require.True(t, found)
require.Equal(t, providerIdentities[0].SDKConsAddress(), providerAddr)
},
},
{
name: "5",
mockSetup: func(ctx sdk.Context, k providerkeeper.Keeper, mocks testkeeper.MockedKeepers) {
},
doActions: func(ctx sdk.Context, k providerkeeper.Keeper) {
err := k.AssignConsumerKey(ctx, chainID,
providerIdentities[0].SDKStakingValidator(),
consumerIdentities[0].TMProtoCryptoPublicKey(),
)
require.NoError(t, err)
err = k.AssignConsumerKey(ctx, chainID,
providerIdentities[0].SDKStakingValidator(),
consumerIdentities[1].TMProtoCryptoPublicKey(),
)
require.NoError(t, err)
providerAddr, found := k.GetValidatorByConsumerAddr(ctx, chainID, consumerIdentities[1].SDKConsAddress())
require.True(t, found)
require.Equal(t, providerIdentities[0].SDKConsAddress(), providerAddr)
},
},
{
name: "6",
mockSetup: func(ctx sdk.Context, k providerkeeper.Keeper, mocks testkeeper.MockedKeepers) {
},
doActions: func(ctx sdk.Context, k providerkeeper.Keeper) {
err := k.AssignConsumerKey(ctx, chainID,
providerIdentities[0].SDKStakingValidator(),
consumerIdentities[0].TMProtoCryptoPublicKey(),
)
require.NoError(t, err)
err = k.AssignConsumerKey(ctx, chainID,
providerIdentities[1].SDKStakingValidator(),
consumerIdentities[0].TMProtoCryptoPublicKey(),
)
require.Error(t, err)
providerAddr, found := k.GetValidatorByConsumerAddr(ctx, chainID, consumerIdentities[0].SDKConsAddress())
require.True(t, found)
require.Equal(t, providerIdentities[0].SDKConsAddress(), providerAddr)
},
},
// (TODO: see https://github.com/cosmos/interchain-security/issues/503)
// {
// name: "7",
// mockSetup: func(ctx sdk.Context, k providerkeeper.Keeper, mocks testkeeper.MockedKeepers) {
// },
// doActions: func(ctx sdk.Context, k providerkeeper.Keeper) {
// },
// },
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
k, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
tc.mockSetup(ctx, k, mocks)
tc.doActions(ctx, k)
require.True(t, checkCorrectPruningProperty(ctx, k, chainID))
ctrl.Finish()
})
}
}
// Represents the validator set of a chain
type ValSet struct {
identities []*cryptotestutil.CryptoIdentity
// indexed by same index as identities
power []int64
}
func CreateValSet(identities []*cryptotestutil.CryptoIdentity) ValSet {
return ValSet{
identities: identities,
power: make([]int64, len(identities)),
}
}
// Apply a list of validator power updates
func (vs *ValSet) apply(updates []abci.ValidatorUpdate) {
// precondition: updates must all have unique keys
// note: an insertion index should always be found
for _, u := range updates {
for i, id := range vs.identities { // n2 looping but n is tiny
// cons := sdk.ConsAddress(utils.GetChangePubKeyAddress(u))
cons := utils.TMCryptoPublicKeyToConsAddr(u.PubKey)
if id.SDKConsAddress().Equals(cons) {
vs.power[i] = u.Power
}
}
}
}
// A key assignment action to be done
type Assignment struct {
val stakingtypes.Validator
ck tmprotocrypto.PublicKey
}
// TestSimulatedAssignmentsAndUpdateApplication tests a series
// of simulated scenarios where random key assignments and validator
// set updates are generated.
// TODO: this does not yet fully test the correct lookup of a provider
// validator from a consumer consensus address, as is needed for handling
// (double sign) slash packets.
func TestSimulatedAssignmentsAndUpdateApplication(t *testing.T) {
CHAINID := "chainID"
// The number of full test executions to run
NUM_EXECUTIONS := 100
// Each test execution mimics the adding of a consumer chain and the
// assignments and power updates of several blocks
NUM_BLOCKS_PER_EXECUTION := 40
// The number of validators to be simulated
NUM_VALIDATORS := 4
// The number of keys that can be used. Keeping this number small is
// good because it increases the chance that different assignments will
// use the same keys, which is something we want to test.
NUM_ASSIGNABLE_KEYS := 12
// The maximum number of key assignment actions to simulate in each
// simulated block, and before the consumer chain is registered.
NUM_ASSIGNMENTS_PER_BLOCK_MAX := 8
// Create some identities for the simulated provider validators to use
providerIdentities := []*cryptotestutil.CryptoIdentity{}
// Create some identities which the provider validators can assign to the consumer chain
consumerIdentities := []*cryptotestutil.CryptoIdentity{}
for i := 0; i < NUM_VALIDATORS; i++ {
providerIdentities = append(providerIdentities, cryptotestutil.NewCryptoIdentityFromIntSeed(i))
}
for i := NUM_VALIDATORS; i < NUM_ASSIGNABLE_KEYS+NUM_VALIDATORS; i++ {
// ATTENTION: uses a different domain of keys for assignments
//
// (TODO: allow consumer identities to overlap with provider identities
// this will be enabled after the testnet
// see https://github.com/cosmos/interchain-security/issues/503)
//
consumerIdentities = append(consumerIdentities, cryptotestutil.NewCryptoIdentityFromIntSeed(i))
}
// Helper: simulates creation of staking module EndBlock updates.
getStakingUpdates := func() (ret []abci.ValidatorUpdate) {
// Get a random set of validators to update. It is important to test subsets of all validators.
validators := rand.Perm(NUM_VALIDATORS)[0:rand.Intn(NUM_VALIDATORS+1)]
for _, i := range validators {
// Power 0, 1, or 2 represents
// deletion, update (from 0 or 2), update (from 0 or 1)
power := rand.Intn(3)
ret = append(ret, abci.ValidatorUpdate{
PubKey: providerIdentities[i].TMProtoCryptoPublicKey(),
Power: int64(power),
})
}
return
}
// Helper: simulates creation of assignment tx's to be done.
getAssignments := func() (ret []Assignment) {
for i, numAssignments := 0, rand.Intn(NUM_ASSIGNMENTS_PER_BLOCK_MAX); i < numAssignments; i++ {
randomIxP := rand.Intn(NUM_VALIDATORS)
randomIxC := rand.Intn(NUM_ASSIGNABLE_KEYS)
ret = append(ret, Assignment{
val: providerIdentities[randomIxP].SDKStakingValidator(),
ck: consumerIdentities[randomIxC].TMProtoCryptoPublicKey(),
})
}
return
}
// Run a randomly simulated execution and test that desired properties hold
// Helper: run a randomly simulated scenario where a consumer chain is added
// (after key assignment actions are done), followed by a series of validator power updates
// and key assignments tx's. For each simulated 'block', the validator set replication
// properties and the pruning property are checked.
runRandomExecution := func() {
k, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
// Create validator sets for the provider and consumer. These are used to check the validator set
// replication property.
providerValset := CreateValSet(providerIdentities)
// NOTE: consumer must have space for provider identities because default key assignments are to provider keys
consumerValset := CreateValSet(append(providerIdentities, consumerIdentities...))
// Mock calls to GetLastValidatorPower to return directly from the providerValset
mocks.MockStakingKeeper.EXPECT().GetLastValidatorPower(
gomock.Any(),
gomock.Any(),
).DoAndReturn(func(_ interface{}, valAddr sdk.ValAddress) int64 {
// When the mocked method is called, locate the appropriate validator
// in the provider valset and return its power.
for i, id := range providerIdentities {
if id.SDKStakingValidator().GetOperator().Equals(valAddr) {
return providerValset.power[i]
}
}
panic("must find validator")
// This can be called 0 or more times per block depending on the random
// assignments that occur
}).AnyTimes()
// Helper: apply some updates to both the provider and consumer valsets
applyUpdates := func(updates []abci.ValidatorUpdate) {
providerValset.apply(updates)
updates, err := k.ApplyKeyAssignmentToValUpdates(ctx, CHAINID, updates)
require.NoError(t, err)
consumerValset.apply(updates)
}
// Helper: apply some key assignment transactions to the system
applyAssignments := func(assignments []Assignment) {
for _, a := range assignments {
// ignore err return, it can be possible for an error to occur
_ = k.AssignConsumerKey(ctx, CHAINID, a.val, a.ck)
}
}
// The consumer chain has not yet been registered
// Apply some randomly generated key assignments
applyAssignments(getAssignments())
// And generate a random provider valset which, in the real system, will
// be put into the consumer genesis.
applyUpdates(getStakingUpdates())
// Register the consumer chain
k.SetConsumerClientId(ctx, CHAINID, "")
// Analogous to the last vscid received from the consumer in a maturity
// Used to check the correct pruning property
greatestPrunedVSCID := -1
// Simulate a number of 'blocks'
// Each block consists of a number of random key assignment tx's
// and a random set of validator power updates
for block := 0; block < NUM_BLOCKS_PER_EXECUTION; block++ {
// Generate and apply assignments and power updates
applyAssignments(getAssignments())
applyUpdates(getStakingUpdates())
// Randomly fast forward the greatest pruned VSCID. This simulates
// delivery of maturity packets from the consumer chain.
prunedVscid := greatestPrunedVSCID + rand.Intn(int(k.GetValidatorSetUpdateId(ctx))+1)
k.PruneKeyAssignments(ctx, CHAINID, uint64(prunedVscid))
greatestPrunedVSCID = prunedVscid
/*
Properties: Validator Set Replication
Each validator set on the provider must be replicated on the consumer.
The property in the real system is somewhat weaker, because the consumer chain can
forward updates to tendermint in batches.
(See https://github.com/cosmos/ibc/blob/main/spec/app/ics-028-cross-chain-validation/system_model_and_properties.md#system-properties)
We test the stronger property, because we abstract over implementation of the consumer
chain. The stronger property implies the weaker property.
*/
// Check validator set replication forward direction
for i, idP := range providerValset.identities {
// For each active validator on the provider chain
if 0 < providerValset.power[i] {
// Get the assigned key
ck, found := k.GetValidatorConsumerPubKey(ctx, CHAINID, idP.SDKConsAddress())
if !found {
// Use default if unassigned
ck = idP.TMProtoCryptoPublicKey()
}
consC := utils.TMCryptoPublicKeyToConsAddr(ck)
// Find the corresponding consumer validator (must always be found)
for j, idC := range consumerValset.identities {
if consC.Equals(idC.SDKConsAddress()) {
require.Equal(t, providerValset.power[i], consumerValset.power[j])
}
}
}
}
// Check validator set replication backward direction
for i := range consumerValset.identities {
// For each active validator on the consumer chain
consC := consumerValset.identities[i].SDKConsAddress()
if 0 < consumerValset.power[i] {
// Get the provider who assigned the key
consP := k.GetProviderAddrFromConsumerAddr(ctx, CHAINID, consC)
// Find the corresponding provider validator (must always be found)
for j, idP := range providerValset.identities {
if idP.SDKConsAddress().Equals(consP) {
require.Equal(t, providerValset.power[j], consumerValset.power[i])
}
}
}
}
// Check that all keys have been or will eventually be pruned.
require.True(t, checkCorrectPruningProperty(ctx, k, CHAINID))
}
ctrl.Finish()
}
for i := 0; i < NUM_EXECUTIONS; i++ {
runRandomExecution()
}
}