-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
executable file
·212 lines (183 loc) · 5.23 KB
/
common.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package types
import (
"encoding/json"
sdk "github.com/ci123chain/ci123chain/pkg/abci/types"
"time"
)
const (
// TODO: Why can't we just have one string description which can be JSON by convention
MaxMonikerLength = 70
MaxIdentityLength = 3000
MaxWebsiteLength = 140
MaxSecurityContactLength = 140
MaxDetailsLength = 280
// constant used in flags to indicate that description field should not be updated
DoNotModifyDesc = "[do-not-modify]"
)
//Rate: 佣金收取率;0-100;
//maxRate: 佣金最大收取率;
//maxChangeRate: 每日最大变动百分比
type CommissionRates struct {
Rate sdk.Dec `json:"rate"`
MaxRate sdk.Dec `json:"max_rate"`
MaxChangeRate sdk.Dec `json:"max_change_rate"`
}
func NewCommissionRates(rate, maxRate, maxChangeRate sdk.Dec) CommissionRates {
return CommissionRates{
Rate: rate,
MaxRate: maxRate,
MaxChangeRate: maxChangeRate,
}
}
func (cr *CommissionRates) Reset() { *cr = CommissionRates{} }
func (cr CommissionRates) Validate() error {
switch {
case cr.MaxRate.IsNegative():
return ErrCommissionNegative
case cr.MaxRate.GT(sdk.OneDec()):
// max rate cannot be greater than 1
return ErrCommissionHuge
case cr.Rate.IsNegative():
// rate cannot be negative
return ErrCommissionNegative
case cr.Rate.GT(cr.MaxRate):
// rate cannot be greater than the max rate
return ErrCommissionGTMaxRate
case cr.MaxChangeRate.IsNegative():
// change rate cannot be negative
return ErrCommissionChangeRateNegative
case cr.MaxChangeRate.GT(cr.MaxRate):
// change rate cannot be greater than the max rate
return ErrCommissionChangeRateGTMaxRate
}
return nil
}
type Description struct {
Moniker string `json:"moniker"`
Identity string `json:"identity"`
Website string `json:"website"`
SecurityContact string `json:"security_contact"`
Details string `json:"details"`
}
func NewDescription(moniker, identity, website, securityContact, details string) Description {
return Description{
Moniker: moniker,
Identity: identity,
Website: website,
SecurityContact: securityContact,
Details: details,
}
}
func (m *Description) Reset() { *m = Description{}}
func (m *Description) GetMoniker() string {
if m != nil {
return m.Moniker
}
return ""
}
func (m Description) UpdateDescription(d2 Description) (Description, error) {
if d2.Moniker == DoNotModifyDesc {
d2.Moniker = m.Moniker
}
if d2.Identity == DoNotModifyDesc {
d2.Identity = m.Identity
}
if d2.Website == DoNotModifyDesc {
d2.Website = m.Website
}
if d2.SecurityContact == DoNotModifyDesc {
d2.SecurityContact = m.SecurityContact
}
if d2.Details == DoNotModifyDesc {
d2.Details = m.Details
}
return NewDescription(
d2.Moniker,
d2.Identity,
d2.Website,
d2.SecurityContact,
d2.Details,
).EnsureLength()
}
func (m *Description) GetIdentity() string {
if m != nil {
return m.Identity
}
return ""
}
func (m *Description) GetWebsite() string {
if m != nil {
return m.Website
}
return ""
}
func (m *Description) GetSecurityContact() string {
if m != nil {
return m.SecurityContact
}
return ""
}
func (m *Description) GetDetails() string {
if m != nil {
return m.Details
}
return ""
}
func (m Description) EnsureLength() (Description, error) {
if len(m.Moniker) > MaxMonikerLength {
return m, ErrInvalidMoniker
}
if len(m.Identity) > MaxIdentityLength {
return m, ErrInvalidIdentity
}
if len(m.Website) > MaxWebsiteLength {
return m, ErrInvalidWebsite
}
if len(m.SecurityContact) > MaxSecurityContactLength {
return m, ErrInvalidSecurityContact
}
if len(m.Details) > MaxDetailsLength {
return m, ErrInvalidDetails
}
return m, nil
}
type Commission struct {
CommissionRates CommissionRates `json:"commission_rates"`
UpdateTime time.Time `json:"update_time"`
}
func (m *Commission) Reset() { *m = Commission{} }
func (m Commission) String() string {
out, _ := json.Marshal(m)
return string(out)
}
func (m Commission) ValidateNewRate(newRate sdk.Dec, blockTime time.Time) error {
switch {
case blockTime.Sub(m.UpdateTime).Hours() < 24:
// new rate cannot be changed more than once within 24 hours
return ErrCommissionUpdateTime
case newRate.IsNegative():
// new rate cannot be negative
return ErrCommissionNegative
case newRate.GT(m.CommissionRates.MaxRate):
// new rate cannot be greater than the max rate
return ErrCommissionGTMaxRate
case newRate.Sub(m.CommissionRates.Rate).GT(m.CommissionRates.MaxChangeRate):
// new rate % points change cannot be greater than the max change rate
return ErrCommissionChangeRateGTMaxRate
}
return nil
}
func NewCommission(rate, maxRate, maxChangeRate sdk.Dec) Commission {
return Commission{
CommissionRates: NewCommissionRates(rate, maxRate, maxChangeRate),
UpdateTime: time.Unix(0, 0).UTC(),
}
}
// NewCommissionWithTime returns an initialized validator commission with a specified
// update time which should be the current block BFT time.
func NewCommissionWithTime(rate, maxRate, maxChangeRate sdk.Dec, updatedAt time.Time) Commission {
return Commission{
CommissionRates: NewCommissionRates(rate, maxRate, maxChangeRate),
UpdateTime: updatedAt,
}
}