refactor: using slices.SortFunc to simplify the code

Signed-off-by: dashangcun <907225865@qq.com>
This commit is contained in:
dashangcun 2025-01-09 23:37:39 +08:00
parent 033de2a05b
commit f519111a5d

View file

@ -28,7 +28,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"slices" "slices"
"sort"
"strings" "strings"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -166,11 +165,13 @@ func (c *Chain) RootAt(height int) common.Hash {
// GetSender returns the address associated with account at the index in the // GetSender returns the address associated with account at the index in the
// pre-funded accounts list. // pre-funded accounts list.
func (c *Chain) GetSender(idx int) (common.Address, uint64) { func (c *Chain) GetSender(idx int) (common.Address, uint64) {
var accounts Addresses accounts := make([]common.Address, 0, len(c.senders))
for addr := range c.senders { for addr := range c.senders {
accounts = append(accounts, addr) accounts = append(accounts, addr)
} }
sort.Sort(accounts) slices.SortFunc(accounts, func(a, b common.Address) int {
return bytes.Compare(a[:], b[:])
})
addr := accounts[idx] addr := accounts[idx]
return addr, c.senders[addr].Nonce return addr, c.senders[addr].Nonce
} }
@ -260,22 +261,6 @@ func loadGenesis(genesisFile string) (core.Genesis, error) {
return gen, nil return gen, nil
} }
type Addresses []common.Address
func (a Addresses) Len() int {
return len(a)
}
func (a Addresses) Less(i, j int) bool {
return bytes.Compare(a[i][:], a[j][:]) < 0
}
func (a Addresses) Swap(i, j int) {
tmp := a[i]
a[i] = a[j]
a[j] = tmp
}
func blocksFromFile(chainfile string, gblock *types.Block) ([]*types.Block, error) { func blocksFromFile(chainfile string, gblock *types.Block) ([]*types.Block, error) {
// Load chain.rlp. // Load chain.rlp.
fh, err := os.Open(chainfile) fh, err := os.Open(chainfile)