Skip to content

Commit a9c18cd

Browse files
fix: add simulation tests for new param change (backport #14728) (#14826)
Co-authored-by: Julien Robert <julien@rbrt.fr>
1 parent 33986a3 commit a9c18cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+930
-726
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
5454

5555
### API Breaking Changes
5656

57+
* (simulation) [#14728](https://github.com/cosmos/cosmos-sdk/pull/14728) Rename the `ParamChanges` field to `LegacyParamChange` and `Contents` to `LegacyProposalContents` in `simulation.SimulationState`. Additionally it adds a `ProposalMsgs` field to `simulation.SimulationState`.
5758
* (x/gov) [#14782](https://github.com/cosmos/cosmos-sdk/pull/14782) Move the `metadata` argument in `govv1.NewProposal` alongside `title` and `summary`.
5859

5960
### Bug Fixes

UPGRADING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ This guide provides instructions for upgrading to specific versions of Cosmos SD
88

99
Remove `RandomizedParams` from `AppModuleSimulation` interface. Previously, it used to generate random parameter changes during simulations, however, it does so through ParamChangeProposal which is now legacy. Since all modules were migrated, we can now safely remove this from `AppModuleSimulation` interface.
1010

11+
Moreover, to support the `MsgUpdateParams` governance proposals for each modules, `AppModuleSimulation` now defines a `AppModule.ProposalMsgs` method in addition to `AppModule.ProposalContents`. That method defines the messages that can be used to submit a proposal and that should be tested in simulation.
12+
13+
When a module has no proposal messages or proposal content to be tested by simulation, the `AppModule.ProposalMsgs` and `AppModule.ProposalContents` methods can be deleted.
14+
1115
### gRPC
1216

1317
A new gRPC service, `proto/cosmos/base/node/v1beta1/query.proto`, has been introduced

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ require (
5858
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6
5959
google.golang.org/grpc v1.51.0
6060
google.golang.org/protobuf v1.28.1
61+
gotest.tools/v3 v3.4.0
6162
pgregory.net/rapid v0.5.3
6263
sigs.k8s.io/yaml v1.3.0
6364
)

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,7 @@ gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C
12611261
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
12621262
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
12631263
gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o=
1264+
gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g=
12641265
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
12651266
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
12661267
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

testutil/sims/simulation_helpers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ func SimulationOperations(app runtime.AppI, cdc codec.JSONCodec, config simtypes
6363
}
6464
}
6565

66-
simState.Contents = app.SimulationManager().GetProposalContents(simState)
66+
simState.LegacyProposalContents = app.SimulationManager().GetProposalContents(simState) //nolint:staticcheck
67+
simState.ProposalMsgs = app.SimulationManager().GetProposalMsgs(simState)
6768
return app.SimulationManager().WeightedOperations(simState)
6869
}
6970

types/module/simulation.go

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,25 @@ type AppModuleSimulation interface {
1919
// randomized genesis states
2020
GenerateGenesisState(input *SimulationState)
2121

22-
// content functions used to simulate governance proposals
23-
ProposalContents(simState SimulationState) []simulation.WeightedProposalContent
24-
2522
// register a func to decode the each module's defined types from their corresponding store key
2623
RegisterStoreDecoder(sdk.StoreDecoderRegistry)
2724

2825
// simulation operations (i.e msgs) with their respective weight
2926
WeightedOperations(simState SimulationState) []simulation.WeightedOperation
3027
}
3128

29+
// HasProposalMsgs defines the messages that can be used to simulate governance (v1) proposals
30+
type HasProposalMsgs interface {
31+
// msg functions used to simulate governance proposals
32+
ProposalMsgs(simState SimulationState) []simulation.WeightedProposalMsg
33+
}
34+
35+
// HasProposalContents defines the contents that can be used to simulate legacy governance (v1beta1) proposals
36+
type HasProposalContents interface {
37+
// content functions used to simulate governance proposals
38+
ProposalContents(simState SimulationState) []simulation.WeightedProposalContent //nolint:staticcheck
39+
}
40+
3241
// SimulationManager defines a simulation manager that provides the high level utility
3342
// for managing and executing simulation functionalities for a group of modules
3443
type SimulationManager struct {
@@ -78,12 +87,28 @@ func NewSimulationManagerFromAppModules(modules map[string]interface{}, override
7887
return NewSimulationManager(simModules...)
7988
}
8089

90+
// Deprecated: Use GetProposalMsgs instead.
8191
// GetProposalContents returns each module's proposal content generator function
8292
// with their default operation weight and key.
8393
func (sm *SimulationManager) GetProposalContents(simState SimulationState) []simulation.WeightedProposalContent {
8494
wContents := make([]simulation.WeightedProposalContent, 0, len(sm.Modules))
8595
for _, module := range sm.Modules {
86-
wContents = append(wContents, module.ProposalContents(simState)...)
96+
if module, ok := module.(HasProposalContents); ok {
97+
wContents = append(wContents, module.ProposalContents(simState)...)
98+
}
99+
}
100+
101+
return wContents
102+
}
103+
104+
// GetProposalMsgs returns each module's proposal msg generator function
105+
// with their default operation weight and key.
106+
func (sm *SimulationManager) GetProposalMsgs(simState SimulationState) []simulation.WeightedProposalMsg {
107+
wContents := make([]simulation.WeightedProposalMsg, 0, len(sm.Modules))
108+
for _, module := range sm.Modules {
109+
if module, ok := module.(HasProposalMsgs); ok {
110+
wContents = append(wContents, module.ProposalMsgs(simState)...)
111+
}
87112
}
88113

89114
return wContents
@@ -117,15 +142,18 @@ func (sm *SimulationManager) WeightedOperations(simState SimulationState) []simu
117142
// SimulationState is the input parameters used on each of the module's randomized
118143
// GenesisState generator function
119144
type SimulationState struct {
120-
AppParams simulation.AppParams
121-
Cdc codec.JSONCodec // application codec
122-
Rand *rand.Rand // random number
123-
GenState map[string]json.RawMessage // genesis state
124-
Accounts []simulation.Account // simulation accounts
125-
InitialStake sdkmath.Int // initial coins per account
126-
NumBonded int64 // number of initially bonded accounts
127-
GenTimestamp time.Time // genesis timestamp
128-
UnbondTime time.Duration // staking unbond time stored to use it as the slashing maximum evidence duration
129-
ParamChanges []simulation.ParamChange // simulated parameter changes from modules
130-
Contents []simulation.WeightedProposalContent // proposal content generator functions with their default weight and app sim key
145+
AppParams simulation.AppParams
146+
Cdc codec.JSONCodec // application codec
147+
Rand *rand.Rand // random number
148+
GenState map[string]json.RawMessage // genesis state
149+
Accounts []simulation.Account // simulation accounts
150+
InitialStake sdkmath.Int // initial coins per account
151+
NumBonded int64 // number of initially bonded accounts
152+
BondDenom string // denom to be used as default
153+
GenTimestamp time.Time // genesis timestamp
154+
UnbondTime time.Duration // staking unbond time stored to use it as the slashing maximum evidence duration
155+
LegacyParamChange []simulation.LegacyParamChange // simulated parameter changes from modules
156+
//nolint:staticcheck
157+
LegacyProposalContents []simulation.WeightedProposalContent // proposal content generator functions with their default weight and app sim key
158+
ProposalMsgs []simulation.WeightedProposalMsg // proposal msg generator functions with their default weight and app sim key
131159
}

types/simulation/types.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ import (
1111
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
1212
)
1313

14+
// Deprecated: Use WeightedProposalMsg instead.
1415
type WeightedProposalContent interface {
1516
AppParamsKey() string // key used to retrieve the value of the weight from the simulation application params
1617
DefaultWeight() int // default weight
1718
ContentSimulatorFn() ContentSimulatorFn // content simulator function
1819
}
1920

21+
// Deprecated: Use MsgSimulatorFn instead.
2022
type ContentSimulatorFn func(r *rand.Rand, ctx sdk.Context, accs []Account) Content
2123

24+
// Deprecated: Use MsgSimulatorFn instead.
2225
type Content interface {
2326
GetTitle() string
2427
GetDescription() string
@@ -28,9 +31,17 @@ type Content interface {
2831
String() string
2932
}
3033

34+
type WeightedProposalMsg interface {
35+
AppParamsKey() string // key used to retrieve the value of the weight from the simulation application params
36+
DefaultWeight() int // default weight
37+
MsgSimulatorFn() MsgSimulatorFn // msg simulator function
38+
}
39+
40+
type MsgSimulatorFn func(r *rand.Rand, ctx sdk.Context, accs []Account) sdk.Msg
41+
3142
type SimValFn func(r *rand.Rand) string
3243

33-
type ParamChange interface {
44+
type LegacyParamChange interface {
3445
Subspace() string
3546
Key() string
3647
SimValue() SimValFn

x/auth/module.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ func (am AppModule) GenerateGenesisState(simState *module.SimulationState) {
178178
simulation.RandomizedGenState(simState, am.randGenAccountsFn)
179179
}
180180

181-
// ProposalContents doesn't return any content functions for governance proposals.
182-
func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
183-
return nil
181+
// ProposalMsgs returns msgs used for governance proposals for simulations.
182+
func (AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg {
183+
return simulation.ProposalMsgs()
184184
}
185185

186186
// RegisterStoreDecoder registers a decoder for auth module's types

x/auth/simulation/params.go

Lines changed: 0 additions & 40 deletions
This file was deleted.

x/auth/simulation/params_test.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)