Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
289c6c3b
Unverified
Commit
289c6c3b
authored
1 year ago
by
Dan Laine
Committed by
GitHub
1 year ago
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
p2p: use slices package for sorting (#27494)
Co-authored-by:
Felix Lange
<
fjl@twurst.com
>
parent
46ec972c
master
No related merge requests found
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
26 additions
and
34 deletions
+26
-34
ntp.go
p2p/discover/ntp.go
+2
-10
table_util_test.go
p2p/discover/table_util_test.go
+5
-5
v4_lookup_test.go
p2p/discover/v4_lookup_test.go
+3
-3
v5_udp_test.go
p2p/discover/v5_udp_test.go
+3
-3
tree.go
p2p/dnsdisc/tree.go
+3
-3
peer.go
p2p/peer.go
+2
-2
protocol.go
p2p/protocol.go
+6
-6
server.go
p2p/server.go
+2
-2
No files found.
p2p/discover/ntp.go
View file @
289c6c3b
...
...
@@ -22,10 +22,10 @@ package discover
import
(
"fmt"
"net"
"sort"
"time"
"github.com/ethereum/go-ethereum/log"
"golang.org/x/exp/slices"
)
const
(
...
...
@@ -33,14 +33,6 @@ const (
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
()
{
...
...
@@ -109,7 +101,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)
s
ort
.
Sort
(
durationSlice
(
drifts
)
)
s
lices
.
Sort
(
drifts
)
drift
:=
time
.
Duration
(
0
)
for
i
:=
1
;
i
<
len
(
drifts
)
-
1
;
i
++
{
...
...
This diff is collapsed.
Click to expand it.
p2p/discover/table_util_test.go
View file @
289c6c3b
...
...
@@ -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
...
...
@@ -217,14 +217,14 @@ func nodeEqual(n1 *enode.Node, n2 *enode.Node) bool {
}
func
sortByID
(
nodes
[]
*
enode
.
Node
)
{
s
ort
.
Slice
(
nodes
,
func
(
i
,
j
int
)
bool
{
return
string
(
nodes
[
i
]
.
ID
()
.
Bytes
())
<
string
(
nodes
[
j
]
.
ID
()
.
Bytes
())
s
lices
.
SortFunc
(
nodes
,
func
(
a
,
b
*
enode
.
Node
)
bool
{
return
string
(
a
.
ID
()
.
Bytes
())
<
string
(
b
.
ID
()
.
Bytes
())
})
}
func
sortedByDistanceTo
(
distbase
enode
.
ID
,
slice
[]
*
node
)
bool
{
return
s
ort
.
SliceIsSorted
(
slice
,
func
(
i
,
j
int
)
bool
{
return
enode
.
DistCmp
(
distbase
,
slice
[
i
]
.
ID
(),
slice
[
j
]
.
ID
())
<
0
return
s
lices
.
IsSortedFunc
(
slice
,
func
(
a
,
b
*
node
)
bool
{
return
enode
.
DistCmp
(
distbase
,
a
.
ID
(),
b
.
ID
())
<
0
})
}
...
...
This diff is collapsed.
Click to expand it.
p2p/discover/v4_lookup_test.go
View file @
289c6c3b
...
...
@@ -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
)
{
...
...
@@ -302,8 +302,8 @@ func (tn *preminedTestnet) closest(n int) (nodes []*enode.Node) {
nodes
=
append
(
nodes
,
tn
.
node
(
d
,
i
))
}
}
s
ort
.
Slice
(
nodes
,
func
(
i
,
j
int
)
bool
{
return
enode
.
DistCmp
(
tn
.
target
.
id
(),
nodes
[
i
]
.
ID
(),
nodes
[
j
]
.
ID
())
<
0
s
lices
.
SortFunc
(
nodes
,
func
(
a
,
b
*
enode
.
Node
)
bool
{
return
enode
.
DistCmp
(
tn
.
target
.
id
(),
a
.
ID
(),
b
.
ID
())
<
0
})
return
nodes
[
:
n
]
}
...
...
This diff is collapsed.
Click to expand it.
p2p/discover/v5_udp_test.go
View file @
289c6c3b
...
...
@@ -24,7 +24,6 @@ import (
"math/rand"
"net"
"reflect"
"sort"
"testing"
"time"
...
...
@@ -35,6 +34,7 @@ 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.
...
...
@@ -61,8 +61,8 @@ func TestUDPv5_lookupE2E(t *testing.T) {
for
i
:=
range
nodes
{
expectedResult
[
i
]
=
nodes
[
i
]
.
Self
()
}
s
ort
.
Slice
(
expectedResult
,
func
(
i
,
j
int
)
bool
{
return
enode
.
DistCmp
(
target
.
ID
(),
expectedResult
[
i
]
.
ID
(),
expectedResult
[
j
]
.
ID
())
<
0
s
lices
.
SortFunc
(
expectedResult
,
func
(
a
,
b
*
enode
.
Node
)
bool
{
return
enode
.
DistCmp
(
target
.
ID
(),
a
.
ID
(),
b
.
ID
())
<
0
})
// Do the lookup.
...
...
This diff is collapsed.
Click to expand it.
p2p/dnsdisc/tree.go
View file @
289c6c3b
...
...
@@ -23,7 +23,6 @@ import (
"encoding/base64"
"fmt"
"io"
"sort"
"strings"
"github.com/ethereum/go-ethereum/crypto"
...
...
@@ -31,6 +30,7 @@ import (
"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.
...
...
@@ -214,8 +214,8 @@ func (t *Tree) build(entries []entry) entry {
}
func
sortByID
(
nodes
[]
*
enode
.
Node
)
[]
*
enode
.
Node
{
s
ort
.
Slice
(
nodes
,
func
(
i
,
j
int
)
bool
{
return
bytes
.
Compare
(
nodes
[
i
]
.
ID
()
.
Bytes
(),
nodes
[
j
]
.
ID
()
.
Bytes
())
<
0
s
lices
.
SortFunc
(
nodes
,
func
(
a
,
b
*
enode
.
Node
)
bool
{
return
bytes
.
Compare
(
a
.
ID
()
.
Bytes
(),
b
.
ID
()
.
Bytes
())
<
0
})
return
nodes
}
...
...
This diff is collapsed.
Click to expand it.
p2p/peer.go
View file @
289c6c3b
...
...
@@ -21,7 +21,6 @@ import (
"fmt"
"io"
"net"
"sort"
"sync"
"time"
...
...
@@ -32,6 +31,7 @@ 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
(
...
...
@@ -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
{
s
ort
.
Sort
(
capsByNameAndVersion
(
caps
)
)
s
lices
.
SortFunc
(
caps
,
Cap
.
Less
)
offset
:=
baseProtocolLength
result
:=
make
(
map
[
string
]
*
protoRW
)
...
...
This diff is collapsed.
Click to expand it.
p2p/protocol.go
View file @
289c6c3b
...
...
@@ -77,10 +77,10 @@ func (cap Cap) String() string {
return
fmt
.
Sprintf
(
"%s/%d"
,
cap
.
Name
,
cap
.
Version
)
}
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
c
s
[
i
]
.
Name
<
cs
[
j
]
.
Name
||
(
cs
[
i
]
.
Name
==
cs
[
j
]
.
Name
&&
cs
[
i
]
.
Version
<
cs
[
j
]
.
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
c
ap
.
Name
<
other
.
Name
}
This diff is collapsed.
Click to expand it.
p2p/server.go
View file @
289c6c3b
...
...
@@ -24,7 +24,6 @@ import (
"errors"
"fmt"
"net"
"sort"
"sync"
"sync/atomic"
"time"
...
...
@@ -39,6 +38,7 @@ 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
(
...
...
@@ -498,7 +498,7 @@ func (srv *Server) setupLocalNode() error {
for
_
,
p
:=
range
srv
.
Protocols
{
srv
.
ourHandshake
.
Caps
=
append
(
srv
.
ourHandshake
.
Caps
,
p
.
cap
())
}
s
ort
.
Sort
(
capsByNameAndVersion
(
srv
.
ourHandshake
.
Caps
)
)
s
lices
.
SortFunc
(
srv
.
ourHandshake
.
Caps
,
Cap
.
Less
)
// Create the local node.
db
,
err
:=
enode
.
OpenDB
(
srv
.
NodeDatabase
)
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment