From 450f0d0a1ff207a8bcd34abf20a1f81c6d1ee330 Mon Sep 17 00:00:00 2001 From: Edgar Aroutiounian Date: Tue, 26 Nov 2019 22:33:24 -0800 Subject: [PATCH] [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 --- shard/committee/assignment.go | 7 +++++++ staking/effective/calculate.go | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/shard/committee/assignment.go b/shard/committee/assignment.go index a81169cac..3d13c83d4 100644 --- a/shard/committee/assignment.go +++ b/shard/committee/assignment.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)) diff --git a/staking/effective/calculate.go b/staking/effective/calculate.go index 1124e1acd..f2b7f593e 100644 --- a/staking/effective/calculate.go +++ b/staking/effective/calculate.go @@ -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 {