-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
params.go
150 lines (127 loc) · 3.52 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
package types
import (
"fmt"
"time"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Default parameter namespace
const (
DefaultSignedBlocksWindow = int64(100)
DefaultDowntimeJailDuration = 60 * 10 * time.Second
)
var (
DefaultMinSignedPerWindow = sdk.NewDecWithPrec(5, 1)
DefaultSlashFractionDoubleSign = math.LegacyNewDec(1).Quo(math.LegacyNewDec(20))
DefaultSlashFractionDowntime = math.LegacyNewDec(1).Quo(math.LegacyNewDec(100))
)
// NewParams creates a new Params object
func NewParams(
signedBlocksWindow int64, minSignedPerWindow sdk.Dec, downtimeJailDuration time.Duration,
slashFractionDoubleSign, slashFractionDowntime sdk.Dec,
) Params {
return Params{
SignedBlocksWindow: signedBlocksWindow,
MinSignedPerWindow: minSignedPerWindow,
DowntimeJailDuration: downtimeJailDuration,
SlashFractionDoubleSign: slashFractionDoubleSign,
SlashFractionDowntime: slashFractionDowntime,
}
}
// DefaultParams defines the parameters for this module
func DefaultParams() Params {
return NewParams(
DefaultSignedBlocksWindow,
DefaultMinSignedPerWindow,
DefaultDowntimeJailDuration,
DefaultSlashFractionDoubleSign,
DefaultSlashFractionDowntime,
)
}
// validate params
func (p Params) Validate() error {
if err := validateSignedBlocksWindow(p.SignedBlocksWindow); err != nil {
return err
}
if err := validateMinSignedPerWindow(p.MinSignedPerWindow); err != nil {
return err
}
if err := validateDowntimeJailDuration(p.DowntimeJailDuration); err != nil {
return err
}
if err := validateSlashFractionDoubleSign(p.SlashFractionDoubleSign); err != nil {
return err
}
if err := validateSlashFractionDowntime(p.SlashFractionDowntime); err != nil {
return err
}
return nil
}
func validateSignedBlocksWindow(i interface{}) error {
v, ok := i.(int64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v <= 0 {
return fmt.Errorf("signed blocks window must be positive: %d", v)
}
return nil
}
func validateMinSignedPerWindow(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("min signed per window cannot be nil: %s", v)
}
if v.IsNegative() {
return fmt.Errorf("min signed per window cannot be negative: %s", v)
}
if v.GT(math.LegacyOneDec()) {
return fmt.Errorf("min signed per window too large: %s", v)
}
return nil
}
func validateDowntimeJailDuration(i interface{}) error {
v, ok := i.(time.Duration)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v <= 0 {
return fmt.Errorf("downtime jail duration must be positive: %s", v)
}
return nil
}
func validateSlashFractionDoubleSign(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("double sign slash fraction cannot be nil: %s", v)
}
if v.IsNegative() {
return fmt.Errorf("double sign slash fraction cannot be negative: %s", v)
}
if v.GT(math.LegacyOneDec()) {
return fmt.Errorf("double sign slash fraction too large: %s", v)
}
return nil
}
func validateSlashFractionDowntime(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("downtime slash fraction cannot be nil: %s", v)
}
if v.IsNegative() {
return fmt.Errorf("downtime slash fraction cannot be negative: %s", v)
}
if v.GT(math.LegacyOneDec()) {
return fmt.Errorf("downtime slash fraction too large: %s", v)
}
return nil
}