-
Notifications
You must be signed in to change notification settings - Fork 30
/
params.go
140 lines (119 loc) · 3.7 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"
yaml "gopkg.in/yaml.v2"
sdk "github.com/Finschia/finschia-sdk/types"
paramtypes "github.com/Finschia/finschia-sdk/x/params/types"
)
// Parameter keys
var (
ParamStoreKeyCommunityTax = []byte("communitytax")
ParamStoreKeyBaseProposerReward = []byte("baseproposerreward")
ParamStoreKeyBonusProposerReward = []byte("bonusproposerreward")
ParamStoreKeyWithdrawAddrEnabled = []byte("withdrawaddrenabled")
)
// ParamKeyTable returns the parameter key table.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// DefaultParams returns default distribution parameters
func DefaultParams() Params {
return Params{
CommunityTax: sdk.NewDecWithPrec(2, 2), // 2%
BaseProposerReward: sdk.NewDecWithPrec(1, 2), // 1%
BonusProposerReward: sdk.NewDecWithPrec(4, 2), // 4%
WithdrawAddrEnabled: true,
}
}
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// ParamSetPairs returns the parameter set pairs.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(ParamStoreKeyCommunityTax, &p.CommunityTax, validateCommunityTax),
paramtypes.NewParamSetPair(ParamStoreKeyBaseProposerReward, &p.BaseProposerReward, validateBaseProposerReward),
paramtypes.NewParamSetPair(ParamStoreKeyBonusProposerReward, &p.BonusProposerReward, validateBonusProposerReward),
paramtypes.NewParamSetPair(ParamStoreKeyWithdrawAddrEnabled, &p.WithdrawAddrEnabled, validateWithdrawAddrEnabled),
}
}
// ValidateBasic performs basic validation on distribution parameters.
func (p Params) ValidateBasic() error {
if p.CommunityTax.IsNegative() || p.CommunityTax.GT(sdk.OneDec()) {
return fmt.Errorf(
"community tax should be non-negative and less than one: %s", p.CommunityTax,
)
}
if p.BaseProposerReward.IsNegative() {
return fmt.Errorf(
"base proposer reward should be positive: %s", p.BaseProposerReward,
)
}
if p.BonusProposerReward.IsNegative() {
return fmt.Errorf(
"bonus proposer reward should be positive: %s", p.BonusProposerReward,
)
}
if v := p.BaseProposerReward.Add(p.BonusProposerReward).Add(p.CommunityTax); v.GT(sdk.OneDec()) {
return fmt.Errorf(
"sum of base, bonus proposer rewards, and community tax cannot be greater than one: %s", v,
)
}
return nil
}
func validateCommunityTax(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("community tax must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("community tax must be positive: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("community tax too large: %s", v)
}
return nil
}
func validateBaseProposerReward(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("base proposer reward must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("base proposer reward must be positive: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("base proposer reward too large: %s", v)
}
return nil
}
func validateBonusProposerReward(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("bonus proposer reward must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("bonus proposer reward must be positive: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("bonus proposer reward too large: %s", v)
}
return nil
}
func validateWithdrawAddrEnabled(i interface{}) error {
_, ok := i.(bool)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}