Skip to content

Commit 94b4db9

Browse files
julienrbrtmergify[bot]
authored andcommitted
fix: add simulation tests for new param change (#14728)
(cherry picked from commit d3c3194) # Conflicts: # CHANGELOG.md # types/module/simulation.go # x/auth/simulation/params.go # x/bank/simulation/params.go # x/distribution/simulation/params.go # x/gov/simulation/operations.go # x/gov/simulation/operations_test.go # x/gov/simulation/params.go # x/mint/simulation/params.go # x/slashing/simulation/params.go # x/staking/simulation/params.go
1 parent 33986a3 commit 94b4db9

Some content is hidden

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

54 files changed

+979
-426
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,22 @@ Ref: https://keepachangelog.com/en/1.0.0/
222222

223223
### API Breaking Changes
224224

225+
<<<<<<< HEAD
226+
=======
227+
* (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`.
228+
* (x/upgrade) [14764](https://github.com/cosmos/cosmos-sdk/pull/14764) The `x/upgrade` module is extracted to have a separate go.mod file which allows it to be a standalone module.
229+
* (x/gov) [#14782](https://github.com/cosmos/cosmos-sdk/pull/14782) Move the `metadata` argument in `govv1.NewProposal` alongside `title` and `summary`.
230+
* (store) [#14746](https://github.com/cosmos/cosmos-sdk/pull/14746) Extract Store in its own go.mod and rename the package to `cosmossdk.io/store`.
231+
* (simulation) [#14751](https://github.com/cosmos/cosmos-sdk/pull/14751) Remove the `MsgType` field from `simulation.OperationInput` struct.
232+
* (crypto/keyring) [#13734](https://github.com/cosmos/cosmos-sdk/pull/13834) The keyring's `Sign` method now takes a new `signMode` argument. It is only used if the signing key is a Ledger hardware device. You can set it to 0 in all other cases.
233+
* (x/evidence) [14724](https://github.com/cosmos/cosmos-sdk/pull/14724) Extract Evidence in its own go.mod and rename the package to `cosmossdk.io/x/evidence`.
234+
* (x/nft) [#14725](https://github.com/cosmos/cosmos-sdk/pull/14725) Extract NFT in its own go.mod and rename the package to `cosmossdk.io/x/nft`.
235+
* (tx) [#14634](https://github.com/cosmos/cosmos-sdk/pull/14634) Move the `tx` go module to `x/tx`.
236+
* (snapshots) [#14597](https://github.com/cosmos/cosmos-sdk/pull/14597) Move `snapshots` to `store/snapshots`, rename and bump proto package to v1.
237+
* (crypto/keyring) [#14151](https://github.com/cosmos/cosmos-sdk/pull/14151) Move keys presentation from `crypto/keyring` to `client/keys`
238+
* (modules) [#13850](https://github.com/cosmos/cosmos-sdk/pull/13850) and [#14046](https://github.com/cosmos/cosmos-sdk/pull/14046) Remove gogoproto stringer annotations. This removes the custom `String()` methods on all types that were using the annotations.
239+
* (x/auth) [#13850](https://github.com/cosmos/cosmos-sdk/pull/13850/) Remove `MarshalYAML` methods from module (`x/...`) types.
240+
>>>>>>> d3c319418 (fix: add simulation tests for new param change (#14728))
225241
* (x/auth) [#13877](https://github.com/cosmos/cosmos-sdk/pull/13877) Rename `AccountKeeper`'s `GetNextAccountNumber` to `NextAccountNumber`.
226242
* (x/evidence) [#13740](https://github.com/cosmos/cosmos-sdk/pull/13740) The `NewQueryEvidenceRequest` function now takes `hash` as a HEX encoded `string`.
227243
* (server) [#13485](https://github.com/cosmos/cosmos-sdk/pull/13485) The `Application` service now requires the `RegisterNodeService` method to be implemented.

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

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: 46 additions & 4 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,6 +142,7 @@ 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 {
145+
<<<<<<< HEAD
120146
AppParams simulation.AppParams
121147
Cdc codec.JSONCodec // application codec
122148
Rand *rand.Rand // random number
@@ -128,4 +154,20 @@ type SimulationState struct {
128154
UnbondTime time.Duration // staking unbond time stored to use it as the slashing maximum evidence duration
129155
ParamChanges []simulation.ParamChange // simulated parameter changes from modules
130156
Contents []simulation.WeightedProposalContent // proposal content generator functions with their default weight and app sim key
157+
=======
158+
AppParams simulation.AppParams
159+
Cdc codec.JSONCodec // application codec
160+
Rand *rand.Rand // random number
161+
GenState map[string]json.RawMessage // genesis state
162+
Accounts []simulation.Account // simulation accounts
163+
InitialStake sdkmath.Int // initial coins per account
164+
NumBonded int64 // number of initially bonded accounts
165+
BondDenom string // denom to be used as default
166+
GenTimestamp time.Time // genesis timestamp
167+
UnbondTime time.Duration // staking unbond time stored to use it as the slashing maximum evidence duration
168+
LegacyParamChange []simulation.LegacyParamChange // simulated parameter changes from modules
169+
//nolint:staticcheck
170+
LegacyProposalContents []simulation.WeightedProposalContent // proposal content generator functions with their default weight and app sim key
171+
ProposalMsgs []simulation.WeightedProposalMsg // proposal msg generator functions with their default weight and app sim key
172+
>>>>>>> d3c319418 (fix: add simulation tests for new param change (#14728))
131173
}

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_test.go

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

x/auth/simulation/proposals.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package simulation
2+
3+
import (
4+
"math/rand"
5+
6+
sdk "github.com/cosmos/cosmos-sdk/types"
7+
"github.com/cosmos/cosmos-sdk/types/address"
8+
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
9+
"github.com/cosmos/cosmos-sdk/x/auth/types"
10+
"github.com/cosmos/cosmos-sdk/x/simulation"
11+
)
12+
13+
// Simulation operation weights constants
14+
const (
15+
DefaultWeightMsgUpdateParams int = 100
16+
17+
OpWeightMsgUpdateParams = "op_weight_msg_update_params" //nolint:gosec
18+
)
19+
20+
// ProposalMsgs defines the module weighted proposals' contents
21+
func ProposalMsgs() []simtypes.WeightedProposalMsg {
22+
return []simtypes.WeightedProposalMsg{
23+
simulation.NewWeightedProposalMsg(
24+
OpWeightMsgUpdateParams,
25+
DefaultWeightMsgUpdateParams,
26+
SimulateMsgUpdateParams,
27+
),
28+
}
29+
}
30+
31+
// SimulateMsgUpdateParams returns a random MsgUpdateParams
32+
func SimulateMsgUpdateParams(r *rand.Rand, _ sdk.Context, _ []simtypes.Account) sdk.Msg {
33+
// use the default gov module account address as authority
34+
var authority sdk.AccAddress = address.Module("gov")
35+
36+
params := types.DefaultParams()
37+
params.MaxMemoCharacters = uint64(simtypes.RandIntBetween(r, 1, 1000))
38+
params.TxSigLimit = uint64(simtypes.RandIntBetween(r, 1, 1000))
39+
params.TxSizeCostPerByte = uint64(simtypes.RandIntBetween(r, 1, 1000))
40+
params.SigVerifyCostED25519 = uint64(simtypes.RandIntBetween(r, 1, 1000))
41+
params.SigVerifyCostSecp256k1 = uint64(simtypes.RandIntBetween(r, 1, 1000))
42+
43+
return &types.MsgUpdateParams{
44+
Authority: authority.String(),
45+
Params: params,
46+
}
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package simulation_test
2+
3+
import (
4+
"math/rand"
5+
"testing"
6+
7+
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
8+
"gotest.tools/v3/assert"
9+
10+
sdk "github.com/cosmos/cosmos-sdk/types"
11+
"github.com/cosmos/cosmos-sdk/types/address"
12+
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
13+
"github.com/cosmos/cosmos-sdk/x/auth/simulation"
14+
"github.com/cosmos/cosmos-sdk/x/auth/types"
15+
)
16+
17+
func TestProposalMsgs(t *testing.T) {
18+
// initialize parameters
19+
s := rand.NewSource(1)
20+
r := rand.New(s)
21+
22+
ctx := sdk.NewContext(nil, tmproto.Header{}, true, nil)
23+
accounts := simtypes.RandomAccounts(r, 3)
24+
25+
// execute ProposalMsgs function
26+
weightedProposalMsgs := simulation.ProposalMsgs()
27+
assert.Assert(t, len(weightedProposalMsgs) == 1)
28+
29+
w0 := weightedProposalMsgs[0]
30+
31+
// tests w0 interface:
32+
assert.Equal(t, simulation.OpWeightMsgUpdateParams, w0.AppParamsKey())
33+
assert.Equal(t, simulation.DefaultWeightMsgUpdateParams, w0.DefaultWeight())
34+
35+
msg := w0.MsgSimulatorFn()(r, ctx, accounts)
36+
msgUpdateParams, ok := msg.(*types.MsgUpdateParams)
37+
assert.Assert(t, ok)
38+
39+
assert.Equal(t, sdk.AccAddress(address.Module("gov")).String(), msgUpdateParams.Authority)
40+
assert.Equal(t, uint64(999), msgUpdateParams.Params.MaxMemoCharacters)
41+
assert.Equal(t, uint64(905), msgUpdateParams.Params.TxSigLimit)
42+
assert.Equal(t, uint64(151), msgUpdateParams.Params.TxSizeCostPerByte)
43+
assert.Equal(t, uint64(213), msgUpdateParams.Params.SigVerifyCostED25519)
44+
assert.Equal(t, uint64(539), msgUpdateParams.Params.SigVerifyCostSecp256k1)
45+
}

x/authz/module/module.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,6 @@ func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
204204
simulation.RandomizedGenState(simState)
205205
}
206206

207-
// ProposalContents returns all the authz content functions used to
208-
// simulate governance proposals.
209-
func (am AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
210-
return nil
211-
}
212-
213207
// RegisterStoreDecoder registers a decoder for authz module's types
214208
func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
215209
sdr[keeper.StoreKey] = simulation.NewDecodeStore(am.cdc)

0 commit comments

Comments
 (0)