/
operations_polls.go
128 lines (104 loc) · 3.86 KB
/
operations_polls.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
package simulation
// DONTCOVER
import (
"math/rand"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
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"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/desmos-labs/desmos/v2/x/staging/posts/keeper"
"github.com/desmos-labs/desmos/v2/x/staging/posts/types"
)
// SimulateMsgAnswerToPoll tests and runs a single msg poll answer where the answering user account already exists
// nolint: funlen
func SimulateMsgAnswerToPoll(
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,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
acc, answers, postID, skip := randomPollAnswerFields(r, ctx, accs, k, ak)
if skip {
return simtypes.NoOpMsg(types.RouterKey, types.ModuleName, "MsgAnswerToPoll"), nil, nil
}
msg := types.NewMsgAnswerPoll(postID, answers, acc.Address.String())
err := sendMsgAnswerPoll(r, app, ak, bk, msg, ctx, chainID, []cryptotypes.PrivKey{acc.PrivKey})
if err != nil {
return simtypes.NoOpMsg(types.RouterKey, types.ModuleName, "MsgAnswerToPoll"), nil, err
}
return simtypes.NewOperationMsg(msg, true, "MsgAnswerToPoll", nil), nil, nil
}
}
// sendMsgAnswerPoll sends a transaction with a MsgAnswerPoll from a provided random account.
func sendMsgAnswerPoll(
r *rand.Rand, app *baseapp.BaseApp, ak authkeeper.AccountKeeper, bk bankkeeper.Keeper,
msg *types.MsgAnswerPoll, ctx sdk.Context, chainID string, privkeys []cryptotypes.PrivKey,
) error {
addr, _ := sdk.AccAddressFromBech32(msg.Answerer)
account := ak.GetAccount(ctx, addr)
coins := bk.SpendableCoins(ctx, account.GetAddress())
fees, err := simtypes.RandomFees(r, ctx, coins)
if err != nil {
return err
}
txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
DefaultGasValue,
chainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
privkeys...,
)
if err != nil {
return err
}
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
if err != nil {
return err
}
return nil
}
// randomPollAnswerFields returns the data used to create a MsgAnswerPoll message
func randomPollAnswerFields(
r *rand.Rand, ctx sdk.Context, accs []simtypes.Account, k keeper.Keeper, ak authkeeper.AccountKeeper,
) (simtypes.Account, []string, string, bool) {
posts := k.GetPosts(ctx)
if len(posts) == 0 {
// Skip cause there are no posts
return simtypes.Account{}, nil, "", true
}
post, _ := RandomPost(r, posts)
// Skip the operation without any error if there is no poll, or the poll is closed
if post.Poll == nil || post.Poll.EndDate.Before(ctx.BlockTime()) {
return simtypes.Account{}, nil, "", true
}
simAccount, _ := simtypes.RandomAcc(r, accs)
acc := ak.GetAccount(ctx, simAccount.Address)
// Skip the operation without error as the account is not valid
if acc == nil {
return simtypes.Account{}, nil, "", true
}
// Skip the operation without err as the poll does not allow to edit answers
_, found := k.GetUserAnswer(ctx, post.PostID, acc.GetAddress().String())
if found && !post.Poll.AllowsAnswerEdits {
return simtypes.Account{}, nil, "", true
}
providedAnswers := post.Poll.ProvidedAnswers
answersLength := 1
if post.Poll.AllowsMultipleAnswers {
answersLength = r.Intn(len(post.Poll.ProvidedAnswers)) + 1 // At least one answer is necessary
}
answers := make([]string, answersLength)
for i := 0; i < answersLength; i++ {
answers[i] = providedAnswers[i].ID
}
return simAccount, answers, post.PostID, false
}