From b41a5239c6de922f2cf2dd48f7f510541dffafc8 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 14 Feb 2020 15:51:51 +0300 Subject: [PATCH] core: always sort the result of GetValidators It wasn't sorted when all validators were elected. There is also no need to do `Unique()` on the result because validators are distinguished by the key, so no two registered validators can have the same key. --- pkg/core/blockchain.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index dce705a94..9d712f16a 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -1409,20 +1409,20 @@ func (bc *Blockchain) GetValidators(txes ...*transaction.Transaction) ([]*keys.P } uniqueSBValidators := standByValidators.Unique() - pubKeys := keys.PublicKeys{} + result := keys.PublicKeys{} for _, validator := range validators { if validator.RegisteredAndHasVotes() || uniqueSBValidators.Contains(validator.PublicKey) { - pubKeys = append(pubKeys, validator.PublicKey) + result = append(result, validator.PublicKey) } } - if pubKeys.Len() >= count { - return pubKeys[:count], nil - } - result := pubKeys.Unique() - for i := 0; i < uniqueSBValidators.Len() && result.Len() < count; i++ { - if !result.Contains(uniqueSBValidators[i]) { - result = append(result, uniqueSBValidators[i]) + if result.Len() >= count { + result = result[:count] + } else { + for i := 0; i < uniqueSBValidators.Len() && result.Len() < count; i++ { + if !result.Contains(uniqueSBValidators[i]) { + result = append(result, uniqueSBValidators[i]) + } } } sort.Sort(result)