-
Notifications
You must be signed in to change notification settings - Fork 39
/
specification_test.go
1853 lines (1711 loc) · 68.6 KB
/
specification_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
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 keeper_test
import (
"fmt"
"testing"
"github.com/google/uuid"
simapp "github.com/provenance-io/provenance/app"
"github.com/provenance-io/provenance/x/metadata/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type SpecKeeperTestSuite struct {
suite.Suite
app *simapp.App
ctx sdk.Context
queryClient types.QueryClient
pubkey1 cryptotypes.PubKey
user1 string
user1Addr sdk.AccAddress
pubkey2 cryptotypes.PubKey
user2 string
user2Addr sdk.AccAddress
scopeSpecUUID uuid.UUID
scopeSpecID types.MetadataAddress
contractSpecUUID1 uuid.UUID
contractSpecID1 types.MetadataAddress
contractSpecID2 types.MetadataAddress
}
func TestSpecKeeperTestSuite(t *testing.T) {
suite.Run(t, new(SpecKeeperTestSuite))
}
func (s *SpecKeeperTestSuite) SetupTest() {
testApp := simapp.Setup(s.T())
ctx := testApp.BaseApp.NewContext(false, tmproto.Header{})
s.pubkey1 = secp256k1.GenPrivKey().PubKey()
s.user1Addr = sdk.AccAddress(s.pubkey1.Address())
s.user1 = s.user1Addr.String()
s.pubkey2 = secp256k1.GenPrivKey().PubKey()
s.user2Addr = sdk.AccAddress(s.pubkey2.Address())
s.user2 = s.user2Addr.String()
s.scopeSpecUUID = uuid.New()
s.scopeSpecID = types.ScopeSpecMetadataAddress(s.scopeSpecUUID)
s.contractSpecUUID1 = uuid.New()
s.contractSpecID1 = types.ContractSpecMetadataAddress(s.contractSpecUUID1)
s.contractSpecID2 = types.ContractSpecMetadataAddress(uuid.New())
s.app = testApp
s.ctx = ctx
s.app.AccountKeeper.SetAccount(s.ctx, s.app.AccountKeeper.NewAccountWithAddress(s.ctx, s.user1Addr))
queryHelper := baseapp.NewQueryServerTestHelper(ctx, testApp.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, testApp.MetadataKeeper)
s.queryClient = types.NewQueryClient(queryHelper)
}
func containsMetadataAddress(arr []types.MetadataAddress, newVal types.MetadataAddress) bool {
found := false
for _, v := range arr {
if v.Equals(newVal) {
found = true
break
}
}
return found
}
func areEquivalentSetsOfMetaAddresses(arr1 []types.MetadataAddress, arr2 []types.MetadataAddress) bool {
if len(arr1) != len(arr2) {
return false
}
for _, v2 := range arr2 {
if !containsMetadataAddress(arr1, v2) {
return false
}
}
return true
}
func containsRecSpec(arr []*types.RecordSpecification, newVal *types.RecordSpecification) bool {
found := false
for _, v := range arr {
if v.SpecificationId.Equals(newVal.SpecificationId) {
found = true
break
}
}
return found
}
func areEquivalentSetsOfRecSpecs(arr1 []*types.RecordSpecification, arr2 []*types.RecordSpecification) bool {
if len(arr1) != len(arr2) {
return false
}
for _, v2 := range arr2 {
if !containsRecSpec(arr1, v2) {
return false
}
}
return true
}
func asRecSpecAddrOrPanic(ma types.MetadataAddress, name string) types.MetadataAddress {
retval, err := ma.AsRecordSpecAddress(name)
if err != nil {
panic(err)
}
return retval
}
func (s *SpecKeeperTestSuite) TestGetSetRemoveRecordSpecification() {
recordName := "record name"
recSpecID := types.RecordSpecMetadataAddress(s.contractSpecUUID1, recordName)
newSpec := types.NewRecordSpecification(
recSpecID,
recordName,
[]*types.InputSpecification{
types.NewInputSpecification(
"input name 1", "type name 1",
types.NewInputSpecificationSourceHash("source hash 1"),
),
types.NewInputSpecification(
"input name 2", "type name 2",
types.NewInputSpecificationSourceHash("source hash 2"),
),
},
"type name",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
)
require.NotNil(s.T(), newSpec, "test setup failure: NewRecordSpecification should not return nil")
spec1, found1 := s.app.MetadataKeeper.GetRecordSpecification(s.ctx, recSpecID)
s.False(found1, "1: get record spec should return false before it has been saved")
s.NotNil(spec1, "1: get record spec should always return a non-nil record spec")
s.app.MetadataKeeper.SetRecordSpecification(s.ctx, *newSpec)
spec2, found2 := s.app.MetadataKeeper.GetRecordSpecification(s.ctx, recSpecID)
s.True(found2, "get record spec should return true after it has been saved")
s.NotNil(spec2, "get record spec should always return a non-nil record spec")
s.Equal(recSpecID, spec2.SpecificationId, "2: get record spec should return a spec containing id provided")
spec3, found3 := s.app.MetadataKeeper.GetRecordSpecification(s.ctx, types.RecordSpecMetadataAddress(s.contractSpecUUID1, "nope"))
s.False(found3, "3: get record spec should return false for an unknown address")
s.NotNil(spec3, "3: get record spec should always return a non-nil record spec")
spec4, found4 := s.app.MetadataKeeper.GetRecordSpecification(s.ctx, types.RecordSpecMetadataAddress(uuid.New(), recordName))
s.False(found4, "4: get record spec should return false for an unknown address")
s.NotNil(spec4, "4: get record spec should always return a non-nil record spec")
remErr1 := s.app.MetadataKeeper.RemoveRecordSpecification(s.ctx, newSpec.SpecificationId)
s.Nil(remErr1, "5: remove should not return any error")
spec6, found6 := s.app.MetadataKeeper.GetRecordSpecification(s.ctx, recSpecID)
s.False(found6, "6: get record spec should return false after it has been removed")
s.NotNil(spec6, "6: get record spec should always return a non-nil record spec")
remErr2 := s.app.MetadataKeeper.RemoveRecordSpecification(s.ctx, recSpecID)
s.Equal(
fmt.Errorf("record specification with id %s not found", recSpecID),
remErr2,
"7: remove error message when not found",
)
}
func (s *SpecKeeperTestSuite) TestIterateRecordSpecs() {
size := 10
specs := make([]*types.RecordSpecification, size)
for i := 0; i < size; i++ {
contractSpecUUID := uuid.New()
recordName := fmt.Sprintf("record name %d", i)
specs[i] = types.NewRecordSpecification(
types.RecordSpecMetadataAddress(contractSpecUUID, recordName),
recordName,
[]*types.InputSpecification{
types.NewInputSpecification(
fmt.Sprintf("input name [%d] 1", i),
fmt.Sprintf("type name [%d] 1", i),
types.NewInputSpecificationSourceHash(fmt.Sprintf("source hash [%d] 1", i)),
),
types.NewInputSpecification(
fmt.Sprintf("input name [%d] 2", i),
fmt.Sprintf("type name [%d] 2", i),
types.NewInputSpecificationSourceHash(fmt.Sprintf("source hash [%d] 2", i)),
),
types.NewInputSpecification(
fmt.Sprintf("input name [%d] 3", i),
fmt.Sprintf("type name [%d] 3", i),
types.NewInputSpecificationSourceRecordID(types.RecordMetadataAddress(uuid.New(), recordName)),
),
},
fmt.Sprintf("type name %d", i),
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
)
s.app.MetadataKeeper.SetRecordSpecification(s.ctx, *specs[i])
}
visitedRecordSpecIDs := make([]types.MetadataAddress, size)
count := 0
err1 := s.app.MetadataKeeper.IterateRecordSpecs(s.ctx, func(spec types.RecordSpecification) (stop bool) {
if containsMetadataAddress(visitedRecordSpecIDs, spec.SpecificationId) {
s.Fail("function IterateRecordSpecs visited the same record specification twice: %s", spec.SpecificationId.String())
}
visitedRecordSpecIDs[count] = spec.SpecificationId
count++
return false
})
s.Nil(err1, "1: function IterateRecordSpecs should not have returned an error")
s.Equal(size, count, "number of record specs iterated through")
shortCount := 0
err2 := s.app.MetadataKeeper.IterateRecordSpecs(s.ctx, func(spec types.RecordSpecification) (stop bool) {
shortCount++
if shortCount == 5 {
return true
}
return false
})
s.Nil(err2, "2: function IterateRecordSpecs should not have returned an error")
s.Equal(5, shortCount, "function IterateRecordSpecs ignored (stop bool) return value")
}
func (s *SpecKeeperTestSuite) TestIterateRecordSpecsForOwner() {
// Create 3 contract specs. One owned by user1, One owned by user2, and One owned by both user1 and user2.
contractSpecs := []*types.ContractSpecification{
types.NewContractSpecification(
types.ContractSpecMetadataAddress(uuid.New()),
types.NewDescription(
"TestIterateContractSpecsForOwner[0]",
"A description for a unit test contract specification - owner: user1",
"http://test.net",
"http://test.net/ico.png",
),
[]string{s.user1Addr.String()},
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
types.NewContractSpecificationSourceHash("somehash0"),
"someclass_0",
),
types.NewContractSpecification(
types.ContractSpecMetadataAddress(uuid.New()),
types.NewDescription(
"TestIterateContractSpecsForOwner[1]",
"A description for a unit test contract specification - owner: user2",
"http://test.net",
"http://test.net/ico.png",
),
[]string{s.user2Addr.String()},
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
types.NewContractSpecificationSourceHash("somehash1"),
"someclass_1",
),
types.NewContractSpecification(
types.ContractSpecMetadataAddress(uuid.New()),
types.NewDescription(
"TestIterateContractSpecsForOwner[2]",
"A description for a unit test contract specification - owners: user1, user2",
"http://test.net",
"http://test.net/ico.png",
),
[]string{s.user1Addr.String(), s.user2Addr.String()},
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
types.NewContractSpecificationSourceHash("somehash2"),
"someclass_2",
),
}
for _, spec := range contractSpecs {
s.app.MetadataKeeper.SetContractSpecification(s.ctx, *spec)
}
// Create 2 record specifications for each contract specification
recordSpecs := []*types.RecordSpecification{
types.NewRecordSpecification(
asRecSpecAddrOrPanic(contractSpecs[0].SpecificationId, "contractspec0recspec0"),
"contractspec0recspec0",
[]*types.InputSpecification{
types.NewInputSpecification(
"inputspec0", "inputspectypename0",
types.NewInputSpecificationSourceHash("sourcehash0"),
),
},
"typename0",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
),
types.NewRecordSpecification(
asRecSpecAddrOrPanic(contractSpecs[0].SpecificationId, "contractspec0recspec1"),
"contractspec0recspec1",
[]*types.InputSpecification{
types.NewInputSpecification(
"inputspec1", "inputspectypename1",
types.NewInputSpecificationSourceHash("sourcehash1"),
),
},
"typename1",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
),
types.NewRecordSpecification(
asRecSpecAddrOrPanic(contractSpecs[1].SpecificationId, "contractspec1recspec2"),
"contractspec1recspec2",
[]*types.InputSpecification{
types.NewInputSpecification(
"inputspec2", "inputspectypename2",
types.NewInputSpecificationSourceHash("sourcehash2"),
),
},
"typename2",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
),
types.NewRecordSpecification(
asRecSpecAddrOrPanic(contractSpecs[1].SpecificationId, "contractspec1recspec3"),
"contractspec1recspec3",
[]*types.InputSpecification{
types.NewInputSpecification(
"inputspec3", "inputspectypename3",
types.NewInputSpecificationSourceHash("sourcehash3"),
),
},
"typename3",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
),
types.NewRecordSpecification(
asRecSpecAddrOrPanic(contractSpecs[2].SpecificationId, "contractspec2recspec4"),
"contractspec2recspec4",
[]*types.InputSpecification{
types.NewInputSpecification(
"inputspec4", "inputspectypename4",
types.NewInputSpecificationSourceHash("sourcehash4"),
),
},
"typename4",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
),
types.NewRecordSpecification(
asRecSpecAddrOrPanic(contractSpecs[2].SpecificationId, "contractspec2recspec5"),
"contractspec2recspec5",
[]*types.InputSpecification{
types.NewInputSpecification(
"inputspec5", "inputspectypename5",
types.NewInputSpecificationSourceHash("sourcehash5"),
),
},
"typename5",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
),
}
for _, spec := range recordSpecs {
s.app.MetadataKeeper.SetRecordSpecification(s.ctx, *spec)
}
user1SpecIDs := []types.MetadataAddress{
recordSpecs[0].SpecificationId, recordSpecs[1].SpecificationId,
recordSpecs[4].SpecificationId, recordSpecs[5].SpecificationId,
}
user2SpecIDs := []types.MetadataAddress{
recordSpecs[2].SpecificationId, recordSpecs[3].SpecificationId,
recordSpecs[4].SpecificationId, recordSpecs[5].SpecificationId,
}
// Make sure all user1 record specs are iterated over
user1SpecIDsIterated := []types.MetadataAddress{}
errUser1 := s.app.MetadataKeeper.IterateRecordSpecsForOwner(s.ctx, s.user1Addr, func(specID types.MetadataAddress) (stop bool) {
user1SpecIDsIterated = append(user1SpecIDsIterated, specID)
return false
})
s.Nil(errUser1, "user1: should not have returned an error")
s.Equal(4, len(user1SpecIDsIterated), "user1: iteration count")
s.True(areEquivalentSetsOfMetaAddresses(user1SpecIDs, user1SpecIDsIterated),
"user1: iterated over unexpected record specs:\n expected: %v\n actual: %v\n",
user1SpecIDs, user1SpecIDsIterated)
// Make sure all user2 record specs are iterated over
user2SpecIDsIterated := []types.MetadataAddress{}
errUser2 := s.app.MetadataKeeper.IterateRecordSpecsForOwner(s.ctx, s.user2Addr, func(specID types.MetadataAddress) (stop bool) {
user2SpecIDsIterated = append(user2SpecIDsIterated, specID)
return false
})
s.Nil(errUser2, "user2: should not have returned an error")
s.Equal(4, len(user2SpecIDsIterated), "user2: iteration count")
s.True(areEquivalentSetsOfMetaAddresses(user2SpecIDs, user2SpecIDsIterated),
"user2: iterated over unexpected record specs:\n expected: %v\n actual: %v\n",
user2SpecIDs, user2SpecIDsIterated)
// Make sure an unknown user address results in zero iterations.
user3Addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
user3Count := 0
errUser3 := s.app.MetadataKeeper.IterateRecordSpecsForOwner(s.ctx, user3Addr, func(specID types.MetadataAddress) (stop bool) {
user3Count++
return false
})
s.Nil(errUser3, "user3: should not have returned an error")
s.Equal(0, user3Count, "user3: iteration count")
// Make sure the stop bool is being recognized.
countStop := 0
errStop := s.app.MetadataKeeper.IterateRecordSpecsForOwner(s.ctx, s.user1Addr, func(specID types.MetadataAddress) (stop bool) {
countStop++
if countStop == 2 {
return true
}
return false
})
s.Nil(errStop, "stop bool: should not have returned an error")
s.Equal(2, countStop, "stop bool: iteration count")
}
func (s *SpecKeeperTestSuite) TestIterateRecordSpecsForContractSpec() {
// Create 3 contract specs.
contractSpecs := []*types.ContractSpecification{
types.NewContractSpecification(
types.ContractSpecMetadataAddress(uuid.New()),
types.NewDescription(
"TestIterateContractSpecsForOwner[0]",
"A description for a unit test contract specification - owner: user1",
"http://test.net",
"http://test.net/ico.png",
),
[]string{s.user1Addr.String()},
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
types.NewContractSpecificationSourceHash("somehash0"),
"someclass_0",
),
types.NewContractSpecification(
types.ContractSpecMetadataAddress(uuid.New()),
types.NewDescription(
"TestIterateContractSpecsForOwner[1]",
"A description for a unit test contract specification - owner: user2",
"http://test.net",
"http://test.net/ico.png",
),
[]string{s.user2Addr.String()},
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
types.NewContractSpecificationSourceHash("somehash1"),
"someclass_1",
),
types.NewContractSpecification(
types.ContractSpecMetadataAddress(uuid.New()),
types.NewDescription(
"TestIterateContractSpecsForOwner[2]",
"A description for a unit test contract specification - owner: user1, user2",
"http://test.net",
"http://test.net/ico.png",
),
[]string{s.user1Addr.String(), s.user2Addr.String()},
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
types.NewContractSpecificationSourceHash("somehash2"),
"someclass_2",
),
}
for _, spec := range contractSpecs {
s.app.MetadataKeeper.SetContractSpecification(s.ctx, *spec)
}
// Create 3 record specs for the 1st contract spec, 2 for the 2nd, and none for the 3rd.
recordSpecs := []*types.RecordSpecification{
types.NewRecordSpecification(
asRecSpecAddrOrPanic(contractSpecs[0].SpecificationId, "contractspec0recspec0"),
"contractspec0recspec0",
[]*types.InputSpecification{
types.NewInputSpecification(
"inputspec0", "inputspectypename0",
types.NewInputSpecificationSourceHash("sourcehash0"),
),
},
"typename0",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
),
types.NewRecordSpecification(
asRecSpecAddrOrPanic(contractSpecs[0].SpecificationId, "contractspec0recspec1"),
"contractspec0recspec1",
[]*types.InputSpecification{
types.NewInputSpecification(
"inputspec1", "inputspectypename1",
types.NewInputSpecificationSourceHash("sourcehash1"),
),
},
"typename1",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
),
types.NewRecordSpecification(
asRecSpecAddrOrPanic(contractSpecs[0].SpecificationId, "contractspec1recspec2"),
"contractspec0recspec2",
[]*types.InputSpecification{
types.NewInputSpecification(
"inputspec2", "inputspectypename2",
types.NewInputSpecificationSourceHash("sourcehash2"),
),
},
"typename2",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
),
types.NewRecordSpecification(
asRecSpecAddrOrPanic(contractSpecs[1].SpecificationId, "contractspec1recspec3"),
"contractspec1recspec3",
[]*types.InputSpecification{
types.NewInputSpecification(
"inputspec3", "inputspectypename3",
types.NewInputSpecificationSourceHash("sourcehash3"),
),
},
"typename3",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
),
types.NewRecordSpecification(
asRecSpecAddrOrPanic(contractSpecs[1].SpecificationId, "contractspec1recspec4"),
"contractspec1recspec4",
[]*types.InputSpecification{
types.NewInputSpecification(
"inputspec4", "inputspectypename4",
types.NewInputSpecificationSourceHash("sourcehash4"),
),
},
"typename4",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
),
}
for _, spec := range recordSpecs {
s.app.MetadataKeeper.SetRecordSpecification(s.ctx, *spec)
}
contractSpec0RecSpecIDs := []types.MetadataAddress{
recordSpecs[0].SpecificationId, recordSpecs[1].SpecificationId, recordSpecs[2].SpecificationId,
}
contractSpec1RecSpecIDs := []types.MetadataAddress{
recordSpecs[3].SpecificationId, recordSpecs[4].SpecificationId,
}
contractSpec2RecSpecIDs := []types.MetadataAddress{}
// Make sure all contract spec 0 record specs are iterated over
contractSpec0SpecIDsIterated := []types.MetadataAddress{}
errContractSpec0 := s.app.MetadataKeeper.IterateRecordSpecsForContractSpec(s.ctx, contractSpecs[0].SpecificationId, func(specID types.MetadataAddress) (stop bool) {
contractSpec0SpecIDsIterated = append(contractSpec0SpecIDsIterated, specID)
return false
})
s.Nil(errContractSpec0, "contract spec 0: should not have returned an error")
s.Equal(3, len(contractSpec0SpecIDsIterated), "contract spec 0: iteration count")
s.True(areEquivalentSetsOfMetaAddresses(contractSpec0RecSpecIDs, contractSpec0SpecIDsIterated),
"contract spec 0: iterated over unexpected record specs:\n expected: %v\n actual: %v\n",
contractSpec0RecSpecIDs, contractSpec0SpecIDsIterated)
// Make sure all contract spec 1 record specs are iterated over
contractSpec1SpecIDsIterated := []types.MetadataAddress{}
errContractSpec1 := s.app.MetadataKeeper.IterateRecordSpecsForContractSpec(s.ctx, contractSpecs[1].SpecificationId, func(specID types.MetadataAddress) (stop bool) {
contractSpec1SpecIDsIterated = append(contractSpec1SpecIDsIterated, specID)
return false
})
s.Nil(errContractSpec1, "contract spec 1: should not have returned an error")
s.Equal(2, len(contractSpec1SpecIDsIterated), "contract spec 1: iteration count")
s.True(areEquivalentSetsOfMetaAddresses(contractSpec1RecSpecIDs, contractSpec1SpecIDsIterated),
"contract spec 1: iterated over unexpected record specs:\n expected: %v\n actual: %v\n",
contractSpec1RecSpecIDs, contractSpec1SpecIDsIterated)
// Make sure no contract spec 2 record specs are iterated over
contractSpec2SpecIDsIterated := []types.MetadataAddress{}
errContractSpec2 := s.app.MetadataKeeper.IterateRecordSpecsForContractSpec(s.ctx, contractSpecs[2].SpecificationId, func(specID types.MetadataAddress) (stop bool) {
contractSpec2SpecIDsIterated = append(contractSpec2SpecIDsIterated, specID)
return false
})
s.Nil(errContractSpec2, "contract spec 2: should not have returned an error")
s.Equal(0, len(contractSpec2SpecIDsIterated), "contract spec 2: iteration count")
s.True(areEquivalentSetsOfMetaAddresses(contractSpec2RecSpecIDs, contractSpec2SpecIDsIterated),
"contract spec 2: iterated over unexpected record specs:\n expected: %v\n actual: %v\n",
contractSpec2RecSpecIDs, contractSpec2SpecIDsIterated)
// Make sure an unknown contract spec results in zero iterations.
unknownContractSpecID := types.ContractSpecMetadataAddress(uuid.New())
unknownContractSpecIDCount := 0
errUnknownContractSpecID := s.app.MetadataKeeper.IterateRecordSpecsForContractSpec(s.ctx, unknownContractSpecID, func(specID types.MetadataAddress) (stop bool) {
unknownContractSpecIDCount++
return false
})
s.Nil(errUnknownContractSpecID, "unknown contract spec id: should not have returned an error")
s.Equal(0, unknownContractSpecIDCount, "unknown contract spec id: iteration count")
// Make sure the stop bool is being recognized.
countStop := 0
errStop := s.app.MetadataKeeper.IterateRecordSpecsForContractSpec(s.ctx, contractSpecs[0].SpecificationId, func(specID types.MetadataAddress) (stop bool) {
countStop++
if countStop == 2 {
return true
}
return false
})
s.Nil(errStop, "stop bool: should not have returned an error")
s.Equal(2, countStop, "stop bool: iteration count")
}
func (s *SpecKeeperTestSuite) TestGetRecordSpecificationsForContractSpecificationID() {
// Create 3 contract specs.
contractSpecs := []*types.ContractSpecification{
types.NewContractSpecification(
types.ContractSpecMetadataAddress(uuid.New()),
types.NewDescription(
"TestIterateContractSpecsForOwner[0]",
"A description for a unit test contract specification - owner: user1",
"http://test.net",
"http://test.net/ico.png",
),
[]string{s.user1Addr.String()},
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
types.NewContractSpecificationSourceHash("somehash0"),
"someclass_0",
),
types.NewContractSpecification(
types.ContractSpecMetadataAddress(uuid.New()),
types.NewDescription(
"TestIterateContractSpecsForOwner[1]",
"A description for a unit test contract specification - owner: user2",
"http://test.net",
"http://test.net/ico.png",
),
[]string{s.user2Addr.String()},
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
types.NewContractSpecificationSourceHash("somehash1"),
"someclass_1",
),
types.NewContractSpecification(
types.ContractSpecMetadataAddress(uuid.New()),
types.NewDescription(
"TestIterateContractSpecsForOwner[2]",
"A description for a unit test contract specification - owner: user1, user2",
"http://test.net",
"http://test.net/ico.png",
),
[]string{s.user1Addr.String(), s.user2Addr.String()},
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
types.NewContractSpecificationSourceHash("somehash2"),
"someclass_2",
),
}
for _, spec := range contractSpecs {
s.app.MetadataKeeper.SetContractSpecification(s.ctx, *spec)
}
// Create 3 record specs for the 1st contract spec, 2 for the 2nd, and none for the 3rd.
recordSpecs := []*types.RecordSpecification{
types.NewRecordSpecification(
asRecSpecAddrOrPanic(contractSpecs[0].SpecificationId, "contractspec0recspec0"),
"contractspec0recspec0",
[]*types.InputSpecification{
types.NewInputSpecification(
"inputspec0", "inputspectypename0",
types.NewInputSpecificationSourceHash("sourcehash0"),
),
},
"typename0",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
),
types.NewRecordSpecification(
asRecSpecAddrOrPanic(contractSpecs[0].SpecificationId, "contractspec0recspec1"),
"contractspec0recspec1",
[]*types.InputSpecification{
types.NewInputSpecification(
"inputspec1", "inputspectypename1",
types.NewInputSpecificationSourceHash("sourcehash1"),
),
},
"typename1",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
),
types.NewRecordSpecification(
asRecSpecAddrOrPanic(contractSpecs[0].SpecificationId, "contractspec1recspec2"),
"contractspec0recspec2",
[]*types.InputSpecification{
types.NewInputSpecification(
"inputspec2", "inputspectypename2",
types.NewInputSpecificationSourceHash("sourcehash2"),
),
},
"typename2",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
),
types.NewRecordSpecification(
asRecSpecAddrOrPanic(contractSpecs[1].SpecificationId, "contractspec1recspec3"),
"contractspec1recspec3",
[]*types.InputSpecification{
types.NewInputSpecification(
"inputspec3", "inputspectypename3",
types.NewInputSpecificationSourceHash("sourcehash3"),
),
},
"typename3",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
),
types.NewRecordSpecification(
asRecSpecAddrOrPanic(contractSpecs[1].SpecificationId, "contractspec1recspec4"),
"contractspec1recspec4",
[]*types.InputSpecification{
types.NewInputSpecification(
"inputspec4", "inputspectypename4",
types.NewInputSpecificationSourceHash("sourcehash4"),
),
},
"typename4",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
),
}
for _, spec := range recordSpecs {
s.app.MetadataKeeper.SetRecordSpecification(s.ctx, *spec)
}
contractSpec0Expected := recordSpecs[0:3]
contractSpec1Expected := recordSpecs[3:5]
contractSpec2Expected := []*types.RecordSpecification{}
// Make sure all contract spec 0 record specs are returned
contractSpecs0Actual, contractSpecs0ActualErr := s.app.MetadataKeeper.GetRecordSpecificationsForContractSpecificationID(s.ctx, contractSpecs[0].SpecificationId)
s.Nil(contractSpecs0ActualErr, "contract spec 0: should not have returned an error")
s.True(areEquivalentSetsOfRecSpecs(contractSpec0Expected, contractSpecs0Actual),
"contract spec 0: unexpected record specs:\n expected: %v\n actual: %v\n",
contractSpec0Expected, contractSpecs0Actual)
// Make sure all contract spec 1 record specs are returned
contractSpecs1Actual, contractSpecs1ActualErr := s.app.MetadataKeeper.GetRecordSpecificationsForContractSpecificationID(s.ctx, contractSpecs[1].SpecificationId)
s.Nil(contractSpecs1ActualErr, "contract spec 1: should not have returned an error")
s.True(areEquivalentSetsOfRecSpecs(contractSpec1Expected, contractSpecs1Actual),
"contract spec 1: unexpected record specs:\n expected: %v\n actual: %v\n",
contractSpec1Expected, contractSpecs1Actual)
// Make sure all contract spec 2 record specs are returned (none)
contractSpecs2Actual, contractSpecs2ActualErr := s.app.MetadataKeeper.GetRecordSpecificationsForContractSpecificationID(s.ctx, contractSpecs[2].SpecificationId)
s.Nil(contractSpecs2ActualErr, "contract spec 2: should not have returned an error")
s.True(areEquivalentSetsOfRecSpecs(contractSpec2Expected, contractSpecs2Actual),
"contract spec 2: unexpected record specs:\n expected: %v\n actual: %v\n",
contractSpec2Expected, contractSpecs2Actual)
// Make sure an unknown contract spec returns empty.
unknownContractSpecID := types.ContractSpecMetadataAddress(uuid.New())
unknownContractSpecIDActual, unknownContractSpecIDActualErr := s.app.MetadataKeeper.GetRecordSpecificationsForContractSpecificationID(s.ctx, unknownContractSpecID)
s.Nil(unknownContractSpecIDActualErr, "unknown contract spec id: should not have returned an error")
s.Equal(0, len(unknownContractSpecIDActual), "unknown contract spec id: count")
}
func (s *SpecKeeperTestSuite) TestValidateRecordSpecUpdate() {
contractSpecUUIDOther := uuid.New()
tests := []struct {
name string
existing *types.RecordSpecification
proposed *types.RecordSpecification
want string
}{
{
"validate basic called on proposed",
nil,
types.NewRecordSpecification(
types.RecordSpecMetadataAddress(s.contractSpecUUID1, "name"),
"name",
[]*types.InputSpecification{},
"",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_SERVICER},
),
"record specification type name cannot be empty",
},
{
"validate basic not called on existing",
types.NewRecordSpecification(
types.RecordSpecMetadataAddress(s.contractSpecUUID1, "name"),
"name",
[]*types.InputSpecification{},
"", // should cause error if ValidateBasic called on it
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_SERVICER},
),
types.NewRecordSpecification(
types.RecordSpecMetadataAddress(s.contractSpecUUID1, "name"),
"name",
[]*types.InputSpecification{},
"typename",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_SERVICER},
),
"",
},
{
"SpecificationIDs must match",
types.NewRecordSpecification(
types.RecordSpecMetadataAddress(s.contractSpecUUID1, "foo"),
"foo",
[]*types.InputSpecification{},
"", // should cause error if ValidateBasic called on it
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_SERVICER},
),
types.NewRecordSpecification(
types.RecordSpecMetadataAddress(contractSpecUUIDOther, "foo"),
"foo",
[]*types.InputSpecification{},
"typename",
types.DefinitionType_DEFINITION_TYPE_RECORD,
[]types.PartyType{types.PartyType_PARTY_TYPE_SERVICER},
),
fmt.Sprintf("cannot update record spec identifier. expected %s, got %s",
types.RecordSpecMetadataAddress(s.contractSpecUUID1, "foo"),
types.RecordSpecMetadataAddress(contractSpecUUIDOther, "foo")),
},
// Names must match - cannot be tested. A changed name will change the spec id.
// So either ValidateBasic will catch that the hashed name doesn't match its part in the ID,
// or the ValidateRecordSpecUpdate will catch the changing specification id.
}
for _, tt := range tests {
tt := tt
s.T().Run(tt.name, func(t *testing.T) {
err := s.app.MetadataKeeper.ValidateRecordSpecUpdate(s.ctx, tt.existing, *tt.proposed)
if err != nil {
require.Equal(t, tt.want, err.Error(), "RecordSpec Keeper ValidateRecordSpecUpdate error")
} else if len(tt.want) > 0 {
t.Errorf("RecordSpec Keeper ValidateRecordSpecUpdate error = nil, expected: %s", tt.want)
}
})
}
}
func (s *SpecKeeperTestSuite) TestGetSetRemoveContractSpecification() {
newSpec := types.NewContractSpecification(
s.contractSpecID1,
types.NewDescription(
"TestGetSetRemoveContractSpecification",
"A description for a unit test contract specification",
"http://test.net",
"http://test.net/ico.png",
),
[]string{s.user1Addr.String()},
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
types.NewContractSpecificationSourceHash("somehash"),
"someclass",
)
require.NotNil(s.T(), newSpec, "test setup failure: NewContractSpecification should not return nil")
spec1, found1 := s.app.MetadataKeeper.GetContractSpecification(s.ctx, s.contractSpecID1)
s.False(found1, "1: get contract spec should return false before it has been saved")
s.NotNil(spec1, "1: get contract spec should always return a non-nil contract spec")
s.app.MetadataKeeper.SetContractSpecification(s.ctx, *newSpec)
spec2, found2 := s.app.MetadataKeeper.GetContractSpecification(s.ctx, s.contractSpecID1)
s.True(found2, "get contract spec should return true after it has been saved")
s.NotNil(spec2, "get contract spec should always return a non-nil contract spec")
s.Equal(s.contractSpecID1, spec2.SpecificationId, "2: get contract spec should return a spec containing id provided")
spec3, found3 := s.app.MetadataKeeper.GetContractSpecification(s.ctx, types.ContractSpecMetadataAddress(uuid.New()))
s.False(found3, "3: get contract spec should return false for an unknown address")
s.NotNil(spec3, "3: get contract spec should always return a non-nil contract spec")
remErr := s.app.MetadataKeeper.RemoveContractSpecification(s.ctx, newSpec.SpecificationId)
s.Nil(remErr, "4: remove should not return any error")
spec5, found5 := s.app.MetadataKeeper.GetContractSpecification(s.ctx, s.contractSpecID1)
s.False(found5, "5: get contract spec should return false after it has been removed")
s.NotNil(spec5, "5: get contract spec should always return a non-nil contract spec")
remErr2 := s.app.MetadataKeeper.RemoveContractSpecification(s.ctx, s.contractSpecID1)
s.Equal(
fmt.Errorf("contract specification with id %s not found", s.contractSpecID1),
remErr2,
"6: remove error message when not found",
)
}
func (s *SpecKeeperTestSuite) TestIterateContractSpecs() {
size := 10
specs := make([]*types.ContractSpecification, size)
for i := 0; i < size; i++ {
specs[i] = types.NewContractSpecification(
types.ContractSpecMetadataAddress(uuid.New()),
types.NewDescription(
fmt.Sprintf("TestIterateContractSpecs[%d]", i),
fmt.Sprintf("The description for entry [%d] in a unit test contract specification", i),
fmt.Sprintf("http://%d.test.net", i),
fmt.Sprintf("http://%d.test.net/ico.png", i),
),
[]string{s.user1Addr.String()},
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
types.NewContractSpecificationSourceHash(fmt.Sprintf("somehash%d", i)),
fmt.Sprintf("someclass_%d", i),
)
s.app.MetadataKeeper.SetContractSpecification(s.ctx, *specs[i])
}
visitedContractSpecIDs := make([]types.MetadataAddress, size)
count := 0
err1 := s.app.MetadataKeeper.IterateContractSpecs(s.ctx, func(spec types.ContractSpecification) (stop bool) {
if containsMetadataAddress(visitedContractSpecIDs, spec.SpecificationId) {
s.Fail("function IterateContractSpecs visited the same contract specification twice: %s", spec.SpecificationId.String())
}
visitedContractSpecIDs[count] = spec.SpecificationId
count++
return false
})
s.Nil(err1, "1: function IterateContractSpecs should not have returned an error")
s.Equal(size, count, "number of contract specs iterated through")
shortCount := 0
err2 := s.app.MetadataKeeper.IterateContractSpecs(s.ctx, func(spec types.ContractSpecification) (stop bool) {
shortCount++
if shortCount == 5 {
return true
}
return false
})
s.Nil(err2, "2: function IterateContractSpecs should not have returned an error")
s.Equal(5, shortCount, "function IterateContractSpecs ignored (stop bool) return value")
}
func (s *SpecKeeperTestSuite) TestIterateContractSpecsForOwner() {
// Create 5 contract specs. Two owned by user1, Two owned by user2, and One owned by both user1 and user2.
specs := make([]*types.ContractSpecification, 5)
user1SpecIDs := make([]types.MetadataAddress, 3)
user2SpecIDs := make([]types.MetadataAddress, 3)
specs[0] = types.NewContractSpecification(
types.ContractSpecMetadataAddress(uuid.New()),
types.NewDescription(
"TestIterateContractSpecsForOwner[0]",
"A description for a unit test contract specification - owner: user1",
"http://test.net",
"http://test.net/ico.png",
),
[]string{s.user1Addr.String()},
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
types.NewContractSpecificationSourceHash("somehash0"),
"someclass_0",
)
user1SpecIDs[0] = specs[0].SpecificationId
specs[1] = types.NewContractSpecification(
types.ContractSpecMetadataAddress(uuid.New()),
types.NewDescription(
"TestIterateContractSpecsForOwner[1]",
"A description for a unit test contract specification - owner: user2",
"http://test.net",
"http://test.net/ico.png",
),
[]string{s.user2Addr.String()},
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
types.NewContractSpecificationSourceHash("somehash1"),
"someclass_1",
)
user2SpecIDs[0] = specs[1].SpecificationId
specs[2] = types.NewContractSpecification(
types.ContractSpecMetadataAddress(uuid.New()),
types.NewDescription(
"TestIterateContractSpecsForOwner[2]",
"A description for a unit test contract specification - owner: user1",
"http://test.net",
"http://test.net/ico.png",
),
[]string{s.user1Addr.String()},
[]types.PartyType{types.PartyType_PARTY_TYPE_OWNER},
types.NewContractSpecificationSourceHash("somehash2"),
"someclass_2",
)
user1SpecIDs[1] = specs[2].SpecificationId
specs[3] = types.NewContractSpecification(
types.ContractSpecMetadataAddress(uuid.New()),
types.NewDescription(
"TestIterateContractSpecsForOwner[3]",
"A description for a unit test contract specification - owner: user2",
"http://test.net",