mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-17 02:10:46 +00:00
consensus: optimize CompareSignersLists
consensus: optimize CompareSignersLists
This commit is contained in:
commit
53f6a8d6d9
1 changed files with 13 additions and 17 deletions
|
|
@ -4,8 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"slices"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/XinFinOrg/XDPoSChain/common"
|
"github.com/XinFinOrg/XDPoSChain/common"
|
||||||
|
|
@ -57,27 +56,24 @@ func ExtractValidatorsFromBytes(byteValidators []byte) ([]int64, error) {
|
||||||
// compare 2 signers lists
|
// compare 2 signers lists
|
||||||
// return true if they are same elements, otherwise return false
|
// return true if they are same elements, otherwise return false
|
||||||
func CompareSignersLists(list1 []common.Address, list2 []common.Address) bool {
|
func CompareSignersLists(list1 []common.Address, list2 []common.Address) bool {
|
||||||
l1 := make([]common.Address, len(list1))
|
if len(list1) != len(list2) {
|
||||||
l2 := make([]common.Address, len(list2))
|
return false
|
||||||
|
}
|
||||||
copy(l1, list1)
|
if len(list1) == 0 {
|
||||||
copy(l2, list2)
|
|
||||||
|
|
||||||
if len(l1) == 0 && len(l2) == 0 {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(l1) != len(l2) {
|
l1 := slices.Clone(list1)
|
||||||
return false
|
l2 := slices.Clone(list2)
|
||||||
}
|
|
||||||
|
|
||||||
sort.Slice(l1, func(i, j int) bool {
|
slices.SortFunc(l1, func(a, b common.Address) int {
|
||||||
return bytes.Compare(l1[i][:], l1[j][:]) == -1
|
return bytes.Compare(a[:], b[:])
|
||||||
})
|
})
|
||||||
sort.Slice(l2, func(i, j int) bool {
|
slices.SortFunc(l2, func(a, b common.Address) int {
|
||||||
return bytes.Compare(l2[i][:], l2[j][:]) == -1
|
return bytes.Compare(a[:], b[:])
|
||||||
})
|
})
|
||||||
return reflect.DeepEqual(l1, l2)
|
|
||||||
|
return slices.Equal(l1, l2)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode extra fields for consensus version >= 2 (XDPoS 2.0 and future versions)
|
// Decode extra fields for consensus version >= 2 (XDPoS 2.0 and future versions)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue