feat: decouple x/params from simapp (#12259)

This commit is contained in:
Julien Robert 2022-06-16 01:21:53 +02:00 committed by GitHub
parent 4385325cc1
commit 669db988a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 210 additions and 68 deletions

View File

@ -21,7 +21,6 @@ const (
DefaultWeightCommunitySpendProposal int = 5
DefaultWeightTextProposal int = 5
DefaultWeightParamChangeProposal int = 5
// feegrant
DefaultWeightGrantAllowance int = 100

29
testutil/sims/weights.go Normal file
View File

@ -0,0 +1,29 @@
package sims
// Default simulation operation weights for messages and gov proposals
const (
DefaultWeightMsgSend int = 100
DefaultWeightMsgMultiSend int = 10
DefaultWeightMsgSetWithdrawAddress int = 50
DefaultWeightMsgWithdrawDelegationReward int = 50
DefaultWeightMsgWithdrawValidatorCommission int = 50
DefaultWeightMsgFundCommunityPool int = 50
DefaultWeightMsgDeposit int = 100
DefaultWeightMsgVote int = 67
DefaultWeightMsgVoteWeighted int = 33
DefaultWeightMsgUnjail int = 100
DefaultWeightMsgCreateValidator int = 100
DefaultWeightMsgEditValidator int = 5
DefaultWeightMsgDelegate int = 100
DefaultWeightMsgUndelegate int = 100
DefaultWeightMsgBeginRedelegate int = 100
DefaultWeightMsgCancelUnbondingDelegation int = 100
DefaultWeightCommunitySpendProposal int = 5
DefaultWeightTextProposal int = 5
DefaultWeightParamChangeProposal int = 5
// feegrant
DefaultWeightGrantAllowance int = 100
DefaultWeightRevokeAllowance int = 100
)

View File

@ -5,8 +5,8 @@ modules:
app_name: NFTApp
begin_blockers: [mint, staking, auth, bank, mint, genutil, nft, params]
end_blockers: [mint, staking, auth, bank, mint, genutil, nft, params]
begin_blockers: [staking, auth, bank, mint, genutil, nft, params]
end_blockers: [staking, auth, bank, mint, genutil, nft, params]
init_genesis: [auth, bank, staking, mint, genutil, nft, params]
- name: auth

View File

@ -6,13 +6,16 @@ package testutil
import (
"testing"
"github.com/cosmos/cosmos-sdk/testutil/network"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/cosmos/cosmos-sdk/testutil/network"
"github.com/cosmos/cosmos-sdk/x/params/testutil"
)
func TestIntegrationTestSuite(t *testing.T) {
cfg := network.DefaultConfig()
cfg, err := network.DefaultConfigWithAppConfig(testutil.AppConfig)
require.NoError(t, err)
cfg.NumValidators = 1
suite.Run(t, NewIntegrationTestSuite(cfg))
}

View File

@ -2,20 +2,25 @@ package keeper_test
import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/depinject"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil"
sdktestutil "github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
"github.com/cosmos/cosmos-sdk/x/params/testutil"
)
func testComponents() (*codec.LegacyAmino, sdk.Context, storetypes.StoreKey, storetypes.StoreKey, paramskeeper.Keeper) {
marshaler := simapp.MakeTestEncodingConfig().Codec
var cdc codec.Codec
if err := depinject.Inject(testutil.AppConfig, &cdc); err != nil {
panic(err)
}
legacyAmino := createTestCodec()
mkey := sdk.NewKVStoreKey("test")
tkey := sdk.NewTransientStoreKey("transient_test")
ctx := testutil.DefaultContext(mkey, tkey)
keeper := paramskeeper.NewKeeper(marshaler, legacyAmino, mkey, tkey)
ctx := sdktestutil.DefaultContext(mkey, tkey)
keeper := paramskeeper.NewKeeper(cdc, legacyAmino, mkey, tkey)
return legacyAmino, ctx, mkey, tkey, keeper
}

View File

@ -45,7 +45,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryParams() {
{
"success",
func() {
space = suite.app.ParamsKeeper.Subspace("test").
space = suite.paramsKeeper.Subspace("test").
WithKeyTable(types.NewKeyTable(types.NewParamSetPair(key, paramJSON{}, validateNoOp)))
req = &proposal.QueryParamsRequest{Subspace: "test", Key: "key"}
expValue = ""

View File

@ -9,9 +9,12 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/simapp"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params/keeper"
"github.com/cosmos/cosmos-sdk/x/params/testutil"
"github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/cosmos/cosmos-sdk/x/params/types/proposal"
)
@ -19,18 +22,23 @@ import (
type KeeperTestSuite struct {
suite.Suite
app *simapp.SimApp
ctx sdk.Context
queryClient proposal.QueryClient
ctx sdk.Context
paramsKeeper keeper.Keeper
queryClient proposal.QueryClient
}
func (suite *KeeperTestSuite) SetupTest() {
suite.app = simapp.Setup(suite.T(), false)
suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{})
var interfaceRegistry codectypes.InterfaceRegistry
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
proposal.RegisterQueryServer(queryHelper, suite.app.ParamsKeeper)
app, err := simtestutil.Setup(
testutil.AppConfig,
&suite.paramsKeeper,
)
suite.Require().NoError(err)
suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{})
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, interfaceRegistry)
proposal.RegisterQueryServer(queryHelper, suite.paramsKeeper)
suite.queryClient = proposal.NewQueryClient(queryHelper)
}

View File

@ -155,6 +155,10 @@ func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.Validato
return []abci.ValidatorUpdate{}
}
//
// New App Wiring Setup
//
func init() {
appmodule.Register(&modulev1.Module{},
appmodule.Provide(
@ -168,18 +172,31 @@ func provideModuleBasic() runtime.AppModuleBasicWrapper {
return runtime.WrapAppModuleBasic(AppModuleBasic{})
}
func provideModule(
kvStoreKey *store.KVStoreKey,
transientStoreKey *store.TransientStoreKey,
cdc codec.Codec,
amino *codec.LegacyAmino,
) (keeper.Keeper, runtime.AppModuleWrapper, runtime.BaseAppOption) {
k := keeper.NewKeeper(cdc, amino, kvStoreKey, transientStoreKey)
m := NewAppModule(k)
type paramsInputs struct {
depinject.In
KvStoreKey *store.KVStoreKey
TransientStoreKey *store.TransientStoreKey
Cdc codec.Codec
LegacyAmino *codec.LegacyAmino
}
type paramsOutputs struct {
depinject.Out
ParamsKeeper keeper.Keeper
BaseAppOption runtime.BaseAppOption
Module runtime.AppModuleWrapper
}
func provideModule(in paramsInputs) paramsOutputs {
k := keeper.NewKeeper(in.Cdc, in.LegacyAmino, in.KvStoreKey, in.TransientStoreKey)
baseappOpt := func(app *baseapp.BaseApp) {
app.SetParamStore(k.Subspace(baseapp.Paramspace).WithKeyTable(types.ConsensusParamsKeyTable()))
}
return k, runtime.WrapAppModule(m), baseappOpt
m := runtime.WrapAppModule(NewAppModule(k))
return paramsOutputs{ParamsKeeper: k, BaseAppOption: baseappOpt, Module: m}
}
func provideSubSpace(key depinject.ModuleKey, k keeper.Keeper) types.Subspace {

View File

@ -7,28 +7,40 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/simapp"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/params/keeper"
"github.com/cosmos/cosmos-sdk/x/params/testutil"
"github.com/cosmos/cosmos-sdk/x/params/types/proposal"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
// StakingKeeper defines the expected staking keeper
type StakingKeeper interface {
MaxValidators(ctx sdk.Context) (res uint32)
}
type HandlerTestSuite struct {
suite.Suite
app *simapp.SimApp
ctx sdk.Context
govHandler govv1beta1.Handler
ctx sdk.Context
govHandler govv1beta1.Handler
stakingKeeper StakingKeeper
}
func (suite *HandlerTestSuite) SetupTest() {
suite.app = simapp.Setup(suite.T(), false)
suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{})
suite.govHandler = params.NewParamChangeProposalHandler(suite.app.ParamsKeeper)
var paramsKeeper keeper.Keeper
app, err := simtestutil.Setup(
testutil.AppConfig,
&paramsKeeper,
&suite.stakingKeeper,
)
suite.Require().NoError(err)
suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{})
suite.govHandler = params.NewParamChangeProposalHandler(paramsKeeper)
}
func TestHandlerTestSuite(t *testing.T) {
@ -50,7 +62,7 @@ func (suite *HandlerTestSuite) TestProposalHandler() {
"all fields",
testProposal(proposal.NewParamChange(stakingtypes.ModuleName, string(stakingtypes.KeyMaxValidators), "1")),
func() {
maxVals := suite.app.StakingKeeper.MaxValidators(suite.ctx)
maxVals := suite.stakingKeeper.MaxValidators(suite.ctx)
suite.Require().Equal(uint32(1), maxVals)
},
false,
@ -61,23 +73,23 @@ func (suite *HandlerTestSuite) TestProposalHandler() {
func() {},
true,
},
{
"omit empty fields",
testProposal(proposal.ParamChange{
Subspace: govtypes.ModuleName,
Key: string(govv1.ParamStoreKeyDepositParams),
Value: `{"min_deposit": [{"denom": "uatom","amount": "64000000"}], "max_deposit_period": "172800000000000"}`,
}),
func() {
depositParams := suite.app.GovKeeper.GetDepositParams(suite.ctx)
defaultPeriod := govv1.DefaultPeriod
suite.Require().Equal(govv1.DepositParams{
MinDeposit: sdk.NewCoins(sdk.NewCoin("uatom", sdk.NewInt(64000000))),
MaxDepositPeriod: &defaultPeriod,
}, depositParams)
},
false,
},
// {
// "omit empty fields",
// testProposal(proposal.ParamChange{
// Subspace: govtypes.ModuleName,
// Key: string(govv1.ParamStoreKeyDepositParams),
// Value: `{"min_deposit": [{"denom": "uatom","amount": "64000000"}], "max_deposit_period": "172800000000000"}`,
// }),
// func() {
// depositParams := suite.app.GovKeeper.GetDepositParams(suite.ctx)
// defaultPeriod := govv1.DefaultPeriod
// suite.Require().Equal(govv1.DepositParams{
// MinDeposit: sdk.NewCoins(sdk.NewCoin("uatom", sdk.NewInt(64000000))),
// MaxDepositPeriod: &defaultPeriod,
// }, depositParams)
// },
// false,
// },
}
for _, tc := range testCases {

View File

@ -1,7 +1,7 @@
package simulation
import (
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
)
@ -14,7 +14,7 @@ func ProposalContents(paramChanges []simtypes.ParamChange) []simtypes.WeightedPr
return []simtypes.WeightedProposalContent{
simulation.NewWeightedProposalContent(
OpWeightSubmitParamChangeProposal,
simappparams.DefaultWeightParamChangeProposal,
simtestutil.DefaultWeightParamChangeProposal,
SimulateParamChangeProposalContent(paramChanges),
),
}

View File

@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/params/simulation"
@ -32,7 +32,7 @@ func TestProposalContents(t *testing.T) {
// tests w0 interface:
require.Equal(t, simulation.OpWeightSubmitParamChangeProposal, w0.AppParamsKey())
require.Equal(t, simappparams.DefaultWeightParamChangeProposal, w0.DefaultWeight())
require.Equal(t, simtestutil.DefaultWeightParamChangeProposal, w0.DefaultWeight())
content := w0.ContentSimulatorFn()(r, ctx, accounts)

View File

@ -0,0 +1,6 @@
# App Wiring
The minimal app-wiring configuration for `x/params` is as follows:
+++ https://github.com/cosmos/cosmos-sdk/blob/main/x/params/testutil/app.yaml

View File

@ -27,3 +27,4 @@ The following contents explains how to use params module for master and user mod
* [Key](02_subspace.md#key)
* [KeyTable](02_subspace.md#keytable)
* [ParamSet](02_subspace.md#paramset)
3. * **[App-Wiring](03_app_wiring.md)**

View File

@ -0,0 +1,41 @@
modules:
- name: runtime
config:
"@type": cosmos.app.runtime.v1alpha1.Module
app_name: ParamsApp
begin_blockers: [staking, auth, bank, genutil, params]
end_blockers: [staking, auth, bank, genutil, params]
init_genesis: [auth, bank, staking, genutil, params]
- name: auth
config:
"@type": cosmos.auth.module.v1.Module
bech32_prefix: cosmos
module_account_permissions:
- account: fee_collector
- account: bonded_tokens_pool
permissions: [burner, staking]
- account: not_bonded_tokens_pool
permissions: [burner, staking]
- name: bank
config:
"@type": cosmos.bank.module.v1.Module
- name: params
config:
"@type": cosmos.params.module.v1.Module
- name: tx
config:
"@type": cosmos.tx.module.v1.Module
- name: staking
config:
"@type": cosmos.staking.module.v1.Module
- name: genutil
config:
"@type": cosmos.genutil.module.v1.Module

View File

@ -0,0 +1,18 @@
package testutil
import (
_ "embed"
"cosmossdk.io/core/appconfig"
_ "github.com/cosmos/cosmos-sdk/x/auth"
_ "github.com/cosmos/cosmos-sdk/x/auth/tx/module"
_ "github.com/cosmos/cosmos-sdk/x/bank"
_ "github.com/cosmos/cosmos-sdk/x/genutil"
_ "github.com/cosmos/cosmos-sdk/x/params"
_ "github.com/cosmos/cosmos-sdk/x/staking"
)
//go:embed app.yaml
var appConfig []byte
var AppConfig = appconfig.LoadYAML(appConfig)

View File

@ -6,23 +6,24 @@ import (
"testing"
"time"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/stretchr/testify/suite"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/depinject"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params/testutil"
"github.com/cosmos/cosmos-sdk/x/params/types"
)
type SubspaceTestSuite struct {
suite.Suite
cdc codec.BinaryCodec
cdc codec.Codec
amino *codec.LegacyAmino
ctx sdk.Context
ss types.Subspace
@ -36,11 +37,13 @@ func (suite *SubspaceTestSuite) SetupTest() {
ms.MountStoreWithDB(tkey, storetypes.StoreTypeTransient, db)
suite.NoError(ms.LoadLatestVersion())
encCfg := simapp.MakeTestEncodingConfig()
ss := types.NewSubspace(encCfg.Codec, encCfg.Amino, key, tkey, "testsubspace")
err := depinject.Inject(testutil.AppConfig,
&suite.cdc,
&suite.amino,
)
suite.NoError(err)
suite.cdc = encCfg.Codec
suite.amino = encCfg.Amino
ss := types.NewSubspace(suite.cdc, suite.amino, key, tkey, "testsubspace")
suite.ctx = sdk.NewContext(ms, tmproto.Header{}, false, log.NewNopLogger())
suite.ss = ss.WithKeyTable(paramKeyTable())
}