forked from bnb-chain/go-sdk
/
stake.go
323 lines (262 loc) · 10.2 KB
/
stake.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package types
import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/go-chain/go-sdk/common/bech32"
"github.com/tendermint/tendermint/crypto"
)
type ValAddress []byte
type BondStatus byte
// nolint
const (
Unbonded BondStatus = 0x00
Unbonding BondStatus = 0x01
Bonded BondStatus = 0x02
)
type (
// Commission defines a commission parameters for a given validator.
Commission struct {
Rate Dec `json:"rate"` // the commission rate charged to delegators
MaxRate Dec `json:"max_rate"` // maximum commission rate which validator can ever charge
MaxChangeRate Dec `json:"max_change_rate"` // maximum daily increase of the validator commission
UpdateTime time.Time `json:"update_time"` // the last time the commission rate was changed
}
// CommissionMsg defines a commission message to be used for creating a
// validator.
CommissionMsg struct {
Rate Dec `json:"rate"` // the commission rate charged to delegators
MaxRate Dec `json:"max_rate"` // maximum commission rate which validator can ever charge
MaxChangeRate Dec `json:"max_change_rate"` // maximum daily increase of the validator commission
}
)
func NewCommission(rate, maxRate, maxChangeRate Dec) Commission {
return Commission{
Rate: rate,
MaxRate: maxRate,
MaxChangeRate: maxChangeRate,
UpdateTime: time.Unix(0, 0).UTC(),
}
}
// Validate performs basic sanity validation checks of initial commission
// parameters. If validation fails, an error is returned.
func (c Commission) Validate() error {
switch {
case c.MaxRate.LT(ZeroDec()):
// max rate cannot be negative
return fmt.Errorf("Commission maxrate %v is negative", c.MaxRate)
case c.MaxRate.GT(OneDec()):
// max rate cannot be greater than 100%
return fmt.Errorf("Commission maxrate %v can't be greater than 100%", c.MaxRate)
case c.Rate.LT(ZeroDec()):
// rate cannot be negative
return fmt.Errorf("Commission rate %v can't be negative ", c.Rate)
case c.Rate.GT(c.MaxRate):
// rate cannot be greater than the max rate
return fmt.Errorf("Commission rate %v can't be greater than maxrate %v", c.Rate, c.MaxRate)
case c.MaxChangeRate.LT(ZeroDec()):
// change rate cannot be negative
return fmt.Errorf("Commission change rate %v can't be negative", c.MaxChangeRate)
case c.MaxChangeRate.GT(c.MaxRate):
// change rate cannot be greater than the max rate
return fmt.Errorf("Commission change rate %v can't be greater than MaxRat %v", c.MaxChangeRate, c.MaxRate)
}
return nil
}
// ValidateNewRate performs basic sanity validation checks of a new commission
// rate. If validation fails, an SDK error is returned.
func (c Commission) ValidateNewRate(newRate Dec, blockTime time.Time) error {
switch {
case blockTime.Sub(c.UpdateTime).Hours() < 24:
// new rate cannot be changed more than once within 24 hours
return fmt.Errorf("new rate %v cannot be changed more than once within 24 hours", blockTime.Sub(c.UpdateTime).Hours())
case newRate.LT(ZeroDec()):
// new rate cannot be negative
return fmt.Errorf("new rate %v cannot be negative", newRate)
case newRate.GT(c.MaxRate):
// new rate cannot be greater than the max rate
return fmt.Errorf("new rate %v cannot be greater than the max rate %v", newRate, c.MaxRate)
case newRate.Sub(c.Rate).Abs().GT(c.MaxChangeRate):
// new rate % points change cannot be greater than the max change rate
return fmt.Errorf("new rate %v points change cannot be greater than the max change rate %v", newRate.Sub(c.Rate).Abs(), c.MaxChangeRate)
}
return nil
}
func (c Commission) String() string {
return fmt.Sprintf("rate: %s, maxRate: %s, maxChangeRate: %s, updateTime: %s",
c.Rate, c.MaxRate, c.MaxChangeRate, c.UpdateTime,
)
}
// Description - description fields for a validator
type Description struct {
Moniker string `json:"moniker"` // name
Identity string `json:"identity"` // optional identity signature (ex. UPort or Keybase)
Website string `json:"website"` // optional website link
Details string `json:"details"` // optional details
}
// Validator defines the total amount of bond shares and their exchange rate to
// coins. Accumulation of interest is modelled as an in increase in the
// exchange rate, and slashing as a decrease. When coins are delegated to this
// validator, the validator is credited with a Delegation whose number of
// bond shares is based on the amount of coins delegated divided by the current
// exchange rate. Voting power can be calculated as total bonds multiplied by
// exchange rate.
type Validator struct {
FeeAddr AccAddress `json:"fee_addr"` // address for fee collection
OperatorAddr ValAddress `json:"operator_address"` // address of the validator's operator; bech encoded in JSON
ConsPubKey string `json:"consensus_pubkey"` // the consensus public key of the validator; bech encoded in JSON
Jailed bool `json:"jailed"` // has the validator been jailed from bonded status?
Status BondStatus `json:"status"` // validator status (bonded/unbonding/unbonded)
Tokens Dec `json:"tokens"` // delegated tokens (incl. self-delegation)
DelegatorShares Dec `json:"delegator_shares"` // total shares issued to a validator's delegators
Description Description `json:"description"` // description terms for the validator
BondHeight int64 `json:"bond_height"` // earliest height as a bonded validator
BondIntraTxCounter int16 `json:"bond_intra_tx_counter"` // block-local tx index of validator change
UnbondingHeight int64 `json:"unbonding_height"` // if unbonding, height at which this validator has begun unbonding
UnbondingMinTime time.Time `json:"unbonding_time"` // if unbonding, min time for the validator to complete unbonding
Commission Commission `json:"commission"` // commission parameters
}
type UnbondingDelegation struct {
DelegatorAddr AccAddress `json:"delegator_addr"` // delegator
ValidatorAddr ValAddress `json:"validator_addr"` // validator unbonding from operator addr
CreationHeight int64 `json:"creation_height"` // height which the unbonding took place
MinTime time.Time `json:"min_time"` // unix time for unbonding completion
InitialBalance Coin `json:"initial_balance"` // atoms initially scheduled to receive at completion
Balance Coin `json:"balance"` // atoms to receive at completion
}
func (va ValAddress) String() string {
bech32PrefixValAddr := Network.Bech32ValidatorAddrPrefix()
bech32Addr, err := bech32.ConvertAndEncode(bech32PrefixValAddr, va.Bytes())
if err != nil {
panic(err)
}
return bech32Addr
}
func (va ValAddress) Bytes() []byte {
return va
}
// MarshalJSON marshals to JSON using Bech32.
func (va ValAddress) MarshalJSON() ([]byte, error) {
return json.Marshal(va.String())
}
// UnmarshalJSON unmarshals from JSON assuming Bech32 encoding.
func (va *ValAddress) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return nil
}
va2, err := ValAddressFromBech32(s)
if err != nil {
return err
}
*va = va2
return nil
}
func ValAddressFromBech32(address string) (addr ValAddress, err error) {
bech32PrefixValAddr := Network.Bech32ValidatorAddrPrefix()
bz, err := GetFromBech32(address, bech32PrefixValAddr)
if err != nil {
return nil, err
}
return ValAddress(bz), nil
}
// consensus node
// ----------------------------------------------------------------------------
// ConsAddress defines a wrapper around bytes meant to present a consensus node.
// When marshaled to a string or JSON, it uses Bech32.
type ConsAddress []byte
// ConsAddressFromHex creates a ConsAddress from a hex string.
func ConsAddressFromHex(address string) (addr ConsAddress, err error) {
if len(address) == 0 {
return addr, errors.New("decoding Bech32 address failed: must provide an address")
}
bz, err := hex.DecodeString(address)
if err != nil {
return nil, err
}
return ConsAddress(bz), nil
}
// ConsAddressFromBech32 creates a ConsAddress from a Bech32 string.
func ConsAddressFromBech32(address string) (addr ConsAddress, err error) {
bz, err := GetFromBech32(address, bech32PrefixConsAddr)
if err != nil {
return nil, err
}
return ConsAddress(bz), nil
}
// get ConsAddress from pubkey
func GetConsAddress(pubkey crypto.PubKey) ConsAddress {
return ConsAddress(pubkey.Address())
}
// Returns boolean for whether two ConsAddress are Equal
func (ca ConsAddress) Equals(ca2 ConsAddress) bool {
if ca.Empty() && ca2.Empty() {
return true
}
return bytes.Compare(ca.Bytes(), ca2.Bytes()) == 0
}
// Returns boolean for whether an ConsAddress is empty
func (ca ConsAddress) Empty() bool {
if ca == nil {
return true
}
ca2 := ConsAddress{}
return bytes.Compare(ca.Bytes(), ca2.Bytes()) == 0
}
// Marshal returns the raw address bytes. It is needed for protobuf
// compatibility.
func (ca ConsAddress) Marshal() ([]byte, error) {
return ca, nil
}
// Unmarshal sets the address to the given data. It is needed for protobuf
// compatibility.
func (ca *ConsAddress) Unmarshal(data []byte) error {
*ca = data
return nil
}
// MarshalJSON marshals to JSON using Bech32.
func (ca ConsAddress) MarshalJSON() ([]byte, error) {
return json.Marshal(ca.String())
}
// UnmarshalJSON unmarshals from JSON assuming Bech32 encoding.
func (ca *ConsAddress) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return nil
}
ca2, err := ConsAddressFromBech32(s)
if err != nil {
return err
}
*ca = ca2
return nil
}
// Bytes returns the raw address bytes.
func (ca ConsAddress) Bytes() []byte {
return ca
}
// String implements the Stringer interface.
func (ca ConsAddress) String() string {
bech32Addr, err := bech32.ConvertAndEncode(bech32PrefixConsAddr, ca.Bytes())
if err != nil {
panic(err)
}
return bech32Addr
}
// Format implements the fmt.Formatter interface.
// nolint: errcheck
func (ca ConsAddress) Format(s fmt.State, verb rune) {
switch verb {
case 's':
s.Write([]byte(fmt.Sprintf("%s", ca.String())))
case 'p':
s.Write([]byte(fmt.Sprintf("%p", ca)))
default:
s.Write([]byte(fmt.Sprintf("%X", []byte(ca))))
}
}