forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delegation.go
471 lines (397 loc) · 15.8 KB
/
delegation.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package types
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/exported"
)
// DVPair is struct that just has a delegator-validator pair with no other data.
// It is intended to be used as a marshalable pointer. For example, a DVPair can be used to construct the
// key to getting an UnbondingDelegation from state.
type DVPair struct {
DelegatorAddress sdk.AccAddress
ValidatorAddress sdk.ValAddress
}
// DVVTriplet is struct that just has a delegator-validator-validator triplet with no other data.
// It is intended to be used as a marshalable pointer. For example, a DVVTriplet can be used to construct the
// key to getting a Redelegation from state.
type DVVTriplet struct {
DelegatorAddress sdk.AccAddress
ValidatorSrcAddress sdk.ValAddress
ValidatorDstAddress sdk.ValAddress
}
// Implements Delegation interface
var _ exported.DelegationI = Delegation{}
// Delegation represents the bond with tokens held by an account. It is
// owned by one delegator, and is associated with the voting power of one
// validator.
type Delegation struct {
DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"`
ValidatorAddress sdk.ValAddress `json:"validator_address" yaml:"validator_address"`
Shares sdk.Dec `json:"shares" yaml:"shares"`
}
// NewDelegation creates a new delegation object
func NewDelegation(delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress,
shares sdk.Dec) Delegation {
return Delegation{
DelegatorAddress: delegatorAddr,
ValidatorAddress: validatorAddr,
Shares: shares,
}
}
// return the delegation
func MustMarshalDelegation(cdc *codec.Codec, delegation Delegation) []byte {
return cdc.MustMarshalBinaryLengthPrefixed(delegation)
}
// return the delegation
func MustUnmarshalDelegation(cdc *codec.Codec, value []byte) Delegation {
delegation, err := UnmarshalDelegation(cdc, value)
if err != nil {
panic(err)
}
return delegation
}
// return the delegation
func UnmarshalDelegation(cdc *codec.Codec, value []byte) (delegation Delegation, err error) {
err = cdc.UnmarshalBinaryLengthPrefixed(value, &delegation)
return delegation, err
}
// nolint
func (d Delegation) Equal(d2 Delegation) bool {
return bytes.Equal(d.DelegatorAddress, d2.DelegatorAddress) &&
bytes.Equal(d.ValidatorAddress, d2.ValidatorAddress) &&
d.Shares.Equal(d2.Shares)
}
// nolint - for Delegation
func (d Delegation) GetDelegatorAddr() sdk.AccAddress { return d.DelegatorAddress }
func (d Delegation) GetValidatorAddr() sdk.ValAddress { return d.ValidatorAddress }
func (d Delegation) GetShares() sdk.Dec { return d.Shares }
// String returns a human readable string representation of a Delegation.
func (d Delegation) String() string {
return fmt.Sprintf(`Delegation:
Delegator: %s
Validator: %s
Shares: %s`, d.DelegatorAddress,
d.ValidatorAddress, d.Shares)
}
// Delegations is a collection of delegations
type Delegations []Delegation
func (d Delegations) String() (out string) {
for _, del := range d {
out += del.String() + "\n"
}
return strings.TrimSpace(out)
}
// UnbondingDelegation stores all of a single delegator's unbonding bonds
// for a single validator in an time-ordered list
type UnbondingDelegation struct {
DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"` // delegator
ValidatorAddress sdk.ValAddress `json:"validator_address" yaml:"validator_address"` // validator unbonding from operator addr
Entries []UnbondingDelegationEntry `json:"entries" yaml:"entries"` // unbonding delegation entries
}
// UnbondingDelegationEntry - entry to an UnbondingDelegation
type UnbondingDelegationEntry struct {
CreationHeight int64 `json:"creation_height" yaml:"creation_height"` // height which the unbonding took place
CompletionTime time.Time `json:"completion_time" yaml:"completion_time"` // time at which the unbonding delegation will complete
InitialBalance sdk.Int `json:"initial_balance" yaml:"initial_balance"` // atoms initially scheduled to receive at completion
Balance sdk.Int `json:"balance" yaml:"balance"` // atoms to receive at completion
}
// IsMature - is the current entry mature
func (e UnbondingDelegationEntry) IsMature(currentTime time.Time) bool {
return !e.CompletionTime.After(currentTime)
}
// NewUnbondingDelegation - create a new unbonding delegation object
func NewUnbondingDelegation(delegatorAddr sdk.AccAddress,
validatorAddr sdk.ValAddress, creationHeight int64, minTime time.Time,
balance sdk.Int) UnbondingDelegation {
entry := NewUnbondingDelegationEntry(creationHeight, minTime, balance)
return UnbondingDelegation{
DelegatorAddress: delegatorAddr,
ValidatorAddress: validatorAddr,
Entries: []UnbondingDelegationEntry{entry},
}
}
// NewUnbondingDelegation - create a new unbonding delegation object
func NewUnbondingDelegationEntry(creationHeight int64, completionTime time.Time,
balance sdk.Int) UnbondingDelegationEntry {
return UnbondingDelegationEntry{
CreationHeight: creationHeight,
CompletionTime: completionTime,
InitialBalance: balance,
Balance: balance,
}
}
// AddEntry - append entry to the unbonding delegation
func (d *UnbondingDelegation) AddEntry(creationHeight int64,
minTime time.Time, balance sdk.Int) {
entry := NewUnbondingDelegationEntry(creationHeight, minTime, balance)
d.Entries = append(d.Entries, entry)
}
// RemoveEntry - remove entry at index i to the unbonding delegation
func (d *UnbondingDelegation) RemoveEntry(i int64) {
d.Entries = append(d.Entries[:i], d.Entries[i+1:]...)
}
// return the unbonding delegation
func MustMarshalUBD(cdc *codec.Codec, ubd UnbondingDelegation) []byte {
return cdc.MustMarshalBinaryLengthPrefixed(ubd)
}
// unmarshal a unbonding delegation from a store value
func MustUnmarshalUBD(cdc *codec.Codec, value []byte) UnbondingDelegation {
ubd, err := UnmarshalUBD(cdc, value)
if err != nil {
panic(err)
}
return ubd
}
// unmarshal a unbonding delegation from a store value
func UnmarshalUBD(cdc *codec.Codec, value []byte) (ubd UnbondingDelegation, err error) {
err = cdc.UnmarshalBinaryLengthPrefixed(value, &ubd)
return ubd, err
}
// nolint
// inefficient but only used in testing
func (d UnbondingDelegation) Equal(d2 UnbondingDelegation) bool {
bz1 := ModuleCdc.MustMarshalBinaryLengthPrefixed(&d)
bz2 := ModuleCdc.MustMarshalBinaryLengthPrefixed(&d2)
return bytes.Equal(bz1, bz2)
}
// String returns a human readable string representation of an UnbondingDelegation.
func (d UnbondingDelegation) String() string {
out := fmt.Sprintf(`Unbonding Delegations between:
Delegator: %s
Validator: %s
Entries:`, d.DelegatorAddress, d.ValidatorAddress)
for i, entry := range d.Entries {
out += fmt.Sprintf(` Unbonding Delegation %d:
Creation Height: %v
Min time to unbond (unix): %v
Expected balance: %s`, i, entry.CreationHeight,
entry.CompletionTime, entry.Balance)
}
return out
}
// UnbondingDelegations is a collection of UnbondingDelegation
type UnbondingDelegations []UnbondingDelegation
func (ubds UnbondingDelegations) String() (out string) {
for _, u := range ubds {
out += u.String() + "\n"
}
return strings.TrimSpace(out)
}
// Redelegation contains the list of a particular delegator's
// redelegating bonds from a particular source validator to a
// particular destination validator
type Redelegation struct {
DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"` // delegator
ValidatorSrcAddress sdk.ValAddress `json:"validator_src_address" yaml:"validator_src_address"` // validator redelegation source operator addr
ValidatorDstAddress sdk.ValAddress `json:"validator_dst_address" yaml:"validator_dst_address"` // validator redelegation destination operator addr
Entries []RedelegationEntry `json:"entries" yaml:"entries"` // redelegation entries
}
// RedelegationEntry - entry to a Redelegation
type RedelegationEntry struct {
CreationHeight int64 `json:"creation_height" yaml:"creation_height"` // height at which the redelegation took place
CompletionTime time.Time `json:"completion_time" yaml:"completion_time"` // time at which the redelegation will complete
InitialBalance sdk.Int `json:"initial_balance" yaml:"initial_balance"` // initial balance when redelegation started
SharesDst sdk.Dec `json:"shares_dst" yaml:"shares_dst"` // amount of destination-validator shares created by redelegation
}
// NewRedelegation - create a new redelegation object
func NewRedelegation(delegatorAddr sdk.AccAddress, validatorSrcAddr,
validatorDstAddr sdk.ValAddress, creationHeight int64,
minTime time.Time, balance sdk.Int,
sharesDst sdk.Dec) Redelegation {
entry := NewRedelegationEntry(creationHeight,
minTime, balance, sharesDst)
return Redelegation{
DelegatorAddress: delegatorAddr,
ValidatorSrcAddress: validatorSrcAddr,
ValidatorDstAddress: validatorDstAddr,
Entries: []RedelegationEntry{entry},
}
}
// NewRedelegation - create a new redelegation object
func NewRedelegationEntry(creationHeight int64,
completionTime time.Time, balance sdk.Int,
sharesDst sdk.Dec) RedelegationEntry {
return RedelegationEntry{
CreationHeight: creationHeight,
CompletionTime: completionTime,
InitialBalance: balance,
SharesDst: sharesDst,
}
}
// IsMature - is the current entry mature
func (e RedelegationEntry) IsMature(currentTime time.Time) bool {
return !e.CompletionTime.After(currentTime)
}
// AddEntry - append entry to the unbonding delegation
func (d *Redelegation) AddEntry(creationHeight int64,
minTime time.Time, balance sdk.Int,
sharesDst sdk.Dec) {
entry := NewRedelegationEntry(creationHeight, minTime, balance, sharesDst)
d.Entries = append(d.Entries, entry)
}
// RemoveEntry - remove entry at index i to the unbonding delegation
func (d *Redelegation) RemoveEntry(i int64) {
d.Entries = append(d.Entries[:i], d.Entries[i+1:]...)
}
// return the redelegation
func MustMarshalRED(cdc *codec.Codec, red Redelegation) []byte {
return cdc.MustMarshalBinaryLengthPrefixed(red)
}
// unmarshal a redelegation from a store value
func MustUnmarshalRED(cdc *codec.Codec, value []byte) Redelegation {
red, err := UnmarshalRED(cdc, value)
if err != nil {
panic(err)
}
return red
}
// unmarshal a redelegation from a store value
func UnmarshalRED(cdc *codec.Codec, value []byte) (red Redelegation, err error) {
err = cdc.UnmarshalBinaryLengthPrefixed(value, &red)
return red, err
}
// nolint
// inefficient but only used in tests
func (d Redelegation) Equal(d2 Redelegation) bool {
bz1 := ModuleCdc.MustMarshalBinaryLengthPrefixed(&d)
bz2 := ModuleCdc.MustMarshalBinaryLengthPrefixed(&d2)
return bytes.Equal(bz1, bz2)
}
// String returns a human readable string representation of a Redelegation.
func (d Redelegation) String() string {
out := fmt.Sprintf(`Redelegations between:
Delegator: %s
Source Validator: %s
Destination Validator: %s
Entries:
`,
d.DelegatorAddress, d.ValidatorSrcAddress, d.ValidatorDstAddress,
)
for i, entry := range d.Entries {
out += fmt.Sprintf(` Redelegation Entry #%d:
Creation height: %v
Min time to unbond (unix): %v
Dest Shares: %s
`,
i, entry.CreationHeight, entry.CompletionTime, entry.SharesDst,
)
}
return strings.TrimRight(out, "\n")
}
// Redelegations are a collection of Redelegation
type Redelegations []Redelegation
func (d Redelegations) String() (out string) {
for _, red := range d {
out += red.String() + "\n"
}
return strings.TrimSpace(out)
}
// ----------------------------------------------------------------------------
// Client Types
// DelegationResponse is equivalent to Delegation except that it contains a balance
// in addition to shares which is more suitable for client responses.
type DelegationResponse struct {
Delegation
Balance sdk.Int `json:"balance" yaml:"balance"`
}
func NewDelegationResp(d sdk.AccAddress, v sdk.ValAddress, s sdk.Dec, b sdk.Int) DelegationResponse {
return DelegationResponse{NewDelegation(d, v, s), b}
}
// String implements the Stringer interface for DelegationResponse.
func (d DelegationResponse) String() string {
return fmt.Sprintf("%s\n Balance: %s", d.Delegation.String(), d.Balance)
}
type delegationRespAlias DelegationResponse
// MarshalJSON implements the json.Marshaler interface. This is so we can
// achieve a flattened structure while embedding other types.
func (d DelegationResponse) MarshalJSON() ([]byte, error) {
return json.Marshal((delegationRespAlias)(d))
}
// UnmarshalJSON implements the json.Unmarshaler interface. This is so we can
// achieve a flattened structure while embedding other types.
func (d *DelegationResponse) UnmarshalJSON(bz []byte) error {
return json.Unmarshal(bz, (*delegationRespAlias)(d))
}
// DelegationResponses is a collection of DelegationResp
type DelegationResponses []DelegationResponse
// String implements the Stringer interface for DelegationResponses.
func (d DelegationResponses) String() (out string) {
for _, del := range d {
out += del.String() + "\n"
}
return strings.TrimSpace(out)
}
// RedelegationResponse is equivalent to a Redelegation except that its entries
// contain a balance in addition to shares which is more suitable for client
// responses.
type RedelegationResponse struct {
Redelegation
Entries []RedelegationEntryResponse `json:"entries" yaml:"entries"` // nolint: structtag
}
// RedelegationEntryResponse is equivalent to a RedelegationEntry except that it
// contains a balance in addition to shares which is more suitable for client
// responses.
type RedelegationEntryResponse struct {
RedelegationEntry
Balance sdk.Int `json:"balance"`
}
func NewRedelegationResponse(d sdk.AccAddress, vSrc, vDst sdk.ValAddress, entries []RedelegationEntryResponse) RedelegationResponse {
return RedelegationResponse{
Redelegation{
DelegatorAddress: d,
ValidatorSrcAddress: vSrc,
ValidatorDstAddress: vDst,
},
entries,
}
}
func NewRedelegationEntryResponse(ch int64, ct time.Time, s sdk.Dec, ib, b sdk.Int) RedelegationEntryResponse {
return RedelegationEntryResponse{NewRedelegationEntry(ch, ct, ib, s), b}
}
// String implements the Stringer interface for RedelegationResp.
func (r RedelegationResponse) String() string {
out := fmt.Sprintf(`Redelegations between:
Delegator: %s
Source Validator: %s
Destination Validator: %s
Entries:
`,
r.DelegatorAddress, r.ValidatorSrcAddress, r.ValidatorDstAddress,
)
for i, entry := range r.Entries {
out += fmt.Sprintf(` Redelegation Entry #%d:
Creation height: %v
Min time to unbond (unix): %v
Initial Balance: %s
Shares: %s
Balance: %s
`,
i, entry.CreationHeight, entry.CompletionTime, entry.InitialBalance, entry.SharesDst, entry.Balance,
)
}
return strings.TrimRight(out, "\n")
}
type redelegationRespAlias RedelegationResponse
// MarshalJSON implements the json.Marshaler interface. This is so we can
// achieve a flattened structure while embedding other types.
func (r RedelegationResponse) MarshalJSON() ([]byte, error) {
return json.Marshal((redelegationRespAlias)(r))
}
// UnmarshalJSON implements the json.Unmarshaler interface. This is so we can
// achieve a flattened structure while embedding other types.
func (r *RedelegationResponse) UnmarshalJSON(bz []byte) error {
return json.Unmarshal(bz, (*redelegationRespAlias)(r))
}
// RedelegationResponses are a collection of RedelegationResp
type RedelegationResponses []RedelegationResponse
func (r RedelegationResponses) String() (out string) {
for _, red := range r {
out += red.String() + "\n"
}
return strings.TrimSpace(out)
}