Skip to content

Commit

Permalink
Revert "p2p: use slices package for sorting (ethereum#27494)"
Browse files Browse the repository at this point in the history
This reverts commit 9bcadfb.
  • Loading branch information
devopsbo3 committed Nov 10, 2023
1 parent b85bbdb commit 8dbdc2b
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 26 deletions.
12 changes: 10 additions & 2 deletions p2p/discover/ntp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,25 @@ package discover
import (
"fmt"
"net"
"sort"
"time"

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

const (
ntpPool = "pool.ntp.org" // ntpPool is the NTP server to query for the current time
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
// one large enough is detected.
func checkClockDrift() {
Expand Down Expand Up @@ -101,7 +109,7 @@ func sntpDrift(measurements int) (time.Duration, error) {
drifts = append(drifts, sent.Sub(t)+elapsed/2)
}
// Calculate average drift (drop two extremities to avoid outliers)
slices.Sort(drifts)
sort.Sort(durationSlice(drifts))

drift := time.Duration(0)
for i := 1; i < len(drifts)-1; i++ {
Expand Down
10 changes: 5 additions & 5 deletions p2p/discover/table_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
"fmt"
"math/rand"
"net"
"sort"
"sync"

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

var nullNode *enode.Node
Expand Down Expand Up @@ -217,14 +217,14 @@ func nodeEqual(n1 *enode.Node, n2 *enode.Node) bool {
}

func sortByID(nodes []*enode.Node) {
slices.SortFunc(nodes, func(a, b *enode.Node) bool {
return string(a.ID().Bytes()) < string(b.ID().Bytes())
sort.Slice(nodes, func(i, j int) bool {
return string(nodes[i].ID().Bytes()) < string(nodes[j].ID().Bytes())
})
}

func sortedByDistanceTo(distbase enode.ID, slice []*node) bool {
return slices.IsSortedFunc(slice, func(a, b *node) bool {
return enode.DistCmp(distbase, a.ID(), b.ID()) < 0
return sort.SliceIsSorted(slice, func(i, j int) bool {
return enode.DistCmp(distbase, slice[i].ID(), slice[j].ID()) < 0
})
}

Expand Down
6 changes: 3 additions & 3 deletions p2p/discover/v4_lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
"crypto/ecdsa"
"fmt"
"net"
"sort"
"testing"

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

func TestUDPv4_Lookup(t *testing.T) {
Expand Down Expand Up @@ -302,8 +302,8 @@ func (tn *preminedTestnet) closest(n int) (nodes []*enode.Node) {
nodes = append(nodes, tn.node(d, i))
}
}
slices.SortFunc(nodes, func(a, b *enode.Node) bool {
return enode.DistCmp(tn.target.id(), a.ID(), b.ID()) < 0
sort.Slice(nodes, func(i, j int) bool {
return enode.DistCmp(tn.target.id(), nodes[i].ID(), nodes[j].ID()) < 0
})
return nodes[:n]
}
Expand Down
6 changes: 3 additions & 3 deletions p2p/discover/v5_udp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"math/rand"
"net"
"reflect"
"sort"
"testing"
"time"

Expand All @@ -34,7 +35,6 @@ import (
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/ethereum/go-ethereum/rlp"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
)

// Real sockets, real crypto: this test checks end-to-end connectivity for UDPv5.
Expand All @@ -61,8 +61,8 @@ func TestUDPv5_lookupE2E(t *testing.T) {
for i := range nodes {
expectedResult[i] = nodes[i].Self()
}
slices.SortFunc(expectedResult, func(a, b *enode.Node) bool {
return enode.DistCmp(target.ID(), a.ID(), b.ID()) < 0
sort.Slice(expectedResult, func(i, j int) bool {
return enode.DistCmp(target.ID(), expectedResult[i].ID(), expectedResult[j].ID()) < 0
})

// Do the lookup.
Expand Down
6 changes: 3 additions & 3 deletions p2p/dnsdisc/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import (
"encoding/base64"
"fmt"
"io"
"sort"
"strings"

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

// Tree is a merkle tree of node records.
Expand Down Expand Up @@ -214,8 +214,8 @@ func (t *Tree) build(entries []entry) entry {
}

func sortByID(nodes []*enode.Node) []*enode.Node {
slices.SortFunc(nodes, func(a, b *enode.Node) bool {
return bytes.Compare(a.ID().Bytes(), b.ID().Bytes()) < 0
sort.Slice(nodes, func(i, j int) bool {
return bytes.Compare(nodes[i].ID().Bytes(), nodes[j].ID().Bytes()) < 0
})
return nodes
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"net"
"sort"
"sync"
"time"

Expand All @@ -31,7 +32,6 @@ import (
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/ethereum/go-ethereum/rlp"
"golang.org/x/exp/slices"
)

var (
Expand Down Expand Up @@ -375,7 +375,7 @@ func countMatchingProtocols(protocols []Protocol, caps []Cap) int {

// matchProtocols creates structures for matching named subprotocols.
func matchProtocols(protocols []Protocol, caps []Cap, rw MsgReadWriter) map[string]*protoRW {
slices.SortFunc(caps, Cap.Less)
sort.Sort(capsByNameAndVersion(caps))
offset := baseProtocolLength
result := make(map[string]*protoRW)

Expand Down
12 changes: 6 additions & 6 deletions p2p/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ func (cap Cap) String() string {
return fmt.Sprintf("%s/%d", cap.Name, cap.Version)
}

// Less defines the canonical sorting order of capabilities.
func (cap Cap) Less(other Cap) bool {
if cap.Name == other.Name {
return cap.Version < other.Version
}
return cap.Name < other.Name
type capsByNameAndVersion []Cap

func (cs capsByNameAndVersion) Len() int { return len(cs) }
func (cs capsByNameAndVersion) Swap(i, j int) { cs[i], cs[j] = cs[j], cs[i] }
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)
}
4 changes: 2 additions & 2 deletions p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"errors"
"fmt"
"net"
"sort"
"sync"
"sync/atomic"
"time"
Expand All @@ -38,7 +39,6 @@ import (
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/p2p/netutil"
"golang.org/x/exp/slices"
)

const (
Expand Down Expand Up @@ -498,7 +498,7 @@ func (srv *Server) setupLocalNode() error {
for _, p := range srv.Protocols {
srv.ourHandshake.Caps = append(srv.ourHandshake.Caps, p.cap())
}
slices.SortFunc(srv.ourHandshake.Caps, Cap.Less)
sort.Sort(capsByNameAndVersion(srv.ourHandshake.Caps))

// Create the local node.
db, err := enode.OpenDB(srv.NodeDatabase)
Expand Down

0 comments on commit 8dbdc2b

Please sign in to comment.