generated from okp4/template-oss
/
params.go
124 lines (102 loc) · 2.72 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
package types
import (
"errors"
"fmt"
"strings"
"sigs.k8s.io/yaml"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewParams creates a new Params object.
func NewParams(
mintDenom string, inflationCoef math.LegacyDec, blocksPerYear uint64,
) Params {
return Params{
MintDenom: mintDenom,
InflationCoef: inflationCoef,
BlocksPerYear: blocksPerYear,
}
}
// DefaultParams returns a default set of parameters.
func DefaultParams() Params {
infMax := math.LegacyNewDecWithPrec(20, 2)
return Params{
MintDenom: sdk.DefaultBondDenom,
InflationCoef: math.LegacyNewDecWithPrec(3, 2),
BlocksPerYear: uint64(60 * 60 * 8766 / 5), // assuming 5-second block times
InflationMax: &infMax,
InflationMin: nil,
}
}
// Validate is used for validating the params.
func (p Params) Validate() error {
if err := validateMintDenom(p.MintDenom); err != nil {
return err
}
if err := validateInflationCoef(p.InflationCoef); err != nil {
return err
}
if err := validateBounds(p.InflationMin, p.InflationMax); err != nil {
return err
}
return validateBlocksPerYear(p.BlocksPerYear)
}
// String implements the Stringer interface.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
func validateMintDenom(i interface{}) error {
v, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if strings.TrimSpace(v) == "" {
return errors.New("mint denom cannot be blank")
}
return sdk.ValidateDenom(v)
}
func validateInflationCoef(i interface{}) error {
v, ok := i.(math.LegacyDec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("inflation coefficient cannot be negative: %s", v)
}
if v.GT(math.LegacyOneDec()) {
// while there's no theoretical limit to the inflation rate, a coefficient of
// 1 or more would lead to hyper-hyperinflation.
return fmt.Errorf("inflation coefficient too large: %s", v)
}
return nil
}
func validateBounds(min, max interface{}) error {
vmin, ok := min.(*math.LegacyDec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", min)
}
vmax, ok := max.(*math.LegacyDec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", max)
}
for _, v := range []*math.LegacyDec{vmin, vmax} {
if v != nil && v.IsNegative() {
return fmt.Errorf("inflation bound cannot be negative: %s", v)
}
}
if vmin != nil && vmax != nil && vmin.GT(*vmax) {
return fmt.Errorf("inflation min cannot greater than inflation max")
}
return nil
}
func validateBlocksPerYear(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("blocks per year must be positive: %d", v)
}
return nil
}