Skip to content
This repository was archived by the owner on Aug 1, 2024. It is now read-only.
/ cosmos-sdk Public archive
forked from cosmos/cosmos-sdk

Commit 0b2e6a8

Browse files
julienrbrttsenart
authored andcommitted
fix: add simulation tests for new param change (cosmos#14728)
1 parent 8786b8d commit 0b2e6a8

Some content is hidden

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

61 files changed

+933
-712
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,11 @@ Ref: https://keepachangelog.com/en/1.0.0/
168168

169169
### API Breaking Changes
170170

171+
* (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`.
171172
* (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.
172173
* (x/gov) [#14782](https://github.com/cosmos/cosmos-sdk/pull/14782) Move the `metadata` argument in `govv1.NewProposal` alongside `title` and `summary`.
173174
* (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`.
174-
* (x/simulation) [#14751](https://github.com/cosmos/cosmos-sdk/pull/14751) Remove the `MsgType` field from `simulation.OperationInput` struct.
175+
* (simulation) [#14751](https://github.com/cosmos/cosmos-sdk/pull/14751) Remove the `MsgType` field from `simulation.OperationInput` struct.
175176
* (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.
176177
* (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`.
177178
* (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`.

UPGRADING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ All the upgrade imports are now renamed to use `cosmossdk.io/x/upgrade` instead
101101

102102
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.
103103

104+
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.
105+
106+
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.
107+
104108
### gRPC
105109

106110
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
@@ -68,7 +68,8 @@ func SimulationOperations(app runtime.AppI, cdc codec.JSONCodec, config simtypes
6868
}
6969
}
7070

71-
simState.Contents = app.SimulationManager().GetProposalContents(simState)
71+
simState.LegacyProposalContents = app.SimulationManager().GetProposalContents(simState) //nolint:staticcheck
72+
simState.ProposalMsgs = app.SimulationManager().GetProposalMsgs(simState)
7273
return app.SimulationManager().WeightedOperations(simState)
7374
}
7475

types/module/simulation.go

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

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

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

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

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

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

types/simulation/types.go

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

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

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

25+
// Deprecated: Use MsgSimulatorFn instead.
2326
type Content interface {
2427
GetTitle() string
2528
GetDescription() string
@@ -29,9 +32,17 @@ type Content interface {
2932
String() string
3033
}
3134

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

34-
type ParamChange interface {
45+
type LegacyParamChange interface {
3546
Subspace() string
3647
Key() string
3748
SimValue() SimValFn

x/auth/module.go

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

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

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

x/auth/simulation/params.go

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

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+
}

0 commit comments

Comments
 (0)