From 825d735aa04d231d9c20c5ecc3892b5b8e2c89b2 Mon Sep 17 00:00:00 2001 From: Roman Akhtariev Date: Mon, 25 Jul 2022 12:52:00 -0700 Subject: [PATCH] let simulator filter out accounts with min balance --- x/incentives/keeper/export_test.go | 7 ----- x/incentives/keeper/gauge.go | 7 ----- x/incentives/keeper/msg_server.go | 4 +-- x/incentives/simulation/operations.go | 37 ++++++++++++++------------- x/incentives/types/gauge.go | 7 +++++ 5 files changed, 28 insertions(+), 34 deletions(-) diff --git a/x/incentives/keeper/export_test.go b/x/incentives/keeper/export_test.go index 47602af7137..9eeb06ac191 100644 --- a/x/incentives/keeper/export_test.go +++ b/x/incentives/keeper/export_test.go @@ -6,13 +6,6 @@ import ( "github.com/osmosis-labs/osmosis/v10/x/incentives/types" ) -var ( - // CreateGaugeFee is the fee required to create a new gauge. - CreateGaugeFee = createGaugeFee - // AddToGagugeFee is the fee required to add to gauge. - AddToGaugeFee = addToGaugeFee -) - // AddGaugeRefByKey appends the provided gauge ID into an array associated with the provided key. func (k Keeper) AddGaugeRefByKey(ctx sdk.Context, key []byte, gaugeID uint64) error { return k.addGaugeRefByKey(ctx, key, gaugeID) diff --git a/x/incentives/keeper/gauge.go b/x/incentives/keeper/gauge.go index cf4dc318c4a..8a8638bc243 100644 --- a/x/incentives/keeper/gauge.go +++ b/x/incentives/keeper/gauge.go @@ -19,13 +19,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -var ( - // createGaugeFee is the fee required to create a new gauge. - createGaugeFee = sdk.NewInt(50 * 1_000_000) - // addToGagugeFee is the fee required to add to gauge. - addToGaugeFee = sdk.NewInt(25 * 1_000_000) -) - // getGaugesFromIterator iterates over everything in a gauge's iterator, until it reaches the end. Return all gauges iterated over. func (k Keeper) getGaugesFromIterator(ctx sdk.Context, iterator db.Iterator) []types.Gauge { gauges := []types.Gauge{} diff --git a/x/incentives/keeper/msg_server.go b/x/incentives/keeper/msg_server.go index f936c19f967..c966ab5f4bb 100644 --- a/x/incentives/keeper/msg_server.go +++ b/x/incentives/keeper/msg_server.go @@ -33,7 +33,7 @@ func (server msgServer) CreateGauge(goCtx context.Context, msg *types.MsgCreateG return nil, err } - if err := server.keeper.chargeFeeIfSufficientFeeDenomBalance(ctx, owner, createGaugeFee, msg.Coins); err != nil { + if err := server.keeper.chargeFeeIfSufficientFeeDenomBalance(ctx, owner, types.CreateGaugeFee, msg.Coins); err != nil { return nil, err } @@ -61,7 +61,7 @@ func (server msgServer) AddToGauge(goCtx context.Context, msg *types.MsgAddToGau return nil, err } - if err := server.keeper.chargeFeeIfSufficientFeeDenomBalance(ctx, owner, addToGaugeFee, msg.Rewards); err != nil { + if err := server.keeper.chargeFeeIfSufficientFeeDenomBalance(ctx, owner, types.AddToGaugeFee, msg.Rewards); err != nil { return nil, err } err = server.keeper.AddToGaugeRewards(ctx, owner, msg.Rewards, msg.GaugeId) diff --git a/x/incentives/simulation/operations.go b/x/incentives/simulation/operations.go index f989028dbee..ee033bcdea3 100644 --- a/x/incentives/simulation/operations.go +++ b/x/incentives/simulation/operations.go @@ -10,6 +10,7 @@ import ( "github.com/osmosis-labs/osmosis/v10/x/incentives/keeper" "github.com/osmosis-labs/osmosis/v10/x/incentives/types" + incentivestypes "github.com/osmosis-labs/osmosis/v10/x/incentives/types" lockuptypes "github.com/osmosis-labs/osmosis/v10/x/lockup/types" "github.com/cosmos/cosmos-sdk/codec" @@ -30,13 +31,6 @@ const ( OpWeightMsgAddToGauge = "op_weight_msg_add_to_gauge" ) -var ( - // createGaugeFee is the fee required to create a new gauge. - createGaugeFee = sdk.NewInt(50 * 1_000_000) - // addToGagugeFee is the fee required to add to gauge. - addToGaugeFee = sdk.NewInt(25 * 1_000_000) -) - // WeightedOperations returns all the operations from the module with their respective weights. func WeightedOperations( appParams simtypes.AppParams, cdc codec.JSONCodec, ak stakingTypes.AccountKeeper, @@ -122,13 +116,10 @@ func SimulateMsgCreateGauge(ak stakingTypes.AccountKeeper, bk stakingTypes.BankK 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) - simCoins := bk.SpendableCoins(ctx, simAccount.Address) - if simCoins.Len() <= 0 || simCoins.AmountOf(appparams.BaseCoinUnit).LT(createGaugeFee) { - return simtypes.NoOpMsg( - types.ModuleName, types.TypeMsgCreateGauge, "Account have no coin"), nil, nil - } + accountsWithEnoughBalance := filterMinBalanceAccounts(ctx, bk, accs, incentivestypes.CreateGaugeFee, appparams.BaseCoinUnit) + simAccount, _ := simtypes.RandomAcc(r, accountsWithEnoughBalance) + simCoins := bk.SpendableCoins(ctx, simAccount.Address) isPerpetual := r.Int()%2 == 0 distributeTo := genQueryCondition(r, ctx.BlockTime(), simCoins, types.DefaultGenesis().LockableDurations) rewards := genRewardCoins(r, simCoins) @@ -161,12 +152,10 @@ func SimulateMsgAddToGauge(ak stakingTypes.AccountKeeper, bk stakingTypes.BankKe 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) + accountsWithEnoughBalance := filterMinBalanceAccounts(ctx, bk, accs, incentivestypes.AddToGaugeFee, appparams.BaseCoinUnit) + + simAccount, _ := simtypes.RandomAcc(r, accountsWithEnoughBalance) simCoins := bk.SpendableCoins(ctx, simAccount.Address) - if simCoins.Len() <= 0 || simCoins.AmountOf(appparams.BaseCoinUnit).LT(addToGaugeFee) { - return simtypes.NoOpMsg( - types.ModuleName, types.TypeMsgAddToGauge, "Account have no coin"), nil, nil - } gauge := RandomGauge(ctx, r, k) if gauge == nil { @@ -198,3 +187,15 @@ func RandomGauge(ctx sdk.Context, r *rand.Rand, k keeper.Keeper) *types.Gauge { } return &gauges[r.Intn(len(gauges))] } + +// filterMinBalanceAccounts returns accounts that have at least the minimum balance of the given denom. +func filterMinBalanceAccounts(ctx sdk.Context, bk stakingTypes.BankKeeper, accounts []simtypes.Account, minBalance sdk.Int, minBalanceDenom string) []simtypes.Account { + accountsWithEnoughBalance := []simtypes.Account{} + for _, acc := range accounts { + accountCoins := bk.SpendableCoins(ctx, acc.Address) + if accountCoins.Len() > 0 && accountCoins.AmountOf(minBalanceDenom).GT(minBalance) { + accountsWithEnoughBalance = append(accountsWithEnoughBalance, acc) + } + } + return accountsWithEnoughBalance +} diff --git a/x/incentives/types/gauge.go b/x/incentives/types/gauge.go index bb0f97d087c..ec3b112d31f 100644 --- a/x/incentives/types/gauge.go +++ b/x/incentives/types/gauge.go @@ -8,6 +8,13 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +var ( + // CreateGaugeFee is the fee required to create a new gauge. + CreateGaugeFee = sdk.NewInt(50 * 1_000_000) + // AddToGagugeFee is the fee required to add to gauge. + AddToGaugeFee = sdk.NewInt(25 * 1_000_000) +) + // NewGauge creates a new gauge struct given the required gauge parameters. func NewGauge(id uint64, isPerpetual bool, distrTo lockuptypes.QueryCondition, coins sdk.Coins, startTime time.Time, numEpochsPaidOver uint64, filledEpochs uint64, distrCoins sdk.Coins) Gauge { return Gauge{