refactor: remove simapp references from x/bank and x/slashing tests (#12897)

## Description

There are still some references to simapp in `x/bank` and `x/slashing`.  This patch removes them.  Somewhat related to forming tests also:

- Flags `LegacySubspace exported.Subspace` as ``optional:"true"`` since x/params is moving to deprecation 



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
This commit is contained in:
Matt Kocubinski
2022-08-11 16:23:45 +00:00
committed by GitHub
parent 6473e733d6
commit 15b04c2a87
18 changed files with 395 additions and 258 deletions
+10
View File
@@ -10,6 +10,7 @@ import (
genutilmodulev1 "cosmossdk.io/api/cosmos/genutil/module/v1"
govmodulev1 "cosmossdk.io/api/cosmos/gov/module/v1"
paramsmodulev1 "cosmossdk.io/api/cosmos/params/module/v1"
slashingmodulev1 "cosmossdk.io/api/cosmos/slashing/module/v1"
stakingmodulev1 "cosmossdk.io/api/cosmos/staking/module/v1"
txmodulev1 "cosmossdk.io/api/cosmos/tx/module/v1"
vestingmodulev1 "cosmossdk.io/api/cosmos/vesting/module/v1"
@@ -142,6 +143,15 @@ func StakingModule() ModuleOption {
}
}
func SlashingModule() ModuleOption {
return func(config *appConfig) {
config.moduleConfigs["slashing"] = &appv1alpha1.ModuleConfig{
Name: "slashing",
Config: appconfig.WrapAny(&slashingmodulev1.Module{}),
}
}
}
func GenutilModule() ModuleOption {
return func(config *appConfig) {
config.moduleConfigs["genutil"] = &appv1alpha1.ModuleConfig{
+45 -23
View File
@@ -68,31 +68,52 @@ func CreateRandomValidatorSet() (*tmtypes.ValidatorSet, error) {
return tmtypes.NewValidatorSet([]*tmtypes.Validator{validator}), nil
}
type GenesisAccount struct {
authtypes.GenesisAccount
Coins sdk.Coins
}
// StartupConfig defines the startup configuration new a test application.
//
// ValidatorSet defines a custom validator set to be validating the app.
// BaseAppOption defines the additional operations that must be run on baseapp before app start.
// AtGenesis defines if the app started should already have produced block or not.
type StartupConfig struct {
ValidatorSet func() (*tmtypes.ValidatorSet, error)
BaseAppOption runtime.BaseAppOption
AtGenesis bool
GenesisAccounts []GenesisAccount
}
func DefaultStartUpConfig() StartupConfig {
priv := secp256k1.GenPrivKey()
ba := authtypes.NewBaseAccount(priv.PubKey().Address().Bytes(), priv.PubKey(), 0, 0)
ga := GenesisAccount{ba, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000)))}
return StartupConfig{
ValidatorSet: CreateRandomValidatorSet,
AtGenesis: false,
GenesisAccounts: []GenesisAccount{ga},
}
}
// Setup initializes a new runtime.App and can inject values into extraOutputs.
// It uses SetupWithConfiguration under the hood.
func Setup(appConfig depinject.Config, extraOutputs ...interface{}) (*runtime.App, error) {
return SetupWithConfiguration(appConfig, CreateRandomValidatorSet, nil, false, extraOutputs...)
return SetupWithConfiguration(appConfig, DefaultStartUpConfig(), extraOutputs...)
}
// SetupAtGenesis initializes a new runtime.App at genesis and can inject values into extraOutputs.
// It uses SetupWithConfiguration under the hood.
func SetupAtGenesis(appConfig depinject.Config, extraOutputs ...interface{}) (*runtime.App, error) {
return SetupWithConfiguration(appConfig, CreateRandomValidatorSet, nil, true, extraOutputs...)
}
// SetupWithBaseAppOption initializes a new runtime.App and can inject values into extraOutputs.
// With specific baseApp options. It uses SetupWithConfiguration under the hood.
func SetupWithBaseAppOption(appConfig depinject.Config, baseAppOption runtime.BaseAppOption, extraOutputs ...interface{}) (*runtime.App, error) {
return SetupWithConfiguration(appConfig, CreateRandomValidatorSet, baseAppOption, false, extraOutputs...)
cfg := DefaultStartUpConfig()
cfg.AtGenesis = true
return SetupWithConfiguration(appConfig, cfg, extraOutputs...)
}
// SetupWithConfiguration initializes a new runtime.App. A Nop logger is set in runtime.App.
// appConfig usually load from a `app.yaml` with `appconfig.LoadYAML`, defines the application configuration.
// validatorSet defines a custom validator set to be validating the app.
// baseAppOption defines the additional operations that must be run on baseapp before app start.
// genesis defines if the app started should already have produced block or not.
// extraOutputs defines the extra outputs to be assigned by the dependency injector (depinject).
func SetupWithConfiguration(appConfig depinject.Config, validatorSet func() (*tmtypes.ValidatorSet, error), baseAppOption runtime.BaseAppOption, genesis bool, extraOutputs ...interface{}) (*runtime.App, error) {
func SetupWithConfiguration(appConfig depinject.Config, startupConfig StartupConfig, extraOutputs ...interface{}) (*runtime.App, error) {
// create the app with depinject
var (
app *runtime.App
@@ -107,8 +128,8 @@ func SetupWithConfiguration(appConfig depinject.Config, validatorSet func() (*tm
return nil, fmt.Errorf("failed to inject dependencies: %w", err)
}
if baseAppOption != nil {
app = appBuilder.Build(log.NewNopLogger(), dbm.NewMemDB(), nil, baseAppOption)
if startupConfig.BaseAppOption != nil {
app = appBuilder.Build(log.NewNopLogger(), dbm.NewMemDB(), nil, startupConfig.BaseAppOption)
} else {
app = appBuilder.Build(log.NewNopLogger(), dbm.NewMemDB(), nil)
}
@@ -117,20 +138,21 @@ func SetupWithConfiguration(appConfig depinject.Config, validatorSet func() (*tm
}
// create validator set
valSet, err := validatorSet()
valSet, err := startupConfig.ValidatorSet()
if err != nil {
return nil, fmt.Errorf("failed to create validator set")
}
// generate genesis account
senderPrivKey := secp256k1.GenPrivKey()
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
balance := banktypes.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))),
var (
balances []banktypes.Balance
genAccounts []authtypes.GenesisAccount
)
for _, ga := range startupConfig.GenesisAccounts {
genAccounts = append(genAccounts, ga.GenesisAccount)
balances = append(balances, banktypes.Balance{Address: ga.GenesisAccount.GetAddress().String(), Coins: ga.Coins})
}
genesisState, err := GenesisStateWithValSet(codec, appBuilder.DefaultGenesis(), valSet, []authtypes.GenesisAccount{acc}, balance)
genesisState, err := GenesisStateWithValSet(codec, appBuilder.DefaultGenesis(), valSet, genAccounts, balances...)
if err != nil {
return nil, fmt.Errorf("failed to create genesis state: %w", err)
}
@@ -151,7 +173,7 @@ func SetupWithConfiguration(appConfig depinject.Config, validatorSet func() (*tm
)
// commit genesis changes
if !genesis {
if !startupConfig.AtGenesis {
app.Commit()
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{
Height: app.LastBlockHeight() + 1,
+59
View File
@@ -2,7 +2,14 @@ package sims
import (
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/require"
types2 "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -71,3 +78,55 @@ func GenSignedMockTx(r *rand.Rand, txConfig client.TxConfig, msgs []sdk.Msg, fee
return tx.GetTx(), nil
}
// SignCheckDeliver checks a generated signed transaction and simulates a
// block commitment with the given transaction. A test assertion is made using
// the parameter 'expPass' against the result. A corresponding result is
// returned.
func SignCheckDeliver(
t *testing.T, txCfg client.TxConfig, app *baseapp.BaseApp, header types.Header, msgs []sdk.Msg,
chainID string, accNums, accSeqs []uint64, expSimPass, expPass bool, priv ...cryptotypes.PrivKey,
) (sdk.GasInfo, *sdk.Result, error) {
tx, err := GenSignedMockTx(
rand.New(rand.NewSource(time.Now().UnixNano())),
txCfg,
msgs,
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},
DefaultGenTxGas,
chainID,
accNums,
accSeqs,
priv...,
)
require.NoError(t, err)
txBytes, err := txCfg.TxEncoder()(tx)
require.Nil(t, err)
// Must simulate now as CheckTx doesn't run Msgs anymore
_, res, err := app.Simulate(txBytes)
if expSimPass {
require.NoError(t, err)
require.NotNil(t, res)
} else {
require.Error(t, err)
require.Nil(t, res)
}
// Simulate a sending a transaction and committing a block
app.BeginBlock(types2.RequestBeginBlock{Header: header})
gInfo, res, err := app.SimDeliver(txCfg.TxEncoder(), tx)
if expPass {
require.NoError(t, err)
require.NotNil(t, res)
} else {
require.Error(t, err)
require.Nil(t, res)
}
app.EndBlock(types2.RequestEndBlock{})
app.Commit()
return gInfo, res, err
}