Sorted Difficulties

This commit is contained in:
Shivam Sharma 2022-02-16 13:34:03 +05:30 committed by Victor Castell
parent e0b43b0116
commit b172ba8fbf

View file

@ -4,6 +4,7 @@ import (
"encoding/hex" "encoding/hex"
"math" "math"
"math/big" "math/big"
"sort"
"strconv" "strconv"
"sync" "sync"
@ -47,11 +48,29 @@ func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error) {
} }
type BlockSigners struct { type BlockSigners struct {
Signers map[common.Address]uint64 Signers []difficultiesKV
Diff int Diff int
Author common.Address 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 // GetSnapshotProposerSequence retrieves the in-turn signers of all sprints in a span
func (api *API) GetSnapshotProposerSequence(number *rpc.BlockNumber) (BlockSigners, error) { func (api *API) GetSnapshotProposerSequence(number *rpc.BlockNumber) (BlockSigners, error) {
snapNumber := *number - 1 snapNumber := *number - 1
@ -72,13 +91,15 @@ func (api *API) GetSnapshotProposerSequence(number *rpc.BlockNumber) (BlockSigne
difficulties[signers[i]] = uint64(len(signers) - (tempIndex - proposerIndex)) difficulties[signers[i]] = uint64(len(signers) - (tempIndex - proposerIndex))
} }
difficulties2 := rankMapDifficulties(difficulties)
author, err := api.GetAuthor(number) author, err := api.GetAuthor(number)
if err != nil { if err != nil {
return BlockSigners{}, err return BlockSigners{}, err
} }
diff := int(difficulties[*author]) diff := int(difficulties[*author])
blockSigners := &BlockSigners{ blockSigners := &BlockSigners{
Signers: difficulties, Signers: difficulties2,
Diff: diff, Diff: diff,
Author: *author, Author: *author,
} }