Skip to content

Commit

Permalink
[1760]: Remove use of MakeTestEncodingConfig from the quarantine module.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpicyLemon committed Apr 15, 2024
1 parent 31c2d51 commit 4a6148d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 51 deletions.
6 changes: 2 additions & 4 deletions x/quarantine/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
abci "github.com/cometbft/cometbft/abci/types"

"cosmossdk.io/core/appmodule"

sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -164,8 +165,5 @@ func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {

// WeightedOperations returns the all the quarantine module operations with their respective weights.
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
return simulation.WeightedOperations(
simState.AppParams, simState.Cdc,
am.accKeeper, am.bankKeeper, am.keeper, am.cdc,
)
return simulation.WeightedOperations(simState, am.accKeeper, am.bankKeeper, am.keeper)
}
62 changes: 27 additions & 35 deletions x/quarantine/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"

simappparams "github.com/provenance-io/provenance/app/params"
"github.com/provenance-io/provenance/x/quarantine"
"github.com/provenance-io/provenance/x/quarantine/keeper"
)
Expand Down Expand Up @@ -47,8 +45,7 @@ const (

// WeightedOperations returns all the operations from the module with their respective weights
func WeightedOperations(
appParams simtypes.AppParams, _ codec.JSONCodec,
ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper, _ cdctypes.AnyUnpacker,
simState module.SimulationState, ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper,
) simulation.WeightedOperations {
var (
weightMsgOptIn int
Expand All @@ -58,28 +55,28 @@ func WeightedOperations(
weightMsgUpdateAutoResponses int
)

appParams.GetOrGenerate(OpMsgOptIn, &weightMsgOptIn, nil,
simState.AppParams.GetOrGenerate(OpMsgOptIn, &weightMsgOptIn, nil,
func(_ *rand.Rand) { weightMsgOptIn = WeightMsgOptIn })
appParams.GetOrGenerate(OpMsgOptOut, &weightMsgOptOut, nil,
simState.AppParams.GetOrGenerate(OpMsgOptOut, &weightMsgOptOut, nil,
func(_ *rand.Rand) { weightMsgOptOut = WeightMsgOptOut })
appParams.GetOrGenerate(OpMsgAccept, &weightMsgAccept, nil,
simState.AppParams.GetOrGenerate(OpMsgAccept, &weightMsgAccept, nil,
func(_ *rand.Rand) { weightMsgAccept = WeightMsgAccept })
appParams.GetOrGenerate(OpMsgDecline, &weightMsgDecline, nil,
simState.AppParams.GetOrGenerate(OpMsgDecline, &weightMsgDecline, nil,
func(_ *rand.Rand) { weightMsgDecline = WeightMsgDecline })
appParams.GetOrGenerate(OpMsgUpdateAutoResponses, &weightMsgUpdateAutoResponses, nil,
simState.AppParams.GetOrGenerate(OpMsgUpdateAutoResponses, &weightMsgUpdateAutoResponses, nil,
func(_ *rand.Rand) { weightMsgUpdateAutoResponses = WeightMsgUpdateAutoResponses })

return simulation.WeightedOperations{
simulation.NewWeightedOperation(weightMsgOptIn, SimulateMsgOptIn(ak, bk)),
simulation.NewWeightedOperation(weightMsgOptOut, SimulateMsgOptOut(ak, bk, k)),
simulation.NewWeightedOperation(weightMsgAccept, SimulateMsgAccept(ak, bk, k)),
simulation.NewWeightedOperation(weightMsgDecline, SimulateMsgDecline(ak, bk, k)),
simulation.NewWeightedOperation(weightMsgUpdateAutoResponses, SimulateMsgUpdateAutoResponses(ak, bk, k)),
simulation.NewWeightedOperation(weightMsgOptIn, SimulateMsgOptIn(simState, ak, bk)),
simulation.NewWeightedOperation(weightMsgOptOut, SimulateMsgOptOut(simState, ak, bk, k)),
simulation.NewWeightedOperation(weightMsgAccept, SimulateMsgAccept(simState, ak, bk, k)),
simulation.NewWeightedOperation(weightMsgDecline, SimulateMsgDecline(simState, ak, bk, k)),
simulation.NewWeightedOperation(weightMsgUpdateAutoResponses, SimulateMsgUpdateAutoResponses(simState, ak, bk, k)),
}
}

// SimulateMsgOptIn opts an account into quarantine.
func SimulateMsgOptIn(ak quarantine.AccountKeeper, bk quarantine.BankKeeper) simtypes.Operation {
func SimulateMsgOptIn(simState module.SimulationState, ak quarantine.AccountKeeper, bk quarantine.BankKeeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
Expand All @@ -98,10 +95,9 @@ func SimulateMsgOptIn(ak quarantine.AccountKeeper, bk quarantine.BankKeeper) sim

account := ak.GetAccount(ctx, acct.Address)

encCfg := simappparams.MakeTestEncodingConfig()
tx, err := simtestutil.GenSignedMockTx(
r,
encCfg.TxConfig,
simState.TxConfig,
[]sdk.Msg{msg},
fees,
simtestutil.DefaultGenTxGas,
Expand All @@ -114,7 +110,7 @@ func SimulateMsgOptIn(ak quarantine.AccountKeeper, bk quarantine.BankKeeper) sim
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to generate mock tx"), nil, err
}

_, _, err = app.SimDeliver(encCfg.TxConfig.TxEncoder(), tx)
_, _, err = app.SimDeliver(simState.TxConfig.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to deliver tx"), nil, err
}
Expand All @@ -124,7 +120,7 @@ func SimulateMsgOptIn(ak quarantine.AccountKeeper, bk quarantine.BankKeeper) sim
}

// SimulateMsgOptOut opts an account out of quarantine.
func SimulateMsgOptOut(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation {
func SimulateMsgOptOut(simState module.SimulationState, ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
Expand Down Expand Up @@ -161,10 +157,9 @@ func SimulateMsgOptOut(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k

account := ak.GetAccount(ctx, acct.Address)

encCfg := simappparams.MakeTestEncodingConfig()
tx, err := simtestutil.GenSignedMockTx(
r,
encCfg.TxConfig,
simState.TxConfig,
[]sdk.Msg{msg},
fees,
simtestutil.DefaultGenTxGas,
Expand All @@ -177,7 +172,7 @@ func SimulateMsgOptOut(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to generate mock tx"), nil, err
}

_, _, err = app.SimDeliver(encCfg.TxConfig.TxEncoder(), tx)
_, _, err = app.SimDeliver(simState.TxConfig.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to deliver tx"), nil, err
}
Expand All @@ -187,7 +182,7 @@ func SimulateMsgOptOut(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k
}

// SimulateMsgAccept accepts quarantined funds.
func SimulateMsgAccept(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation {
func SimulateMsgAccept(simState module.SimulationState, ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
Expand Down Expand Up @@ -234,10 +229,9 @@ func SimulateMsgAccept(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k

account := ak.GetAccount(ctx, acct.Address)

encCfg := simappparams.MakeTestEncodingConfig()
tx, err := simtestutil.GenSignedMockTx(
r,
encCfg.TxConfig,
simState.TxConfig,
[]sdk.Msg{msg},
fees,
simtestutil.DefaultGenTxGas,
Expand All @@ -250,7 +244,7 @@ func SimulateMsgAccept(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to generate mock tx"), nil, err
}

_, _, err = app.SimDeliver(encCfg.TxConfig.TxEncoder(), tx)
_, _, err = app.SimDeliver(simState.TxConfig.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to deliver tx"), nil, err
}
Expand All @@ -260,7 +254,7 @@ func SimulateMsgAccept(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k
}

// SimulateMsgDecline declines quarantined funds.
func SimulateMsgDecline(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation {
func SimulateMsgDecline(simState module.SimulationState, ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
Expand Down Expand Up @@ -307,10 +301,9 @@ func SimulateMsgDecline(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k

account := ak.GetAccount(ctx, acct.Address)

encCfg := simappparams.MakeTestEncodingConfig()
tx, err := simtestutil.GenSignedMockTx(
r,
encCfg.TxConfig,
simState.TxConfig,
[]sdk.Msg{msg},
fees,
simtestutil.DefaultGenTxGas,
Expand All @@ -323,7 +316,7 @@ func SimulateMsgDecline(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to generate mock tx"), nil, err
}

_, _, err = app.SimDeliver(encCfg.TxConfig.TxEncoder(), tx)
_, _, err = app.SimDeliver(simState.TxConfig.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to deliver tx"), nil, err
}
Expand All @@ -333,7 +326,7 @@ func SimulateMsgDecline(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k
}

// SimulateMsgUpdateAutoResponses updates an accounts auto-responses
func SimulateMsgUpdateAutoResponses(ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation {
func SimulateMsgUpdateAutoResponses(simState module.SimulationState, ak quarantine.AccountKeeper, bk quarantine.BankKeeper, k keeper.Keeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
Expand Down Expand Up @@ -391,10 +384,9 @@ func SimulateMsgUpdateAutoResponses(ak quarantine.AccountKeeper, bk quarantine.B

account := ak.GetAccount(ctx, acct.Address)

encCfg := simappparams.MakeTestEncodingConfig()
tx, err := simtestutil.GenSignedMockTx(
r,
encCfg.TxConfig,
simState.TxConfig,
[]sdk.Msg{msg},
fees,
simtestutil.DefaultGenTxGas,
Expand All @@ -407,7 +399,7 @@ func SimulateMsgUpdateAutoResponses(ak quarantine.AccountKeeper, bk quarantine.B
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to generate mock tx"), nil, err
}

_, _, err = app.SimDeliver(encCfg.TxConfig.TxEncoder(), tx)
_, _, err = app.SimDeliver(simState.TxConfig.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(quarantine.ModuleName, msgType, "unable to deliver tx"), nil, err
}
Expand Down
28 changes: 16 additions & 12 deletions x/quarantine/simulation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/suite"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/bank/testutil"

Expand Down Expand Up @@ -48,10 +49,16 @@ func (s *SimTestSuite) SetupTest() {
s.ctx = s.app.BaseApp.NewContext(false)
}

func (s *SimTestSuite) TestWeightedOperations() {
cdc := s.app.AppCodec()
appParams := make(simtypes.AppParams)
// MakeTestSimState creates a new module.SimulationState struct with the fields needed by the functions being tested.
func (s *SimTestSuite) MakeTestSimState() module.SimulationState {
return module.SimulationState{
AppParams: make(simtypes.AppParams),
Cdc: s.app.AppCodec(),
TxConfig: s.app.GetTxConfig(),
}
}

func (s *SimTestSuite) TestWeightedOperations() {
expected := []struct {
weight int
opsMsgRoute string
Expand All @@ -64,10 +71,7 @@ func (s *SimTestSuite) TestWeightedOperations() {
{simulation.WeightMsgUpdateAutoResponses, quarantine.ModuleName, simulation.TypeMsgUpdateAutoResponses},
}

weightedOps := simulation.WeightedOperations(
appParams, cdc,
s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper, cdc,
)
weightedOps := simulation.WeightedOperations(s.MakeTestSimState(), s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper)

s.Require().Len(weightedOps, len(expected), "weighted ops")

Expand All @@ -92,7 +96,7 @@ func (s *SimTestSuite) TestSimulateMsgOptIn() {
r := rand.New(rand.NewSource(1))
accounts := s.getTestingAccounts(r, 10)

op := simulation.SimulateMsgOptIn(s.app.AccountKeeper, s.app.BankKeeper)
op := simulation.SimulateMsgOptIn(s.MakeTestSimState(), s.app.AccountKeeper, s.app.BankKeeper)
opMsg, futureOps, err := op(r, s.app.BaseApp, s.ctx, accounts, "")
s.Require().NoError(err, "running SimulateMsgOptIn op")

Expand All @@ -113,7 +117,7 @@ func (s *SimTestSuite) TestSimulateMsgOptOut() {
err := s.app.QuarantineKeeper.SetOptIn(s.ctx, accounts[0].Address)
s.Require().NoError(err, "SetOptIn on accounts[0]")

op := simulation.SimulateMsgOptOut(s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper)
op := simulation.SimulateMsgOptOut(s.MakeTestSimState(), s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper)
opMsg, futureOps, err := op(r, s.app.BaseApp, s.ctx, accounts, "")
s.Require().NoError(err, "running SimulateMsgOptIn op")

Expand All @@ -139,7 +143,7 @@ func (s *SimTestSuite) TestSimulateMsgAccept() {
err = s.app.BankKeeper.SendCoins(s.ctx, accounts[1].Address, accounts[0].Address, toSend)
s.Require().NoError(err, "SendCoins")

op := simulation.SimulateMsgAccept(s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper)
op := simulation.SimulateMsgAccept(s.MakeTestSimState(), s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper)
opMsg, futureOps, err := op(r, s.app.BaseApp, s.ctx, accounts, "")
s.Require().NoError(err, "running SimulateMsgOptIn op")

Expand All @@ -165,7 +169,7 @@ func (s *SimTestSuite) TestSimulateMsgDecline() {
err = s.app.BankKeeper.SendCoins(s.ctx, accounts[1].Address, accounts[0].Address, toSend)
s.Require().NoError(err, "SendCoins")

op := simulation.SimulateMsgDecline(s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper)
op := simulation.SimulateMsgDecline(s.MakeTestSimState(), s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper)
opMsg, futureOps, err := op(r, s.app.BaseApp, s.ctx, accounts, "")
s.Require().NoError(err, "running SimulateMsgOptIn op")

Expand All @@ -186,7 +190,7 @@ func (s *SimTestSuite) TestSimulateMsgUpdateAutoResponses() {
err := s.app.QuarantineKeeper.SetOptIn(s.ctx, accounts[0].Address)
s.Require().NoError(err, "SetOptIn on accounts[0]")

op := simulation.SimulateMsgUpdateAutoResponses(s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper)
op := simulation.SimulateMsgUpdateAutoResponses(s.MakeTestSimState(), s.app.AccountKeeper, s.app.BankKeeper, s.app.QuarantineKeeper)
opMsg, futureOps, err := op(r, s.app.BaseApp, s.ctx, accounts, "")
s.Require().NoError(err, "running SimulateMsgOptIn op")

Expand Down

0 comments on commit 4a6148d

Please sign in to comment.