|
|
|
@ -519,7 +519,29 @@ func (hmy *Harmony) GetDelegationsByDelegatorByBlock( |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// UndelegationPayouts ..
|
|
|
|
|
type UndelegationPayouts map[common.Address]map[common.Address]*big.Int |
|
|
|
|
type UndelegationPayouts struct { |
|
|
|
|
Data map[common.Address]map[common.Address]*big.Int |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func NewUndelegationPayouts() *UndelegationPayouts { |
|
|
|
|
return &UndelegationPayouts{ |
|
|
|
|
Data: make(map[common.Address]map[common.Address]*big.Int), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (u *UndelegationPayouts) SetPayoutByDelegatorAddrAndValidatorAddr( |
|
|
|
|
delegator, validator common.Address, amount *big.Int, |
|
|
|
|
) { |
|
|
|
|
if u.Data[delegator] == nil { |
|
|
|
|
u.Data[delegator] = make(map[common.Address]*big.Int) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if totalPayout, ok := u.Data[delegator][validator]; ok { |
|
|
|
|
u.Data[delegator][validator] = new(big.Int).Add(totalPayout, amount) |
|
|
|
|
} else { |
|
|
|
|
u.Data[delegator][validator] = amount |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetUndelegationPayouts returns the undelegation payouts for each delegator
|
|
|
|
|
//
|
|
|
|
@ -528,16 +550,17 @@ type UndelegationPayouts map[common.Address]map[common.Address]*big.Int |
|
|
|
|
// This not a problem if a full (archival) DB is used.
|
|
|
|
|
func (hmy *Harmony) GetUndelegationPayouts( |
|
|
|
|
ctx context.Context, epoch *big.Int, |
|
|
|
|
) (UndelegationPayouts, error) { |
|
|
|
|
) (*UndelegationPayouts, error) { |
|
|
|
|
if !hmy.IsPreStakingEpoch(epoch) { |
|
|
|
|
return nil, fmt.Errorf("not pre-staking epoch or later") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
payouts, ok := hmy.undelegationPayoutsCache.Get(epoch.Uint64()) |
|
|
|
|
if ok { |
|
|
|
|
return payouts.(UndelegationPayouts), nil |
|
|
|
|
result := payouts.(UndelegationPayouts) |
|
|
|
|
return &result, nil |
|
|
|
|
} |
|
|
|
|
undelegationPayouts := UndelegationPayouts{} |
|
|
|
|
undelegationPayouts := NewUndelegationPayouts() |
|
|
|
|
// require second to last block as saved undelegations are AFTER undelegations are payed out
|
|
|
|
|
blockNumber := shard.Schedule.EpochLastBlock(epoch.Uint64()) - 1 |
|
|
|
|
undelegationPayoutBlock, err := hmy.BlockByNumber(ctx, rpc.BlockNumber(blockNumber)) |
|
|
|
@ -556,14 +579,7 @@ func (hmy *Harmony) GetUndelegationPayouts( |
|
|
|
|
for _, delegation := range wrapper.Delegations { |
|
|
|
|
withdraw := delegation.RemoveUnlockedUndelegations(epoch, wrapper.LastEpochInCommittee, lockingPeriod, noEarlyUnlock) |
|
|
|
|
if withdraw.Cmp(bigZero) == 1 { |
|
|
|
|
if undelegationPayouts[delegation.DelegatorAddress] == nil { |
|
|
|
|
undelegationPayouts[delegation.DelegatorAddress] = make(map[common.Address]*big.Int) |
|
|
|
|
} |
|
|
|
|
if totalPayout, ok := undelegationPayouts[delegation.DelegatorAddress][validator]; ok { |
|
|
|
|
undelegationPayouts[delegation.DelegatorAddress][validator] = new(big.Int).Add(totalPayout, withdraw) |
|
|
|
|
} else { |
|
|
|
|
undelegationPayouts[delegation.DelegatorAddress][validator] = withdraw |
|
|
|
|
} |
|
|
|
|
undelegationPayouts.SetPayoutByDelegatorAddrAndValidatorAddr(validator, delegation.DelegatorAddress, withdraw) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|