Skip to content

Commit

Permalink
test: ensure only sdk.DefaultBondDenom is used for fees in our modu…
Browse files Browse the repository at this point in the history
…les simulation (#743)

* init `rand` seed in non-determinism simulation

* add random fee function to `sample`

* fix simulation for `msgParticipate`

* use custom `GenAndDeliverTxWithRandFees`
  • Loading branch information
giunatale authored Apr 24, 2022
1 parent 9bab52f commit 10f3301
Show file tree
Hide file tree
Showing 17 changed files with 323 additions and 79 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ release/
.envrc
.vscode

# OS
.DS_Store

# Localnet
localnet/node?/data/*.db
localnet/node?/data/snapshots
Expand Down
3 changes: 2 additions & 1 deletion app/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/rand"
"os"
"testing"
"time"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -50,7 +51,6 @@ func interBlockCacheOpt() func(*baseapp.BaseApp) {
return baseapp.SetInterBlockCache(store.NewCommitKVStoreCacheManager())
}

// go test ./app -v -benchmem -run=^$ -bench ^BenchmarkSimulation -Commit=true -cpuprofile cpu.out
func BenchmarkSimulation(b *testing.B) {
simapp.FlagVerboseValue = true
simapp.FlagOnOperationValue = true
Expand Down Expand Up @@ -118,6 +118,7 @@ func TestAppStateDeterminism(t *testing.T) {
config.OnOperation = true
config.AllInvariants = true

rand.Seed(time.Now().Unix())
numSeeds := 3
numTimesToRunPerSeed := 5
appHashList := make([]json.RawMessage, numTimesToRunPerSeed)
Expand Down
2 changes: 1 addition & 1 deletion testutil/sample/campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func CampaignParams(r *rand.Rand) campaign.Params {
maxTotalSupply := campaign.DefaultMaxTotalSupply

// assign random small amount of staking denom
campaignCreationFee := sdk.NewCoins(sdk.NewInt64Coin(BondDenom, r.Int63n(100)+1))
campaignCreationFee := sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, r.Int63n(100)+1))

return campaign.NewParams(minTotalSupply, maxTotalSupply, campaignCreationFee)
}
Expand Down
2 changes: 1 addition & 1 deletion testutil/sample/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func LaunchParams(r *rand.Rand) launch.Params {
minLaunchTime := r.Int63n(10) + launch.DefaultMinLaunchTime

// assign random small amount of staking denom
chainCreationFee := sdk.NewCoins(sdk.NewInt64Coin(BondDenom, r.Int63n(100)+1))
chainCreationFee := sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, r.Int63n(100)+1))

return launch.NewParams(minLaunchTime, maxLaunchTime, launch.DefaultRevertDelay, chainCreationFee)
}
Expand Down
29 changes: 26 additions & 3 deletions testutil/sample/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import (
)

const (
// BondDenom defines the bond denom used in testing
BondDenom = "stake"

simAccountsNb = 100
)

Expand All @@ -36,3 +33,29 @@ func SimAccounts() (accounts []simtypes.Account) {
func Rand() *rand.Rand {
return rand.New(rand.NewSource(time.Now().Unix()))
}

// Fees returns a random fee by selecting a random amount of bond denomination
// from the account's available balance. If the user doesn't have enough funds for
// paying fees, it returns empty coins.
func Fees(r *rand.Rand, spendableCoins sdk.Coins) (sdk.Coins, error) {
if spendableCoins.Empty() {
return nil, nil
}

bondDenomAmt := spendableCoins.AmountOf(sdk.DefaultBondDenom)
if bondDenomAmt.IsZero() {
return nil, nil
}

amt, err := simtypes.RandPositiveInt(r, bondDenomAmt)
if err != nil {
return nil, err
}

if amt.IsZero() {
return nil, nil
}

fees := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, amt))
return fees, nil
}
56 changes: 56 additions & 0 deletions testutil/simulation/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package simulation

import (
"github.com/cosmos/cosmos-sdk/simapp/helpers"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
sdksimulation "github.com/cosmos/cosmos-sdk/x/simulation"

"github.com/tendermint/spn/testutil/sample"
)

// GenAndDeliverTxWithRandFees generates a transaction with a random fee and delivers it.
func GenAndDeliverTxWithRandFees(txCtx sdksimulation.OperationInput, gas uint64) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
account := txCtx.AccountKeeper.GetAccount(txCtx.Context, txCtx.SimAccount.Address)
spendable := txCtx.Bankkeeper.SpendableCoins(txCtx.Context, account.GetAddress())

var fees sdk.Coins
var err error

coins, hasNeg := spendable.SafeSub(txCtx.CoinsSpentInMsg)
if hasNeg {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "message doesn't leave room for fees"), nil, err
}

fees, err = sample.Fees(txCtx.R, coins)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to generate fees"), nil, err
}
return GenAndDeliverTx(txCtx, fees, gas)
}

// GenAndDeliverTx generates a transactions and delivers it.
func GenAndDeliverTx(txCtx sdksimulation.OperationInput, fees sdk.Coins, gas uint64) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
account := txCtx.AccountKeeper.GetAccount(txCtx.Context, txCtx.SimAccount.Address)
tx, err := helpers.GenTx(
txCtx.TxGen,
[]sdk.Msg{txCtx.Msg},
fees,
gas,
txCtx.Context.ChainID(),
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
txCtx.SimAccount.PrivKey,
)

if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to generate mock tx"), nil, err
}

_, _, err = txCtx.App.Deliver(txCtx.TxGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to deliver tx"), nil, err
}

return simtypes.NewOperationMsg(txCtx.Msg, true, "", txCtx.Cdc), nil, nil
}
8 changes: 5 additions & 3 deletions x/campaign/simulation/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/simulation"
sdksimulation "github.com/cosmos/cosmos-sdk/x/simulation"

"github.com/tendermint/spn/testutil/sample"
"github.com/tendermint/spn/testutil/simulation"
"github.com/tendermint/spn/x/campaign/keeper"
"github.com/tendermint/spn/x/campaign/types"
)
Expand All @@ -32,7 +34,7 @@ func deliverSimTx(
msg TypedMsg,
coinsSpent sdk.Coins,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
txCtx := simulation.OperationInput{
txCtx := sdksimulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Expand All @@ -46,7 +48,7 @@ func deliverSimTx(
ModuleName: types.ModuleName,
CoinsSpentInMsg: coinsSpent,
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
return simulation.GenAndDeliverTxWithRandFees(txCtx, helpers.DefaultGenTxGas)
}

// SimulateMsgCreateCampaign simulates a MsgCreateCampaign message
Expand Down
48 changes: 25 additions & 23 deletions x/launch/simulation/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
sdksimulation "github.com/cosmos/cosmos-sdk/x/simulation"

"github.com/tendermint/spn/testutil/sample"
"github.com/tendermint/spn/testutil/simulation"
"github.com/tendermint/spn/x/launch/keeper"
"github.com/tendermint/spn/x/launch/types"
)
Expand Down Expand Up @@ -44,7 +46,7 @@ func SimulateMsgCreateChain(ak types.AccountKeeper, bk types.BankKeeper, k keepe
false,
0,
)
txCtx := simulation.OperationInput{
txCtx := sdksimulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Expand All @@ -58,7 +60,7 @@ func SimulateMsgCreateChain(ak types.AccountKeeper, bk types.BankKeeper, k keepe
ModuleName: types.ModuleName,
CoinsSpentInMsg: creationFee,
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
return simulation.GenAndDeliverTxWithRandFees(txCtx, helpers.DefaultGenTxGas)
}
}

Expand Down Expand Up @@ -107,7 +109,7 @@ func SimulateMsgEditChain(ak types.AccountKeeper, bk types.BankKeeper, k keeper.
modify,
)

txCtx := simulation.OperationInput{
txCtx := sdksimulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Expand All @@ -121,7 +123,7 @@ func SimulateMsgEditChain(ak types.AccountKeeper, bk types.BankKeeper, k keeper.
ModuleName: types.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
return simulation.GenAndDeliverTxWithRandFees(txCtx, helpers.DefaultGenTxGas)
}
}

Expand All @@ -144,7 +146,7 @@ func SimulateMsgRequestAddGenesisAccount(ak types.AccountKeeper, bk types.BankKe
simAccount.Address.String(),
chain.LaunchID,
)
txCtx := simulation.OperationInput{
txCtx := sdksimulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Expand All @@ -158,7 +160,7 @@ func SimulateMsgRequestAddGenesisAccount(ak types.AccountKeeper, bk types.BankKe
ModuleName: types.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
return simulation.GenAndDeliverTxWithRandFees(txCtx, helpers.DefaultGenTxGas)
}
}

Expand All @@ -181,7 +183,7 @@ func SimulateMsgRequestAddVestingAccount(ak types.AccountKeeper, bk types.BankKe
simAccount.Address.String(),
chain.LaunchID,
)
txCtx := simulation.OperationInput{
txCtx := sdksimulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Expand All @@ -195,7 +197,7 @@ func SimulateMsgRequestAddVestingAccount(ak types.AccountKeeper, bk types.BankKe
ModuleName: types.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
return simulation.GenAndDeliverTxWithRandFees(txCtx, helpers.DefaultGenTxGas)
}
}

Expand Down Expand Up @@ -262,7 +264,7 @@ func SimulateMsgRequestRemoveAccount(ak types.AccountKeeper, bk types.BankKeeper
accAddr,
)

txCtx := simulation.OperationInput{
txCtx := sdksimulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Expand All @@ -276,7 +278,7 @@ func SimulateMsgRequestRemoveAccount(ak types.AccountKeeper, bk types.BankKeeper
ModuleName: types.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
return simulation.GenAndDeliverTxWithRandFees(txCtx, helpers.DefaultGenTxGas)
}
}

Expand All @@ -299,7 +301,7 @@ func SimulateMsgRequestAddValidator(ak types.AccountKeeper, bk types.BankKeeper,
simAccount.Address.String(),
chain.LaunchID,
)
txCtx := simulation.OperationInput{
txCtx := sdksimulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Expand All @@ -313,7 +315,7 @@ func SimulateMsgRequestAddValidator(ak types.AccountKeeper, bk types.BankKeeper,
ModuleName: types.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
return simulation.GenAndDeliverTxWithRandFees(txCtx, helpers.DefaultGenTxGas)
}
}

Expand All @@ -333,7 +335,7 @@ func SimulateMsgRequestRemoveValidator(ak types.AccountKeeper, bk types.BankKeep
valAcc.Address,
valAcc.LaunchID,
)
txCtx := simulation.OperationInput{
txCtx := sdksimulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Expand All @@ -347,7 +349,7 @@ func SimulateMsgRequestRemoveValidator(ak types.AccountKeeper, bk types.BankKeep
ModuleName: types.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
return simulation.GenAndDeliverTxWithRandFees(txCtx, helpers.DefaultGenTxGas)
}
}

Expand All @@ -369,7 +371,7 @@ func SimulateMsgTriggerLaunch(ak types.AccountKeeper, bk types.BankKeeper, k kee
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgTriggerLaunch, err.Error()), nil, nil
}
msg := sample.MsgTriggerLaunch(r, simAccount.Address.String(), chain.LaunchID)
txCtx := simulation.OperationInput{
txCtx := sdksimulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Expand All @@ -383,7 +385,7 @@ func SimulateMsgTriggerLaunch(ak types.AccountKeeper, bk types.BankKeeper, k kee
ModuleName: types.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
return simulation.GenAndDeliverTxWithRandFees(txCtx, helpers.DefaultGenTxGas)
}
}

Expand Down Expand Up @@ -417,7 +419,7 @@ func SimulateMsgSettleRequest(ak types.AccountKeeper, bk types.BankKeeper, k kee
msg.Approve = false
}

txCtx := simulation.OperationInput{
txCtx := sdksimulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Expand All @@ -431,7 +433,7 @@ func SimulateMsgSettleRequest(ak types.AccountKeeper, bk types.BankKeeper, k kee
ModuleName: types.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
return simulation.GenAndDeliverTxWithRandFees(txCtx, helpers.DefaultGenTxGas)
}
}

Expand All @@ -458,7 +460,7 @@ func SimulateMsgRevertLaunch(ak types.AccountKeeper, bk types.BankKeeper, k keep
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgRevertLaunch, err.Error()), nil, nil
}
msg := sample.MsgRevertLaunch(simAccount.Address.String(), chain.LaunchID)
txCtx := simulation.OperationInput{
txCtx := sdksimulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Expand All @@ -472,7 +474,7 @@ func SimulateMsgRevertLaunch(ak types.AccountKeeper, bk types.BankKeeper, k keep
ModuleName: types.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
return simulation.GenAndDeliverTxWithRandFees(txCtx, helpers.DefaultGenTxGas)
}
}

Expand Down Expand Up @@ -501,7 +503,7 @@ func SimulateMsgUpdateLaunchInformation(ak types.AccountKeeper, bk types.BankKee
modify,
!modify && r.Intn(100) < 50,
)
txCtx := simulation.OperationInput{
txCtx := sdksimulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Expand All @@ -515,6 +517,6 @@ func SimulateMsgUpdateLaunchInformation(ak types.AccountKeeper, bk types.BankKee
ModuleName: types.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
return simulation.GenAndDeliverTxWithRandFees(txCtx, helpers.DefaultGenTxGas)
}
}
Loading

0 comments on commit 10f3301

Please sign in to comment.