-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
140 lines (113 loc) · 3.08 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
package types
import (
"fmt"
"time"
sdk "github.com/ayher/anatha/types"
params "github.com/ayher/anatha/x/params/subspace"
)
const (
DefaultPeriod time.Duration = time.Hour * 24 * 3 // devnet: time.Second * 120
)
var (
DefaultQuorum = sdk.NewDecWithPrec(334, 3)
DefaultThreshold = sdk.NewDecWithPrec(5, 1)
)
var (
ParamStoreKeyVotingParams = []byte("votingparams")
ParamStoreKeyTallyParams = []byte("tallyparams")
)
func ParamKeyTable() params.KeyTable {
return params.NewKeyTable(
params.NewParamSetPair(ParamStoreKeyVotingParams, VotingParams{}, validateVotingParams),
params.NewParamSetPair(ParamStoreKeyTallyParams, TallyParams{}, validateTallyParams),
)
}
type TallyParams struct {
Quorum sdk.Dec `json:"quorum" yaml:"quorum"`
Threshold sdk.Dec `json:"threshold" yaml:"threshold"`
}
func NewTallyParams(quorum sdk.Dec, threshold sdk.Dec) TallyParams {
return TallyParams{
Quorum: quorum,
Threshold: threshold,
}
}
func DefaultTallyParams() TallyParams {
return NewTallyParams(DefaultQuorum, DefaultThreshold)
}
func (tp TallyParams) String() string {
return fmt.Sprintf(`Tally Params:
Quorum: %s
Threshold: %s`,
tp.Quorum, tp.Threshold)
}
func validateTallyParams(i interface{}) error {
v, ok := i.(TallyParams)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.Quorum.IsNegative() {
return fmt.Errorf("quorom cannot be negative: %s", v.Quorum)
}
if v.Quorum.GT(sdk.OneDec()) {
return fmt.Errorf("quorom too large: %s", v)
}
if !v.Threshold.IsPositive() {
return fmt.Errorf("vote threshold must be positive: %s", v.Threshold)
}
if v.Threshold.GT(sdk.OneDec()) {
return fmt.Errorf("vote threshold too large: %s", v)
}
return nil
}
type VotingParams struct {
VotingPeriod time.Duration `json:"voting_period" yaml:"voting_period"`
}
func NewVotingParams(votingPeriod time.Duration) VotingParams {
return VotingParams{
VotingPeriod: votingPeriod,
}
}
func DefaultVotingParams() VotingParams {
return NewVotingParams(DefaultPeriod)
}
func (vp VotingParams) String() string {
return fmt.Sprintf(`Voting Params:
Voting Period: %s`, vp.VotingPeriod)
}
func validateVotingParams(i interface{}) error {
v, ok := i.(VotingParams)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.VotingPeriod <= 0 {
return fmt.Errorf("voting period must be positive: %s", v.VotingPeriod)
}
return nil
}
type Params struct {
VotingParams VotingParams `json:"voting_params" yaml:"voting_params"`
TallyParams TallyParams `json:"tally_params" yaml:"tally_params"`
}
func (gp Params) String() string {
return gp.VotingParams.String() + "\n" +
gp.TallyParams.String()
}
func NewParams(vp VotingParams, tp TallyParams) Params {
return Params{
VotingParams: vp,
TallyParams: tp,
}
}
func DefaultParams() Params {
return NewParams(DefaultVotingParams(), DefaultTallyParams())
}
func (p Params) Validate() error {
if err := validateVotingParams(p.VotingParams); err != nil {
return err
}
if err := validateTallyParams(p.TallyParams); err != nil {
return err
}
return nil
}