Skip to content

Commit

Permalink
let simulator filter out accounts with min balance
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn committed Jul 25, 2022
1 parent 8698876 commit 825d735
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 34 deletions.
7 changes: 0 additions & 7 deletions x/incentives/keeper/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 0 additions & 7 deletions x/incentives/keeper/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
4 changes: 2 additions & 2 deletions x/incentives/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
37 changes: 19 additions & 18 deletions x/incentives/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
7 changes: 7 additions & 0 deletions x/incentives/types/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 825d735

Please sign in to comment.