-
Notifications
You must be signed in to change notification settings - Fork 1
/
operations.go
804 lines (693 loc) · 25.5 KB
/
operations.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
package simulation
import (
"errors"
"fmt"
"math/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
"github.com/google/uuid"
"github.com/elesto-dao/elesto/v4/x/did"
"github.com/elesto-dao/elesto/v4/x/did/keeper"
)
var ownersAndIds map[string][]string
var (
TypeMsgCreateDidDocument = sdk.MsgTypeURL(&did.MsgCreateDidDocument{})
TypeMsgUpdateDidDocument = sdk.MsgTypeURL(&did.MsgUpdateDidDocument{})
TypeMsgAddVerification = sdk.MsgTypeURL(&did.MsgAddVerification{})
TypeMsgRevokeVerification = sdk.MsgTypeURL(&did.MsgRevokeVerification{})
TypeMsgSetVerificationRelationships = sdk.MsgTypeURL(&did.MsgSetVerificationRelationships{})
TypeMsgAddService = sdk.MsgTypeURL(&did.MsgAddService{})
TypeMsgDeleteService = sdk.MsgTypeURL(&did.MsgDeleteService{})
TypeMsgAddController = sdk.MsgTypeURL(&did.MsgAddController{})
TypeMsgDeleteController = sdk.MsgTypeURL(&did.MsgDeleteController{})
)
const (
opWeightMsgCreateDidDocument = "op_weight_msg_create_did_document"
opWeightMsgUpdateDidDocument = "op_weight_msg_update_did_document"
opWeightMsgAddVerification = "op_weight_msg_create_add_verification"
opWeightMsgRevokeVerification = "op_weight_msg_create_revoke_verification"
opWeightMsgSetVerificationRelationships = "op_weight_msg_create_set_verification_relationships"
opWeightMsgAddService = "op_weight_msg_create_add_service"
opWeightMsgDeleteService = "op_weight_msg_create_delete_service"
opWeightMsgAddController = "op_weight_msg_create_add_controller"
opWeightMsgDeleteController = "op_weight_msg_create_delete_controller"
// TODO: Determine the simulation weight value
defaultWeightMsgCreateDidDocument int = 100
defaultWeightMsgUpdateDidDocument int = 100
defaultWeightMsgAddVerification int = 100
defaultWeightMsgRevokeVerification int = 100
defaultWeightMsgSetVerificationRelationships int = 200
defaultWeightMsgAddService int = 100
defaultWeightMsgDeleteService int = 100
defaultWeightMsgAddController int = 100
defaultWeightMsgDeleteController int = 100
// this line is used by starport scaffolding # simapp/module/const
)
func init() {
ownersAndIds = make(map[string][]string)
}
// WeightedOperations returns the all the gov module operations with their respective weights.
func WeightedOperations(simState module.SimulationState, didKeeper keeper.Keeper, bk did.BankKeeper, ak did.AccountKeeper) []simtypes.WeightedOperation {
operations := make([]simtypes.WeightedOperation, 0)
var weightMsgCreateDidDocument int
simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgCreateDidDocument, &weightMsgCreateDidDocument, nil,
func(_ *rand.Rand) {
weightMsgCreateDidDocument = defaultWeightMsgCreateDidDocument
},
)
var weightMsgUpdateDidDocument int
simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgUpdateDidDocument, &weightMsgUpdateDidDocument, nil,
func(_ *rand.Rand) {
weightMsgUpdateDidDocument = defaultWeightMsgUpdateDidDocument
},
)
var weightMsgAddVerification int
simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgAddVerification, &weightMsgAddVerification, nil,
func(_ *rand.Rand) {
weightMsgAddVerification = defaultWeightMsgAddVerification
},
)
var weightMsgRevokeVerification int
simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgRevokeVerification, &weightMsgRevokeVerification, nil,
func(_ *rand.Rand) {
weightMsgRevokeVerification = defaultWeightMsgRevokeVerification
},
)
var weightMsgSetVerificationRelationships int
simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgSetVerificationRelationships, &weightMsgSetVerificationRelationships, nil,
func(_ *rand.Rand) {
weightMsgSetVerificationRelationships = defaultWeightMsgSetVerificationRelationships
},
)
var weightMsgAddService int
simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgAddService, &weightMsgAddService, nil,
func(_ *rand.Rand) {
weightMsgAddService = defaultWeightMsgAddService
},
)
var weightMsgDeleteService int
simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgDeleteService, &weightMsgDeleteService, nil,
func(_ *rand.Rand) {
weightMsgDeleteService = defaultWeightMsgDeleteService
},
)
var weightMsgAddController int
simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgAddController, &weightMsgAddController, nil,
func(_ *rand.Rand) {
weightMsgAddController = defaultWeightMsgAddController
},
)
var weightMsgDeleteController int
simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgDeleteController, &weightMsgDeleteController, nil,
func(_ *rand.Rand) {
weightMsgDeleteController = defaultWeightMsgDeleteController
},
)
operations = append(operations, simulation.NewWeightedOperation(
weightMsgCreateDidDocument,
SimulateMsgCreateDidDocument(didKeeper, bk, ak),
))
operations = append(operations, simulation.NewWeightedOperation(
weightMsgUpdateDidDocument,
SimulateMsgUpdateDidDocument(didKeeper, bk, ak),
))
operations = append(operations, simulation.NewWeightedOperation(
weightMsgAddVerification,
SimulateMsgAddVerification(didKeeper, bk, ak),
))
operations = append(operations, simulation.NewWeightedOperation(
weightMsgRevokeVerification,
SimulateMsgRevokeVerification(didKeeper, bk, ak),
))
operations = append(operations, simulation.NewWeightedOperation(
weightMsgSetVerificationRelationships,
SimulateMsgSetVerificationRelationships(didKeeper, bk, ak),
))
operations = append(operations, simulation.NewWeightedOperation(
weightMsgAddService,
SimulateMsgAddService(didKeeper, bk, ak),
))
operations = append(operations, simulation.NewWeightedOperation(
weightMsgDeleteService,
SimulateMsgDeleteService(didKeeper, bk, ak),
))
operations = append(operations, simulation.NewWeightedOperation(
weightMsgAddController,
SimulateMsgAddController(didKeeper, bk, ak),
))
operations = append(operations, simulation.NewWeightedOperation(
weightMsgDeleteController,
SimulateMsgDeleteController(didKeeper, bk, ak),
))
// this line is used by starport scaffolding # simapp/module/operation
return operations
}
// SimulateMsgCreateDidDocument simulates a MsgCreateDidDocument message
func SimulateMsgCreateDidDocument(k keeper.Keeper, bk did.BankKeeper, ak did.AccountKeeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
didOwner, _ := simtypes.RandomAcc(r, accs)
ownerAddress := didOwner.Address.String()
id := genRandomUUID(r, ownerAddress)
didID := did.NewChainDID(ctx.ChainID(), id)
vmID := didID.NewVerificationMethodID(ownerAddress)
vmType := did.EcdsaSecp256k1VerificationKey2019
vm := did.NewVerification(
did.NewVerificationMethod(
vmID,
didID,
did.NewPublicKeyMultibase(didOwner.PubKey.Bytes()),
vmType,
),
[]string{did.Authentication},
nil,
)
_, found := k.GetDidDocument(ctx, []byte(didID))
_, _ = did.NewDidDocument(didID.String(), did.WithVerifications(vm))
msg := did.NewMsgCreateDidDocument(
didID.String(),
did.Verifications{vm},
did.Services{},
ownerAddress,
)
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: msg,
MsgType: TypeMsgCreateDidDocument,
Context: ctx,
SimAccount: didOwner,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: did.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
opMsg, fOp, err := simulation.GenAndDeliverTxWithRandFees(txCtx)
if found {
// return an error that will not stop simulation as the did was found
return simtypes.NoOpMsg(
did.ModuleName,
TypeMsgCreateDidDocument,
"did found, could not create did",
), nil, nil
}
return opMsg, fOp, err
}
}
// SimulateMsgUpdateDidDocument simulates a MsgUpdateDidDocument message
func SimulateMsgUpdateDidDocument(k keeper.Keeper, bk did.BankKeeper, ak did.AccountKeeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
didOwner, _ := simtypes.RandomAcc(r, accs)
ownerAddress := didOwner.Address.String()
id := returnRandomUUID(r, ownerAddress)
didID := did.NewChainDID(ctx.ChainID(), id)
// build vm 1
vmKey, _ := simtypes.RandomAcc(r, accs)
keyAddress := vmKey.Address.String()
didvmID := returnRandomUUID(r, keyAddress)
didvmkeyID := did.NewChainDID(ctx.ChainID(), didvmID)
vmkeyID := didvmkeyID.NewVerificationMethodID(keyAddress)
vmkeyType := did.EcdsaSecp256k1VerificationKey2019
vm := did.NewVerification(
did.NewVerificationMethod(
vmkeyID,
didID,
did.NewPublicKeyMultibase(vmKey.PubKey.Bytes()),
vmkeyType,
),
[]string{did.Authentication},
nil,
)
// build vm 2
vmKey2, _ := simtypes.RandomAcc(r, accs)
accAddress2 := sdk.AccAddress(vmKey2.PubKey.Address())
vmID2 := didID.NewVerificationMethodID(accAddress2.String())
vmType2 := did.CosmosAccountAddress
vm2 := did.NewVerification(
did.NewVerificationMethod(
vmID2,
didID,
did.NewBlockchainAccountID("elesto", accAddress2.String()),
vmType2,
),
[]string{did.KeyAgreement, did.AssertionMethod, did.Authentication},
nil,
)
// create services to add to did doc
service1 := did.NewService(fmt.Sprint("service:emti-agent1", r.Int()), "DIDComm", "https://agents.elesto.app.beta.starport.cloud/emti")
service2 := did.NewService(fmt.Sprint("service:emti-agent", r.Int()), "DIDComm", "https://agents.elesto.app.beta.starport.cloud/emti")
service3 := did.NewService(fmt.Sprint("service:emti-agent", r.Int()), "DIDComm", "https://agents.elesto.app.beta.starport.cloud/emti")
// create controllers to add to did doc
controller, _ := simtypes.RandomAcc(r, accs)
controllerAddress := sdk.AccAddress(controller.PubKey.Address())
controllerDidID := did.NewKeyDID(controllerAddress.String())
// update the did document
didDoc, found := k.GetDidDocument(ctx, []byte(didID))
if err := didDoc.AddControllers(controllerDidID.String()); err != nil {
return simtypes.NoOpMsg(
did.ModuleName,
TypeMsgUpdateDidDocument,
"did not found, could not add controller",
), nil, err
}
if err := didDoc.AddVerifications(vm, vm2); err != nil {
if errors.Is(err, did.ErrInvalidInput) {
return simtypes.NoOpMsg(did.ModuleName, TypeMsgUpdateDidDocument,
"duplicate verification methods",
), nil, nil
}
return simtypes.NoOpMsg(did.ModuleName, TypeMsgUpdateDidDocument,
"did not found, could not add vm",
), nil, err
}
if err := didDoc.AddServices(service1, service2, service3); err != nil {
return simtypes.NoOpMsg(
did.ModuleName,
TypeMsgUpdateDidDocument,
"did not found, could not add service",
), nil, err
}
msg := did.NewMsgUpdateDidDocument(
&didDoc,
ownerAddress,
)
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: msg,
MsgType: TypeMsgUpdateDidDocument,
Context: ctx,
SimAccount: didOwner,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: did.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
opMsg, fOp, err := simulation.GenAndDeliverTxWithRandFees(txCtx)
if !found {
// return an error that will not stop simulation as the did was not found
return simtypes.NoOpMsg(
did.ModuleName,
TypeMsgUpdateDidDocument,
"did not found, could not add verification method",
), nil, nil
}
return opMsg, fOp, err
}
}
// SimulateMsgAddVerification simulates a MsgAddVerification message
func SimulateMsgAddVerification(k keeper.Keeper, bk did.BankKeeper, ak did.AccountKeeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
didOwner, _ := simtypes.RandomAcc(r, accs)
ownerAddress := didOwner.Address.String()
vmKey, _ := simtypes.RandomAcc(r, accs)
keyAddress := vmKey.Address.String()
id := returnRandomUUID(r, ownerAddress)
didID := did.NewChainDID(ctx.ChainID(), id)
vmID := returnRandomUUID(r, keyAddress)
didvmkeyID := did.NewChainDID(ctx.ChainID(), vmID)
vmkeyID := didvmkeyID.NewVerificationMethodID(keyAddress)
vmkeyType := did.EcdsaSecp256k1VerificationKey2019
didDoc, found := k.GetDidDocument(ctx, []byte(didID))
for _, vm := range didDoc.VerificationMethod {
if vmkeyID == vm.Id {
return simtypes.NoOpMsg(
did.ModuleName,
TypeMsgAddVerification,
"vm already exists, could not add verification method",
), nil, nil
}
}
vm := did.NewVerification(
did.NewVerificationMethod(
vmkeyID,
didID,
did.NewPublicKeyMultibase(vmKey.PubKey.Bytes()),
vmkeyType,
),
[]string{did.Authentication},
nil,
)
msg := did.NewMsgAddVerification(
didID.String(),
vm,
ownerAddress,
)
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: msg,
MsgType: TypeMsgAddVerification,
Context: ctx,
SimAccount: didOwner,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: did.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
opMsg, fOp, err := simulation.GenAndDeliverTxWithRandFees(txCtx)
if !found {
// return an error that will not stop simulation as the did was not found or the verification method already exists
return simtypes.NoOpMsg(
did.ModuleName,
TypeMsgAddVerification,
"did not found, could not add verification method",
), nil, nil
}
return opMsg, fOp, err
}
}
// SimulateMsgRevokeVerification simulates a MsgRevokeVerification message
func SimulateMsgRevokeVerification(k keeper.Keeper, bk did.BankKeeper, ak did.AccountKeeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
didOwner, _ := simtypes.RandomAcc(r, accs)
ownerAddress := didOwner.Address.String()
id := returnRandomUUID(r, ownerAddress)
didID := did.NewChainDID(ctx.ChainID(), id)
didDoc, found := k.GetDidDocument(ctx, []byte(didID))
vm := didDoc.VerificationMethod
// return an error that will not stop simulation as the did was not found or the verification method does not exists
if !found {
return simtypes.NoOpMsg(
did.ModuleName,
TypeMsgRevokeVerification,
"did not found, could not remove verification method",
), nil, nil
}
// return an error that will not stop simulation if the length of the VM array is 1
if len(vm) == 1 {
return simtypes.NoOpMsg(
did.ModuleName,
TypeMsgRevokeVerification,
"could not remove verification method as it would break the did document",
), nil, nil
}
msg := did.NewMsgRevokeVerification(
didID.String(),
vm[1].Id,
ownerAddress,
)
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: msg,
MsgType: TypeMsgRevokeVerification,
Context: ctx,
SimAccount: didOwner,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: did.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
opMsg, fOp, err := simulation.GenAndDeliverTxWithRandFees(txCtx)
return opMsg, fOp, err
}
}
// SimulateMsgSetVerificationRelationships simulates a MsgSetVerificationRelationships message
func SimulateMsgSetVerificationRelationships(k keeper.Keeper, bk did.BankKeeper, ak did.AccountKeeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
didOwner, _ := simtypes.RandomAcc(r, accs)
ownerAddress := didOwner.Address.String()
id := returnRandomUUID(r, ownerAddress)
didID := did.NewChainDID(ctx.ChainID(), id)
didDoc, found := k.GetDidDocument(ctx, []byte(didID))
vm := didDoc.VerificationMethod
// return an error that will not stop simulation as the did was not found or the verification method does not exists
if !found {
return simtypes.NoOpMsg(
did.ModuleName,
TypeMsgSetVerificationRelationships,
"did not found, could not remove verification method",
), nil, nil
}
// return an error that will not stop simulation if the length of the VM array is 1
if len(vm) == 1 {
return simtypes.NoOpMsg(
did.ModuleName,
TypeMsgSetVerificationRelationships,
"could not remove verification method as it would break the did document",
), nil, nil
}
msg := did.NewMsgSetVerificationRelationships(
didID.String(),
vm[1].Id,
[]string{did.KeyAgreement, did.CapabilityDelegation},
ownerAddress,
)
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: msg,
MsgType: TypeMsgSetVerificationRelationships,
Context: ctx,
SimAccount: didOwner,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: did.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
}
}
// SimulateMsgAddService simulates a MsgAddService message
func SimulateMsgAddService(k keeper.Keeper, bk did.BankKeeper, ak did.AccountKeeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
didOwner, _ := simtypes.RandomAcc(r, accs)
ownerAddress := didOwner.Address.String()
id := returnRandomUUID(r, ownerAddress)
didID := did.NewChainDID(ctx.ChainID(), id)
didDoc, found := k.GetDidDocument(ctx, []byte(didID))
serviceID := "service:emtiagent" + fmt.Sprint(len(didDoc.Service))
serviceType := "DIDComm"
serviceURL := "https://agents.elesto.app.beta.starport.cloud/emti"
newService := did.NewService(serviceID, serviceType, serviceURL)
msg := did.NewMsgAddService(
didID.String(),
newService,
ownerAddress,
)
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: msg,
MsgType: TypeMsgAddService,
Context: ctx,
SimAccount: didOwner,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: did.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
opMsg, fOp, err := simulation.GenAndDeliverTxWithRandFees(txCtx)
// return an error that will not stop simulation as the did was not found
if !found {
return simtypes.NoOpMsg(
did.ModuleName,
TypeMsgAddService,
"did not found, could not add service",
), nil, nil
}
return opMsg, fOp, err
}
}
// SimulateMsgDeleteService simulates a MsgDeleteService message
func SimulateMsgDeleteService(k keeper.Keeper, bk did.BankKeeper, ak did.AccountKeeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
didOwner, _ := simtypes.RandomAcc(r, accs)
ownerAddress := didOwner.Address.String()
id := returnRandomUUID(r, ownerAddress)
didID := did.NewChainDID(ctx.ChainID(), id)
didDoc, found := k.GetDidDocument(ctx, []byte(didID))
service := didDoc.Service
serviceID := "service:emtiagent" + fmt.Sprint(len(didDoc.Service))
msg := did.NewMsgDeleteService(
didID.String(),
serviceID,
ownerAddress,
)
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: msg,
MsgType: TypeMsgDeleteService,
Context: ctx,
SimAccount: didOwner,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: did.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
opMsg, fOp, err := simulation.GenAndDeliverTxWithRandFees(txCtx)
// return an error that will not stop simulation as the did was not found
if !found {
return simtypes.NoOpMsg(
did.ModuleName,
TypeMsgDeleteService,
"did not found, could not remove service",
), nil, nil
}
// return an error that will not stop simulation if the length of the Service array is 0
if len(service) == 0 {
return simtypes.NoOpMsg(
did.ModuleName,
TypeMsgDeleteService,
"could not remove verification method as it would break the did document",
), nil, nil
}
return opMsg, fOp, err
}
}
// SimulateMsgAddController simulates a MsgAddController message
func SimulateMsgAddController(k keeper.Keeper, bk did.BankKeeper, ak did.AccountKeeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
didOwner, _ := simtypes.RandomAcc(r, accs)
ownerAddress := didOwner.Address.String()
didController, _ := simtypes.RandomAcc(r, accs)
controllerAddress := didController.Address.String()
id := returnRandomUUID(r, ownerAddress)
didID := did.NewChainDID(ctx.ChainID(), id)
controllerDidID := did.NewKeyDID(controllerAddress)
_, found := k.GetDidDocument(ctx, []byte(didID))
msg := did.NewMsgAddController(
didID.String(),
controllerDidID.String(),
ownerAddress,
)
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: msg,
MsgType: TypeMsgAddController,
Context: ctx,
SimAccount: didOwner,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: did.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
opMsg, fOp, err := simulation.GenAndDeliverTxWithRandFees(txCtx)
// return an error that will not stop simulation as the did was not found
if !found {
return simtypes.NoOpMsg(
did.ModuleName,
TypeMsgAddController,
"did not found, could not add controller",
), nil, nil
}
return opMsg, fOp, err
}
}
// SimulateMsgDeleteController simulates a MsgDeleteController message
func SimulateMsgDeleteController(k keeper.Keeper, bk did.BankKeeper, ak did.AccountKeeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
didOwner, _ := simtypes.RandomAcc(r, accs)
ownerAddress := didOwner.Address.String()
didController, _ := simtypes.RandomAcc(r, accs)
controllerAddress := didController.Address.String()
id := returnRandomUUID(r, ownerAddress)
didID := did.NewChainDID(ctx.ChainID(), id)
controllerDidID := did.NewKeyDID(controllerAddress)
msg := did.NewMsgDeleteController(
didID.String(),
controllerDidID.String(),
ownerAddress,
)
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: msg,
MsgType: TypeMsgDeleteController,
Context: ctx,
SimAccount: didOwner,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: did.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
opMsg, fOp, err := simulation.GenAndDeliverTxWithRandFees(txCtx)
didDoc, found := k.GetDidDocument(ctx, []byte(didID))
// return an error that will not stop simulation as the did was not found
if !found {
return simtypes.NoOpMsg(
did.ModuleName,
TypeMsgDeleteController,
"did not found, could not remove service",
), nil, nil
}
controller := didDoc.Controller
// return an error that will not stop simulation if the length of the Controller array is 0
if len(controller) == 0 {
return simtypes.NoOpMsg(
did.ModuleName,
TypeMsgDeleteController,
"could not remove verification method as it would break the did document",
), nil, nil
}
return opMsg, fOp, err
}
}
// stores owners and ids for msg create did
func genRandomUUID(r *rand.Rand, acc string) string {
if _, found := ownersAndIds[acc]; !found {
id := uuid.New().String()
ownersAndIds[acc] = append(ownersAndIds[acc], id)
return id
}
idx := r.Intn(2 * len(ownersAndIds[acc]))
if idx >= len(ownersAndIds[acc]) {
id := uuid.New().String()
ownersAndIds[acc] = append(ownersAndIds[acc], id)
return id
}
return ownersAndIds[acc][idx]
}
// return random uuid or uuid from store based on acc
func returnRandomUUID(r *rand.Rand, acc string) string {
if _, found := ownersAndIds[acc]; !found {
return uuid.New().String()
}
idx := r.Intn(2 * len(ownersAndIds[acc]))
if idx >= len(ownersAndIds[acc]) {
return uuid.New().String()
}
return ownersAndIds[acc][idx]
}