Skip to content

Commit fc14374

Browse files
committed
add sim exchangeRateVote and Delegatefeed
1 parent 02ecb9e commit fc14374

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

x/oracle/simulation/operations.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
//nolint:gosec
2424
const (
2525
OpWeightMsgAggregateExchangeRatePrevote = "op_weight_msg_exchange_rate_aggregate_prevote"
26+
OpWeightMsgAggregateExchangeRateVote = "op_weight_msg_exchange_rate_aggregate_vote"
27+
OpWeightMsgDelegateFeedConsent = "op_weight_msg_exchange_feed_consent"
2628

2729
salt = "89b8164ca0b4b8703ae9ab25962f3dd6d1de5d656f5442971a93b2ca7893f654"
2830
)
@@ -57,6 +59,8 @@ func WeightedOperations(
5759
) simulation.WeightedOperations {
5860
var (
5961
weightMsgAggregateExchangeRatePrevote int
62+
weightMsgAggregateExchangeRateVote int
63+
weightMsgDelegateFeedConsent int
6064
voteHashMap = make(map[string]string)
6165
)
6266

@@ -65,12 +69,30 @@ func WeightedOperations(
6569
weightMsgAggregateExchangeRatePrevote = simappparams.DefaultWeightMsgSend * 2
6670
},
6771
)
72+
simstate.AppParams.GetOrGenerate(simstate.Cdc, OpWeightMsgAggregateExchangeRateVote, &weightMsgAggregateExchangeRateVote, nil,
73+
func(_ *rand.Rand) {
74+
weightMsgAggregateExchangeRateVote = simappparams.DefaultWeightMsgSend * 2
75+
},
76+
)
77+
simstate.AppParams.GetOrGenerate(simstate.Cdc, OpWeightMsgDelegateFeedConsent, &weightMsgDelegateFeedConsent, nil,
78+
func(_ *rand.Rand) {
79+
weightMsgDelegateFeedConsent = simappparams.DefaultWeightMsgSetWithdrawAddress
80+
},
81+
)
6882

6983
return simulation.WeightedOperations{
7084
simulation.NewWeightedOperation(
7185
weightMsgAggregateExchangeRatePrevote,
7286
SimulateMsgAggregateExchangeRatePrevote(ak, bk, k, voteHashMap),
7387
),
88+
simulation.NewWeightedOperation(
89+
weightMsgAggregateExchangeRateVote,
90+
SimulateMsgAggregateExchangeRateVote(ak, bk, k, voteHashMap),
91+
),
92+
simulation.NewWeightedOperation(
93+
weightMsgDelegateFeedConsent,
94+
SimulateMsgDelegateFeedConsent(ak, bk, k),
95+
),
7496
}
7597
}
7698

@@ -118,6 +140,83 @@ func SimulateMsgAggregateExchangeRatePrevote(
118140
}
119141
}
120142

143+
// SimulateMsgAggregateExchangeRateVote generates a MsgAggregateExchangeRateVote with random values.
144+
func SimulateMsgAggregateExchangeRateVote(
145+
ak types.AccountKeeper,
146+
bk bankkeeper.Keeper,
147+
k keeper.Keeper,
148+
voteHashMap map[string]string,
149+
) simtypes.Operation {
150+
return func(
151+
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
152+
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
153+
simAccount, _ := simtypes.RandomAcc(r, accs)
154+
address := sdk.ValAddress(simAccount.Address)
155+
noop := func(comment string) simtypes.OperationMsg {
156+
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(new(types.MsgAggregateExchangeRateVote)), comment)
157+
}
158+
159+
// ensure the validator exists
160+
val := k.StakingKeeper.Validator(ctx, address)
161+
if val == nil || !val.IsBonded() {
162+
return noop("unable to find validator"), nil, nil
163+
}
164+
165+
// ensure vote hash exists
166+
exchangeRatesStr, ok := voteHashMap[address.String()]
167+
if !ok {
168+
return noop("vote hash does not exist"), nil, nil
169+
}
170+
171+
// get prevote
172+
prevote, err := k.GetAggregateExchangeRatePrevote(ctx, address)
173+
if err != nil {
174+
return noop("prevote not found"), nil, nil
175+
}
176+
177+
params := k.GetParams(ctx)
178+
if (uint64(ctx.BlockHeight())/params.VotePeriod)-(prevote.SubmitBlock/params.VotePeriod) != 1 {
179+
return noop("reveal period of submitted vote does not match with registered prevote"), nil, nil
180+
}
181+
182+
feederAddr, _ := k.GetFeederDelegation(ctx, address)
183+
feederSimAccount, _ := simtypes.FindAccount(accs, feederAddr)
184+
msg := types.NewMsgAggregateExchangeRateVote(salt, exchangeRatesStr, feederAddr, address)
185+
186+
return deliver(r, app, ctx, ak, bk, feederSimAccount, msg, nil)
187+
}
188+
}
189+
190+
// SimulateMsgDelegateFeedConsent generates a MsgDelegateFeedConsent with random values.
191+
func SimulateMsgDelegateFeedConsent(ak types.AccountKeeper, bk bankkeeper.Keeper, k keeper.Keeper) simtypes.Operation {
192+
return func(
193+
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
194+
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
195+
simAccount, _ := simtypes.RandomAcc(r, accs)
196+
delegateAccount, _ := simtypes.RandomAcc(r, accs)
197+
valAddress := sdk.ValAddress(simAccount.Address)
198+
delegateValAddress := sdk.ValAddress(delegateAccount.Address)
199+
noop := func(comment string) simtypes.OperationMsg {
200+
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(new(types.MsgDelegateFeedConsent)), comment)
201+
}
202+
203+
// ensure the validator exists
204+
val := k.StakingKeeper.Validator(ctx, valAddress)
205+
if val == nil {
206+
return noop("unable to find validator"), nil, nil
207+
}
208+
209+
// ensure the target address is not a validator
210+
val2 := k.StakingKeeper.Validator(ctx, delegateValAddress)
211+
if val2 != nil {
212+
return noop("unable to delegate to validator"), nil, nil
213+
}
214+
215+
msg := types.NewMsgDelegateFeedConsent(valAddress, delegateAccount.Address)
216+
return deliver(r, app, ctx, ak, bk, simAccount, msg, nil)
217+
}
218+
}
219+
121220
func deliver(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, ak simulation.AccountKeeper,
122221
bk bankkeeper.Keeper, from simtypes.Account, msg sdk.Msg, coins sdk.Coins,
123222
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {

0 commit comments

Comments
 (0)