From f9c6a0d77c842896747249ceb7db7961215e5b18 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 12 Feb 2020 18:22:31 +0300 Subject: [PATCH] core: make voting choose the best validators They need to be sorted in order for voting system to work. --- pkg/core/blockchain.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 73b293564..8110c37cf 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -1344,6 +1344,18 @@ func (bc *Blockchain) GetValidators(txes ...*transaction.Transaction) ([]*keys.P } validators := cache.GetValidators() + sort.Slice(validators, func(i, j int) bool { + // Unregistered validators go to the end of the list. + if validators[i].Registered != validators[j].Registered { + return validators[i].Registered + } + // The most-voted validators should end up in the front of the list. + if validators[i].Votes != validators[j].Votes { + return validators[i].Votes > validators[j].Votes + } + // Ties are broken with public keys. + return validators[i].PublicKey.Cmp(validators[j].PublicKey) == -1 + }) count := state.GetValidatorsWeightedAverage(validators) standByValidators, err := bc.GetStandByValidators() @@ -1361,7 +1373,6 @@ func (bc *Blockchain) GetValidators(txes ...*transaction.Transaction) ([]*keys.P pubKeys = append(pubKeys, validator.PublicKey) } } - sort.Sort(sort.Reverse(pubKeys)) if pubKeys.Len() >= count { return pubKeys[:count], nil }