-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis.go
87 lines (75 loc) · 2.7 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
80
81
82
83
84
85
86
87
package simulation
import (
"encoding/json"
"fmt"
"math/rand"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/elysium-station/black/x/blackdist/types"
)
// SecondsPerYear is the number of seconds in a year
const (
SecondsPerYear = 31536000
// BaseAprPadding sets the minimum inflation to the calculated SPR inflation rate from being 0.0
BaseAprPadding = "0.000000003022265980"
)
// RandomizedGenState generates a random GenesisState for blackdist module
func RandomizedGenState(simState *module.SimulationState) {
params := genRandomParams(simState)
if err := params.Validate(); err != nil {
panic(err)
}
blackdistGenesis := types.NewGenesisState(params, types.DefaultPreviousBlockTime)
if err := blackdistGenesis.Validate(); err != nil {
panic(err)
}
bz, err := json.MarshalIndent(&blackdistGenesis, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Selected randomly generated %s parameters:\n%s\n", types.ModuleName, bz)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(blackdistGenesis)
}
func genRandomParams(simState *module.SimulationState) types.Params {
periods := genRandomPeriods(simState.Rand, simState.GenTimestamp)
params := types.NewParams(true, periods, types.DefaultInfraParams)
return params
}
func genRandomPeriods(r *rand.Rand, timestamp time.Time) []types.Period {
var periods []types.Period
numPeriods := simulation.RandIntBetween(r, 1, 10)
periodStart := timestamp
for i := 0; i < numPeriods; i++ {
// set periods to be between 1-3 days
durationMultiplier := simulation.RandIntBetween(r, 1, 3)
duration := time.Duration(int64(24*durationMultiplier)) * time.Hour
periodEnd := periodStart.Add(duration)
inflation := genRandomInflation(r)
period := types.NewPeriod(periodStart, periodEnd, inflation)
periods = append(periods, period)
periodStart = periodEnd
}
return periods
}
func genRandomInflation(r *rand.Rand) sdk.Dec {
// If sim.RandomDecAmount is less than base apr padding, add base apr padding
aprPadding, _ := sdk.NewDecFromStr(BaseAprPadding)
extraAprInflation := simulation.RandomDecAmount(r, sdk.MustNewDecFromStr("0.25"))
for extraAprInflation.LT(aprPadding) {
extraAprInflation = extraAprInflation.Add(aprPadding)
}
aprInflation := sdk.OneDec().Add(extraAprInflation)
// convert APR inflation to SPR (inflation per second)
inflationSpr, err := aprInflation.ApproxRoot(uint64(SecondsPerYear))
if err != nil {
panic(fmt.Sprintf("error generating random inflation %v", err))
}
return inflationSpr
}
func genRandomActive(r *rand.Rand) bool {
threshold := 50
value := simulation.RandIntBetween(r, 1, 100)
return value > threshold
}