refactor!: instantiate client.TxConfig once for simulations (#15875)

This commit is contained in:
Mark Rushakoff
2023-04-19 12:47:27 +00:00
committed by GitHub
parent aa683247d5
commit 7b10ada768
35 changed files with 481 additions and 218 deletions
+55 -21
View File
@@ -6,10 +6,10 @@ import (
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
@@ -35,8 +35,12 @@ const (
// WeightedOperations returns all the operations from the module with their respective weights
func WeightedOperations(
appParams simtypes.AppParams, cdc codec.JSONCodec, ak types.AccountKeeper,
bk types.BankKeeper, k *keeper.Keeper,
appParams simtypes.AppParams,
cdc codec.JSONCodec,
txGen client.TxConfig,
ak types.AccountKeeper,
bk types.BankKeeper,
k *keeper.Keeper,
) simulation.WeightedOperations {
var (
weightMsgCreateValidator int
@@ -86,33 +90,38 @@ func WeightedOperations(
return simulation.WeightedOperations{
simulation.NewWeightedOperation(
weightMsgCreateValidator,
SimulateMsgCreateValidator(ak, bk, k),
SimulateMsgCreateValidator(txGen, ak, bk, k),
),
simulation.NewWeightedOperation(
weightMsgEditValidator,
SimulateMsgEditValidator(ak, bk, k),
SimulateMsgEditValidator(txGen, ak, bk, k),
),
simulation.NewWeightedOperation(
weightMsgDelegate,
SimulateMsgDelegate(ak, bk, k),
SimulateMsgDelegate(txGen, ak, bk, k),
),
simulation.NewWeightedOperation(
weightMsgUndelegate,
SimulateMsgUndelegate(ak, bk, k),
SimulateMsgUndelegate(txGen, ak, bk, k),
),
simulation.NewWeightedOperation(
weightMsgBeginRedelegate,
SimulateMsgBeginRedelegate(ak, bk, k),
SimulateMsgBeginRedelegate(txGen, ak, bk, k),
),
simulation.NewWeightedOperation(
weightMsgCancelUnbondingDelegation,
SimulateMsgCancelUnbondingDelegate(ak, bk, k),
SimulateMsgCancelUnbondingDelegate(txGen, ak, bk, k),
),
}
}
// SimulateMsgCreateValidator generates a MsgCreateValidator with random values
func SimulateMsgCreateValidator(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper) simtypes.Operation {
func SimulateMsgCreateValidator(
txGen client.TxConfig,
ak types.AccountKeeper,
bk types.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) {
@@ -177,7 +186,7 @@ func SimulateMsgCreateValidator(ak types.AccountKeeper, bk types.BankKeeper, k *
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: moduletestutil.MakeTestTxConfig(),
TxGen: txGen,
Cdc: nil,
Msg: msg,
Context: ctx,
@@ -191,7 +200,12 @@ func SimulateMsgCreateValidator(ak types.AccountKeeper, bk types.BankKeeper, k *
}
// SimulateMsgEditValidator generates a MsgEditValidator with random values
func SimulateMsgEditValidator(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper) simtypes.Operation {
func SimulateMsgEditValidator(
txGen client.TxConfig,
ak types.AccountKeeper,
bk types.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) {
@@ -235,7 +249,7 @@ func SimulateMsgEditValidator(ak types.AccountKeeper, bk types.BankKeeper, k *ke
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: moduletestutil.MakeTestTxConfig(),
TxGen: txGen,
Cdc: nil,
Msg: msg,
Context: ctx,
@@ -251,7 +265,12 @@ func SimulateMsgEditValidator(ak types.AccountKeeper, bk types.BankKeeper, k *ke
}
// SimulateMsgDelegate generates a MsgDelegate with random values
func SimulateMsgDelegate(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper) simtypes.Operation {
func SimulateMsgDelegate(
txGen client.TxConfig,
ak types.AccountKeeper,
bk types.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) {
@@ -302,7 +321,7 @@ func SimulateMsgDelegate(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: moduletestutil.MakeTestTxConfig(),
TxGen: txGen,
Cdc: nil,
Msg: msg,
Context: ctx,
@@ -316,7 +335,12 @@ func SimulateMsgDelegate(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.
}
// SimulateMsgUndelegate generates a MsgUndelegate with random values
func SimulateMsgUndelegate(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper) simtypes.Operation {
func SimulateMsgUndelegate(
txGen client.TxConfig,
ak types.AccountKeeper,
bk types.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) {
@@ -379,7 +403,7 @@ func SimulateMsgUndelegate(ak types.AccountKeeper, bk types.BankKeeper, k *keepe
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: moduletestutil.MakeTestTxConfig(),
TxGen: txGen,
Cdc: nil,
Msg: msg,
Context: ctx,
@@ -395,7 +419,12 @@ func SimulateMsgUndelegate(ak types.AccountKeeper, bk types.BankKeeper, k *keepe
}
// SimulateMsgCancelUnbondingDelegate generates a MsgCancelUnbondingDelegate with random values
func SimulateMsgCancelUnbondingDelegate(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper) simtypes.Operation {
func SimulateMsgCancelUnbondingDelegate(
txGen client.TxConfig,
ak types.AccountKeeper,
bk types.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) {
@@ -460,7 +489,7 @@ func SimulateMsgCancelUnbondingDelegate(ak types.AccountKeeper, bk types.BankKee
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: moduletestutil.MakeTestTxConfig(),
TxGen: txGen,
Cdc: nil,
Msg: msg,
Context: ctx,
@@ -476,7 +505,12 @@ func SimulateMsgCancelUnbondingDelegate(ak types.AccountKeeper, bk types.BankKee
}
// SimulateMsgBeginRedelegate generates a MsgBeginRedelegate with random values
func SimulateMsgBeginRedelegate(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper) simtypes.Operation {
func SimulateMsgBeginRedelegate(
txGen client.TxConfig,
ak types.AccountKeeper,
bk types.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) {
@@ -563,7 +597,7 @@ func SimulateMsgBeginRedelegate(ak types.AccountKeeper, bk types.BankKeeper, k *
txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: moduletestutil.MakeTestTxConfig(),
TxGen: txGen,
Cdc: nil,
Msg: msg,
Context: ctx,
+11 -9
View File
@@ -13,6 +13,7 @@ import (
abci "github.com/cometbft/cometbft/abci/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
cmttypes "github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/client"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/runtime"
@@ -42,6 +43,7 @@ type SimTestSuite struct {
suite.Suite
r *rand.Rand
txConfig client.TxConfig
accounts []simtypes.Account
ctx sdk.Context
app *runtime.App
@@ -86,7 +88,7 @@ func (s *SimTestSuite) SetupTest() {
stakingKeeper *stakingkeeper.Keeper
)
app, err := simtestutil.SetupWithConfiguration(testutil.AppConfig, startupCfg, &bankKeeper, &accountKeeper, &mintKeeper, &distrKeeper, &stakingKeeper)
app, err := simtestutil.SetupWithConfiguration(testutil.AppConfig, startupCfg, &s.txConfig, &bankKeeper, &accountKeeper, &mintKeeper, &distrKeeper, &stakingKeeper)
require.NoError(s.T(), err)
ctx := app.BaseApp.NewContext(false, cmtproto.Header{})
@@ -122,7 +124,7 @@ func (s *SimTestSuite) TestWeightedOperations() {
cdc := s.encCfg.Codec
appParams := make(simtypes.AppParams)
weightesOps := simulation.WeightedOperations(appParams, cdc, s.accountKeeper,
weightedOps := simulation.WeightedOperations(appParams, cdc, s.txConfig, s.accountKeeper,
s.bankKeeper, s.stakingKeeper,
)
@@ -139,7 +141,7 @@ func (s *SimTestSuite) TestWeightedOperations() {
{simulation.DefaultWeightMsgCancelUnbondingDelegation, types.ModuleName, sdk.MsgTypeURL(&types.MsgCancelUnbondingDelegation{})},
}
for i, w := range weightesOps {
for i, w := range weightedOps {
operationMsg, _, _ := w.Op()(s.r, s.app.BaseApp, s.ctx, s.accounts, s.ctx.ChainID())
// require.NoError(t, err) // TODO check if it should be NoError
@@ -160,7 +162,7 @@ func (s *SimTestSuite) TestSimulateMsgCreateValidator() {
s.app.BeginBlock(abci.RequestBeginBlock{Header: cmtproto.Header{Height: s.app.LastBlockHeight() + 1, AppHash: s.app.LastCommitID().Hash}})
// execute operation
op := simulation.SimulateMsgCreateValidator(s.accountKeeper, s.bankKeeper, s.stakingKeeper)
op := simulation.SimulateMsgCreateValidator(s.txConfig, s.accountKeeper, s.bankKeeper, s.stakingKeeper)
operationMsg, futureOperations, err := op(s.r, s.app.BaseApp, s.ctx, s.accounts[1:], "")
require.NoError(err)
@@ -205,7 +207,7 @@ func (s *SimTestSuite) TestSimulateMsgCancelUnbondingDelegation() {
s.app.BeginBlock(abci.RequestBeginBlock{Header: cmtproto.Header{Height: s.app.LastBlockHeight() + 1, AppHash: s.app.LastCommitID().Hash, Time: blockTime}})
// execute operation
op := simulation.SimulateMsgCancelUnbondingDelegate(s.accountKeeper, s.bankKeeper, s.stakingKeeper)
op := simulation.SimulateMsgCancelUnbondingDelegate(s.txConfig, s.accountKeeper, s.bankKeeper, s.stakingKeeper)
accounts := []simtypes.Account{delegator}
operationMsg, futureOperations, err := op(s.r, s.app.BaseApp, ctx, accounts, "")
require.NoError(err)
@@ -234,7 +236,7 @@ func (s *SimTestSuite) TestSimulateMsgEditValidator() {
s.app.BeginBlock(abci.RequestBeginBlock{Header: cmtproto.Header{Height: s.app.LastBlockHeight() + 1, AppHash: s.app.LastCommitID().Hash, Time: blockTime}})
// execute operation
op := simulation.SimulateMsgEditValidator(s.accountKeeper, s.bankKeeper, s.stakingKeeper)
op := simulation.SimulateMsgEditValidator(s.txConfig, s.accountKeeper, s.bankKeeper, s.stakingKeeper)
operationMsg, futureOperations, err := op(s.r, s.app.BaseApp, ctx, s.accounts, "")
require.NoError(err)
@@ -255,7 +257,7 @@ func (s *SimTestSuite) TestSimulateMsgDelegate() {
ctx := s.ctx.WithBlockTime(blockTime)
// execute operation
op := simulation.SimulateMsgDelegate(s.accountKeeper, s.bankKeeper, s.stakingKeeper)
op := simulation.SimulateMsgDelegate(s.txConfig, s.accountKeeper, s.bankKeeper, s.stakingKeeper)
operationMsg, futureOperations, err := op(s.r, s.app.BaseApp, ctx, s.accounts[1:], "")
require.NoError(err)
@@ -294,7 +296,7 @@ func (s *SimTestSuite) TestSimulateMsgUndelegate() {
s.app.BeginBlock(abci.RequestBeginBlock{Header: cmtproto.Header{Height: s.app.LastBlockHeight() + 1, AppHash: s.app.LastCommitID().Hash, Time: blockTime}})
// execute operation
op := simulation.SimulateMsgUndelegate(s.accountKeeper, s.bankKeeper, s.stakingKeeper)
op := simulation.SimulateMsgUndelegate(s.txConfig, s.accountKeeper, s.bankKeeper, s.stakingKeeper)
operationMsg, futureOperations, err := op(s.r, s.app.BaseApp, ctx, s.accounts, "")
require.NoError(err)
@@ -337,7 +339,7 @@ func (s *SimTestSuite) TestSimulateMsgBeginRedelegate() {
s.app.BeginBlock(abci.RequestBeginBlock{Header: cmtproto.Header{Height: s.app.LastBlockHeight() + 1, AppHash: s.app.LastCommitID().Hash, Time: blockTime}})
// execute operation
op := simulation.SimulateMsgBeginRedelegate(s.accountKeeper, s.bankKeeper, s.stakingKeeper)
op := simulation.SimulateMsgBeginRedelegate(s.txConfig, s.accountKeeper, s.bankKeeper, s.stakingKeeper)
operationMsg, futureOperations, err := op(s.r, s.app.BaseApp, ctx, s.accounts, "")
s.T().Logf("operation message: %v", operationMsg)
require.NoError(err)