Skip to content

Commit 6f25296

Browse files
committed
consensus/XDPoS: use slices package for sorting
1 parent 2652df3 commit 6f25296

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

consensus/XDPoS/api.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"math/big"
2424
"os"
2525
"path/filepath"
26-
"sort"
26+
"slices"
2727
"strconv"
2828
"strings"
2929

@@ -470,8 +470,11 @@ func (api *API) getRewardFileNamesInRange(begin, end *rpc.BlockNumber) ([]reward
470470
if err != nil {
471471
return nil, err
472472
}
473+
if len(files) == 0 {
474+
return nil, errors.New("no file in rewards folder")
475+
}
473476

474-
var rewardFileNames = []rewardFileName{}
477+
rewardFileNames := make([]rewardFileName, 0, len(files))
475478
for _, file := range files {
476479
if !file.IsDir() {
477480
filePrefix, fileSuffix, found := strings.Cut(file.Name(), ".")
@@ -490,26 +493,29 @@ func (api *API) getRewardFileNamesInRange(begin, end *rpc.BlockNumber) ([]reward
490493
}
491494
}
492495
}
493-
494-
sort.Slice(rewardFileNames, func(i, j int) bool {
495-
return rewardFileNames[i].epochBlockNum < rewardFileNames[j].epochBlockNum
496-
})
497-
498-
epochNumbers := make([]int, len(rewardFileNames))
499-
for i, obj := range rewardFileNames {
500-
epochNumbers[i] = obj.epochBlockNum
496+
if len(rewardFileNames) == 0 {
497+
return nil, errors.New("no reward file in rewards folder")
501498
}
502499

503-
startIndex := sort.SearchInts(epochNumbers, int(beginHeader.Number.Int64()))
504-
endIndex := sort.SearchInts(epochNumbers, int(endHeader.Number.Int64()))
505-
if endIndex == len(epochNumbers) {
506-
endIndex-- //this is to prevent endIndex out of bounds when endInput is higher than last reward(epoch) block but lower than latest block
500+
slices.SortFunc(rewardFileNames, func(a, b rewardFileName) int {
501+
return a.epochBlockNum - b.epochBlockNum
502+
})
503+
startIndex, _ := slices.BinarySearchFunc(rewardFileNames, int(beginHeader.Number.Int64()), func(rfn rewardFileName, number int) int {
504+
return rfn.epochBlockNum - number
505+
})
506+
if startIndex == len(rewardFileNames) {
507+
// retrun early if startIndex is out of bounds
508+
return []rewardFileName{}, nil
507509
}
508-
509-
var rewardfileNamesInRange []rewardFileName
510-
for i := startIndex; i <= endIndex; i++ {
511-
rewardfileNamesInRange = append(rewardfileNamesInRange, rewardFileNames[i])
510+
endIndex, _ := slices.BinarySearchFunc(rewardFileNames, int(endHeader.Number.Int64()), func(rfn rewardFileName, number int) int {
511+
return rfn.epochBlockNum - number
512+
})
513+
if endIndex != len(rewardFileNames) {
514+
endIndex++ // include the endIndex file
512515
}
516+
// create a new slice by copy in case of rewardFileNames is large
517+
rewardfileNamesInRange := make([]rewardFileName, endIndex-startIndex)
518+
copy(rewardfileNamesInRange, rewardFileNames[startIndex:endIndex])
513519
return rewardfileNamesInRange, nil
514520
}
515521

0 commit comments

Comments
 (0)