/
params.go
81 lines (66 loc) · 1.9 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
package types
import (
"errors"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
// Parameter keys
var (
KeyVotePeriod = []byte("voteperiod")
KeyVoteThreshold = []byte("votethreshold")
)
var _ paramtypes.ParamSet = &Params{}
// ParamKeyTable returns the parameter key table.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// DefaultParams returns default oracle parameters
func DefaultParams() Params {
return Params{
VotePeriod: 5,
VoteThreshold: sdk.NewDecWithPrec(67, 2), // 67%
}
}
// ParamSetPairs returns the parameter set pairs.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyVotePeriod, &p.VotePeriod, validateVotePeriod),
paramtypes.NewParamSetPair(KeyVoteThreshold, &p.VoteThreshold, validateVoteThreshold),
}
}
// ValidateBasic performs basic validation on oracle parameters.
func (p *Params) ValidateBasic() error {
if err := validateVotePeriod(p.VotePeriod); err != nil {
return err
}
if err := validateVoteThreshold(p.VoteThreshold); err != nil {
return err
}
return nil
}
func validateVotePeriod(i interface{}) error {
votePeriod, ok := i.(int64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if votePeriod < 4 || votePeriod > 100 {
return fmt.Errorf(
"vote period should be between 4 and 100 blocks: %d", votePeriod,
)
}
return nil
}
func validateVoteThreshold(i interface{}) error {
voteThreshold, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if voteThreshold.IsNil() {
return errors.New("vote threshold cannot be nil")
}
if voteThreshold.LTE(sdk.ZeroDec()) || voteThreshold.GT(sdk.OneDec()) {
return fmt.Errorf("vote threshold value must be within the 0% - 100% range, got: %s", voteThreshold)
}
return nil
}