mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 19:00:46 +00:00
consensus/XDPoS: use slices package for sorting (#1710)
This commit is contained in:
parent
ec08863ba0
commit
af7e479ce9
1 changed files with 25 additions and 19 deletions
|
|
@ -23,7 +23,7 @@ import (
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
|
@ -534,8 +534,11 @@ func (api *API) getRewardFileNamesInRange(begin, end *rpc.BlockNumber) ([]reward
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if len(files) == 0 {
|
||||||
|
return nil, errors.New("no file in rewards folder")
|
||||||
|
}
|
||||||
|
|
||||||
var rewardFileNames = []rewardFileName{}
|
rewardFileNames := make([]rewardFileName, 0, len(files))
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if !file.IsDir() {
|
if !file.IsDir() {
|
||||||
filePrefix, fileSuffix, found := strings.Cut(file.Name(), ".")
|
filePrefix, fileSuffix, found := strings.Cut(file.Name(), ".")
|
||||||
|
|
@ -554,27 +557,30 @@ func (api *API) getRewardFileNamesInRange(begin, end *rpc.BlockNumber) ([]reward
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(rewardFileNames) == 0 {
|
||||||
|
return nil, errors.New("no reward file in rewards folder")
|
||||||
|
}
|
||||||
|
|
||||||
sort.Slice(rewardFileNames, func(i, j int) bool {
|
slices.SortFunc(rewardFileNames, func(a, b rewardFileName) int {
|
||||||
return rewardFileNames[i].epochBlockNum < rewardFileNames[j].epochBlockNum
|
return a.epochBlockNum - b.epochBlockNum
|
||||||
})
|
})
|
||||||
|
startIndex, _ := slices.BinarySearchFunc(rewardFileNames, int(beginHeader.Number.Int64()), func(rfn rewardFileName, number int) int {
|
||||||
epochNumbers := make([]int, len(rewardFileNames))
|
return rfn.epochBlockNum - number
|
||||||
for i, obj := range rewardFileNames {
|
})
|
||||||
epochNumbers[i] = obj.epochBlockNum
|
if startIndex == len(rewardFileNames) {
|
||||||
|
// retrun early if startIndex is out of bounds
|
||||||
|
return []rewardFileName{}, nil
|
||||||
|
}
|
||||||
|
endIndex, _ := slices.BinarySearchFunc(rewardFileNames, int(endHeader.Number.Int64()), func(rfn rewardFileName, number int) int {
|
||||||
|
return rfn.epochBlockNum - number
|
||||||
|
})
|
||||||
|
if endIndex != len(rewardFileNames) {
|
||||||
|
endIndex++ // include the endIndex file
|
||||||
}
|
}
|
||||||
|
|
||||||
startIndex := sort.SearchInts(epochNumbers, int(beginHeader.Number.Int64()))
|
// compact the slice's memory
|
||||||
endIndex := sort.SearchInts(epochNumbers, int(endHeader.Number.Int64()))
|
ret := rewardFileNames[startIndex:endIndex]
|
||||||
if endIndex == len(epochNumbers) {
|
return ret[:len(ret):len(ret)], nil
|
||||||
endIndex-- //this is to prevent endIndex out of bounds when endInput is higher than last reward(epoch) block but lower than latest block
|
|
||||||
}
|
|
||||||
|
|
||||||
var rewardfileNamesInRange []rewardFileName
|
|
||||||
for i := startIndex; i <= endIndex; i++ {
|
|
||||||
rewardfileNamesInRange = append(rewardfileNamesInRange, rewardFileNames[i])
|
|
||||||
}
|
|
||||||
return rewardfileNamesInRange, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getEpochReward(account common.Address, header *types.Header) (AccountEpochReward, error) {
|
func getEpochReward(account common.Address, header *types.Header) (AccountEpochReward, error) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue