-
Notifications
You must be signed in to change notification settings - Fork 0
/
delegation.go
384 lines (332 loc) · 13 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
package types
import (
"bytes"
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// 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 {
DelegatorAddr sdk.AccAddress
ValidatorAddr 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 {
DelegatorAddr sdk.AccAddress
ValidatorSrcAddr sdk.ValAddress
ValidatorDstAddr sdk.ValAddress
}
// 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
// pubKey.
type Delegation struct {
DelegatorAddr sdk.AccAddress `json:"delegator_addr"`
ValidatorAddr sdk.ValAddress `json:"validator_addr"`
Shares sdk.Dec `json:"shares"`
Height int64 `json:"-"` // Last height bond updated
CrossStake bool `json:"cross_stake"`
}
type delegationValue struct {
Shares sdk.Dec
Height int64
CrossStake bool
}
//________________________________________________________________
type SimplifiedDelegation struct {
DelegatorAddr sdk.AccAddress `json:"delegator_addr"`
Shares sdk.Dec `json:"shares"`
CrossStake bool `json:"cross_stake"`
}
func MustMarshalSimplifiedDelegations(cdc *codec.Codec, simDels []SimplifiedDelegation) []byte {
return cdc.MustMarshalBinaryLengthPrefixed(simDels)
}
func MustUnmarshalSimplifiedDelegations(cdc *codec.Codec, value []byte) (simDels []SimplifiedDelegation) {
err := cdc.UnmarshalBinaryLengthPrefixed(value, &simDels)
if err != nil {
panic(err)
}
return simDels
}
// return the delegation without fields contained within the key for the store
func MustMarshalDelegation(cdc *codec.Codec, delegation Delegation) []byte {
val := delegationValue{
delegation.Shares,
delegation.Height,
delegation.CrossStake,
}
return cdc.MustMarshalBinaryLengthPrefixed(val)
}
// return the delegation without fields contained within the key for the store
func MustUnmarshalDelegation(cdc *codec.Codec, key, value []byte) Delegation {
delegation, err := UnmarshalDelegation(cdc, key, value)
if err != nil {
panic(err)
}
return delegation
}
// return the delegation without fields contained within the key for the store
func UnmarshalDelegation(cdc *codec.Codec, key, value []byte) (delegation Delegation, err error) {
var storeValue delegationValue
err = cdc.UnmarshalBinaryLengthPrefixed(value, &storeValue)
if err != nil {
err = fmt.Errorf("%v", ErrNoDelegation(DefaultCodespace).Data())
return
}
addrs := key[1:] // remove prefix bytes
if len(addrs) != 2*sdk.AddrLen {
err = fmt.Errorf("%v", ErrBadDelegationAddr(DefaultCodespace).Data())
return
}
delAddr := sdk.AccAddress(addrs[:sdk.AddrLen])
valAddr := sdk.ValAddress(addrs[sdk.AddrLen:])
return Delegation{
DelegatorAddr: delAddr,
ValidatorAddr: valAddr,
Shares: storeValue.Shares,
Height: storeValue.Height,
CrossStake: storeValue.CrossStake,
}, nil
}
func MustUnmarshalDelegationValAsKey(cdc *codec.Codec, key, value []byte) Delegation {
delegation, err := UnmarshalDelegationValAsKey(cdc, key, value)
if err != nil {
panic(err)
}
return delegation
}
// return the delegation without fields contained within the key for the store.
// Validator and delegator position in the key need to be exchanged
func UnmarshalDelegationValAsKey(cdc *codec.Codec, key, value []byte) (realDelegation Delegation, err error) {
delegation, err := UnmarshalDelegation(cdc, key, value)
if err != nil {
return
}
realDelegation.DelegatorAddr = delegation.ValidatorAddr.Bytes()
realDelegation.ValidatorAddr = delegation.DelegatorAddr.Bytes()
realDelegation.Shares = delegation.Shares
realDelegation.Height = delegation.Height
realDelegation.CrossStake = delegation.CrossStake
return realDelegation, nil
}
// nolint
func (d Delegation) Equal(d2 Delegation) bool {
return bytes.Equal(d.DelegatorAddr, d2.DelegatorAddr) &&
bytes.Equal(d.ValidatorAddr, d2.ValidatorAddr) &&
d.Height == d2.Height &&
d.Shares.Equal(d2.Shares) &&
d.CrossStake == d2.CrossStake
}
// ensure fulfills the sdk validator types
var _ sdk.Delegation = Delegation{}
// nolint - for sdk.Delegation
func (d Delegation) GetDelegatorAddr() sdk.AccAddress { return d.DelegatorAddr }
func (d Delegation) GetValidatorAddr() sdk.ValAddress { return d.ValidatorAddr }
func (d Delegation) GetShares() sdk.Dec { return d.Shares }
// HumanReadableString returns a human readable string representation of a
// Delegation. An error is returned if the Delegation's delegator or validator
// addresses cannot be Bech32 encoded.
func (d Delegation) HumanReadableString() (string, error) {
resp := "Delegation \n"
resp += fmt.Sprintf("Delegator: %s\n", d.DelegatorAddr)
resp += fmt.Sprintf("Validator: %s\n", d.ValidatorAddr)
resp += fmt.Sprintf("Shares: %s\n", d.Shares.String())
resp += fmt.Sprintf("Height: %d\n", d.Height)
resp += fmt.Sprintf("Cross stake: %t", d.CrossStake)
return resp, nil
}
// UnbondingDelegation reflects a delegation's passive unbonding queue.
type UnbondingDelegation struct {
DelegatorAddr sdk.AccAddress `json:"delegator_addr"` // delegator
ValidatorAddr sdk.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 sdk.Coin `json:"initial_balance"` // atoms initially scheduled to receive at completion
Balance sdk.Coin `json:"balance"` // atoms to receive at completion
CrossStake bool `json:"cross_stake"` // native staking or cross-chain staking
}
type ubdValue struct {
CreationHeight int64
MinTime time.Time
InitialBalance sdk.Coin
Balance sdk.Coin
CrossStake bool
}
// return the unbonding delegation without fields contained within the key for the store
func MustMarshalUBD(cdc *codec.Codec, ubd UnbondingDelegation) []byte {
val := ubdValue{
ubd.CreationHeight,
ubd.MinTime,
ubd.InitialBalance,
ubd.Balance,
ubd.CrossStake,
}
return cdc.MustMarshalBinaryLengthPrefixed(val)
}
// unmarshal a unbonding delegation from a store key and value
func MustUnmarshalUBD(cdc *codec.Codec, key, value []byte) UnbondingDelegation {
ubd, err := UnmarshalUBD(cdc, key, value)
if err != nil {
panic(err)
}
return ubd
}
// unmarshal a unbonding delegation from a store key and value
func UnmarshalUBD(cdc *codec.Codec, key, value []byte) (ubd UnbondingDelegation, err error) {
var storeValue ubdValue
err = cdc.UnmarshalBinaryLengthPrefixed(value, &storeValue)
if err != nil {
return
}
addrs := key[1:] // remove prefix bytes
if len(addrs) != 2*sdk.AddrLen {
err = fmt.Errorf("%v", ErrBadDelegationAddr(DefaultCodespace).Data())
return
}
delAddr := sdk.AccAddress(addrs[:sdk.AddrLen])
valAddr := sdk.ValAddress(addrs[sdk.AddrLen:])
return UnbondingDelegation{
DelegatorAddr: delAddr,
ValidatorAddr: valAddr,
CreationHeight: storeValue.CreationHeight,
MinTime: storeValue.MinTime,
InitialBalance: storeValue.InitialBalance,
Balance: storeValue.Balance,
CrossStake: storeValue.CrossStake,
}, nil
}
// nolint
func (d UnbondingDelegation) Equal(d2 UnbondingDelegation) bool {
bz1 := MsgCdc.MustMarshalBinaryLengthPrefixed(&d)
bz2 := MsgCdc.MustMarshalBinaryLengthPrefixed(&d2)
return bytes.Equal(bz1, bz2)
}
// HumanReadableString returns a human readable string representation of an
// UnbondingDelegation. An error is returned if the UnbondingDelegation's
// delegator or validator addresses cannot be Bech32 encoded.
func (d UnbondingDelegation) HumanReadableString() (string, error) {
resp := "Unbonding Delegation \n"
resp += fmt.Sprintf("Delegator: %s\n", d.DelegatorAddr)
resp += fmt.Sprintf("Validator: %s\n", d.ValidatorAddr)
resp += fmt.Sprintf("Creation height: %v\n", d.CreationHeight)
resp += fmt.Sprintf("Min time to unbond (unix): %v\n", d.MinTime)
resp += fmt.Sprintf("Expected balance: %s\n", d.Balance.String())
resp += fmt.Sprintf("Cross stake: %t", d.CrossStake)
return resp, nil
}
// Redelegation reflects a delegation's passive re-delegation queue.
type Redelegation struct {
DelegatorAddr sdk.AccAddress `json:"delegator_addr"` // delegator
ValidatorSrcAddr sdk.ValAddress `json:"validator_src_addr"` // validator redelegation source operator addr
ValidatorDstAddr sdk.ValAddress `json:"validator_dst_addr"` // validator redelegation destination operator addr
CreationHeight int64 `json:"creation_height"` // height which the redelegation took place
MinTime time.Time `json:"min_time"` // unix time for redelegation completion
InitialBalance sdk.Coin `json:"initial_balance"` // initial balance when redelegation started
Balance sdk.Coin `json:"balance"` // current balance
SharesSrc sdk.Dec `json:"shares_src"` // amount of source shares redelegating
SharesDst sdk.Dec `json:"shares_dst"` // amount of destination shares redelegating
}
type redValue struct {
CreationHeight int64
MinTime time.Time
InitialBalance sdk.Coin
Balance sdk.Coin
SharesSrc sdk.Dec
SharesDst sdk.Dec
}
// return the redelegation without fields contained within the key for the store
func MustMarshalRED(cdc *codec.Codec, red Redelegation) []byte {
val := redValue{
red.CreationHeight,
red.MinTime,
red.InitialBalance,
red.Balance,
red.SharesSrc,
red.SharesDst,
}
return cdc.MustMarshalBinaryLengthPrefixed(val)
}
// unmarshal a redelegation from a store key and value
func MustUnmarshalRED(cdc *codec.Codec, key, value []byte) Redelegation {
red, err := UnmarshalRED(cdc, key, value)
if err != nil {
panic(err)
}
return red
}
// unmarshal a redelegation from a store key and value
func UnmarshalRED(cdc *codec.Codec, key, value []byte) (red Redelegation, err error) {
var storeValue redValue
err = cdc.UnmarshalBinaryLengthPrefixed(value, &storeValue)
if err != nil {
return
}
addrs := key[1:] // remove prefix bytes
if len(addrs) != 3*sdk.AddrLen {
err = fmt.Errorf("%v", ErrBadRedelegationAddr(DefaultCodespace).Data())
return
}
delAddr := sdk.AccAddress(addrs[:sdk.AddrLen])
valSrcAddr := sdk.ValAddress(addrs[sdk.AddrLen : 2*sdk.AddrLen])
valDstAddr := sdk.ValAddress(addrs[2*sdk.AddrLen:])
return Redelegation{
DelegatorAddr: delAddr,
ValidatorSrcAddr: valSrcAddr,
ValidatorDstAddr: valDstAddr,
CreationHeight: storeValue.CreationHeight,
MinTime: storeValue.MinTime,
InitialBalance: storeValue.InitialBalance,
Balance: storeValue.Balance,
SharesSrc: storeValue.SharesSrc,
SharesDst: storeValue.SharesDst,
}, nil
}
// nolint
func (d Redelegation) Equal(d2 Redelegation) bool {
bz1 := MsgCdc.MustMarshalBinaryLengthPrefixed(&d)
bz2 := MsgCdc.MustMarshalBinaryLengthPrefixed(&d2)
return bytes.Equal(bz1, bz2)
}
// HumanReadableString returns a human readable string representation of a
// Redelegation. An error is returned if the UnbondingDelegation's delegator or
// validator addresses cannot be Bech32 encoded.
func (d Redelegation) HumanReadableString() (string, error) {
resp := "Redelegation \n"
resp += fmt.Sprintf("Delegator: %s\n", d.DelegatorAddr)
resp += fmt.Sprintf("Source Validator: %s\n", d.ValidatorSrcAddr)
resp += fmt.Sprintf("Destination Validator: %s\n", d.ValidatorDstAddr)
resp += fmt.Sprintf("Creation height: %v\n", d.CreationHeight)
resp += fmt.Sprintf("Min time to unbond (unix): %v\n", d.MinTime)
resp += fmt.Sprintf("Source shares: %s\n", d.SharesSrc.String())
resp += fmt.Sprintf("Destination shares: %s\n", d.SharesDst.String())
resp += fmt.Sprintf("Balance: %s", d.Balance.String())
return resp, nil
}
// ----------------------------------------------------------------------------
// 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.Coin `json:"balance"`
}
// NewDelegationResp creates a new DelegationResponse instance
func NewDelegationResp(
delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress, shares sdk.Dec, balance sdk.Coin,
) DelegationResponse {
return DelegationResponse{
Delegation: Delegation{DelegatorAddr: delegatorAddr, ValidatorAddr: validatorAddr, Shares: shares},
Balance: balance,
}
}
func (dr DelegationResponse) HumanReadableString() (string, error) {
resp := "Delegation \n"
resp += fmt.Sprintf("Delegator: %s\n", dr.DelegatorAddr)
resp += fmt.Sprintf("Validator: %s\n", dr.ValidatorAddr)
resp += fmt.Sprintf("Balance: %s", dr.Balance.String())
return resp, nil
}