mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
Revert "internal/ethapi, les: use slices package for sorting (#27492)"
This reverts commit 051b1a1fd9.
This commit is contained in:
parent
339796446e
commit
39aaddb4c8
3 changed files with 56 additions and 22 deletions
|
|
@ -17,6 +17,7 @@
|
|||
package ethapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"encoding/json"
|
||||
|
|
@ -24,6 +25,7 @@ import (
|
|||
"hash"
|
||||
"math/big"
|
||||
"reflect"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -46,7 +48,6 @@ 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) {
|
||||
|
|
@ -648,13 +649,19 @@ type Account struct {
|
|||
addr common.Address
|
||||
}
|
||||
|
||||
func newAccounts(n int) (accounts []Account) {
|
||||
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) {
|
||||
for i := 0; i < n; i++ {
|
||||
key, _ := crypto.GenerateKey()
|
||||
addr := crypto.PubkeyToAddress(key.PublicKey)
|
||||
accounts = append(accounts, Account{key: key, addr: addr})
|
||||
}
|
||||
slices.SortFunc(accounts, func(a, b Account) bool { return a.addr.Less(b.addr) })
|
||||
sort.Sort(accounts)
|
||||
return accounts
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -180,19 +180,35 @@ func (sq *servingQueue) threadController() {
|
|||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
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]
|
||||
}
|
||||
|
||||
// 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 []*peerTasks
|
||||
var peerList peerList
|
||||
if sq.best != nil {
|
||||
sq.queue.Push(sq.best, sq.best.priority)
|
||||
}
|
||||
|
|
@ -215,9 +231,7 @@ func (sq *servingQueue) freezePeers() {
|
|||
tasks.list = append(tasks.list, task)
|
||||
tasks.sumTime += task.expTime
|
||||
}
|
||||
slices.SortFunc(peerList, func(a, b *peerTasks) bool {
|
||||
return a.priority < b.priority
|
||||
})
|
||||
sort.Sort(peerList)
|
||||
drop := true
|
||||
for _, tasks := range peerList {
|
||||
if drop {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -340,9 +340,24 @@ func (l *Limiter) Stop() {
|
|||
l.cond.Signal()
|
||||
}
|
||||
|
||||
type dropListItem struct {
|
||||
nq *nodeQueue
|
||||
priority float64
|
||||
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]
|
||||
}
|
||||
|
||||
// dropRequests selects the nodes with the highest queued request cost to selection
|
||||
|
|
@ -351,7 +366,7 @@ type dropListItem struct {
|
|||
func (l *Limiter) dropRequests() {
|
||||
var (
|
||||
sumValue float64
|
||||
list []dropListItem
|
||||
list dropList
|
||||
)
|
||||
for _, nq := range l.nodes {
|
||||
sumValue += nq.value
|
||||
|
|
@ -369,9 +384,7 @@ func (l *Limiter) dropRequests() {
|
|||
priority: w / float64(nq.sumCost),
|
||||
})
|
||||
}
|
||||
slices.SortFunc(list, func(a, b dropListItem) bool {
|
||||
return a.priority < b.priority
|
||||
})
|
||||
sort.Sort(list)
|
||||
for _, item := range list {
|
||||
for _, request := range item.nq.queue {
|
||||
close(request.process)
|
||||
|
|
|
|||
Loading…
Reference in a new issue