Skip to content

Commit b16512f

Browse files
committed
consensus/XDPoS: use slices package for sorting
1 parent 4c22aa9 commit b16512f

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

@@ -486,8 +486,11 @@ func (api *API) getRewardFileNamesInRange(begin, end *rpc.BlockNumber) ([]reward
486486
if err != nil {
487487
return nil, err
488488
}
489+
if len(files) == 0 {
490+
return nil, errors.New("no file in rewards folder")
491+
}
489492

490-
var rewardFileNames = []rewardFileName{}
493+
rewardFileNames := make([]rewardFileName, 0, len(files))
491494
for _, file := range files {
492495
if !file.IsDir() {
493496
filePrefix, fileSuffix, found := strings.Cut(file.Name(), ".")
@@ -506,27 +509,30 @@ func (api *API) getRewardFileNamesInRange(begin, end *rpc.BlockNumber) ([]reward
506509
}
507510
}
508511
}
512+
if len(rewardFileNames) == 0 {
513+
return nil, errors.New("no reward file in rewards folder")
514+
}
509515

510-
sort.Slice(rewardFileNames, func(i, j int) bool {
511-
return rewardFileNames[i].epochBlockNum < rewardFileNames[j].epochBlockNum
516+
slices.SortFunc(rewardFileNames, func(a, b rewardFileName) int {
517+
return a.epochBlockNum - b.epochBlockNum
512518
})
513-
514-
epochNumbers := make([]int, len(rewardFileNames))
515-
for i, obj := range rewardFileNames {
516-
epochNumbers[i] = obj.epochBlockNum
519+
startIndex, _ := slices.BinarySearchFunc(rewardFileNames, int(beginHeader.Number.Int64()), func(rfn rewardFileName, number int) int {
520+
return rfn.epochBlockNum - number
521+
})
522+
if startIndex == len(rewardFileNames) {
523+
// retrun early if startIndex is out of bounds
524+
return []rewardFileName{}, nil
517525
}
518-
519-
startIndex := sort.SearchInts(epochNumbers, int(beginHeader.Number.Int64()))
520-
endIndex := sort.SearchInts(epochNumbers, int(endHeader.Number.Int64()))
521-
if endIndex == len(epochNumbers) {
522-
endIndex-- //this is to prevent endIndex out of bounds when endInput is higher than last reward(epoch) block but lower than latest block
526+
endIndex, _ := slices.BinarySearchFunc(rewardFileNames, int(endHeader.Number.Int64()), func(rfn rewardFileName, number int) int {
527+
return rfn.epochBlockNum - number
528+
})
529+
if endIndex != len(rewardFileNames) {
530+
endIndex++ // include the endIndex file
523531
}
524532

525-
var rewardfileNamesInRange []rewardFileName
526-
for i := startIndex; i <= endIndex; i++ {
527-
rewardfileNamesInRange = append(rewardfileNamesInRange, rewardFileNames[i])
528-
}
529-
return rewardfileNamesInRange, nil
533+
// compact the slice's memory
534+
ret := rewardFileNames[startIndex:endIndex]
535+
return ret[:len(ret):len(ret)], nil
530536
}
531537

532538
func getEpochReward(account common.Address, header *types.Header) (AccountEpochReward, error) {

0 commit comments

Comments
 (0)