consensus: optimize CompareSignersLists

consensus: optimize CompareSignersLists
This commit is contained in:
Wanwiset Peerapatanapokin 2025-10-16 12:13:49 +04:00 committed by GitHub
commit 53f6a8d6d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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)