Ensure the order of undelegate entries

pull/1831/head
Rongjian Lan 5 years ago
parent c68bb619fe
commit cc4a520f6c
  1. 1
      core/state_transition.go
  2. 1
      internal/chain/engine.go
  3. 8
      staking/types/delegation.go

@ -394,7 +394,6 @@ func (st *StateTransition) applyDelegateTx(delegate *staking.Delegate) error {
// Firstly use the tokens in undelegation to delegate (redelegate) // Firstly use the tokens in undelegation to delegate (redelegate)
undelegateAmount := big.NewInt(0).Set(delegate.Amount) undelegateAmount := big.NewInt(0).Set(delegate.Amount)
// Use the latest undelegated token first as it has the longest remaining locking time. // Use the latest undelegated token first as it has the longest remaining locking time.
// TODO: always order the list by epoch.
i := len(delegation.Entries) - 1 i := len(delegation.Entries) - 1
for ; i >= 0; i-- { for ; i >= 0; i-- {
if delegation.Entries[i].Amount.Cmp(undelegateAmount) <= 0 { if delegation.Entries[i].Amount.Cmp(undelegateAmount) <= 0 {

@ -187,7 +187,6 @@ func (e *engineImpl) Finalize(
delegation := wrapper.Delegations[i] delegation := wrapper.Delegations[i]
totalWithdraw := big.NewInt(0) totalWithdraw := big.NewInt(0)
count := 0 count := 0
// TODO: need ot make sure the entries are ordered by epoch
for j := range delegation.Entries { for j := range delegation.Entries {
if delegation.Entries[j].Epoch.Cmp(header.Epoch()) > 14 { // need to wait at least 14 epochs to withdraw; if delegation.Entries[j].Epoch.Cmp(header.Epoch()) > 14 { // need to wait at least 14 epochs to withdraw;
totalWithdraw.Add(totalWithdraw, delegation.Entries[j].Amount) totalWithdraw.Add(totalWithdraw, delegation.Entries[j].Amount)

@ -3,6 +3,7 @@ package types
import ( import (
"errors" "errors"
"math/big" "math/big"
"sort"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
) )
@ -59,7 +60,14 @@ func (d *Delegation) Undelegate(epoch *big.Int, amt *big.Int) error {
if !exist { if !exist {
item := UndelegationEntry{amt, epoch} item := UndelegationEntry{amt, epoch}
d.Entries = append(d.Entries, &item) d.Entries = append(d.Entries, &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 },
)
} }
return nil return nil
} }

Loading…
Cancel
Save