p2p: use slices package for sorting (#27494)
Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
parent
46ec972c9c
commit
289c6c3b15
@ -22,10 +22,10 @@ package discover
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"sort"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -33,14 +33,6 @@ const (
|
|||||||
ntpChecks = 3 // Number of measurements to do against the NTP server
|
ntpChecks = 3 // Number of measurements to do against the NTP server
|
||||||
)
|
)
|
||||||
|
|
||||||
// durationSlice attaches the methods of sort.Interface to []time.Duration,
|
|
||||||
// sorting in increasing order.
|
|
||||||
type durationSlice []time.Duration
|
|
||||||
|
|
||||||
func (s durationSlice) Len() int { return len(s) }
|
|
||||||
func (s durationSlice) Less(i, j int) bool { return s[i] < s[j] }
|
|
||||||
func (s durationSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
||||||
|
|
||||||
// checkClockDrift queries an NTP server for clock drifts and warns the user if
|
// checkClockDrift queries an NTP server for clock drifts and warns the user if
|
||||||
// one large enough is detected.
|
// one large enough is detected.
|
||||||
func checkClockDrift() {
|
func checkClockDrift() {
|
||||||
@ -109,7 +101,7 @@ func sntpDrift(measurements int) (time.Duration, error) {
|
|||||||
drifts = append(drifts, sent.Sub(t)+elapsed/2)
|
drifts = append(drifts, sent.Sub(t)+elapsed/2)
|
||||||
}
|
}
|
||||||
// Calculate average drift (drop two extremities to avoid outliers)
|
// Calculate average drift (drop two extremities to avoid outliers)
|
||||||
sort.Sort(durationSlice(drifts))
|
slices.Sort(drifts)
|
||||||
|
|
||||||
drift := time.Duration(0)
|
drift := time.Duration(0)
|
||||||
for i := 1; i < len(drifts)-1; i++ {
|
for i := 1; i < len(drifts)-1; i++ {
|
||||||
|
@ -24,12 +24,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"sort"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
var nullNode *enode.Node
|
var nullNode *enode.Node
|
||||||
@ -217,14 +217,14 @@ func nodeEqual(n1 *enode.Node, n2 *enode.Node) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sortByID(nodes []*enode.Node) {
|
func sortByID(nodes []*enode.Node) {
|
||||||
sort.Slice(nodes, func(i, j int) bool {
|
slices.SortFunc(nodes, func(a, b *enode.Node) bool {
|
||||||
return string(nodes[i].ID().Bytes()) < string(nodes[j].ID().Bytes())
|
return string(a.ID().Bytes()) < string(b.ID().Bytes())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func sortedByDistanceTo(distbase enode.ID, slice []*node) bool {
|
func sortedByDistanceTo(distbase enode.ID, slice []*node) bool {
|
||||||
return sort.SliceIsSorted(slice, func(i, j int) bool {
|
return slices.IsSortedFunc(slice, func(a, b *node) bool {
|
||||||
return enode.DistCmp(distbase, slice[i].ID(), slice[j].ID()) < 0
|
return enode.DistCmp(distbase, a.ID(), b.ID()) < 0
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,13 +20,13 @@ import (
|
|||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"sort"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/p2p/discover/v4wire"
|
"github.com/ethereum/go-ethereum/p2p/discover/v4wire"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUDPv4_Lookup(t *testing.T) {
|
func TestUDPv4_Lookup(t *testing.T) {
|
||||||
@ -302,8 +302,8 @@ func (tn *preminedTestnet) closest(n int) (nodes []*enode.Node) {
|
|||||||
nodes = append(nodes, tn.node(d, i))
|
nodes = append(nodes, tn.node(d, i))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sort.Slice(nodes, func(i, j int) bool {
|
slices.SortFunc(nodes, func(a, b *enode.Node) bool {
|
||||||
return enode.DistCmp(tn.target.id(), nodes[i].ID(), nodes[j].ID()) < 0
|
return enode.DistCmp(tn.target.id(), a.ID(), b.ID()) < 0
|
||||||
})
|
})
|
||||||
return nodes[:n]
|
return nodes[:n]
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,6 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -35,6 +34,7 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Real sockets, real crypto: this test checks end-to-end connectivity for UDPv5.
|
// Real sockets, real crypto: this test checks end-to-end connectivity for UDPv5.
|
||||||
@ -61,8 +61,8 @@ func TestUDPv5_lookupE2E(t *testing.T) {
|
|||||||
for i := range nodes {
|
for i := range nodes {
|
||||||
expectedResult[i] = nodes[i].Self()
|
expectedResult[i] = nodes[i].Self()
|
||||||
}
|
}
|
||||||
sort.Slice(expectedResult, func(i, j int) bool {
|
slices.SortFunc(expectedResult, func(a, b *enode.Node) bool {
|
||||||
return enode.DistCmp(target.ID(), expectedResult[i].ID(), expectedResult[j].ID()) < 0
|
return enode.DistCmp(target.ID(), a.ID(), b.ID()) < 0
|
||||||
})
|
})
|
||||||
|
|
||||||
// Do the lookup.
|
// Do the lookup.
|
||||||
|
@ -23,7 +23,6 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
@ -31,6 +30,7 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
"golang.org/x/crypto/sha3"
|
"golang.org/x/crypto/sha3"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Tree is a merkle tree of node records.
|
// Tree is a merkle tree of node records.
|
||||||
@ -214,8 +214,8 @@ func (t *Tree) build(entries []entry) entry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sortByID(nodes []*enode.Node) []*enode.Node {
|
func sortByID(nodes []*enode.Node) []*enode.Node {
|
||||||
sort.Slice(nodes, func(i, j int) bool {
|
slices.SortFunc(nodes, func(a, b *enode.Node) bool {
|
||||||
return bytes.Compare(nodes[i].ID().Bytes(), nodes[j].ID().Bytes()) < 0
|
return bytes.Compare(a.ID().Bytes(), b.ID().Bytes()) < 0
|
||||||
})
|
})
|
||||||
return nodes
|
return nodes
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"sort"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -32,6 +31,7 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -375,7 +375,7 @@ func countMatchingProtocols(protocols []Protocol, caps []Cap) int {
|
|||||||
|
|
||||||
// matchProtocols creates structures for matching named subprotocols.
|
// matchProtocols creates structures for matching named subprotocols.
|
||||||
func matchProtocols(protocols []Protocol, caps []Cap, rw MsgReadWriter) map[string]*protoRW {
|
func matchProtocols(protocols []Protocol, caps []Cap, rw MsgReadWriter) map[string]*protoRW {
|
||||||
sort.Sort(capsByNameAndVersion(caps))
|
slices.SortFunc(caps, Cap.Less)
|
||||||
offset := baseProtocolLength
|
offset := baseProtocolLength
|
||||||
result := make(map[string]*protoRW)
|
result := make(map[string]*protoRW)
|
||||||
|
|
||||||
|
@ -77,10 +77,10 @@ func (cap Cap) String() string {
|
|||||||
return fmt.Sprintf("%s/%d", cap.Name, cap.Version)
|
return fmt.Sprintf("%s/%d", cap.Name, cap.Version)
|
||||||
}
|
}
|
||||||
|
|
||||||
type capsByNameAndVersion []Cap
|
// Less defines the canonical sorting order of capabilities.
|
||||||
|
func (cap Cap) Less(other Cap) bool {
|
||||||
func (cs capsByNameAndVersion) Len() int { return len(cs) }
|
if cap.Name == other.Name {
|
||||||
func (cs capsByNameAndVersion) Swap(i, j int) { cs[i], cs[j] = cs[j], cs[i] }
|
return cap.Version < other.Version
|
||||||
func (cs capsByNameAndVersion) Less(i, j int) bool {
|
}
|
||||||
return cs[i].Name < cs[j].Name || (cs[i].Name == cs[j].Name && cs[i].Version < cs[j].Version)
|
return cap.Name < other.Name
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"sort"
|
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
@ -39,6 +38,7 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||||
"github.com/ethereum/go-ethereum/p2p/nat"
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
||||||
"github.com/ethereum/go-ethereum/p2p/netutil"
|
"github.com/ethereum/go-ethereum/p2p/netutil"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -498,7 +498,7 @@ func (srv *Server) setupLocalNode() error {
|
|||||||
for _, p := range srv.Protocols {
|
for _, p := range srv.Protocols {
|
||||||
srv.ourHandshake.Caps = append(srv.ourHandshake.Caps, p.cap())
|
srv.ourHandshake.Caps = append(srv.ourHandshake.Caps, p.cap())
|
||||||
}
|
}
|
||||||
sort.Sort(capsByNameAndVersion(srv.ourHandshake.Caps))
|
slices.SortFunc(srv.ourHandshake.Caps, Cap.Less)
|
||||||
|
|
||||||
// Create the local node.
|
// Create the local node.
|
||||||
db, err := enode.OpenDB(srv.NodeDatabase)
|
db, err := enode.OpenDB(srv.NodeDatabase)
|
||||||
|
Loading…
Reference in New Issue
Block a user