Skip to content
Open
Show file tree
Hide file tree
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
20 changes: 8 additions & 12 deletions core/mkalloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"fmt"
"math/big"
"os"
"sort"
"slices"
"strconv"

"github.com/XinFinOrg/XDPoSChain/core"
Expand All @@ -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 {
Expand Down
23 changes: 8 additions & 15 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"encoding/binary"
"errors"
"math/big"
"sort"
"slices"

"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/types"
Expand Down Expand Up @@ -608,23 +608,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
}
Expand All @@ -647,7 +637,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
}
Expand All @@ -669,7 +659,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)
Expand All @@ -685,7 +675,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) int {
// NOTE: sorting in descending number order.
return b.Header.Number.Cmp(a.Header.Number)
})
if len(badBlocks) > badBlockToKeep {
badBlocks = badBlocks[:badBlockToKeep]
}
Expand Down