|
|
|
@ -22,14 +22,14 @@ const ( |
|
|
|
|
// owned by one delegator, and is associated with the voting power of one
|
|
|
|
|
// validator.
|
|
|
|
|
type Delegation struct { |
|
|
|
|
DelegatorAddress common.Address `json:"delegator_address" yaml:"delegator_address"` |
|
|
|
|
Amount *big.Int `json:"amount" yaml:"amount"` |
|
|
|
|
Reward *big.Int `json:"reward" yaml:"reward"` |
|
|
|
|
Entries []*UndelegationEntry `json:"entries" yaml:"entries"` |
|
|
|
|
DelegatorAddress common.Address `json:"delegator_address" yaml:"delegator_address"` |
|
|
|
|
Amount *big.Int `json:"amount" yaml:"amount"` |
|
|
|
|
Reward *big.Int `json:"reward" yaml:"reward"` |
|
|
|
|
Undelegations []*Undelegation `json:"undelegations" yaml:"undelegations"` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// UndelegationEntry represents one undelegation entry
|
|
|
|
|
type UndelegationEntry struct { |
|
|
|
|
// Undelegation represents one undelegation entry
|
|
|
|
|
type Undelegation struct { |
|
|
|
|
Amount *big.Int |
|
|
|
|
Epoch *big.Int |
|
|
|
|
} |
|
|
|
@ -54,7 +54,7 @@ func (d *Delegation) Undelegate(epoch *big.Int, amt *big.Int) error { |
|
|
|
|
d.Amount.Sub(d.Amount, amt) |
|
|
|
|
|
|
|
|
|
exist := false |
|
|
|
|
for _, entry := range d.Entries { |
|
|
|
|
for _, entry := range d.Undelegations { |
|
|
|
|
if entry.Epoch.Cmp(epoch) == 0 { |
|
|
|
|
exist = true |
|
|
|
|
entry.Amount.Add(entry.Amount, amt) |
|
|
|
@ -63,13 +63,13 @@ func (d *Delegation) Undelegate(epoch *big.Int, amt *big.Int) error { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if !exist { |
|
|
|
|
item := UndelegationEntry{amt, epoch} |
|
|
|
|
d.Entries = append(d.Entries, &item) |
|
|
|
|
item := Undelegation{amt, epoch} |
|
|
|
|
d.Undelegations = append(d.Undelegations, &item) |
|
|
|
|
|
|
|
|
|
// Always sort the undelegate by epoch in increasing order
|
|
|
|
|
sort.SliceStable( |
|
|
|
|
d.Entries, |
|
|
|
|
func(i, j int) bool { return d.Entries[i].Epoch.Cmp(d.Entries[j].Epoch) < 0 }, |
|
|
|
|
d.Undelegations, |
|
|
|
|
func(i, j int) bool { return d.Undelegations[i].Epoch.Cmp(d.Undelegations[j].Epoch) < 0 }, |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -79,7 +79,7 @@ func (d *Delegation) Undelegate(epoch *big.Int, amt *big.Int) error { |
|
|
|
|
// TotalInUndelegation - return the total amount of token in undelegation (locking period)
|
|
|
|
|
func (d *Delegation) TotalInUndelegation() *big.Int { |
|
|
|
|
total := big.NewInt(0) |
|
|
|
|
for _, entry := range d.Entries { |
|
|
|
|
for _, entry := range d.Undelegations { |
|
|
|
|
total.Add(total, entry.Amount) |
|
|
|
|
} |
|
|
|
|
return total |
|
|
|
@ -88,14 +88,14 @@ func (d *Delegation) TotalInUndelegation() *big.Int { |
|
|
|
|
// DeleteEntry - delete an entry from the undelegation
|
|
|
|
|
// Opimize it
|
|
|
|
|
func (d *Delegation) DeleteEntry(epoch *big.Int) { |
|
|
|
|
entries := []*UndelegationEntry{} |
|
|
|
|
for i, entry := range d.Entries { |
|
|
|
|
entries := []*Undelegation{} |
|
|
|
|
for i, entry := range d.Undelegations { |
|
|
|
|
if entry.Epoch.Cmp(epoch) == 0 { |
|
|
|
|
entries = append(d.Entries[:i], d.Entries[i+1:]...) |
|
|
|
|
entries = append(d.Undelegations[:i], d.Undelegations[i+1:]...) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if entries != nil { |
|
|
|
|
d.Entries = entries |
|
|
|
|
d.Undelegations = entries |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -103,15 +103,15 @@ func (d *Delegation) DeleteEntry(epoch *big.Int) { |
|
|
|
|
func (d *Delegation) RemoveUnlockedUndelegations(curEpoch *big.Int) *big.Int { |
|
|
|
|
totalWithdraw := big.NewInt(0) |
|
|
|
|
count := 0 |
|
|
|
|
for j := range d.Entries { |
|
|
|
|
if big.NewInt(0).Sub(curEpoch, d.Entries[j].Epoch).Int64() > LockPeriodInEpoch { // need to wait at least 14 epochs to withdraw;
|
|
|
|
|
totalWithdraw.Add(totalWithdraw, d.Entries[j].Amount) |
|
|
|
|
for j := range d.Undelegations { |
|
|
|
|
if big.NewInt(0).Sub(curEpoch, d.Undelegations[j].Epoch).Int64() > LockPeriodInEpoch { // need to wait at least 14 epochs to withdraw;
|
|
|
|
|
totalWithdraw.Add(totalWithdraw, d.Undelegations[j].Amount) |
|
|
|
|
count++ |
|
|
|
|
} else { |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
d.Entries = d.Entries[count:] |
|
|
|
|
d.Undelegations = d.Undelegations[count:] |
|
|
|
|
return totalWithdraw |
|
|
|
|
} |
|
|
|
|