Skip to content

Commit

Permalink
Limit simulation fee token to only sdk.DefaultBondDenom (#42)
Browse files Browse the repository at this point in the history
* filtered simulation fee tokens to only use sdk.DefaultBondDenom

* fixed simulation
  • Loading branch information
sunnya97 authored and ValarDragon committed Nov 19, 2021
1 parent 0d94171 commit 8416815
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 12 deletions.
11 changes: 11 additions & 0 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,17 @@ func (coins Coins) GetDenomByIndex(i int) string {
return coins[i].Denom
}

// FilterDenoms returns the coins with only the passed in denoms
func (coins Coins) FilterDenoms(denoms []string) Coins {
filteredCoins := NewCoins()

for _, denom := range denoms {
filteredCoins = filteredCoins.Add(NewCoin(denom, coins.AmountOf(denom)))
}

return filteredCoins
}

// IsAllPositive returns true if there is at least one coin and all currencies
// have a positive value.
func (coins Coins) IsAllPositive() bool {
Expand Down
9 changes: 6 additions & 3 deletions x/authz/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ func SimulateMsgGrant(ak authz.AccountKeeper, bk authz.BankKeeper, _ keeper.Keep

granterAcc := ak.GetAccount(ctx, granter.Address)
spendableCoins := bk.SpendableCoins(ctx, granter.Address)
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
feeCoins := spendableCoins.FilterDenoms([]string{sdk.DefaultBondDenom})
fees, err := simtypes.RandomFees(r, ctx, feeCoins)
if err != nil {
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgGrant, err.Error()), nil, err
}
Expand Down Expand Up @@ -174,7 +175,8 @@ func SimulateMsgRevoke(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Kee
}

spendableCoins := bk.SpendableCoins(ctx, granterAddr)
fees, err := simtypes.RandomFees(r, ctx, spendableCoins)
feeCoins := spendableCoins.FilterDenoms([]string{sdk.DefaultBondDenom})
fees, err := simtypes.RandomFees(r, ctx, feeCoins)
if err != nil {
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgRevoke, "fee error"), nil, err
}
Expand Down Expand Up @@ -260,7 +262,8 @@ func SimulateMsgExec(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keepe
}

granteeSpendableCoins := bk.SpendableCoins(ctx, granteeAddr)
fees, err := simtypes.RandomFees(r, ctx, granteeSpendableCoins)
feeCoins := granteeSpendableCoins.FilterDenoms([]string{sdk.DefaultBondDenom})
fees, err := simtypes.RandomFees(r, ctx, feeCoins)
if err != nil {
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, "fee error"), nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions x/bank/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ func sendMsgSend(

coins, hasNeg := spendable.SafeSub(msg.Amount)
if !hasNeg {
fees, err = simtypes.RandomFees(r, ctx, coins)
feeCoins := coins.FilterDenoms([]string{sdk.DefaultBondDenom})
fees, err = simtypes.RandomFees(r, ctx, feeCoins)
if err != nil {
return err
}
Expand Down Expand Up @@ -353,7 +354,8 @@ func sendMsgMultiSend(

coins, hasNeg := spendable.SafeSub(msg.Inputs[0].Coins)
if !hasNeg {
fees, err = simtypes.RandomFees(r, ctx, coins)
feeCoins := coins.FilterDenoms([]string{sdk.DefaultBondDenom})
fees, err = simtypes.RandomFees(r, ctx, feeCoins)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion x/distribution/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ func SimulateMsgFundCommunityPool(ak types.AccountKeeper, bk types.BankKeeper, k

coins, hasNeg := spendable.SafeSub(fundAmount)
if !hasNeg {
fees, err = simtypes.RandomFees(r, ctx, coins)
feeCoins := coins.FilterDenoms([]string{sdk.DefaultBondDenom})
fees, err = simtypes.RandomFees(r, ctx, feeCoins)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgFundCommunityPool, "unable to generate fees"), nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions x/gov/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ func SimulateMsgSubmitProposal(
var fees sdk.Coins
coins, hasNeg := spendable.SafeSub(deposit)
if !hasNeg {
fees, err = simtypes.RandomFees(r, ctx, coins)
feeCoins := coins.FilterDenoms([]string{sdk.DefaultBondDenom})
fees, err = simtypes.RandomFees(r, ctx, feeCoins)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate fees"), nil, err
}
Expand Down Expand Up @@ -235,7 +236,8 @@ func SimulateMsgDeposit(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Ke
var fees sdk.Coins
coins, hasNeg := spendable.SafeSub(deposit)
if !hasNeg {
fees, err = simtypes.RandomFees(r, ctx, coins)
feeCoins := coins.FilterDenoms([]string{sdk.DefaultBondDenom})
fees, err = simtypes.RandomFees(r, ctx, feeCoins)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate fees"), nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion x/simulation/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func GenAndDeliverTxWithRandFees(txCtx OperationInput) (simtypes.OperationMsg, [
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "message doesn't leave room for fees"), nil, err
}

fees, err = simtypes.RandomFees(txCtx.R, txCtx.Context, coins)
spendableFeeCoins := coins.FilterDenoms([]string{sdk.DefaultBondDenom})
fees, err = simtypes.RandomFees(txCtx.R, txCtx.Context, spendableFeeCoins)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to generate fees"), nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion x/slashing/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ func SimulateMsgUnjail(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Kee
account := ak.GetAccount(ctx, sdk.AccAddress(validator.GetOperator()))
spendable := bk.SpendableCoins(ctx, account.GetAddress())

fees, err := simtypes.RandomFees(r, ctx, spendable)
feeCoins := spendable.FilterDenoms([]string{sdk.DefaultBondDenom})
fees, err := simtypes.RandomFees(r, ctx, feeCoins)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgUnjail, "unable to generate fees"), nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions x/staking/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ func SimulateMsgCreateValidator(ak types.AccountKeeper, bk types.BankKeeper, k k

coins, hasNeg := spendable.SafeSub(sdk.Coins{selfDelegation})
if !hasNeg {
fees, err = simtypes.RandomFees(r, ctx, coins)
feeCoins := coins.FilterDenoms([]string{sdk.DefaultBondDenom})
fees, err = simtypes.RandomFees(r, ctx, feeCoins)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgCreateValidator, "unable to generate fees"), nil, err
}
Expand Down Expand Up @@ -267,7 +268,8 @@ func SimulateMsgDelegate(ak types.AccountKeeper, bk types.BankKeeper, k keeper.K

coins, hasNeg := spendable.SafeSub(sdk.Coins{bondAmt})
if !hasNeg {
fees, err = simtypes.RandomFees(r, ctx, coins)
feeCoins := coins.FilterDenoms([]string{sdk.DefaultBondDenom})
fees, err = simtypes.RandomFees(r, ctx, feeCoins)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgDelegate, "unable to generate fees"), nil, err
}
Expand Down

0 comments on commit 8416815

Please sign in to comment.