cosmos-sdk/x/authz/simulation/genesis.go
MD Aleem 21295060ab
fix: remove time.now check from authz (#11129)
* Merge pull request from GHSA-2p6r-37p9-89p2

* test: adding authz grant tests

* fix TestCLITxGrantAuthorization/Invalid_expiration_time test case

* comment out the test

* reenable test

* master update

* update changelog and cli test

* fix keeper tests

* fix decoder test

* cleaning

* wip - tests

* fix cli test

* add a comment

* update TestMsgGrantAuthorization

* udpate changelog

* fix sims

* fix sims

* update changelog

* Update x/authz/authorization_grant.go

Co-authored-by: Robert Zaremba <robert@zaremba.ch>
Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>
2022-02-07 15:38:45 +01:00

62 lines
2.0 KiB
Go

package simulation
import (
"math/rand"
"time"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta2"
)
// genGrant returns a slice of authorization grants.
func genGrant(r *rand.Rand, accounts []simtypes.Account, genT time.Time) []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),
Expiration: genT.AddDate(1, 0, 0),
}
}
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)))))
authorizations[1] = newAnyAuthorization(authz.NewGenericAuthorization(sdk.MsgTypeURL(&v1beta2.MsgSubmitProposal{})))
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
}
// RandomizedGenState generates a random GenesisState for authz.
func RandomizedGenState(simState *module.SimulationState) {
var grants []authz.GrantAuthorization
simState.AppParams.GetOrGenerate(
simState.Cdc, "authz", &grants, simState.Rand,
func(r *rand.Rand) { grants = genGrant(r, simState.Accounts, simState.GenTimestamp) },
)
authzGrantsGenesis := authz.NewGenesisState(grants)
simState.GenState[authz.ModuleName] = simState.Cdc.MustMarshalJSON(authzGrantsGenesis)
}