diff --git a/x/did/simulation/operations.go b/x/did/simulation/operations.go index 964b742a..ed99940a 100644 --- a/x/did/simulation/operations.go +++ b/x/did/simulation/operations.go @@ -1,6 +1,7 @@ package simulation import ( + "errors" "fmt" "math/rand" @@ -16,6 +17,8 @@ import ( "github.com/elesto-dao/elesto/v2/x/did/keeper" ) +var ownersAndIds map[string][]string + var ( TypeMsgCreateDidDocument = sdk.MsgTypeURL(&did.MsgCreateDidDocument{}) TypeMsgUpdateDidDocument = sdk.MsgTypeURL(&did.MsgUpdateDidDocument{}) @@ -53,6 +56,10 @@ const ( // 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) @@ -177,8 +184,8 @@ func SimulateMsgCreateDidDocument(k keeper.Keeper, bk did.BankKeeper, ak did.Acc ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { didOwner, _ := simtypes.RandomAcc(r, accs) ownerAddress := didOwner.Address.String() - id := uuid.New() - didID := did.NewChainDID(ctx.ChainID(), id.String()) + id := genRandomUUID(r, ownerAddress) + didID := did.NewChainDID(ctx.ChainID(), id) vmID := didID.NewVerificationMethodID(ownerAddress) vmType := did.EcdsaSecp256k1VerificationKey2019 vm := did.NewVerification( @@ -237,12 +244,15 @@ func SimulateMsgUpdateDidDocument(k keeper.Keeper, bk did.BankKeeper, ak did.Acc ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { didOwner, _ := simtypes.RandomAcc(r, accs) ownerAddress := didOwner.Address.String() - didID := did.NewChainDID(ctx.ChainID(), ownerAddress) + id := returnRandomUUID(r, ownerAddress) + didID := did.NewChainDID(ctx.ChainID(), id) // build vm 1 vmKey, _ := simtypes.RandomAcc(r, accs) keyAddress := vmKey.Address.String() - didvmkeyID := did.NewChainDID(ctx.ChainID(), keyAddress) + + didvmID := returnRandomUUID(r, keyAddress) + didvmkeyID := did.NewChainDID(ctx.ChainID(), didvmID) vmkeyID := didvmkeyID.NewVerificationMethodID(keyAddress) vmkeyType := did.EcdsaSecp256k1VerificationKey2019 vm := did.NewVerification( @@ -293,6 +303,12 @@ func SimulateMsgUpdateDidDocument(k keeper.Keeper, bk did.BankKeeper, ak did.Acc } 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 @@ -352,9 +368,11 @@ func SimulateMsgAddVerification(k keeper.Keeper, bk did.BankKeeper, ak did.Accou vmKey, _ := simtypes.RandomAcc(r, accs) keyAddress := vmKey.Address.String() - didID := did.NewChainDID(ctx.ChainID(), ownerAddress) + id := returnRandomUUID(r, ownerAddress) + didID := did.NewChainDID(ctx.ChainID(), id) - didvmkeyID := did.NewChainDID(ctx.ChainID(), keyAddress) + vmID := returnRandomUUID(r, keyAddress) + didvmkeyID := did.NewChainDID(ctx.ChainID(), vmID) vmkeyID := didvmkeyID.NewVerificationMethodID(keyAddress) vmkeyType := did.EcdsaSecp256k1VerificationKey2019 @@ -423,7 +441,8 @@ func SimulateMsgRevokeVerification(k keeper.Keeper, bk did.BankKeeper, ak did.Ac ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { didOwner, _ := simtypes.RandomAcc(r, accs) ownerAddress := didOwner.Address.String() - didID := did.NewChainDID(ctx.ChainID(), ownerAddress) + id := returnRandomUUID(r, ownerAddress) + didID := did.NewChainDID(ctx.ChainID(), id) didDoc, found := k.GetDidDocument(ctx, []byte(didID)) vm := didDoc.VerificationMethod @@ -478,7 +497,8 @@ func SimulateMsgSetVerificationRelationships(k keeper.Keeper, bk did.BankKeeper, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { didOwner, _ := simtypes.RandomAcc(r, accs) ownerAddress := didOwner.Address.String() - didID := did.NewChainDID(ctx.ChainID(), ownerAddress) + id := returnRandomUUID(r, ownerAddress) + didID := did.NewChainDID(ctx.ChainID(), id) didDoc, found := k.GetDidDocument(ctx, []byte(didID)) vm := didDoc.VerificationMethod @@ -533,7 +553,8 @@ func SimulateMsgAddService(k keeper.Keeper, bk did.BankKeeper, ak did.AccountKee ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { didOwner, _ := simtypes.RandomAcc(r, accs) ownerAddress := didOwner.Address.String() - didID := did.NewChainDID(ctx.ChainID(), ownerAddress) + 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)) @@ -584,7 +605,9 @@ func SimulateMsgDeleteService(k keeper.Keeper, bk did.BankKeeper, ak did.Account ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { didOwner, _ := simtypes.RandomAcc(r, accs) ownerAddress := didOwner.Address.String() - didID := did.NewChainDID(ctx.ChainID(), ownerAddress) + id := returnRandomUUID(r, ownerAddress) + didID := did.NewChainDID(ctx.ChainID(), id) + didDoc, found := k.GetDidDocument(ctx, []byte(didID)) service := didDoc.Service @@ -644,7 +667,8 @@ func SimulateMsgAddController(k keeper.Keeper, bk did.BankKeeper, ak did.Account didController, _ := simtypes.RandomAcc(r, accs) controllerAddress := didController.Address.String() - didID := did.NewChainDID(ctx.ChainID(), ownerAddress) + id := returnRandomUUID(r, ownerAddress) + didID := did.NewChainDID(ctx.ChainID(), id) controllerDidID := did.NewKeyDID(controllerAddress) _, found := k.GetDidDocument(ctx, []byte(didID)) @@ -694,7 +718,8 @@ func SimulateMsgDeleteController(k keeper.Keeper, bk did.BankKeeper, ak did.Acco didController, _ := simtypes.RandomAcc(r, accs) controllerAddress := didController.Address.String() - didID := did.NewChainDID(ctx.ChainID(), ownerAddress) + id := returnRandomUUID(r, ownerAddress) + didID := did.NewChainDID(ctx.ChainID(), id) controllerDidID := did.NewKeyDID(controllerAddress) msg := did.NewMsgDeleteController( @@ -745,3 +770,35 @@ func SimulateMsgDeleteController(k keeper.Keeper, bk did.BankKeeper, ak did.Acco 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] +}