github.com/cosmos/cosmos-sdk@v0.50.10/x/authz/simulation/genesis.go (about) 1 package simulation 2 3 import ( 4 "math/rand" 5 "time" 6 7 sdkmath "cosmossdk.io/math" 8 9 codectypes "github.com/cosmos/cosmos-sdk/codec/types" 10 sdk "github.com/cosmos/cosmos-sdk/types" 11 "github.com/cosmos/cosmos-sdk/types/module" 12 simtypes "github.com/cosmos/cosmos-sdk/types/simulation" 13 "github.com/cosmos/cosmos-sdk/x/authz" 14 banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" 15 v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" 16 ) 17 18 // genGrant returns a slice of authorization grants. 19 func genGrant(r *rand.Rand, accounts []simtypes.Account, genT time.Time) []authz.GrantAuthorization { 20 authorizations := make([]authz.GrantAuthorization, len(accounts)-1) 21 for i := 0; i < len(accounts)-1; i++ { 22 granter := accounts[i] 23 grantee := accounts[i+1] 24 var expiration *time.Time 25 if i%3 != 0 { // generate some grants with no expire time 26 e := genT.AddDate(1, 0, 0) 27 expiration = &e 28 } 29 authorizations[i] = authz.GrantAuthorization{ 30 Granter: granter.Address.String(), 31 Grantee: grantee.Address.String(), 32 Authorization: generateRandomGrant(r), 33 Expiration: expiration, 34 } 35 } 36 37 return authorizations 38 } 39 40 func generateRandomGrant(r *rand.Rand) *codectypes.Any { 41 authorizations := make([]*codectypes.Any, 2) 42 sendAuthz := banktypes.NewSendAuthorization(sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(1000))), nil) 43 authorizations[0] = newAnyAuthorization(sendAuthz) 44 authorizations[1] = newAnyAuthorization(authz.NewGenericAuthorization(sdk.MsgTypeURL(&v1.MsgSubmitProposal{}))) 45 46 return authorizations[r.Intn(len(authorizations))] 47 } 48 49 func newAnyAuthorization(a authz.Authorization) *codectypes.Any { 50 any, err := codectypes.NewAnyWithValue(a) 51 if err != nil { 52 panic(err) 53 } 54 55 return any 56 } 57 58 // RandomizedGenState generates a random GenesisState for authz. 59 func RandomizedGenState(simState *module.SimulationState) { 60 var grants []authz.GrantAuthorization 61 simState.AppParams.GetOrGenerate("authz", &grants, simState.Rand, func(r *rand.Rand) { 62 grants = genGrant(r, simState.Accounts, simState.GenTimestamp) 63 }) 64 65 authzGrantsGenesis := authz.NewGenesisState(grants) 66 67 simState.GenState[authz.ModuleName] = simState.Cdc.MustMarshalJSON(authzGrantsGenesis) 68 }