Skip to content

Commit

Permalink
internal/ethapi, les: use slices package for sorting (#27492)
Browse files Browse the repository at this point in the history
Co-authored-by: Felix Lange <fjl@twurst.com>
  • Loading branch information
2 people authored and cffls committed Aug 16, 2023
1 parent 89bf486 commit 0b7b8f6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 61 deletions.
13 changes: 3 additions & 10 deletions internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
package ethapi

import (
"bytes"
"context"
"crypto/ecdsa"
"encoding/json"
"errors"
"hash"
"math/big"
"reflect"
"sort"
"testing"
"time"

Expand All @@ -48,6 +46,7 @@ import (
"github.com/ethereum/go-ethereum/rpc"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/sha3"
"golang.org/x/exp/slices"
)

func TestTransaction_RoundTripRpcJSON(t *testing.T) {
Expand Down Expand Up @@ -651,19 +650,13 @@ type Account struct {
addr common.Address
}

type Accounts []Account

func (a Accounts) Len() int { return len(a) }
func (a Accounts) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Accounts) Less(i, j int) bool { return bytes.Compare(a[i].addr.Bytes(), a[j].addr.Bytes()) < 0 }

func newAccounts(n int) (accounts Accounts) {
func newAccounts(n int) (accounts []Account) {
for i := 0; i < n; i++ {
key, _ := crypto.GenerateKey()
addr := crypto.PubkeyToAddress(key.PublicKey)
accounts = append(accounts, Account{key: key, addr: addr})
}
sort.Sort(accounts)
slices.SortFunc(accounts, func(a, b Account) bool { return a.addr.Less(b.addr) })
return accounts
}

Expand Down
39 changes: 11 additions & 28 deletions les/servingqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package les

import (
"sort"
"sync"
"sync/atomic"

"github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/common/prque"
"golang.org/x/exp/slices"
)

// servingQueue allows running tasks in a limited number of threads and puts the
Expand Down Expand Up @@ -191,37 +191,19 @@ func (sq *servingQueue) threadController() {
}
}

type (
// peerTasks lists the tasks received from a given peer when selecting peers to freeze
peerTasks struct {
peer *clientPeer
list []*servingTask
sumTime uint64
priority float64
}
// peerList is a sortable list of peerTasks
peerList []*peerTasks
)

func (l peerList) Len() int {
return len(l)
}

func (l peerList) Less(i, j int) bool {
return l[i].priority < l[j].priority
}

func (l peerList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
// peerTasks lists the tasks received from a given peer when selecting peers to freeze
type peerTasks struct {
peer *clientPeer
list []*servingTask
sumTime uint64
priority float64
}

// freezePeers selects the peers with the worst priority queued tasks and freezes
// them until burstTime goes under burstDropLimit or all peers are frozen
func (sq *servingQueue) freezePeers() {
peerMap := make(map[*clientPeer]*peerTasks)

var peerList peerList

var peerList []*peerTasks
if sq.best != nil {
sq.queue.Push(sq.best, sq.best.priority)
}
Expand All @@ -248,8 +230,9 @@ func (sq *servingQueue) freezePeers() {
tasks.list = append(tasks.list, task)
tasks.sumTime += task.expTime
}
sort.Sort(peerList)

slices.SortFunc(peerList, func(a, b *peerTasks) bool {
return a.priority < b.priority
})
drop := true
for _, tasks := range peerList {
if drop {
Expand Down
31 changes: 8 additions & 23 deletions les/utils/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package utils

import (
"sort"
"sync"

"github.com/ethereum/go-ethereum/p2p/enode"
"golang.org/x/exp/slices"
)

const maxSelectionWeight = 1000000000 // maximum selection weight of each individual node/address group
Expand Down Expand Up @@ -378,24 +378,9 @@ func (l *Limiter) Stop() {
l.cond.Signal()
}

type (
dropList []dropListItem
dropListItem struct {
nq *nodeQueue
priority float64
}
)

func (l dropList) Len() int {
return len(l)
}

func (l dropList) Less(i, j int) bool {
return l[i].priority < l[j].priority
}

func (l dropList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
type dropListItem struct {
nq *nodeQueue
priority float64
}

// dropRequests selects the nodes with the highest queued request cost to selection
Expand All @@ -404,7 +389,7 @@ func (l dropList) Swap(i, j int) {
func (l *Limiter) dropRequests() {
var (
sumValue float64
list dropList
list []dropListItem
)

for _, nq := range l.nodes {
Expand All @@ -426,9 +411,9 @@ func (l *Limiter) dropRequests() {
priority: w / float64(nq.sumCost),
})
}

sort.Sort(list)

slices.SortFunc(list, func(a, b dropListItem) bool {
return a.priority < b.priority
})
for _, item := range list {
for _, request := range item.nq.queue {
close(request.process)
Expand Down

0 comments on commit 0b7b8f6

Please sign in to comment.