Skip to content

Commit 584cd16

Browse files
committed
core: use slices package for sorting ethereum#27489 ethereum#27909
1 parent b49f6cb commit 584cd16

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

core/mkalloc.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
"fmt"
3131
"math/big"
3232
"os"
33-
"sort"
33+
"slices"
3434
"strconv"
3535

3636
"github.com/XinFinOrg/XDPoSChain/core"
@@ -39,23 +39,19 @@ import (
3939

4040
type allocItem struct{ Addr, Balance *big.Int }
4141

42-
type allocList []allocItem
43-
44-
func (a allocList) Len() int { return len(a) }
45-
func (a allocList) Less(i, j int) bool { return a[i].Addr.Cmp(a[j].Addr) < 0 }
46-
func (a allocList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
47-
48-
func makelist(g *core.Genesis) allocList {
49-
a := make(allocList, 0, len(g.Alloc))
42+
func makelist(g *core.Genesis) []allocItem {
43+
items := make([]allocItem, 0, len(g.Alloc))
5044
for addr, account := range g.Alloc {
5145
if len(account.Storage) > 0 || len(account.Code) > 0 || account.Nonce != 0 {
5246
panic(fmt.Sprintf("can't encode account %x", addr))
5347
}
5448
bigAddr := new(big.Int).SetBytes(addr.Bytes())
55-
a = append(a, allocItem{bigAddr, account.Balance})
49+
items = append(items, allocItem{bigAddr, account.Balance})
5650
}
57-
sort.Sort(a)
58-
return a
51+
slices.SortFunc(items, func(a, b allocItem) int {
52+
return a.Addr.Cmp(b.Addr)
53+
})
54+
return items
5955
}
6056

6157
func makealloc(g *core.Genesis) string {

core/rawdb/accessors_chain.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"encoding/binary"
2222
"errors"
2323
"math/big"
24-
"sort"
24+
"slices"
2525

2626
"github.com/XinFinOrg/XDPoSChain/common"
2727
"github.com/XinFinOrg/XDPoSChain/core/types"
@@ -608,23 +608,13 @@ type badBlock struct {
608608
Body *types.Body
609609
}
610610

611-
// badBlockList implements the sort interface to allow sorting a list of
612-
// bad blocks by their number in the reverse order.
613-
type badBlockList []*badBlock
614-
615-
func (s badBlockList) Len() int { return len(s) }
616-
func (s badBlockList) Less(i, j int) bool {
617-
return s[i].Header.Number.Uint64() < s[j].Header.Number.Uint64()
618-
}
619-
func (s badBlockList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
620-
621611
// ReadBadBlock retrieves the bad block with the corresponding block hash.
622612
func ReadBadBlock(db ethdb.Reader, hash common.Hash) *types.Block {
623613
blob, err := db.Get(badBlockKey)
624614
if err != nil {
625615
return nil
626616
}
627-
var badBlocks badBlockList
617+
var badBlocks []*badBlock
628618
if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
629619
return nil
630620
}
@@ -647,7 +637,7 @@ func ReadAllBadBlocks(db ethdb.Reader) []*types.Block {
647637
if err != nil {
648638
return nil
649639
}
650-
var badBlocks badBlockList
640+
var badBlocks []*badBlock
651641
if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
652642
return nil
653643
}
@@ -669,7 +659,7 @@ func WriteBadBlock(db ethdb.KeyValueStore, block *types.Block) {
669659
if err != nil {
670660
log.Warn("Failed to load old bad blocks", "error", err)
671661
}
672-
var badBlocks badBlockList
662+
var badBlocks []*badBlock
673663
if len(blob) > 0 {
674664
if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
675665
log.Crit("Failed to decode old bad blocks", "error", err)
@@ -685,7 +675,10 @@ func WriteBadBlock(db ethdb.KeyValueStore, block *types.Block) {
685675
Header: block.Header(),
686676
Body: block.Body(),
687677
})
688-
sort.Sort(sort.Reverse(badBlocks))
678+
slices.SortFunc(badBlocks, func(a, b *badBlock) int {
679+
// Note: sorting in descending number order.
680+
return b.Header.Number.Cmp(a.Header.Number)
681+
})
689682
if len(badBlocks) > badBlockToKeep {
690683
badBlocks = badBlocks[:badBlockToKeep]
691684
}

0 commit comments

Comments
 (0)