core: use slices package for sorting (#27489)
Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
@@ -22,7 +22,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"sort"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
@@ -31,6 +30,7 @@ import (
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
// ReadCanonicalHash retrieves the hash assigned to a canonical block number.
|
||||
@@ -836,23 +836,13 @@ type badBlock struct {
|
||||
Body *types.Body
|
||||
}
|
||||
|
||||
// badBlockList implements the sort interface to allow sorting a list of
|
||||
// bad blocks by their number in the reverse order.
|
||||
type badBlockList []*badBlock
|
||||
|
||||
func (s badBlockList) Len() int { return len(s) }
|
||||
func (s badBlockList) Less(i, j int) bool {
|
||||
return s[i].Header.Number.Uint64() < s[j].Header.Number.Uint64()
|
||||
}
|
||||
func (s badBlockList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
|
||||
// ReadBadBlock retrieves the bad block with the corresponding block hash.
|
||||
func ReadBadBlock(db ethdb.Reader, hash common.Hash) *types.Block {
|
||||
blob, err := db.Get(badBlockKey)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var badBlocks badBlockList
|
||||
var badBlocks []*badBlock
|
||||
if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -871,7 +861,7 @@ func ReadAllBadBlocks(db ethdb.Reader) []*types.Block {
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var badBlocks badBlockList
|
||||
var badBlocks []*badBlock
|
||||
if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -889,7 +879,7 @@ func WriteBadBlock(db ethdb.KeyValueStore, block *types.Block) {
|
||||
if err != nil {
|
||||
log.Warn("Failed to load old bad blocks", "error", err)
|
||||
}
|
||||
var badBlocks badBlockList
|
||||
var badBlocks []*badBlock
|
||||
if len(blob) > 0 {
|
||||
if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
|
||||
log.Crit("Failed to decode old bad blocks", "error", err)
|
||||
@@ -905,7 +895,10 @@ func WriteBadBlock(db ethdb.KeyValueStore, block *types.Block) {
|
||||
Header: block.Header(),
|
||||
Body: block.Body(),
|
||||
})
|
||||
sort.Sort(sort.Reverse(badBlocks))
|
||||
slices.SortFunc(badBlocks, func(a, b *badBlock) bool {
|
||||
// Note: sorting in descending number order.
|
||||
return a.Header.Number.Uint64() >= b.Header.Number.Uint64()
|
||||
})
|
||||
if len(badBlocks) > badBlockToKeep {
|
||||
badBlocks = badBlocks[:badBlockToKeep]
|
||||
}
|
||||
|
||||
@@ -19,12 +19,12 @@ package rawdb
|
||||
import (
|
||||
"math/big"
|
||||
"reflect"
|
||||
"sort"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
func TestChainIterator(t *testing.T) {
|
||||
@@ -92,9 +92,11 @@ func TestChainIterator(t *testing.T) {
|
||||
}
|
||||
}
|
||||
if !c.reverse {
|
||||
sort.Ints(numbers)
|
||||
slices.Sort(numbers)
|
||||
} else {
|
||||
sort.Sort(sort.Reverse(sort.IntSlice(numbers)))
|
||||
slices.SortFunc(numbers, func(a, b int) bool {
|
||||
return a > b // Sort descending
|
||||
})
|
||||
}
|
||||
if !reflect.DeepEqual(numbers, c.expect) {
|
||||
t.Fatalf("Case %d failed, visit element mismatch, want %v, got %v", i, c.expect, numbers)
|
||||
|
||||
Reference in New Issue
Block a user