Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions consensus/XDPoS/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"math/big"
"os"
"path/filepath"
"sort"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -486,8 +486,11 @@ func (api *API) getRewardFileNamesInRange(begin, end *rpc.BlockNumber) ([]reward
if err != nil {
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 {
if !file.IsDir() {
filePrefix, fileSuffix, found := strings.Cut(file.Name(), ".")
Expand All @@ -506,27 +509,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 {
return rewardFileNames[i].epochBlockNum < rewardFileNames[j].epochBlockNum
slices.SortFunc(rewardFileNames, func(a, b rewardFileName) int {
return a.epochBlockNum - b.epochBlockNum
})

epochNumbers := make([]int, len(rewardFileNames))
for i, obj := range rewardFileNames {
epochNumbers[i] = obj.epochBlockNum
startIndex, _ := slices.BinarySearchFunc(rewardFileNames, int(beginHeader.Number.Int64()), func(rfn rewardFileName, number int) int {
return rfn.epochBlockNum - number
})
if startIndex == len(rewardFileNames) {
// retrun early if startIndex is out of bounds
return []rewardFileName{}, nil
}

startIndex := sort.SearchInts(epochNumbers, int(beginHeader.Number.Int64()))
endIndex := sort.SearchInts(epochNumbers, int(endHeader.Number.Int64()))
if endIndex == len(epochNumbers) {
endIndex-- //this is to prevent endIndex out of bounds when endInput is higher than last reward(epoch) block but lower than latest block
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
}

var rewardfileNamesInRange []rewardFileName
for i := startIndex; i <= endIndex; i++ {
rewardfileNamesInRange = append(rewardfileNamesInRange, rewardFileNames[i])
}
return rewardfileNamesInRange, nil
// compact the slice's memory
ret := rewardFileNames[startIndex:endIndex]
return ret[:len(ret):len(ret)], nil
}

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