Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(did): simulation modifications #240

Merged
merged 2 commits into from
Sep 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 69 additions & 12 deletions x/did/simulation/operations.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package simulation

import (
"errors"
"fmt"
"math/rand"

Expand All @@ -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{})
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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]
}