/
operations_relationships.go
137 lines (114 loc) · 4.35 KB
/
operations_relationships.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
package simulation
// DONTCOVER
import (
"math/rand"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/desmos-labs/desmos/v7/testutil/simtesting"
subspacessim "github.com/desmos-labs/desmos/v7/x/subspaces/simulation"
"github.com/desmos-labs/desmos/v7/x/relationships/keeper"
"github.com/desmos-labs/desmos/v7/x/relationships/types"
)
// SimulateMsgCreateRelationship tests and runs a single MsgCreateRelationship
func SimulateMsgCreateRelationship(
k keeper.Keeper, sk types.SubspacesKeeper, ak authkeeper.AccountKeeper, bk bankkeeper.Keeper,
) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
) (OperationMsg simtypes.OperationMsg, futureOps []simtypes.FutureOperation, err error) {
acc, relationship, skip := randomCreateRelationshipFields(r, ctx, accs, k, sk)
if skip {
return simtypes.NoOpMsg(types.RouterKey, "MsgCreateRelationship", "skip"), nil, nil
}
msg := types.NewMsgCreateRelationship(relationship.Creator, relationship.Counterparty, relationship.SubspaceID)
return simtesting.SendMsg(r, app, ak, bk, msg, ctx, acc)
}
}
// randomCreateRelationshipFields returns the data used to build a random MsgCreateRelationship
func randomCreateRelationshipFields(
r *rand.Rand, ctx sdk.Context, accs []simtypes.Account, k keeper.Keeper, sk types.SubspacesKeeper,
) (sender simtypes.Account, relationship types.Relationship, skip bool) {
// Get a sender
if len(accs) == 0 {
skip = true
return
}
sender, _ = simtypes.RandomAcc(r, accs)
senderAddr := sender.Address.String()
// Get a receiver
receiver, _ := simtypes.RandomAcc(r, accs)
receiverAddr := receiver.Address.String()
// Get a subspace
subspaces := sk.GetAllSubspaces(ctx)
if len(subspaces) == 0 {
// Skip because there are no subspaces
skip = true
return
}
subspace := subspacessim.RandomSubspace(r, subspaces)
subspaceID := subspace.ID
// Skip if the sender and receiver are equals
if senderAddr == receiverAddr {
skip = true
return
}
// Skip if the receiver has blocked the sender
if k.HasUserBlocked(ctx, receiverAddr, senderAddr, subspaceID) {
skip = true
return
}
// Skip if relationships already exists
if k.HasRelationship(ctx, senderAddr, receiverAddr, subspaceID) {
skip = true
return
}
return sender, types.NewRelationship(senderAddr, receiverAddr, subspaceID), false
}
// --------------------------------------------------------------------------------------------------------------------
// SimulateMsgDeleteRelationship tests and runs a single MsgDeleteRelationship
func SimulateMsgDeleteRelationship(
k keeper.Keeper, ak authkeeper.AccountKeeper, bk bankkeeper.Keeper,
) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (OperationMsg simtypes.OperationMsg, futureOps []simtypes.FutureOperation, err error) {
acc, counterparty, subspace, skip := randomDeleteRelationshipFields(r, ctx, accs, k)
if skip {
return simtypes.NoOpMsg(types.RouterKey, "MsgDeleteRelationship", "skip"), nil, nil
}
msg := types.NewMsgDeleteRelationship(acc.Address.String(), counterparty, subspace)
return simtesting.SendMsg(r, app, ak, bk, msg, ctx, acc)
}
}
// randomDeleteRelationshipFields returns the data used to build a random MsgDeleteRelationship
func randomDeleteRelationshipFields(
r *rand.Rand, ctx sdk.Context, accs []simtypes.Account, k keeper.Keeper,
) (user simtypes.Account, counterparty string, subspace uint64, skip bool) {
// Get a user
if len(accs) == 0 {
skip = true
return
}
user, _ = simtypes.RandomAcc(r, accs)
userAddr := user.Address.String()
// Get the user relationships
var relationships []types.Relationship
k.IterateRelationships(ctx, func(_ int64, relationship types.Relationship) (stop bool) {
if relationship.Creator == userAddr {
relationships = append(relationships, relationship)
}
return false
})
if len(relationships) == 0 {
// Skip because there are no relationships
skip = true
return
}
// Get a random relationship
relationship := RandomRelationship(r, relationships)
return user, relationship.Counterparty, relationship.SubspaceID, false
}