Skip to content

Commit f20616e

Browse files
committed
p2p: use slices package for sorting ethereum#27494
1 parent b49f6cb commit f20616e

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

p2p/discover/ntp.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ package discover
2222
import (
2323
"fmt"
2424
"net"
25-
"sort"
25+
"slices"
2626
"time"
2727

2828
"github.com/XinFinOrg/XDPoSChain/log"
@@ -33,14 +33,6 @@ const (
3333
ntpChecks = 3 // Number of measurements to do against the NTP server
3434
)
3535

36-
// durationSlice attaches the methods of sort.Interface to []time.Duration,
37-
// sorting in increasing order.
38-
type durationSlice []time.Duration
39-
40-
func (s durationSlice) Len() int { return len(s) }
41-
func (s durationSlice) Less(i, j int) bool { return s[i] < s[j] }
42-
func (s durationSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
43-
4436
// checkClockDrift queries an NTP server for clock drifts and warns the user if
4537
// one large enough is detected.
4638
func checkClockDrift() {
@@ -109,7 +101,7 @@ func sntpDrift(measurements int) (time.Duration, error) {
109101
drifts = append(drifts, sent.Sub(t)+elapsed/2)
110102
}
111103
// Calculate average drif (drop two extremities to avoid outliers)
112-
sort.Sort(durationSlice(drifts))
104+
slices.Sort(drifts)
113105

114106
drift := time.Duration(0)
115107
for i := 1; i < len(drifts)-1; i++ {

p2p/peer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"fmt"
2222
"io"
2323
"net"
24-
"sort"
24+
"slices"
2525
"sync"
2626
"time"
2727

@@ -329,7 +329,7 @@ func countMatchingProtocols(protocols []Protocol, caps []Cap) int {
329329

330330
// matchProtocols creates structures for matching named subprotocols.
331331
func matchProtocols(protocols []Protocol, caps []Cap, rw MsgReadWriter) map[string]*protoRW {
332-
sort.Sort(capsByNameAndVersion(caps))
332+
slices.SortFunc(caps, Cap.Cmp)
333333
offset := baseProtocolLength
334334
result := make(map[string]*protoRW)
335335

p2p/protocol.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package p2p
1818

1919
import (
20+
"cmp"
2021
"fmt"
22+
"strings"
2123

2224
"github.com/XinFinOrg/XDPoSChain/p2p/discover"
2325
)
@@ -72,10 +74,10 @@ func (cap Cap) String() string {
7274
return fmt.Sprintf("%s/%d", cap.Name, cap.Version)
7375
}
7476

75-
type capsByNameAndVersion []Cap
76-
77-
func (cs capsByNameAndVersion) Len() int { return len(cs) }
78-
func (cs capsByNameAndVersion) Swap(i, j int) { cs[i], cs[j] = cs[j], cs[i] }
79-
func (cs capsByNameAndVersion) Less(i, j int) bool {
80-
return cs[i].Name < cs[j].Name || (cs[i].Name == cs[j].Name && cs[i].Version < cs[j].Version)
77+
// Cmp defines the canonical sorting order of capabilities.
78+
func (cap Cap) Cmp(other Cap) int {
79+
if cap.Name == other.Name {
80+
return cmp.Compare(cap.Version, other.Version)
81+
}
82+
return strings.Compare(cap.Name, other.Name)
8183
}

0 commit comments

Comments
 (0)