2021-01-25 08:41:30 -08:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
|
2021-05-11 08:33:33 -07:00
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-01-25 08:41:30 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
|
|
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
2021-05-06 11:23:48 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/authz"
|
2021-05-11 08:33:33 -07:00
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
2022-01-04 08:10:02 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta2"
|
2021-01-25 08:41:30 -08:00
|
|
|
)
|
|
|
|
|
2021-05-11 08:33:33 -07:00
|
|
|
// genGrant returns a slice of authorization grants.
|
|
|
|
func genGrant(r *rand.Rand, accounts []simtypes.Account) []authz.GrantAuthorization {
|
|
|
|
authorizations := make([]authz.GrantAuthorization, len(accounts)-1)
|
|
|
|
for i := 0; i < len(accounts)-1; i++ {
|
|
|
|
granter := accounts[i]
|
|
|
|
grantee := accounts[i+1]
|
|
|
|
authorizations[i] = authz.GrantAuthorization{
|
|
|
|
Granter: granter.Address.String(),
|
|
|
|
Grantee: grantee.Address.String(),
|
|
|
|
Authorization: generateRandomGrant(r),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return authorizations
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateRandomGrant(r *rand.Rand) *codectypes.Any {
|
|
|
|
authorizations := make([]*codectypes.Any, 2)
|
|
|
|
authorizations[0] = newAnyAuthorization(banktypes.NewSendAuthorization(sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000)))))
|
2022-01-04 08:10:02 -08:00
|
|
|
authorizations[1] = newAnyAuthorization(authz.NewGenericAuthorization(sdk.MsgTypeURL(&v1beta2.MsgSubmitProposal{})))
|
2021-05-11 08:33:33 -07:00
|
|
|
|
|
|
|
return authorizations[r.Intn(len(authorizations))]
|
|
|
|
}
|
|
|
|
|
|
|
|
func newAnyAuthorization(a authz.Authorization) *codectypes.Any {
|
|
|
|
any, err := codectypes.NewAnyWithValue(a)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return any
|
2021-01-25 08:41:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RandomizedGenState generates a random GenesisState for authz.
|
|
|
|
func RandomizedGenState(simState *module.SimulationState) {
|
2021-05-06 11:23:48 -07:00
|
|
|
var grants []authz.GrantAuthorization
|
2021-01-25 08:41:30 -08:00
|
|
|
simState.AppParams.GetOrGenerate(
|
2021-05-06 11:23:48 -07:00
|
|
|
simState.Cdc, "authz", &grants, simState.Rand,
|
2021-05-11 08:33:33 -07:00
|
|
|
func(r *rand.Rand) { grants = genGrant(r, simState.Accounts) },
|
2021-01-25 08:41:30 -08:00
|
|
|
)
|
|
|
|
|
2021-05-11 08:33:33 -07:00
|
|
|
authzGrantsGenesis := authz.NewGenesisState(grants)
|
2021-01-25 08:41:30 -08:00
|
|
|
|
2021-05-06 11:23:48 -07:00
|
|
|
simState.GenState[authz.ModuleName] = simState.Cdc.MustMarshalJSON(authzGrantsGenesis)
|
2021-01-25 08:41:30 -08:00
|
|
|
}
|