-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgs.go
180 lines (144 loc) · 6.01 KB
/
msgs.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
package simulation
import (
"fmt"
"math/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/simulation"
"github.com/coinexchain/cet-sdk/modules/asset"
"github.com/coinexchain/cet-sdk/modules/comment"
"github.com/coinexchain/cet-sdk/modules/comment/internal/keepers"
"github.com/coinexchain/cet-sdk/modules/comment/internal/types"
simulationx "github.com/coinexchain/cet-sdk/simulation"
)
// TODO
func SimulateCreateNewThread(k keepers.Keeper, ask asset.Keeper, ak auth.AccountKeeper) simulation.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) (
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {
//create new token comment msg
msg, err := createNewThread(r, ctx, k, ask, ak, accs)
if err != nil {
return simulation.NoOpMsg(types.ModuleName), nil, nil
}
//get #token-comment
lastCommnet := k.GetCommentCount(ctx, msg.Token)
//handle msg
handler := comment.NewHandler(k)
ok := simulationx.SimulateHandleMsg(msg, handler, ctx)
if !ok {
return simulation.NewOperationMsg(msg, ok, ""), nil, nil
}
//verify msg is correctly handled
ok = verifyCreateNewThread(ctx, k, msg, lastCommnet)
if !ok {
return simulation.NewOperationMsg(msg, ok, ""), nil, fmt.Errorf("new token comment creation failed")
}
return simulation.NewOperationMsg(msg, ok, ""), nil, nil
}
}
func createNewThread(r *rand.Rand, ctx sdk.Context, k keepers.Keeper, ask asset.Keeper, ak auth.AccountKeeper, accs []simulation.Account) (types.MsgCommentToken, error) {
fromAcc := simulation.RandomAcc(r, accs)
//randomly select token to comment
token := randomToken(r, ctx, ask)
if token == nil {
return types.MsgCommentToken{}, fmt.Errorf("no token to comment")
}
//randomly select amount of cet to donate
donation := simulationx.RandomCET(r, ctx, ak, fromAcc)
//generate title
title := randomUTF8OrBytes(r, r.Intn(types.MaxTitleSize), true)
//randomly generate content & contentType
contentType, content := randomContent(r)
//construct msgCommentToken to create a new comment
msg := types.NewMsgCommentToken(fromAcc.Address, token.GetSymbol(), donation, string(title), string(content), contentType, nil)
if msg.ValidateBasic() != nil {
return types.MsgCommentToken{}, fmt.Errorf("msg expected to pass validation check")
}
return *msg, nil
}
func verifyCreateNewThread(ctx sdk.Context, k keepers.Keeper, msg types.MsgCommentToken, lastComment uint64) bool {
comment := k.GetCommentCount(ctx, msg.Token)
return comment == lastComment+1
}
func SimulateCreateCommentRefs(k keepers.Keeper, ask asset.Keeper, ak auth.AccountKeeper) simulation.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) (
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) {
//create new token comment msg
msg, err := createCommentReferences(r, ctx, k, ask, ak, accs)
if err != nil {
return simulation.NoOpMsg(types.ModuleName), nil, nil
}
//get #token-comment
lastCommnet := k.GetCommentCount(ctx, msg.Token)
oldCoins := make(map[string]sdk.Coins)
for _, ref := range msg.References {
oldAcc := ak.GetAccount(ctx, ref.RewardTarget)
oldCoins[oldAcc.GetAddress().String()] = oldAcc.GetCoins()
}
//handle msg
handler := comment.NewHandler(k)
ok := simulationx.SimulateHandleMsg(msg, handler, ctx)
if !ok {
return simulation.NewOperationMsg(msg, ok, ""), nil, nil
}
//verify msg is correctly handled
ok = verifyCreateCommentRefs(ctx, k, ak, msg, lastCommnet, oldCoins)
if !ok {
return simulation.NewOperationMsg(msg, ok, ""), nil, fmt.Errorf("token comment references handle failed")
}
return simulation.NewOperationMsg(msg, ok, ""), nil, nil
}
}
func createCommentReferences(r *rand.Rand, ctx sdk.Context, k keepers.Keeper, ask asset.Keeper, ak auth.AccountKeeper, accs []simulation.Account) (types.MsgCommentToken, error) {
fromAcc := simulation.RandomAcc(r, accs)
fromAuthAcc := ak.GetAccount(ctx, fromAcc.Address)
//randomly select token to comment
token, refs := randomCommentRef(r, ctx, k, ask, fromAuthAcc, accs)
if token == nil {
return types.MsgCommentToken{}, fmt.Errorf("no token comment to reference")
}
//randomly select amount of cet to donate
donation := simulationx.RandomCET(r, ctx, ak, fromAcc)
//construct msgCommentToken to create a new comment
msg := types.NewMsgCommentToken(fromAcc.Address, token.GetSymbol(), donation, "", "", types.RawBytes, refs)
if msg.ValidateBasic() != nil {
return types.MsgCommentToken{}, fmt.Errorf("msg expected to pass validation check")
}
return *msg, nil
}
func randomCommentRef(r *rand.Rand, ctx sdk.Context, k keepers.Keeper, ask asset.Keeper, fromAcc auth.Account, accs []simulation.Account) (token asset.Token, refs []types.CommentRef) {
totalComment := k.GetAllCommentCount(ctx)
if totalComment == nil {
return nil, []types.CommentRef{}
}
//generate len CommentRef
token, ids := randomTokenCommentRef(r, ctx, k, ask)
for i := 0; i < len(ids); i = i + 1 {
rewardTarget := simulation.RandomAcc(r, accs)
denom, amt := simulationx.RandomAccCoins(r, fromAcc)
attitude := r.Intn(int(types.Condolences-types.Like)) + int(types.Like)
refs = append(refs, types.CommentRef{
ID: ids[i],
RewardTarget: rewardTarget.Address,
RewardToken: denom,
RewardAmount: amt,
Attitudes: []int32{int32(attitude)},
})
}
return
}
func verifyCreateCommentRefs(ctx sdk.Context, k keepers.Keeper, ak auth.AccountKeeper, msg types.MsgCommentToken, lastComment uint64, oldCoins map[string]sdk.Coins) bool {
comment := k.GetCommentCount(ctx, msg.Token)
if comment != lastComment+1 {
return false
}
for _, ref := range msg.References {
newCoins := ak.GetAccount(ctx, ref.RewardTarget).GetCoins()
rewardCoins := sdk.NewCoins(sdk.NewCoin(ref.RewardToken, sdk.NewInt(ref.RewardAmount)))
if !oldCoins[ref.RewardTarget.String()].Add(rewardCoins).IsEqual(newCoins) {
return false
}
}
return true
}