[effective] Guard median call site against zero length SlotList (#1900)

* [effective] Guard median call site against zero length SlotList

* [committee] Exit early if stakedSlotsCount is 0, committee just made up of hmy
pull/1901/head
Edgar Aroutiounian 5 years ago committed by GitHub
parent f9668c7eaf
commit 450f0d0a1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      shard/committee/assignment.go
  2. 11
      staking/effective/calculate.go

@ -149,6 +149,13 @@ func eposStakedCommittee(
nil,
})
}
if stakedSlotsCount == 0 {
utils.Logger().Info().Int("slots-for-epos", stakedSlotsCount).
Msg("committe composed only of harmony node")
return superComm, nil
}
staked := effective.Apply(essentials, stakedSlotsCount)
shardBig := big.NewInt(int64(shardCount))

@ -66,6 +66,12 @@ func (s Slots) JSON() string {
}
func median(stakes []SlotPurchase) numeric.Dec {
if len(stakes) == 0 {
utils.Logger().Error().Int("non-zero", len(stakes)).
Msg("Input to median has len 0, check caller")
}
sort.SliceStable(
stakes,
func(i, j int) bool { return stakes[i].Dec.LTE(stakes[j].Dec) },
@ -115,6 +121,11 @@ func Apply(shortHand map[common.Address]SlotOrder, pull int) Slots {
pull = l
}
picks := eposedSlots[:pull]
if len(picks) == 0 {
return Slots{}
}
median := median(picks)
for i := range picks {

Loading…
Cancel
Save