From b172ba8fbf06debf2f57bc3f0ee6a5884cffcb7e Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Wed, 16 Feb 2022 13:34:03 +0530 Subject: [PATCH] Sorted Difficulties --- consensus/bor/api.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/consensus/bor/api.go b/consensus/bor/api.go index 1a2d140b21..c6433ce092 100644 --- a/consensus/bor/api.go +++ b/consensus/bor/api.go @@ -4,6 +4,7 @@ import ( "encoding/hex" "math" "math/big" + "sort" "strconv" "sync" @@ -47,11 +48,29 @@ func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error) { } type BlockSigners struct { - Signers map[common.Address]uint64 + Signers []difficultiesKV Diff int Author common.Address } +type difficultiesKV struct { + Signer common.Address + Difficulty uint64 +} + +func rankMapDifficulties(values map[common.Address]uint64) []difficultiesKV { + + var ss []difficultiesKV + for k, v := range values { + ss = append(ss, difficultiesKV{k, v}) + } + sort.Slice(ss, func(i, j int) bool { + return ss[i].Difficulty > ss[j].Difficulty + }) + + return ss +} + // GetSnapshotProposerSequence retrieves the in-turn signers of all sprints in a span func (api *API) GetSnapshotProposerSequence(number *rpc.BlockNumber) (BlockSigners, error) { snapNumber := *number - 1 @@ -72,13 +91,15 @@ func (api *API) GetSnapshotProposerSequence(number *rpc.BlockNumber) (BlockSigne difficulties[signers[i]] = uint64(len(signers) - (tempIndex - proposerIndex)) } + difficulties2 := rankMapDifficulties(difficulties) + author, err := api.GetAuthor(number) if err != nil { return BlockSigners{}, err } diff := int(difficulties[*author]) blockSigners := &BlockSigners{ - Signers: difficulties, + Signers: difficulties2, Diff: diff, Author: *author, }