## Description Closes: #11331 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
78 lines
2.2 KiB
Go
78 lines
2.2 KiB
Go
package simulation
|
|
|
|
import (
|
|
"math/rand"
|
|
"time"
|
|
|
|
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/feegrant"
|
|
)
|
|
|
|
// genFeeGrants returns a slice of randomly generated allowances.
|
|
func genFeeGrants(r *rand.Rand, accounts []simtypes.Account) []feegrant.Grant {
|
|
allowances := make([]feegrant.Grant, len(accounts)-1)
|
|
for i := 0; i < len(accounts)-1; i++ {
|
|
granter := accounts[i].Address
|
|
grantee := accounts[i+1].Address
|
|
allowances[i] = generateRandomAllowances(granter, grantee, r)
|
|
}
|
|
return allowances
|
|
}
|
|
|
|
func generateRandomAllowances(granter, grantee sdk.AccAddress, r *rand.Rand) feegrant.Grant {
|
|
allowances := make([]feegrant.Grant, 3)
|
|
spendLimit := sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100)))
|
|
periodSpendLimit := sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(10)))
|
|
|
|
basic := feegrant.BasicAllowance{
|
|
SpendLimit: spendLimit,
|
|
}
|
|
|
|
basicAllowance, err := feegrant.NewGrant(granter, grantee, &basic)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
allowances[0] = basicAllowance
|
|
|
|
periodicAllowance, err := feegrant.NewGrant(granter, grantee, &feegrant.PeriodicAllowance{
|
|
Basic: basic,
|
|
PeriodSpendLimit: periodSpendLimit,
|
|
Period: time.Hour,
|
|
})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
allowances[1] = periodicAllowance
|
|
|
|
filteredAllowance, err := feegrant.NewGrant(granter, grantee, &feegrant.AllowedMsgAllowance{
|
|
Allowance: basicAllowance.GetAllowance(),
|
|
AllowedMessages: []string{"/cosmos.gov.v1.MsgSubmitProposal"},
|
|
})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
allowances[2] = filteredAllowance
|
|
|
|
return allowances[r.Intn(len(allowances))]
|
|
}
|
|
|
|
// RandomizedGenState generates a random GenesisState for feegrant
|
|
func RandomizedGenState(simState *module.SimulationState) {
|
|
var feegrants []feegrant.Grant
|
|
|
|
simState.AppParams.GetOrGenerate(
|
|
simState.Cdc, "feegrant", &feegrants, simState.Rand,
|
|
func(r *rand.Rand) { feegrants = genFeeGrants(r, simState.Accounts) },
|
|
)
|
|
|
|
feegrantGenesis := feegrant.NewGenesisState(feegrants)
|
|
bz, err := simState.Cdc.MarshalJSON(feegrantGenesis)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
simState.GenState[feegrant.ModuleName] = bz
|
|
}
|