Skip to content

Commit 8748d09

Browse files
committed
Add simsx support to modules
1 parent 4b995aa commit 8748d09

Some content is hidden

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

57 files changed

+2079
-300
lines changed

simapp/sim_bench_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func BenchmarkFullAppSimulation(b *testing.B) {
4848
app := NewSimApp(logger, db, nil, true, appOptions, interBlockCacheOpt(), baseapp.SetChainID(simsx.SimAppChainID))
4949

5050
// run randomized simulation
51-
simParams,_, simErr := simulation.SimulateFromSeedX(
51+
simParams, _, simErr := simulation.SimulateFromSeedX(
5252
b,
5353
log.NewNopLogger(),
5454
os.Stdout,

simsx/delivery.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"math/rand"
88

99
"github.com/cosmos/cosmos-sdk/baseapp"
10-
1110
"github.com/cosmos/cosmos-sdk/client"
1211
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
1312
"github.com/cosmos/cosmos-sdk/testutil/sims"

simsx/environment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func NewChainDataSource(
289289
// AnyAccount returns a random SimAccount matching the filter criteria. Module accounts are excluded.
290290
// In case of an error or no matching account found, the reporter is set to skip and an empty value is returned.
291291
func (c *ChainDataSource) AnyAccount(r SimulationReporter, filters ...SimAccountFilter) SimAccount {
292-
acc := c.randomAccount(r, 5, filters...)
292+
acc := c.randomAccount(r, 1, filters...)
293293
return acc
294294
}
295295

simsx/runner.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"strings"
1010
"testing"
1111

12-
servertypes "github.com/cosmos/cosmos-sdk/server/types"
13-
1412
dbm "github.com/cosmos/cosmos-db"
1513
"github.com/stretchr/testify/require"
1614

@@ -21,6 +19,7 @@ import (
2119
"github.com/cosmos/cosmos-sdk/client/flags"
2220
"github.com/cosmos/cosmos-sdk/codec"
2321
"github.com/cosmos/cosmos-sdk/runtime"
22+
servertypes "github.com/cosmos/cosmos-sdk/server/types"
2423
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
2524
sdk "github.com/cosmos/cosmos-sdk/types"
2625
"github.com/cosmos/cosmos-sdk/types/module"

x/auth/module.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/cosmos/cosmos-sdk/client"
1717
"github.com/cosmos/cosmos-sdk/codec"
1818
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
19+
"github.com/cosmos/cosmos-sdk/simsx"
1920
sdk "github.com/cosmos/cosmos-sdk/types"
2021
"github.com/cosmos/cosmos-sdk/types/module"
2122
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
@@ -171,10 +172,16 @@ func (am AppModule) GenerateGenesisState(simState *module.SimulationState) {
171172
}
172173

173174
// ProposalMsgs returns msgs used for governance proposals for simulations.
175+
// Deprecated: migrate to ProposalMsgsX. This method is ignored when ProposalMsgsX exists and will be removed in the future.
174176
func (AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg {
175177
return simulation.ProposalMsgs()
176178
}
177179

180+
// ProposalMsgsX registers governance proposal messages in the simulation registry.
181+
func (AppModule) ProposalMsgsX(weights simsx.WeightSource, reg simsx.Registry) {
182+
reg.Add(weights.Get("msg_update_params", 100), simulation.MsgUpdateParamsFactory())
183+
}
184+
178185
// RegisterStoreDecoder registers a decoder for auth module's types
179186
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
180187
sdr[types.StoreKey] = simtypes.NewStoreDecoderFuncFromCollectionsSchema(am.accountKeeper.Schema)

x/auth/simulation/genesis.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package simulation
22

33
import (
4-
"encoding/json"
5-
"fmt"
64
"math/rand"
75

86
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -112,11 +110,5 @@ func RandomizedGenState(simState *module.SimulationState, randGenAccountsFn type
112110
genesisAccs := randGenAccountsFn(simState)
113111

114112
authGenesis := types.NewGenesisState(params, genesisAccs)
115-
116-
bz, err := json.MarshalIndent(&authGenesis.Params, "", " ")
117-
if err != nil {
118-
panic(err)
119-
}
120-
fmt.Printf("Selected randomly generated auth parameters:\n%s\n", bz)
121113
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(authGenesis)
122114
}

x/auth/simulation/msg_factory.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package simulation
2+
3+
import (
4+
"context"
5+
6+
"github.com/cosmos/cosmos-sdk/simsx"
7+
"github.com/cosmos/cosmos-sdk/x/auth/types"
8+
)
9+
10+
func MsgUpdateParamsFactory() simsx.SimMsgFactoryFn[*types.MsgUpdateParams] {
11+
return func(_ context.Context, testData *simsx.ChainDataSource, reporter simsx.SimulationReporter) ([]simsx.SimAccount, *types.MsgUpdateParams) {
12+
r := testData.Rand()
13+
params := types.DefaultParams()
14+
params.MaxMemoCharacters = r.Uint64InRange(1, 1000)
15+
params.TxSigLimit = r.Uint64InRange(1, 1000)
16+
params.TxSizeCostPerByte = r.Uint64InRange(1, 1000)
17+
params.SigVerifyCostED25519 = r.Uint64InRange(1, 1000)
18+
params.SigVerifyCostSecp256k1 = r.Uint64InRange(1, 1000)
19+
20+
return nil, &types.MsgUpdateParams{
21+
Authority: testData.ModuleAccountAddress(reporter, "gov"),
22+
Params: params,
23+
}
24+
}
25+
}

x/auth/simulation/proposals.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import (
1111
)
1212

1313
// Simulation operation weights constants
14+
// Deprecated: will be removed in the future
1415
const (
1516
DefaultWeightMsgUpdateParams int = 100
16-
17-
OpWeightMsgUpdateParams = "op_weight_msg_update_params"
17+
OpWeightMsgUpdateParams = "op_weight_msg_update_params"
1818
)
1919

2020
// ProposalMsgs defines the module weighted proposals' contents
21+
// Deprecated: migrate to MsgUpdateParamsFactory instead
2122
func ProposalMsgs() []simtypes.WeightedProposalMsg {
2223
return []simtypes.WeightedProposalMsg{
2324
simulation.NewWeightedProposalMsg(
@@ -29,6 +30,7 @@ func ProposalMsgs() []simtypes.WeightedProposalMsg {
2930
}
3031

3132
// SimulateMsgUpdateParams returns a random MsgUpdateParams
33+
// Deprecated: will be removed in the future
3234
func SimulateMsgUpdateParams(r *rand.Rand, _ sdk.Context, _ []simtypes.Account) sdk.Msg {
3335
// use the default gov module account address as authority
3436
var authority sdk.AccAddress = address.Module("gov")

x/authz/module/module.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
sdkclient "github.com/cosmos/cosmos-sdk/client"
2020
"github.com/cosmos/cosmos-sdk/codec"
2121
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
22+
"github.com/cosmos/cosmos-sdk/simsx"
2223
sdk "github.com/cosmos/cosmos-sdk/types"
2324
"github.com/cosmos/cosmos-sdk/types/module"
2425
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
@@ -196,10 +197,18 @@ func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
196197
}
197198

198199
// WeightedOperations returns the all the gov module operations with their respective weights.
200+
// Deprecated: migrate to WeightedOperationsX. This method is ignored when WeightedOperationsX exists and will be removed in the future
199201
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
200202
return simulation.WeightedOperations(
201203
am.registry,
202204
simState.AppParams, simState.Cdc, simState.TxConfig,
203205
am.accountKeeper, am.bankKeeper, am.keeper,
204206
)
205207
}
208+
209+
// WeightedOperationsX registers weighted authz module operations for simulation.
210+
func (am AppModule) WeightedOperationsX(weights simsx.WeightSource, reg simsx.Registry) {
211+
reg.Add(weights.Get("msg_grant", 100), simulation.MsgGrantFactory())
212+
reg.Add(weights.Get("msg_revoke", 90), simulation.MsgRevokeFactory(am.keeper))
213+
reg.Add(weights.Get("msg_exec", 90), simulation.MsgExecFactory(am.keeper))
214+
}

x/authz/simulation/msg_factory.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package simulation
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/cosmos/cosmos-sdk/simsx"
8+
sdk "github.com/cosmos/cosmos-sdk/types"
9+
"github.com/cosmos/cosmos-sdk/x/authz"
10+
"github.com/cosmos/cosmos-sdk/x/authz/keeper"
11+
banktype "github.com/cosmos/cosmos-sdk/x/bank/types"
12+
)
13+
14+
func MsgGrantFactory() simsx.SimMsgFactoryFn[*authz.MsgGrant] {
15+
return func(ctx context.Context, testData *simsx.ChainDataSource, reporter simsx.SimulationReporter) ([]simsx.SimAccount, *authz.MsgGrant) {
16+
granter := testData.AnyAccount(reporter, simsx.WithSpendableBalance())
17+
grantee := testData.AnyAccount(reporter, simsx.ExcludeAccounts(granter))
18+
spendLimit := granter.LiquidBalance().RandSubsetCoins(reporter, simsx.WithSendEnabledCoins())
19+
20+
r := testData.Rand()
21+
var expiration *time.Time
22+
if t1 := r.Timestamp(); !t1.Before(simsx.BlockTime(ctx)) {
23+
expiration = &t1
24+
}
25+
// pick random authorization
26+
authorizations := []authz.Authorization{
27+
banktype.NewSendAuthorization(spendLimit, nil),
28+
authz.NewGenericAuthorization(sdk.MsgTypeURL(&banktype.MsgSend{})),
29+
}
30+
randomAuthz := simsx.OneOf(r, authorizations)
31+
32+
msg, err := authz.NewMsgGrant(granter.Address, grantee.Address, randomAuthz, expiration)
33+
if err != nil {
34+
reporter.Skip(err.Error())
35+
return nil, nil
36+
}
37+
return []simsx.SimAccount{granter}, msg
38+
}
39+
}
40+
41+
func MsgExecFactory(k keeper.Keeper) simsx.SimMsgFactoryFn[*authz.MsgExec] {
42+
return func(ctx context.Context, testData *simsx.ChainDataSource, reporter simsx.SimulationReporter) ([]simsx.SimAccount, *authz.MsgExec) {
43+
bankSendOnlyFilter := func(a authz.Authorization) bool {
44+
_, ok := a.(*banktype.SendAuthorization)
45+
return ok
46+
}
47+
granterAddr, granteeAddr, gAuthz := findGrant(ctx, k, reporter, bankSendOnlyFilter)
48+
granter := testData.GetAccountbyAccAddr(reporter, granterAddr)
49+
grantee := testData.GetAccountbyAccAddr(reporter, granteeAddr)
50+
if reporter.IsSkipped() {
51+
return nil, nil
52+
}
53+
amount := granter.LiquidBalance().RandSubsetCoins(reporter, simsx.WithSendEnabledCoins())
54+
amount = amount.Min(gAuthz.(*banktype.SendAuthorization).SpendLimit)
55+
56+
payloadMsg := []sdk.Msg{banktype.NewMsgSend(granter.Address, grantee.Address, amount)}
57+
msgExec := authz.NewMsgExec(grantee.Address, payloadMsg)
58+
return []simsx.SimAccount{grantee}, &msgExec
59+
}
60+
}
61+
62+
func MsgRevokeFactory(k keeper.Keeper) simsx.SimMsgFactoryFn[*authz.MsgRevoke] {
63+
return func(ctx context.Context, testData *simsx.ChainDataSource, reporter simsx.SimulationReporter) ([]simsx.SimAccount, *authz.MsgRevoke) {
64+
granterAddr, granteeAddr, auth := findGrant(ctx, k, reporter)
65+
granter := testData.GetAccountbyAccAddr(reporter, granterAddr)
66+
grantee := testData.GetAccountbyAccAddr(reporter, granteeAddr)
67+
if reporter.IsSkipped() {
68+
return nil, nil
69+
}
70+
msgExec := authz.NewMsgRevoke(granter.Address, grantee.Address, auth.MsgTypeURL())
71+
return []simsx.SimAccount{granter}, &msgExec
72+
}
73+
}
74+
75+
func findGrant(
76+
ctx context.Context,
77+
k keeper.Keeper,
78+
reporter simsx.SimulationReporter,
79+
acceptFilter ...func(a authz.Authorization) bool,
80+
) (granterAddr, granteeAddr sdk.AccAddress, auth authz.Authorization) {
81+
var innerErr error
82+
k.IterateGrants(ctx, func(granter, grantee sdk.AccAddress, grant authz.Grant) bool {
83+
a, err2 := grant.GetAuthorization()
84+
if err2 != nil {
85+
innerErr = err2
86+
return true
87+
}
88+
for _, filter := range acceptFilter {
89+
if !filter(a) {
90+
return false
91+
}
92+
}
93+
granterAddr, granteeAddr, auth = granter, grantee, a
94+
return true
95+
})
96+
if innerErr != nil {
97+
reporter.Skip(innerErr.Error())
98+
return
99+
}
100+
if auth == nil {
101+
reporter.Skip("no grant found")
102+
}
103+
return
104+
}

0 commit comments

Comments
 (0)