core: use slices package for sorting #27489 #27909 (#1701)

This commit is contained in:
Daniel Liu 2025-12-07 18:13:24 +08:00 committed by GitHub
parent 0ad629d484
commit 62dbd3bceb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 14 deletions

View file

@ -30,7 +30,7 @@ import (
"fmt"
"math/big"
"os"
"sort"
"slices"
"strconv"
"github.com/XinFinOrg/XDPoSChain/core"
@ -39,23 +39,19 @@ import (
type allocItem struct{ Addr, Balance *big.Int }
type allocList []allocItem
func (a allocList) Len() int { return len(a) }
func (a allocList) Less(i, j int) bool { return a[i].Addr.Cmp(a[j].Addr) < 0 }
func (a allocList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func makelist(g *core.Genesis) allocList {
a := make(allocList, 0, len(g.Alloc))
func makelist(g *core.Genesis) []allocItem {
items := make([]allocItem, 0, len(g.Alloc))
for addr, account := range g.Alloc {
if len(account.Storage) > 0 || len(account.Code) > 0 || account.Nonce != 0 {
panic(fmt.Sprintf("can't encode account %x", addr))
}
bigAddr := new(big.Int).SetBytes(addr.Bytes())
a = append(a, allocItem{bigAddr, account.Balance})
items = append(items, allocItem{bigAddr, account.Balance})
}
sort.Sort(a)
return a
slices.SortFunc(items, func(a, b allocItem) int {
return a.Addr.Cmp(b.Addr)
})
return items
}
func makealloc(g *core.Genesis) string {

View file

@ -676,8 +676,8 @@ func WriteBadBlock(db ethdb.KeyValueStore, block *types.Block) {
Body: block.Body(),
})
slices.SortFunc(badBlocks, func(a, b *badBlock) int {
// Note: sorting in descending number order.
return -a.Header.Number.Cmp(b.Header.Number)
// NOTE: sorting in descending number order.
return b.Header.Number.Cmp(a.Header.Number)
})
if len(badBlocks) > badBlockToKeep {
badBlocks = badBlocks[:badBlockToKeep]