-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
161 lines (133 loc) · 3.78 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
package types
import (
stderr "errors"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"gopkg.in/yaml.v2"
)
const (
DefaultBlockChurnInterval = 241920
DEFAULTPOWER = 10000
DEFAULTSTEP = 1000
DEFAULTRATIO = 80
TARGETQUOTA = "30000000000000000000aavax,30000000000000000000abnb,500000000000000000000000ausdc,500000000000000000000000ausdt,10000000000000000000aeth,500000000000000000000000ujolt"
)
var (
KeyBlockChurnInterval = []byte("blockChurnInterval")
KeyPower = []byte("power")
KeyStep = []byte("step")
KeyRatio = []byte("candidateRatio")
KeyTargetQuota = []byte("targetQuota")
KeyHistoryLength = []byte("quotaHistoryLength")
)
var _ paramtypes.ParamSet = (*Params)(nil)
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyBlockChurnInterval, &p.BlockChurnInterval, validateInteger),
paramtypes.NewParamSetPair(KeyPower, &p.Power, validateInteger),
paramtypes.NewParamSetPair(KeyStep, &p.Step, validateInteger),
paramtypes.NewParamSetPair(KeyRatio, &p.CandidateRatio, validateFloat),
paramtypes.NewParamSetPair(KeyTargetQuota, &p.TargetQuota, validateArray),
paramtypes.NewParamSetPair(KeyHistoryLength, &p.HistoryLength, validateInteger32),
}
}
// NewParams creates a new Params instance
func NewParams(blockChurnInterval, power, step int64, ratio sdk.Dec, targetQuota sdk.Coins, historyLength int32) Params {
return Params{
BlockChurnInterval: blockChurnInterval,
Power: power,
Step: step,
CandidateRatio: ratio,
TargetQuota: targetQuota,
HistoryLength: historyLength,
}
}
// DefaultParams returns a default set of parameters.
func DefaultParams() Params {
tokensQuota, err := sdk.ParseCoinsNormalized(TARGETQUOTA)
if err != nil {
panic("invaild target quota")
}
return NewParams(
DefaultBlockChurnInterval,
DEFAULTPOWER,
DEFAULTSTEP,
sdk.NewDecWithPrec(DEFAULTRATIO, 2),
tokensQuota,
14400,
)
}
// String returns a human readable string representation of the parameters.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// Validate validates a set of params
func (p Params) Validate() error {
if err := validateInteger(p.BlockChurnInterval); err != nil {
return err
}
if err := validateInteger(p.Power); err != nil {
return err
}
if err := validateInteger(p.Step); err != nil {
return err
}
if err := validateInteger(p.CandidateRatio); err != nil {
return err
}
if err := validateArray(p.TargetQuota); err != nil {
return err
}
if err := validateInteger(p.HistoryLength); err != nil {
return err
}
return nil
}
func validateInteger(i interface{}) error {
v, ok := i.(int64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v < 0 {
return stderr.New("invalid height")
}
return nil
}
func validateInteger32(i interface{}) error {
v, ok := i.(int32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v < 0 {
return stderr.New("invalid height")
}
return nil
}
func validateFloat(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("goal bonded cannot be negative: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("goal bonded too large: %s", v)
}
return nil
}
func validateArray(i interface{}) error {
v, ok := i.(sdk.Coins)
if !ok {
return fmt.Errorf("not a valid coins structure")
}
if v.Empty() || v.IsValid() {
return nil
}
return fmt.Errorf("invalid coins list")
}