-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparams.go
169 lines (148 loc) · 4.28 KB
/
params.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package types
import (
"errors"
"fmt"
"strings"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var (
DefaultMintDenom = sdk.DefaultBondDenom
DefaultInflationRateChange = math.LegacyNewDecWithPrec(13, 2)
DefaultInflationMax = math.LegacyNewDecWithPrec(20, 2)
DefaultInflationMin = math.LegacyNewDecWithPrec(7, 2)
DefaultGoalBonded = math.LegacyNewDecWithPrec(67, 2)
DefaultBlocksPerYear = uint64(60 * 60 * 8766 / 5) // assuming 5 seconds block times
DefaultDistributionProportions = DistributionProportions{
Staking: math.LegacyNewDecWithPrec(3, 1), // 0.3
FundedAddresses: math.LegacyNewDecWithPrec(4, 1), // 0.4
CommunityPool: math.LegacyNewDecWithPrec(3, 1), // 0.3
}
DefaultFundedAddresses []WeightedAddress
)
// NewParams creates a new Params instance.
func NewParams(
mintDenom string,
inflationRateChange,
inflationMax,
inflationMin,
goalBonded math.LegacyDec,
blocksPerYear uint64,
proportions DistributionProportions,
fundedAddrs []WeightedAddress,
) Params {
return Params{
MintDenom: mintDenom,
InflationRateChange: inflationRateChange,
InflationMax: inflationMax,
InflationMin: inflationMin,
GoalBonded: goalBonded,
BlocksPerYear: blocksPerYear,
DistributionProportions: proportions,
FundedAddresses: fundedAddrs,
}
}
// DefaultParams returns default minting module parameters
func DefaultParams() Params {
return NewParams(
DefaultMintDenom,
DefaultInflationRateChange,
DefaultInflationMax,
DefaultInflationMin,
DefaultGoalBonded,
DefaultBlocksPerYear,
DefaultDistributionProportions,
DefaultFundedAddresses,
)
}
// Validate validates the set of params.
func (p Params) Validate() error {
if err := validateMintDenom(p.MintDenom); err != nil {
return err
}
if err := validateDec(p.InflationRateChange); err != nil {
return err
}
if err := validateDec(p.InflationMax); err != nil {
return err
}
if err := validateDec(p.InflationMin); err != nil {
return err
}
if err := validateDec(p.GoalBonded); err != nil {
return err
}
if err := validateBlocksPerYear(p.BlocksPerYear); err != nil {
return err
}
if p.InflationMax.LT(p.InflationMin) {
return fmt.Errorf(
"max inflation (%s) must be greater than or equal to min inflation (%s)",
p.InflationMax, p.InflationMin,
)
}
if err := validateDistributionProportions(p.DistributionProportions); err != nil {
return err
}
return validateWeightedAddresses(p.FundedAddresses)
}
func validateMintDenom(v string) error {
if strings.TrimSpace(v) == "" {
return errors.New("mint denom cannot be blank")
}
return sdk.ValidateDenom(v)
}
func validateDec(v math.LegacyDec) error {
if v.IsNegative() {
return fmt.Errorf("cannot be negative: %s", v)
}
if v.GT(math.LegacyOneDec()) {
return fmt.Errorf("dec too large: %s", v)
}
return nil
}
func validateBlocksPerYear(v uint64) error {
if v == 0 {
return fmt.Errorf("blocks per year must be positive: %d", v)
}
return nil
}
func validateDistributionProportions(v DistributionProportions) error {
if v.Staking.IsNegative() {
return errors.New("staking distribution ratio should not be negative")
}
if v.FundedAddresses.IsNegative() {
return errors.New("funded addresses distribution ratio should not be negative")
}
if v.CommunityPool.IsNegative() {
return errors.New("community pool distribution ratio should not be negative")
}
totalProportions := v.Staking.Add(v.FundedAddresses).Add(v.CommunityPool)
if !totalProportions.Equal(math.LegacyNewDec(1)) {
return errors.New("total distributions ratio should be 1")
}
return nil
}
func validateWeightedAddresses(v []WeightedAddress) error {
if len(v) == 0 {
return nil
}
weightSum := math.LegacyNewDec(0)
for i, w := range v {
_, err := sdk.AccAddressFromBech32(w.Address)
if err != nil {
return fmt.Errorf("invalid address at index %d", i)
}
if !w.Weight.IsPositive() {
return fmt.Errorf("non-positive weight at index %d", i)
}
if w.Weight.GT(math.LegacyNewDec(1)) {
return fmt.Errorf("more than 1 weight at index %d", i)
}
weightSum = weightSum.Add(w.Weight)
}
if !weightSum.Equal(math.LegacyNewDec(1)) {
return fmt.Errorf("invalid weight sum: %s", weightSum.String())
}
return nil
}