Skip to content

Commit

Permalink
refactor!: instantiate client.TxConfig once for simulations (#15875)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-rushakoff committed Apr 19, 2023
1 parent aa68324 commit 7b10ada
Show file tree
Hide file tree
Showing 35 changed files with 481 additions and 218 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
All implementations of `InterfaceRegistry` by other users must now embed the official implementation.
* `AminoCodec` is marked as deprecated.
* (x/crisis) [#15852](https://github.com/cosmos/cosmos-sdk/pull/15852) Crisis keeper now takes a instance of the address codec to be able to decode user addresses
* (x/slashing) [#15875](https://github.com/cosmos/cosmos-sdk/pull/15875) `x/slashing.NewAppModule` now requires an `InterfaceRegistry` parameter.

### Client Breaking Changes

Expand Down
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func NewSimApp(
feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry),
gov.NewAppModule(appCodec, &app.GovKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(govtypes.ModuleName)),
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, nil, app.GetSubspace(minttypes.ModuleName)),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName)),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName), app.interfaceRegistry),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(distrtypes.ModuleName)),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName)),
upgrade.NewAppModule(app.UpgradeKeeper, app.AccountKeeper.GetAddressCodec()),
Expand Down
2 changes: 2 additions & 0 deletions testutil/sims/simulation_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"
"github.com/cosmos/cosmos-sdk/types/module"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
)

Expand Down Expand Up @@ -53,6 +54,7 @@ func SimulationOperations(app runtime.AppI, cdc codec.JSONCodec, config simtypes
simState := module.SimulationState{
AppParams: make(simtypes.AppParams),
Cdc: cdc,
TxConfig: moduletestutil.MakeTestTxConfig(),
BondDenom: sdk.DefaultBondDenom,
}

Expand Down
2 changes: 2 additions & 0 deletions types/module/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/simulation"
)
Expand Down Expand Up @@ -142,6 +143,7 @@ func (sm *SimulationManager) WeightedOperations(simState SimulationState) []simu
type SimulationState struct {
AppParams simulation.AppParams
Cdc codec.JSONCodec // application codec
TxConfig client.TxConfig // Shared TxConfig; this is expensive to create and stateless, so create it once up front.
Rand *rand.Rand // random number
GenState map[string]json.RawMessage // genesis state
Accounts []simulation.Account // simulation accounts
Expand Down
2 changes: 1 addition & 1 deletion x/authz/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
return simulation.WeightedOperations(
am.registry,
simState.AppParams, simState.Cdc,
simState.AppParams, simState.Cdc, simState.TxConfig,
am.accountKeeper, am.bankKeeper, am.keeper,
)
}
39 changes: 29 additions & 10 deletions x/authz/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"time"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"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"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/cosmos/cosmos-sdk/x/authz"
"github.com/cosmos/cosmos-sdk/x/authz/keeper"
banktype "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand Down Expand Up @@ -44,6 +44,7 @@ func WeightedOperations(
registry cdctypes.InterfaceRegistry,
appParams simtypes.AppParams,
cdc codec.JSONCodec,
txGen client.TxConfig,
ak authz.AccountKeeper,
bk authz.BankKeeper,
k keeper.Keeper,
Expand Down Expand Up @@ -72,24 +73,32 @@ func WeightedOperations(
},
)

pCdc := codec.NewProtoCodec(registry)

return simulation.WeightedOperations{
simulation.NewWeightedOperation(
weightMsgGrant,
SimulateMsgGrant(codec.NewProtoCodec(registry), ak, bk, k),
SimulateMsgGrant(pCdc, txGen, ak, bk, k),
),
simulation.NewWeightedOperation(
weightExec,
SimulateMsgExec(codec.NewProtoCodec(registry), ak, bk, k, registry),
SimulateMsgExec(pCdc, txGen, ak, bk, k, registry),
),
simulation.NewWeightedOperation(
weightRevoke,
SimulateMsgRevoke(codec.NewProtoCodec(registry), ak, bk, k),
SimulateMsgRevoke(pCdc, txGen, ak, bk, k),
),
}
}

// SimulateMsgGrant generates a MsgGrant with random values.
func SimulateMsgGrant(cdc *codec.ProtoCodec, ak authz.AccountKeeper, bk authz.BankKeeper, _ keeper.Keeper) simtypes.Operation {
func SimulateMsgGrant(
cdc *codec.ProtoCodec,
txCfg client.TxConfig,
ak authz.AccountKeeper,
bk authz.BankKeeper,
_ keeper.Keeper,
) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
Expand Down Expand Up @@ -123,7 +132,6 @@ func SimulateMsgGrant(cdc *codec.ProtoCodec, ak authz.AccountKeeper, bk authz.Ba
if err != nil {
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgGrant, err.Error()), nil, err
}
txCfg := tx.NewTxConfig(cdc, tx.DefaultSignModes)
tx, err := simtestutil.GenSignedMockTx(
r,
txCfg,
Expand Down Expand Up @@ -157,7 +165,13 @@ func generateRandomAuthorization(r *rand.Rand, spendLimit sdk.Coins) authz.Autho
}

// SimulateMsgRevoke generates a MsgRevoke with random values.
func SimulateMsgRevoke(cdc *codec.ProtoCodec, ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keeper) simtypes.Operation {
func SimulateMsgRevoke(
cdc *codec.ProtoCodec,
txCfg client.TxConfig,
ak authz.AccountKeeper,
bk authz.BankKeeper,
k keeper.Keeper,
) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
Expand Down Expand Up @@ -194,7 +208,6 @@ func SimulateMsgRevoke(cdc *codec.ProtoCodec, ak authz.AccountKeeper, bk authz.B
}

msg := authz.NewMsgRevoke(granterAddr, granteeAddr, a.MsgTypeURL())
txCfg := tx.NewTxConfig(cdc, tx.DefaultSignModes)
account := ak.GetAccount(ctx, granterAddr)
tx, err := simtestutil.GenSignedMockTx(
r,
Expand All @@ -221,7 +234,14 @@ func SimulateMsgRevoke(cdc *codec.ProtoCodec, ak authz.AccountKeeper, bk authz.B
}

// SimulateMsgExec generates a MsgExec with random values.
func SimulateMsgExec(cdc *codec.ProtoCodec, ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keeper, unpacker cdctypes.AnyUnpacker) simtypes.Operation {
func SimulateMsgExec(
cdc *codec.ProtoCodec,
txCfg client.TxConfig,
ak authz.AccountKeeper,
bk authz.BankKeeper,
k keeper.Keeper,
unpacker cdctypes.AnyUnpacker,
) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
Expand Down Expand Up @@ -288,7 +308,6 @@ func SimulateMsgExec(cdc *codec.ProtoCodec, ak authz.AccountKeeper, bk authz.Ban
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, "fee error"), nil, err
}

txCfg := tx.NewTxConfig(cdc, tx.DefaultSignModes)
granteeAcc := ak.GetAccount(ctx, granteeAddr)
tx, err := simtestutil.GenSignedMockTx(
r,
Expand Down
11 changes: 7 additions & 4 deletions x/authz/simulation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
Expand All @@ -34,6 +35,7 @@ type SimTestSuite struct {
legacyAmino *codec.LegacyAmino
codec codec.Codec
interfaceRegistry codectypes.InterfaceRegistry
txConfig client.TxConfig
accountKeeper authkeeper.AccountKeeper
bankKeeper bankkeeper.Keeper
authzKeeper authzkeeper.Keeper
Expand All @@ -45,6 +47,7 @@ func (suite *SimTestSuite) SetupTest() {
&suite.legacyAmino,
&suite.codec,
&suite.interfaceRegistry,
&suite.txConfig,
&suite.accountKeeper,
&suite.bankKeeper,
&suite.authzKeeper,
Expand All @@ -58,7 +61,7 @@ func (suite *SimTestSuite) TestWeightedOperations() {
cdc := suite.codec
appParams := make(simtypes.AppParams)

weightedOps := simulation.WeightedOperations(suite.interfaceRegistry, appParams, cdc, suite.accountKeeper,
weightedOps := simulation.WeightedOperations(suite.interfaceRegistry, appParams, cdc, suite.txConfig, suite.accountKeeper,
suite.bankKeeper, suite.authzKeeper)

s := rand.NewSource(3)
Expand Down Expand Up @@ -125,7 +128,7 @@ func (suite *SimTestSuite) TestSimulateGrant() {
grantee := accounts[1]

// execute operation
op := simulation.SimulateMsgGrant(codec.NewProtoCodec(suite.interfaceRegistry), suite.accountKeeper, suite.bankKeeper, suite.authzKeeper)
op := simulation.SimulateMsgGrant(codec.NewProtoCodec(suite.interfaceRegistry), suite.txConfig, suite.accountKeeper, suite.bankKeeper, suite.authzKeeper)
operationMsg, futureOperations, err := op(r, suite.app.BaseApp, ctx, accounts, "")
suite.Require().NoError(err)

Expand Down Expand Up @@ -163,7 +166,7 @@ func (suite *SimTestSuite) TestSimulateRevoke() {
suite.Require().NoError(err)

// execute operation
op := simulation.SimulateMsgRevoke(codec.NewProtoCodec(suite.interfaceRegistry), suite.accountKeeper, suite.bankKeeper, suite.authzKeeper)
op := simulation.SimulateMsgRevoke(codec.NewProtoCodec(suite.interfaceRegistry), suite.txConfig, suite.accountKeeper, suite.bankKeeper, suite.authzKeeper)
operationMsg, futureOperations, err := op(r, suite.app.BaseApp, suite.ctx, accounts, "")
suite.Require().NoError(err)

Expand Down Expand Up @@ -198,7 +201,7 @@ func (suite *SimTestSuite) TestSimulateExec() {
suite.Require().NoError(err)

// execute operation
op := simulation.SimulateMsgExec(codec.NewProtoCodec(suite.interfaceRegistry), suite.accountKeeper, suite.bankKeeper, suite.authzKeeper, suite.codec)
op := simulation.SimulateMsgExec(codec.NewProtoCodec(suite.interfaceRegistry), suite.txConfig, suite.accountKeeper, suite.bankKeeper, suite.authzKeeper, suite.codec)
operationMsg, futureOperations, err := op(r, suite.app.BaseApp, suite.ctx, accounts, "")
suite.Require().NoError(err)

Expand Down
2 changes: 1 addition & 1 deletion x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (am AppModule) RegisterStoreDecoder(_ simtypes.StoreDecoderRegistry) {}
// WeightedOperations returns the all the gov module operations with their respective weights.
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
return simulation.WeightedOperations(
simState.AppParams, simState.Cdc, am.accountKeeper, am.keeper,
simState.AppParams, simState.Cdc, simState.TxConfig, am.accountKeeper, am.keeper,
)
}

Expand Down
Loading

0 comments on commit 7b10ada

Please sign in to comment.