/
operations.go
178 lines (151 loc) · 5.97 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
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
package simulation
// DONTCOVER
import (
"math/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
"github.com/sei-protocol/sei-chain/x/oracle/keeper"
"github.com/sei-protocol/sei-chain/x/oracle/types"
)
// Simulation operation weights constants
//
//nolint:gosec
const (
OpWeightMsgAggregateExchangeRateVote = "op_weight_msg_exchange_rate_aggregate_vote"
OpWeightMsgDelegateFeedConsent = "op_weight_msg_exchange_feed_consent"
)
var voteHashMap = make(map[string]string)
// WeightedOperations returns all the operations from the module with their respective weights
func WeightedOperations(
appParams simtypes.AppParams,
cdc codec.JSONCodec,
ak types.AccountKeeper,
bk types.BankKeeper,
k keeper.Keeper,
) simulation.WeightedOperations {
var (
weightMsgAggregateExchangeRateVote int
weightMsgDelegateFeedConsent int
)
appParams.GetOrGenerate(cdc, OpWeightMsgAggregateExchangeRateVote, &weightMsgAggregateExchangeRateVote, nil,
func(_ *rand.Rand) {
weightMsgAggregateExchangeRateVote = simappparams.DefaultWeightMsgSend * 2
},
)
appParams.GetOrGenerate(cdc, OpWeightMsgDelegateFeedConsent, &weightMsgDelegateFeedConsent, nil,
func(_ *rand.Rand) {
weightMsgDelegateFeedConsent = simappparams.DefaultWeightMsgSetWithdrawAddress
},
)
return simulation.WeightedOperations{
simulation.NewWeightedOperation(
weightMsgAggregateExchangeRateVote,
SimulateMsgAggregateExchangeRateVote(ak, bk, k),
),
simulation.NewWeightedOperation(
weightMsgDelegateFeedConsent,
SimulateMsgDelegateFeedConsent(ak, bk, k),
),
}
}
// SimulateMsgAggregateExchangeRateVote generates a MsgAggregateExchangeRateVote with random values.
//nolint: funlen
func SimulateMsgAggregateExchangeRateVote(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) {
simAccount, _ := simtypes.RandomAcc(r, accs)
address := sdk.ValAddress(simAccount.Address)
// ensure the validator exists
val := k.StakingKeeper.Validator(ctx, address)
if val == nil || !val.IsBonded() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAggregateExchangeRateVote, "unable to find validator"), nil, nil
}
// ensure vote hash exists
exchangeRatesStr, ok := voteHashMap[address.String()]
if !ok {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAggregateExchangeRateVote, "vote hash not exists"), nil, nil
}
feederAddr := k.GetFeederDelegation(ctx, address)
feederSimAccount, _ := simtypes.FindAccount(accs, feederAddr)
feederAccount := ak.GetAccount(ctx, feederAddr)
spendableCoins := bk.SpendableCoins(ctx, feederAddr)
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAggregateExchangeRateVote, "unable to generate fees"), nil, err
}
msg := types.NewMsgAggregateExchangeRateVote(exchangeRatesStr, feederAddr, address)
txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
chainID,
[]uint64{feederAccount.GetAccountNumber()},
[]uint64{feederAccount.GetSequence()},
feederSimAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
}
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
}
}
// SimulateMsgDelegateFeedConsent generates a MsgDelegateFeedConsent with random values.
//nolint: funlen
func SimulateMsgDelegateFeedConsent(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) {
simAccount, _ := simtypes.RandomAcc(r, accs)
delegateAccount, _ := simtypes.RandomAcc(r, accs)
valAddress := sdk.ValAddress(simAccount.Address)
delegateValAddress := sdk.ValAddress(delegateAccount.Address)
account := ak.GetAccount(ctx, simAccount.Address)
// ensure the validator exists
val := k.StakingKeeper.Validator(ctx, valAddress)
if val == nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgDelegateFeedConsent, "unable to find validator"), nil, nil
}
// ensure the target address is not a validator
val2 := k.StakingKeeper.Validator(ctx, delegateValAddress)
if val2 != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgDelegateFeedConsent, "unable to delegate to validator"), nil, nil
}
spendableCoins := bk.SpendableCoins(ctx, account.GetAddress())
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAggregateExchangeRateVote, "unable to generate fees"), nil, err
}
msg := types.NewMsgDelegateFeedConsent(valAddress, delegateAccount.Address)
txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
chainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
simAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
}
_, _, err = app.Deliver(txGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
}
}