-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
delegation.go
356 lines (295 loc) · 10.9 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
package types
import (
"encoding/json"
"strings"
"time"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Implements Delegation interface
var _ DelegationI = Delegation{}
// NewDelegation creates a new delegation object
func NewDelegation(delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress, shares math.LegacyDec) Delegation {
return Delegation{
DelegatorAddress: delegatorAddr.String(),
ValidatorAddress: validatorAddr.String(),
Shares: shares,
}
}
// MustMarshalDelegation returns the delegation bytes. Panics if fails
func MustMarshalDelegation(cdc codec.BinaryCodec, delegation Delegation) []byte {
return cdc.MustMarshal(&delegation)
}
// MustUnmarshalDelegation return the unmarshaled delegation from bytes.
// Panics if fails.
func MustUnmarshalDelegation(cdc codec.BinaryCodec, value []byte) Delegation {
delegation, err := UnmarshalDelegation(cdc, value)
if err != nil {
panic(err)
}
return delegation
}
// return the delegation
func UnmarshalDelegation(cdc codec.BinaryCodec, value []byte) (delegation Delegation, err error) {
err = cdc.Unmarshal(value, &delegation)
return delegation, err
}
func (d Delegation) GetDelegatorAddr() sdk.AccAddress {
delAddr := sdk.MustAccAddressFromBech32(d.DelegatorAddress)
return delAddr
}
func (d Delegation) GetValidatorAddr() sdk.ValAddress {
addr, err := sdk.ValAddressFromBech32(d.ValidatorAddress)
if err != nil {
panic(err)
}
return addr
}
func (d Delegation) GetShares() math.LegacyDec { return 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)
}
func NewUnbondingDelegationEntry(creationHeight int64, completionTime time.Time, balance math.Int, unbondingID uint64) UnbondingDelegationEntry {
return UnbondingDelegationEntry{
CreationHeight: creationHeight,
CompletionTime: completionTime,
InitialBalance: balance,
Balance: balance,
UnbondingId: unbondingID,
UnbondingOnHoldRefCount: 0,
}
}
// IsMature - is the current entry mature
func (e UnbondingDelegationEntry) IsMature(currentTime time.Time) bool {
return !e.CompletionTime.After(currentTime)
}
// OnHold - is the current entry on hold due to external modules
func (e UnbondingDelegationEntry) OnHold() bool {
return e.UnbondingOnHoldRefCount > 0
}
// return the unbonding delegation entry
func MustMarshalUBDE(cdc codec.BinaryCodec, ubd UnbondingDelegationEntry) []byte {
return cdc.MustMarshal(&ubd)
}
// unmarshal a unbonding delegation entry from a store value
func MustUnmarshalUBDE(cdc codec.BinaryCodec, value []byte) UnbondingDelegationEntry {
ubd, err := UnmarshalUBDE(cdc, value)
if err != nil {
panic(err)
}
return ubd
}
// unmarshal a unbonding delegation entry from a store value
func UnmarshalUBDE(cdc codec.BinaryCodec, value []byte) (ubd UnbondingDelegationEntry, err error) {
err = cdc.Unmarshal(value, &ubd)
return ubd, err
}
// NewUnbondingDelegation - create a new unbonding delegation object
func NewUnbondingDelegation(
delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress,
creationHeight int64, minTime time.Time, balance math.Int, id uint64,
) UnbondingDelegation {
return UnbondingDelegation{
DelegatorAddress: delegatorAddr.String(),
ValidatorAddress: validatorAddr.String(),
Entries: []UnbondingDelegationEntry{
NewUnbondingDelegationEntry(creationHeight, minTime, balance, id),
},
}
}
// AddEntry - append entry to the unbonding delegation
func (ubd *UnbondingDelegation) AddEntry(creationHeight int64, minTime time.Time, balance math.Int, unbondingID uint64) bool {
// Check the entries exists with creation_height and complete_time
entryIndex := -1
for index, ubdEntry := range ubd.Entries {
if ubdEntry.CreationHeight == creationHeight && ubdEntry.CompletionTime.Equal(minTime) {
entryIndex = index
break
}
}
// entryIndex exists
if entryIndex != -1 {
ubdEntry := ubd.Entries[entryIndex]
ubdEntry.Balance = ubdEntry.Balance.Add(balance)
ubdEntry.InitialBalance = ubdEntry.InitialBalance.Add(balance)
// update the entry
ubd.Entries[entryIndex] = ubdEntry
return false
}
// append the new unbond delegation entry
entry := NewUnbondingDelegationEntry(creationHeight, minTime, balance, unbondingID)
ubd.Entries = append(ubd.Entries, entry)
return true
}
// RemoveEntry - remove entry at index i to the unbonding delegation
func (ubd *UnbondingDelegation) RemoveEntry(i int64) {
ubd.Entries = append(ubd.Entries[:i], ubd.Entries[i+1:]...)
}
// return the unbonding delegation
func MustMarshalUBD(cdc codec.BinaryCodec, ubd UnbondingDelegation) []byte {
return cdc.MustMarshal(&ubd)
}
// unmarshal a unbonding delegation from a store value
func MustUnmarshalUBD(cdc codec.BinaryCodec, 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.BinaryCodec, value []byte) (ubd UnbondingDelegation, err error) {
err = cdc.Unmarshal(value, &ubd)
return ubd, err
}
// 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)
}
func NewRedelegationEntry(creationHeight int64, completionTime time.Time, balance math.Int, sharesDst math.LegacyDec, id uint64) RedelegationEntry {
return RedelegationEntry{
CreationHeight: creationHeight,
CompletionTime: completionTime,
InitialBalance: balance,
SharesDst: sharesDst,
UnbondingId: id,
UnbondingOnHoldRefCount: 0,
}
}
// IsMature - is the current entry mature
func (e RedelegationEntry) IsMature(currentTime time.Time) bool {
return !e.CompletionTime.After(currentTime)
}
// OnHold - is the current entry on hold due to external modules
func (e RedelegationEntry) OnHold() bool {
return e.UnbondingOnHoldRefCount > 0
}
func NewRedelegation(
delegatorAddr sdk.AccAddress, validatorSrcAddr, validatorDstAddr sdk.ValAddress,
creationHeight int64, minTime time.Time, balance math.Int, sharesDst math.LegacyDec, id uint64,
) Redelegation {
return Redelegation{
DelegatorAddress: delegatorAddr.String(),
ValidatorSrcAddress: validatorSrcAddr.String(),
ValidatorDstAddress: validatorDstAddr.String(),
Entries: []RedelegationEntry{
NewRedelegationEntry(creationHeight, minTime, balance, sharesDst, id),
},
}
}
// AddEntry - append entry to the unbonding delegation
func (red *Redelegation) AddEntry(creationHeight int64, minTime time.Time, balance math.Int, sharesDst math.LegacyDec, id uint64) {
entry := NewRedelegationEntry(creationHeight, minTime, balance, sharesDst, id)
red.Entries = append(red.Entries, entry)
}
// RemoveEntry - remove entry at index i to the unbonding delegation
func (red *Redelegation) RemoveEntry(i int64) {
red.Entries = append(red.Entries[:i], red.Entries[i+1:]...)
}
// MustMarshalRED returns the Redelegation bytes. Panics if fails.
func MustMarshalRED(cdc codec.BinaryCodec, red Redelegation) []byte {
return cdc.MustMarshal(&red)
}
// MustUnmarshalRED unmarshals a redelegation from a store value. Panics if fails.
func MustUnmarshalRED(cdc codec.BinaryCodec, value []byte) Redelegation {
red, err := UnmarshalRED(cdc, value)
if err != nil {
panic(err)
}
return red
}
// UnmarshalRED unmarshals a redelegation from a store value
func UnmarshalRED(cdc codec.BinaryCodec, value []byte) (red Redelegation, err error) {
err = cdc.Unmarshal(value, &red)
return red, err
}
// 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
// NewDelegationResp creates a new DelegationResponse instance
func NewDelegationResp(
delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress, shares math.LegacyDec, balance sdk.Coin,
) DelegationResponse {
return DelegationResponse{
Delegation: NewDelegation(delegatorAddr, validatorAddr, shares),
Balance: 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)
}
// NewRedelegationResponse crates a new RedelegationEntryResponse instance.
func NewRedelegationResponse(
delegatorAddr sdk.AccAddress, validatorSrc, validatorDst sdk.ValAddress, entries []RedelegationEntryResponse,
) RedelegationResponse {
return RedelegationResponse{
Redelegation: Redelegation{
DelegatorAddress: delegatorAddr.String(),
ValidatorSrcAddress: validatorSrc.String(),
ValidatorDstAddress: validatorDst.String(),
},
Entries: entries,
}
}
// NewRedelegationEntryResponse creates a new RedelegationEntryResponse instance.
func NewRedelegationEntryResponse(
creationHeight int64, completionTime time.Time, sharesDst math.LegacyDec, initialBalance, balance math.Int, unbondingID uint64,
) RedelegationEntryResponse {
return RedelegationEntryResponse{
RedelegationEntry: NewRedelegationEntry(creationHeight, completionTime, initialBalance, sharesDst, unbondingID),
Balance: balance,
}
}
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)
}