Skip to content

Commit 90caa2c

Browse files
authored
p2p: new dial scheduler (ethereum#20592)
* p2p: new dial scheduler This change replaces the peer-to-peer dial scheduler with a new and improved implementation. The new code is better than the previous implementation in two key aspects: - The time between discovery of a node and dialing that node is significantly lower in the new version. The old dialState kept a buffer of nodes and launched a task to refill it whenever the buffer became empty. This worked well with the discovery interface we used to have, but doesn't really work with the new iterator-based discovery API. - Selection of static dial candidates (created by Server.AddPeer or through static-nodes.json) performs much better for large amounts of static peers. Connections to static nodes are now limited like dynanic dials and can no longer overstep MaxPeers or the dial ratio. * p2p/simulations/adapters: adapt to new NodeDialer interface * p2p: re-add check for self in checkDial * p2p: remove peersetCh * p2p: allow static dials when discovery is disabled * p2p: add test for dialScheduler.removeStatic * p2p: remove blank line * p2p: fix documentation of maxDialPeers * p2p: change "ok" to "added" in static node log * p2p: improve dialTask docs Also increase log level for "Can't resolve node" * p2p: ensure dial resolver is truly nil without discovery * p2p: add "looking for peers" log message * p2p: clean up Server.run comments * p2p: fix maxDialedConns for maxpeers < dialRatio Always allocate at least one dial slot unless dialing is disabled using NoDial or MaxPeers == 0. Most importantly, this fixes MaxPeers == 1 to dedicate the sole slot to dialing instead of listening. * p2p: fix RemovePeer to disconnect the peer again Also make RemovePeer synchronous and add a test. * p2p: remove "Connection set up" log message * p2p: clean up connection logging We previously logged outgoing connection failures up to three times. - in SetupConn() as "Setting up connection failed addr=..." - in setupConn() with an error-specific message and "id=... addr=..." - in dial() as "Dial error task=..." This commit ensures a single log message is emitted per failure and adds "id=... addr=... conn=..." everywhere (id= omitted when the ID isn't known yet). Also avoid printing a log message when a static dial fails but can't be resolved because discv4 is disabled. The light client hit this case all the time, increasing the message count to four lines per failed connection. * p2p: document that RemovePeer blocks
1 parent 5f2002b commit 90caa2c

File tree

8 files changed

+1244
-1048
lines changed

8 files changed

+1244
-1048
lines changed

p2p/dial.go

+395-196
Large diffs are not rendered by default.

p2p/dial_test.go

+583-501
Large diffs are not rendered by default.

p2p/peer_test.go

+42-2
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@
1717
package p2p
1818

1919
import (
20+
"encoding/binary"
2021
"errors"
2122
"fmt"
2223
"math/rand"
2324
"net"
2425
"reflect"
26+
"strconv"
27+
"strings"
2528
"testing"
2629
"time"
2730

2831
"github.com/ethereum/go-ethereum/log"
32+
"github.com/ethereum/go-ethereum/p2p/enode"
33+
"github.com/ethereum/go-ethereum/p2p/enr"
2934
)
3035

3136
var discard = Protocol{
@@ -45,10 +50,45 @@ var discard = Protocol{
4550
},
4651
}
4752

53+
// uintID encodes i into a node ID.
54+
func uintID(i uint16) enode.ID {
55+
var id enode.ID
56+
binary.BigEndian.PutUint16(id[:], i)
57+
return id
58+
}
59+
60+
// newNode creates a node record with the given address.
61+
func newNode(id enode.ID, addr string) *enode.Node {
62+
var r enr.Record
63+
if addr != "" {
64+
// Set the port if present.
65+
if strings.Contains(addr, ":") {
66+
hs, ps, err := net.SplitHostPort(addr)
67+
if err != nil {
68+
panic(fmt.Errorf("invalid address %q", addr))
69+
}
70+
port, err := strconv.Atoi(ps)
71+
if err != nil {
72+
panic(fmt.Errorf("invalid port in %q", addr))
73+
}
74+
r.Set(enr.TCP(port))
75+
r.Set(enr.UDP(port))
76+
addr = hs
77+
}
78+
// Set the IP.
79+
ip := net.ParseIP(addr)
80+
if ip == nil {
81+
panic(fmt.Errorf("invalid IP %q", addr))
82+
}
83+
r.Set(enr.IP(ip))
84+
}
85+
return enode.SignNull(&r, id)
86+
}
87+
4888
func testPeer(protos []Protocol) (func(), *conn, *Peer, <-chan error) {
4989
fd1, fd2 := net.Pipe()
50-
c1 := &conn{fd: fd1, node: newNode(randomID(), nil), transport: newTestTransport(&newkey().PublicKey, fd1)}
51-
c2 := &conn{fd: fd2, node: newNode(randomID(), nil), transport: newTestTransport(&newkey().PublicKey, fd2)}
90+
c1 := &conn{fd: fd1, node: newNode(randomID(), ""), transport: newTestTransport(&newkey().PublicKey, fd1)}
91+
c2 := &conn{fd: fd2, node: newNode(randomID(), ""), transport: newTestTransport(&newkey().PublicKey, fd2)}
5292
for _, p := range protos {
5393
c1.caps = append(c1.caps, p.cap())
5494
c2.caps = append(c2.caps, p.cap())

0 commit comments

Comments
 (0)