-
Notifications
You must be signed in to change notification settings - Fork 352
/
params.go
191 lines (158 loc) · 4.63 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package types
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
tmtime "github.com/tendermint/tendermint/types/time"
// cdptypes "github.com/kava-labs/kava/x/cdp/types"
)
// Parameter keys and default values
var (
KeyActive = []byte("Active")
KeyPeriods = []byte("Periods")
KeyInfra = []byte("InfrastructureParams")
DefaultActive = false
DefaultPeriods = []Period{}
DefaultInfraParams = InfrastructureParams{}
DefaultPreviousBlockTime = tmtime.Canonical(time.Unix(1, 0))
GovDenom = "ukava" // TODO: replace with cdptypes.DefaultGovDenom
)
func NewParams(active bool, periods []Period, infraParams InfrastructureParams) Params {
return Params{
Active: active,
Periods: periods,
InfrastructureParams: infraParams,
}
}
func DefaultParams() Params {
return NewParams(DefaultActive, DefaultPeriods, DefaultInfraParams)
}
func (p Params) String() string {
periods := "Periods\n"
for _, pr := range p.Periods {
periods += fmt.Sprintf("%s\n", pr)
}
return fmt.Sprintf(`Params:
Active: %t
Periods %s`, p.Active, periods)
}
func (p Params) Equal(other Params) bool {
if p.Active != other.Active {
return false
}
if len(p.Periods) != len(other.Periods) {
return false
}
for i, period := range p.Periods {
if !period.Equal(other.Periods[i]) {
return false
}
}
return true
}
// ParamKeyTable Key declaration for parameters
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyActive, &p.Active, validateActiveParam),
paramtypes.NewParamSetPair(KeyPeriods, &p.Periods, validatePeriodsParams),
paramtypes.NewParamSetPair(KeyInfra, &p.InfrastructureParams, validateInfraParams),
}
}
// Validate checks that the parameters have valid values.
func (p Params) Validate() error {
if err := validateActiveParam(p.Active); err != nil {
return err
}
return validatePeriodsParams(p.Periods)
}
// NewPeriod returns a new instance of Period
func NewPeriod(start time.Time, end time.Time, inflation sdk.Dec) Period {
return Period{
Start: start,
End: end,
Inflation: inflation,
}
}
type Periods []Period
// String implements fmt.Stringer
func (pr Period) String() string {
return fmt.Sprintf(`Period:
Start: %s
End: %s
Inflation: %s`, pr.Start, pr.End, pr.Inflation)
}
func NewInfraParams(p Periods, pr PartnerRewards, cr CoreRewards) InfrastructureParams {
return InfrastructureParams{
InfrastructurePeriods: p,
PartnerRewards: pr,
CoreRewards: cr,
}
}
func NewPartnerReward(addr sdk.AccAddress, rps sdk.Coin) PartnerReward {
return PartnerReward{
Address: addr,
RewardsPerSecond: rps,
}
}
func NewCoreReward(addr sdk.AccAddress, w sdk.Dec) CoreReward {
return CoreReward{
Address: addr,
Weight: w,
}
}
func validateActiveParam(i interface{}) error {
_, ok := i.(bool)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validatePeriodsParams(i interface{}) error {
periods, ok := i.([]Period)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
prevEnd := tmtime.Canonical(time.Unix(0, 0))
for _, pr := range periods {
if pr.End.Before(pr.Start) {
return fmt.Errorf("end time for period is before start time: %s", pr)
}
if pr.Start.Before(prevEnd) {
return fmt.Errorf("periods must be in chronological order: %s", periods)
}
prevEnd = pr.End
if pr.Start.Unix() <= 0 || pr.End.Unix() <= 0 {
return fmt.Errorf("start or end time cannot be zero: %s", pr)
}
// TODO: validate period Inflation?
}
return nil
}
func validateInfraParams(i interface{}) error {
infraParams, ok := i.(InfrastructureParams)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
for _, pr := range infraParams.InfrastructurePeriods {
if pr.End.Before(pr.Start) {
return fmt.Errorf("end time for period is before start time: %s", pr)
}
prevEnd := tmtime.Canonical(time.Unix(0, 0))
if pr.Start.Before(prevEnd) {
return fmt.Errorf("periods must be in chronological order: %s", infraParams.InfrastructurePeriods)
}
prevEnd = pr.End
if pr.Start.Unix() <= 0 || pr.End.Unix() <= 0 {
return fmt.Errorf("start or end time cannot be zero: %s", pr)
}
// TODO: validate period Inflation?
}
return nil
}
type CoreRewards []CoreReward
type PartnerRewards []PartnerReward