-
Notifications
You must be signed in to change notification settings - Fork 55
/
operations.go
121 lines (107 loc) 路 3.44 KB
/
operations.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
package simulation
import (
"math/rand"
"sort"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
appparams "github.com/crescent-network/crescent/app/params"
utils "github.com/crescent-network/crescent/types"
"github.com/crescent-network/crescent/x/claim/keeper"
"github.com/crescent-network/crescent/x/claim/types"
)
const (
OpWeightMsgClaim = "op_weight_msg_claim"
)
var (
Gas = uint64(20000000)
Fees = sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000))
)
func WeightedOperations(
appParams simtypes.AppParams, cdc codec.JSONCodec,
ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper,
) simulation.WeightedOperations {
var weightMsgClaim int
appParams.GetOrGenerate(cdc, OpWeightMsgClaim, &weightMsgClaim, nil, func(_ *rand.Rand) {
weightMsgClaim = appparams.DefaultWeightMsgClaim
})
return simulation.WeightedOperations{
simulation.NewWeightedOperation(
weightMsgClaim,
SimulateMsgClaim(ak, bk, k),
),
}
}
func SimulateMsgClaim(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
accs = utils.ShuffleSimAccounts(r, accs)
airdrops := k.GetAllAirdrops(ctx)
rand.Shuffle(len(airdrops), func(i, j int) {
airdrops[i], airdrops[j] = airdrops[j], airdrops[i]
})
var simAccount simtypes.Account
var airdrop types.Airdrop
var claimRecord types.ClaimRecord
var cond types.ConditionType
skip := true
loop:
for _, simAccount = range accs {
for _, airdrop = range airdrops {
var found bool
claimRecord, found = k.GetClaimRecordByRecipient(ctx, airdrop.Id, simAccount.Address)
if !found {
continue
}
conditions := unclaimedConditions(airdrop, claimRecord)
for _, cond = range conditions {
if err := k.ValidateCondition(ctx, simAccount.Address, cond); err == nil {
skip = false
break loop
}
}
}
}
if skip {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgClaim, "no account to claim"), nil, nil
}
spendable := bk.SpendableCoins(ctx, simAccount.Address)
msg := types.NewMsgClaim(airdrop.Id, simAccount.Address, cond)
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: appparams.MakeTestEncodingConfig().TxConfig,
Msg: msg,
MsgType: msg.Type(),
Context: ctx,
SimAccount: simAccount,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: types.ModuleName,
CoinsSpentInMsg: spendable,
}
return utils.GenAndDeliverTxWithFees(txCtx, Gas, Fees)
}
}
func unclaimedConditions(airdrop types.Airdrop, claimRecord types.ClaimRecord) []types.ConditionType {
conditionSet := map[types.ConditionType]struct{}{}
for _, cond := range airdrop.Conditions {
conditionSet[cond] = struct{}{}
}
for _, cond := range claimRecord.ClaimedConditions {
delete(conditionSet, cond)
}
var conditions []types.ConditionType
for cond := range conditionSet {
conditions = append(conditions, cond)
}
// Sort conditions for deterministic simulation, since map keys are not sorted.
sort.Slice(conditions, func(i, j int) bool {
return conditions[i] < conditions[j]
})
return conditions
}