This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
genesis.go
79 lines (66 loc) · 2.29 KB
/
genesis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package simulation
import (
"fmt"
"math/rand"
"time"
sdk "github.com/Pylons-tech/cosmos-sdk/types"
"github.com/Pylons-tech/cosmos-sdk/types/module"
simtypes "github.com/Pylons-tech/cosmos-sdk/types/simulation"
"github.com/Pylons-tech/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.v1beta1.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)
}
fmt.Printf("Selected randomly generated %s parameters:\n%s\n", feegrant.ModuleName, bz)
simState.GenState[feegrant.ModuleName] = bz
}