-
Notifications
You must be signed in to change notification settings - Fork 170
/
operations.go
245 lines (211 loc) · 8.53 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package simulation
import (
"fmt"
"math/rand"
"sort"
"strings"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/simulation"
appparams "github.com/umee-network/umee/v3/app/params"
umeesim "github.com/umee-network/umee/v3/util/sim"
"github.com/umee-network/umee/v3/x/oracle/keeper"
"github.com/umee-network/umee/v3/x/oracle/types"
)
// Simulation operation weights constants
const (
OpWeightMsgAggregateExchangeRatePrevote = "op_weight_msg_exchange_rate_aggregate_prevote" //nolint: gosec
OpWeightMsgAggregateExchangeRateVote = "op_weight_msg_exchange_rate_aggregate_vote" //nolint: gosec
OpWeightMsgDelegateFeedConsent = "op_weight_msg_exchange_feed_consent" //nolint: gosec
salt = "89b8164ca0b4b8703ae9ab25962f3dd6d1de5d656f5442971a93b2ca7893f654"
)
var (
acceptList = []string{types.UmeeSymbol, types.USDDenom}
umeePrice = sdk.MustNewDecFromStr("25.71")
)
// GenerateExchangeRatesString generates a canonical string representation of
// the aggregated exchange rates.
func GenerateExchangeRatesString(prices map[string]sdk.Dec) string {
exchangeRates := make([]string, len(prices))
i := 0
// aggregate exchange rates as "<base>:<price>"
for base, avgPrice := range prices {
exchangeRates[i] = fmt.Sprintf("%s:%s", base, avgPrice.String())
i++
}
sort.Strings(exchangeRates)
return strings.Join(exchangeRates, ",")
}
// WeightedOperations returns all the operations from the module with their respective weights
func WeightedOperations(
appParams simtypes.AppParams,
cdc codec.JSONCodec,
ak types.AccountKeeper,
bk bankkeeper.Keeper,
k keeper.Keeper,
) simulation.WeightedOperations {
var (
weightMsgAggregateExchangeRatePrevote int
weightMsgAggregateExchangeRateVote int
weightMsgDelegateFeedConsent int
voteHashMap = make(map[string]string)
)
appParams.GetOrGenerate(cdc, OpWeightMsgAggregateExchangeRatePrevote, &weightMsgAggregateExchangeRatePrevote, nil,
func(_ *rand.Rand) {
weightMsgAggregateExchangeRatePrevote = simappparams.DefaultWeightMsgSend * 2
},
)
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(
weightMsgAggregateExchangeRatePrevote,
SimulateMsgAggregateExchangeRatePrevote(ak, bk, k, voteHashMap),
),
simulation.NewWeightedOperation(
weightMsgAggregateExchangeRateVote,
SimulateMsgAggregateExchangeRateVote(ak, bk, k, voteHashMap),
),
simulation.NewWeightedOperation(
weightMsgDelegateFeedConsent,
SimulateMsgDelegateFeedConsent(ak, bk, k),
),
}
}
// SimulateMsgAggregateExchangeRatePrevote generates a MsgAggregateExchangeRatePrevote with random values.
func SimulateMsgAggregateExchangeRatePrevote(
ak types.AccountKeeper,
bk bankkeeper.Keeper,
k keeper.Keeper,
voteHashMap map[string]string,
) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
simAccount := accs[1] // , _ := simtypes.RandomAcc(r, accs)
address := sdk.ValAddress(simAccount.Address)
noop := func(comment string) simtypes.OperationMsg {
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(new(types.MsgAggregateExchangeRatePrevote)), comment)
}
// ensure the validator exists
val := k.StakingKeeper.Validator(ctx, address)
if val == nil || !val.IsBonded() {
return noop("unable to find validator"), nil, nil
}
// check for an existing prevote
_, err := k.GetAggregateExchangeRatePrevote(ctx, address)
if err == nil {
return noop("prevote already exists for this validator"), nil, nil
}
prices := make(map[string]sdk.Dec, len(acceptList))
for _, denom := range acceptList {
prices[denom] = umeePrice.Add(simtypes.RandomDecAmount(r, sdk.NewDec(1)))
}
exchangeRatesStr := GenerateExchangeRatesString(prices)
voteHash := types.GetAggregateVoteHash(salt, exchangeRatesStr, address)
feederAddr, _ := k.GetFeederDelegation(ctx, address)
feederSimAccount, _ := simtypes.FindAccount(accs, feederAddr)
msg := types.NewMsgAggregateExchangeRatePrevote(voteHash, feederAddr, address)
voteHashMap[address.String()] = exchangeRatesStr
return deliver(r, app, ctx, ak, bk, feederSimAccount, msg, nil)
}
}
// SimulateMsgAggregateExchangeRateVote generates a MsgAggregateExchangeRateVote with random values.
func SimulateMsgAggregateExchangeRateVote(
ak types.AccountKeeper,
bk bankkeeper.Keeper,
k keeper.Keeper,
voteHashMap map[string]string,
) 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)
noop := func(comment string) simtypes.OperationMsg {
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(new(types.MsgAggregateExchangeRateVote)), comment)
}
// ensure the validator exists
val := k.StakingKeeper.Validator(ctx, address)
if val == nil || !val.IsBonded() {
return noop("unable to find validator"), nil, nil
}
// ensure vote hash exists
exchangeRatesStr, ok := voteHashMap[address.String()]
if !ok {
return noop("vote hash does not exist"), nil, nil
}
// get prevote
prevote, err := k.GetAggregateExchangeRatePrevote(ctx, address)
if err != nil {
return noop("prevote not found"), nil, nil
}
params := k.GetParams(ctx)
if (uint64(ctx.BlockHeight())/params.VotePeriod)-(prevote.SubmitBlock/params.VotePeriod) != 1 {
return noop("reveal period of submitted vote does not match with registered prevote"), nil, nil
}
feederAddr, _ := k.GetFeederDelegation(ctx, address)
feederSimAccount, _ := simtypes.FindAccount(accs, feederAddr)
msg := types.NewMsgAggregateExchangeRateVote(salt, exchangeRatesStr, feederAddr, address)
return deliver(r, app, ctx, ak, bk, feederSimAccount, msg, nil)
}
}
// SimulateMsgDelegateFeedConsent generates a MsgDelegateFeedConsent with random values.
func SimulateMsgDelegateFeedConsent(ak types.AccountKeeper, bk bankkeeper.Keeper, 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)
noop := func(comment string) simtypes.OperationMsg {
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(new(types.MsgDelegateFeedConsent)), comment)
}
// ensure the validator exists
val := k.StakingKeeper.Validator(ctx, valAddress)
if val == nil {
return noop("unable to find validator"), nil, nil
}
// ensure the target address is not a validator
val2 := k.StakingKeeper.Validator(ctx, delegateValAddress)
if val2 != nil {
return noop("unable to delegate to validator"), nil, nil
}
msg := types.NewMsgDelegateFeedConsent(valAddress, delegateAccount.Address)
return deliver(r, app, ctx, ak, bk, simAccount, msg, nil)
}
}
func deliver(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, ak simulation.AccountKeeper,
bk bankkeeper.Keeper, from simtypes.Account, msg sdk.Msg, coins sdk.Coins,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
cfg := simappparams.MakeTestEncodingConfig()
o := simulation.OperationInput{
R: r,
App: app,
TxGen: cfg.TxConfig,
Cdc: cfg.Codec.(*codec.ProtoCodec),
Msg: msg,
MsgType: sdk.MsgTypeURL(msg),
Context: ctx,
SimAccount: from,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: types.ModuleName,
CoinsSpentInMsg: coins,
}
// note: leverage operations are more expensive!
return umeesim.GenAndDeliver(bk, o, appparams.DefaultGasLimit*50)
}