aboutsummaryrefslogtreecommitdiffstats
path: root/p2p
diff options
context:
space:
mode:
authorDaniel A. Nagy <nagy.da@gmail.com>2015-05-08 23:55:53 +0800
committerDaniel A. Nagy <nagy.da@gmail.com>2015-05-08 23:55:53 +0800
commit62dd9833ec768e2026bccb1cf7a8ef4263b9286d (patch)
tree0a091d99afd7f8cf5e3a6d4522c30ceef8559a55 /p2p
parent3a01e3e39b9ce83ecb7444319407ee8bb00e3bf6 (diff)
parentc8fc4cebe63073fd77d5f553a4f0cec36a4ccb4b (diff)
downloaddexon-62dd9833ec768e2026bccb1cf7a8ef4263b9286d.tar
dexon-62dd9833ec768e2026bccb1cf7a8ef4263b9286d.tar.gz
dexon-62dd9833ec768e2026bccb1cf7a8ef4263b9286d.tar.bz2
dexon-62dd9833ec768e2026bccb1cf7a8ef4263b9286d.tar.lz
dexon-62dd9833ec768e2026bccb1cf7a8ef4263b9286d.tar.xz
dexon-62dd9833ec768e2026bccb1cf7a8ef4263b9286d.tar.zst
dexon-62dd9833ec768e2026bccb1cf7a8ef4263b9286d.zip
Merge branch 'develop' of github.com:ethereum/go-ethereum into develop
Diffstat (limited to 'p2p')
-rw-r--r--p2p/handshake.go24
-rw-r--r--p2p/handshake_test.go5
-rw-r--r--p2p/peer.go12
-rw-r--r--p2p/server.go77
-rw-r--r--p2p/server_test.go147
5 files changed, 223 insertions, 42 deletions
diff --git a/p2p/handshake.go b/p2p/handshake.go
index 8e611cfd5..4cdcee6d4 100644
--- a/p2p/handshake.go
+++ b/p2p/handshake.go
@@ -65,26 +65,26 @@ type protoHandshake struct {
ID discover.NodeID
}
-// setupConn starts a protocol session on the given connection.
-// It runs the encryption handshake and the protocol handshake.
-// If dial is non-nil, the connection the local node is the initiator.
-// If atcap is true, the connection will be disconnected with DiscTooManyPeers
-// after the key exchange.
-func setupConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, atcap bool, trusted map[discover.NodeID]bool) (*conn, error) {
+// setupConn starts a protocol session on the given connection. It
+// runs the encryption handshake and the protocol handshake. If dial
+// is non-nil, the connection the local node is the initiator. If
+// keepconn returns false, the connection will be disconnected with
+// DiscTooManyPeers after the key exchange.
+func setupConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, keepconn func(discover.NodeID) bool) (*conn, error) {
if dial == nil {
- return setupInboundConn(fd, prv, our, atcap, trusted)
+ return setupInboundConn(fd, prv, our, keepconn)
} else {
- return setupOutboundConn(fd, prv, our, dial, atcap, trusted)
+ return setupOutboundConn(fd, prv, our, dial, keepconn)
}
}
-func setupInboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, atcap bool, trusted map[discover.NodeID]bool) (*conn, error) {
+func setupInboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, keepconn func(discover.NodeID) bool) (*conn, error) {
secrets, err := receiverEncHandshake(fd, prv, nil)
if err != nil {
return nil, fmt.Errorf("encryption handshake failed: %v", err)
}
rw := newRlpxFrameRW(fd, secrets)
- if atcap && !trusted[secrets.RemoteID] {
+ if !keepconn(secrets.RemoteID) {
SendItems(rw, discMsg, DiscTooManyPeers)
return nil, errors.New("we have too many peers")
}
@@ -99,13 +99,13 @@ func setupInboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, a
return &conn{rw, rhs}, nil
}
-func setupOutboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, atcap bool, trusted map[discover.NodeID]bool) (*conn, error) {
+func setupOutboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, keepconn func(discover.NodeID) bool) (*conn, error) {
secrets, err := initiatorEncHandshake(fd, prv, dial.ID, nil)
if err != nil {
return nil, fmt.Errorf("encryption handshake failed: %v", err)
}
rw := newRlpxFrameRW(fd, secrets)
- if atcap && !trusted[secrets.RemoteID] {
+ if !keepconn(secrets.RemoteID) {
SendItems(rw, discMsg, DiscTooManyPeers)
return nil, errors.New("we have too many peers")
}
diff --git a/p2p/handshake_test.go b/p2p/handshake_test.go
index 9018e14f2..ab75921a3 100644
--- a/p2p/handshake_test.go
+++ b/p2p/handshake_test.go
@@ -141,9 +141,10 @@ func TestSetupConn(t *testing.T) {
fd0, fd1 := net.Pipe()
done := make(chan struct{})
+ keepalways := func(discover.NodeID) bool { return true }
go func() {
defer close(done)
- conn0, err := setupConn(fd0, prv0, hs0, node1, false, nil)
+ conn0, err := setupConn(fd0, prv0, hs0, node1, keepalways)
if err != nil {
t.Errorf("outbound side error: %v", err)
return
@@ -156,7 +157,7 @@ func TestSetupConn(t *testing.T) {
}
}()
- conn1, err := setupConn(fd1, prv1, hs1, nil, false, nil)
+ conn1, err := setupConn(fd1, prv1, hs1, nil, keepalways)
if err != nil {
t.Fatalf("inbound side error: %v", err)
}
diff --git a/p2p/peer.go b/p2p/peer.go
index cdf9ba965..ac691f2ce 100644
--- a/p2p/peer.go
+++ b/p2p/peer.go
@@ -211,6 +211,18 @@ func (p *Peer) handle(msg Msg) error {
return nil
}
+func countMatchingProtocols(protocols []Protocol, caps []Cap) int {
+ n := 0
+ for _, cap := range caps {
+ for _, proto := range protocols {
+ if proto.Name == cap.Name && proto.Version == cap.Version {
+ n++
+ }
+ }
+ }
+ return n
+}
+
// matchProtocols creates structures for matching named subprotocols.
func matchProtocols(protocols []Protocol, caps []Cap, rw MsgReadWriter) map[string]*protoRW {
sort.Sort(capsByName(caps))
diff --git a/p2p/server.go b/p2p/server.go
index 5e0c917fc..3c6fb5893 100644
--- a/p2p/server.go
+++ b/p2p/server.go
@@ -22,10 +22,11 @@ const (
refreshPeersInterval = 30 * time.Second
staticPeerCheckInterval = 15 * time.Second
- // This is the maximum number of inbound connection
- // that are allowed to linger between 'accepted' and
- // 'added as peer'.
- maxAcceptConns = 50
+ // Maximum number of concurrently handshaking inbound connections.
+ maxAcceptConns = 10
+
+ // Maximum number of concurrently dialing outbound connections.
+ maxDialingConns = 10
// total timeout for encryption handshake and protocol
// handshake in both directions.
@@ -52,6 +53,11 @@ type Server struct {
// connected. It must be greater than zero.
MaxPeers int
+ // MaxPendingPeers is the maximum number of peers that can be pending in the
+ // handshake phase, counted separately for inbound and outbound connections.
+ // Zero defaults to preset values.
+ MaxPendingPeers int
+
// Name sets the node name of this server.
// Use common.MakeName to create a name that follows existing conventions.
Name string
@@ -120,7 +126,7 @@ type Server struct {
peerWG sync.WaitGroup // active peer goroutines
}
-type setupFunc func(net.Conn, *ecdsa.PrivateKey, *protoHandshake, *discover.Node, bool, map[discover.NodeID]bool) (*conn, error)
+type setupFunc func(net.Conn, *ecdsa.PrivateKey, *protoHandshake, *discover.Node, func(discover.NodeID) bool) (*conn, error)
type newPeerHook func(*Peer)
// Peers returns all connected peers.
@@ -331,8 +337,12 @@ func (srv *Server) listenLoop() {
// This channel acts as a semaphore limiting
// active inbound connections that are lingering pre-handshake.
// If all slots are taken, no further connections are accepted.
- slots := make(chan struct{}, maxAcceptConns)
- for i := 0; i < maxAcceptConns; i++ {
+ tokens := maxAcceptConns
+ if srv.MaxPendingPeers > 0 {
+ tokens = srv.MaxPendingPeers
+ }
+ slots := make(chan struct{}, tokens)
+ for i := 0; i < tokens; i++ {
slots <- struct{}{}
}
@@ -401,7 +411,15 @@ func (srv *Server) dialLoop() {
defer srv.loopWG.Done()
defer refresh.Stop()
- // TODO: maybe limit number of active dials
+ // Limit the number of concurrent dials
+ tokens := maxDialingConns
+ if srv.MaxPendingPeers > 0 {
+ tokens = srv.MaxPendingPeers
+ }
+ slots := make(chan struct{}, tokens)
+ for i := 0; i < tokens; i++ {
+ slots <- struct{}{}
+ }
dial := func(dest *discover.Node) {
// Don't dial nodes that would fail the checks in addPeer.
// This is important because the connection handshake is a lot
@@ -413,11 +431,14 @@ func (srv *Server) dialLoop() {
if !ok || dialing[dest.ID] {
return
}
+ // Request a dial slot to prevent CPU exhaustion
+ <-slots
dialing[dest.ID] = true
srv.peerWG.Add(1)
go func() {
srv.dialNode(dest)
+ slots <- struct{}{}
dialed <- dest
}()
}
@@ -485,17 +506,7 @@ func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
// the callers of startPeer added the peer to the wait group already.
fd.SetDeadline(time.Now().Add(handshakeTimeout))
- // Check capacity, but override for static nodes
- srv.lock.RLock()
- atcap := len(srv.peers) == srv.MaxPeers
- if dest != nil {
- if _, ok := srv.staticNodes[dest.ID]; ok {
- atcap = false
- }
- }
- srv.lock.RUnlock()
-
- conn, err := srv.setupFunc(fd, srv.PrivateKey, srv.ourHandshake, dest, atcap, srv.trustedNodes)
+ conn, err := srv.setupFunc(fd, srv.PrivateKey, srv.ourHandshake, dest, srv.keepconn)
if err != nil {
fd.Close()
glog.V(logger.Debug).Infof("Handshake with %v failed: %v", fd.RemoteAddr(), err)
@@ -507,7 +518,7 @@ func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
conn: fd, rtimeout: frameReadTimeout, wtimeout: frameWriteTimeout,
}
p := newPeer(fd, conn, srv.Protocols)
- if ok, reason := srv.addPeer(conn.ID, p); !ok {
+ if ok, reason := srv.addPeer(conn, p); !ok {
glog.V(logger.Detail).Infof("Not adding %v (%v)\n", p, reason)
p.politeDisconnect(reason)
srv.peerWG.Done()
@@ -518,6 +529,21 @@ func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
go srv.runPeer(p)
}
+// preflight checks whether a connection should be kept. it runs
+// after the encryption handshake, as soon as the remote identity is
+// known.
+func (srv *Server) keepconn(id discover.NodeID) bool {
+ srv.lock.RLock()
+ defer srv.lock.RUnlock()
+ if _, ok := srv.staticNodes[id]; ok {
+ return true // static nodes are always allowed
+ }
+ if _, ok := srv.trustedNodes[id]; ok {
+ return true // trusted nodes are always allowed
+ }
+ return len(srv.peers) < srv.MaxPeers
+}
+
func (srv *Server) runPeer(p *Peer) {
glog.V(logger.Debug).Infof("Added %v\n", p)
srvjslog.LogJson(&logger.P2PConnected{
@@ -538,13 +564,18 @@ func (srv *Server) runPeer(p *Peer) {
})
}
-func (srv *Server) addPeer(id discover.NodeID, p *Peer) (bool, DiscReason) {
+func (srv *Server) addPeer(conn *conn, p *Peer) (bool, DiscReason) {
+ // drop connections with no matching protocols.
+ if len(srv.Protocols) > 0 && countMatchingProtocols(srv.Protocols, conn.protoHandshake.Caps) == 0 {
+ return false, DiscUselessPeer
+ }
+ // add the peer if it passes the other checks.
srv.lock.Lock()
defer srv.lock.Unlock()
- if ok, reason := srv.checkPeer(id); !ok {
+ if ok, reason := srv.checkPeer(conn.ID); !ok {
return false, reason
}
- srv.peers[id] = p
+ srv.peers[conn.ID] = p
return true, 0
}
diff --git a/p2p/server_test.go b/p2p/server_test.go
index 3f9db343c..bf9df31ab 100644
--- a/p2p/server_test.go
+++ b/p2p/server_test.go
@@ -22,8 +22,11 @@ func startTestServer(t *testing.T, pf newPeerHook) *Server {
ListenAddr: "127.0.0.1:0",
PrivateKey: newkey(),
newPeerHook: pf,
- setupFunc: func(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, atcap bool, trusted map[discover.NodeID]bool) (*conn, error) {
+ setupFunc: func(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, keepconn func(discover.NodeID) bool) (*conn, error) {
id := randomID()
+ if !keepconn(id) {
+ return nil, DiscAlreadyConnected
+ }
rw := newRlpxFrameRW(fd, secrets{
MAC: zero16,
AES: zero16,
@@ -200,7 +203,7 @@ func TestServerDisconnectAtCap(t *testing.T) {
// Run the handshakes just like a real peer would.
key := newkey()
hs := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
- _, err = setupConn(conn, key, hs, srv.Self(), false, srv.trustedNodes)
+ _, err = setupConn(conn, key, hs, srv.Self(), keepalways)
if i == nconns-1 {
// When handling the last connection, the server should
// disconnect immediately instead of running the protocol
@@ -250,7 +253,7 @@ func TestServerStaticPeers(t *testing.T) {
// Run the handshakes just like a real peer would, and wait for completion
key := newkey()
shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
- if _, err = setupConn(conn, key, shake, server.Self(), false, server.trustedNodes); err != nil {
+ if _, err = setupConn(conn, key, shake, server.Self(), keepalways); err != nil {
t.Fatalf("conn %d: unexpected error: %v", i, err)
}
<-started
@@ -344,7 +347,7 @@ func TestServerTrustedPeers(t *testing.T) {
// Run the handshakes just like a real peer would, and wait for completion
key := newkey()
shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
- if _, err = setupConn(conn, key, shake, server.Self(), false, server.trustedNodes); err != nil {
+ if _, err = setupConn(conn, key, shake, server.Self(), keepalways); err != nil {
t.Fatalf("conn %d: unexpected error: %v", i, err)
}
<-started
@@ -357,7 +360,7 @@ func TestServerTrustedPeers(t *testing.T) {
defer conn.Close()
shake := &protoHandshake{Version: baseProtocolVersion, ID: trusted.ID}
- if _, err = setupConn(conn, key, shake, server.Self(), false, server.trustedNodes); err != nil {
+ if _, err = setupConn(conn, key, shake, server.Self(), keepalways); err != nil {
t.Fatalf("trusted node: unexpected error: %v", err)
}
select {
@@ -369,6 +372,136 @@ func TestServerTrustedPeers(t *testing.T) {
}
}
+// Tests that a failed dial will temporarily throttle a peer.
+func TestServerMaxPendingDials(t *testing.T) {
+ defer testlog(t).detach()
+
+ // Start a simple test server
+ server := &Server{
+ ListenAddr: "127.0.0.1:0",
+ PrivateKey: newkey(),
+ MaxPeers: 10,
+ MaxPendingPeers: 1,
+ }
+ if err := server.Start(); err != nil {
+ t.Fatal("failed to start test server: %v", err)
+ }
+ defer server.Stop()
+
+ // Simulate two separate remote peers
+ peers := make(chan *discover.Node, 2)
+ conns := make(chan net.Conn, 2)
+ for i := 0; i < 2; i++ {
+ listener, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ t.Fatalf("listener %d: failed to setup: %v", i, err)
+ }
+ defer listener.Close()
+
+ addr := listener.Addr().(*net.TCPAddr)
+ peers <- &discover.Node{
+ ID: discover.PubkeyID(&newkey().PublicKey),
+ IP: addr.IP,
+ TCP: uint16(addr.Port),
+ }
+ go func() {
+ conn, err := listener.Accept()
+ if err == nil {
+ conns <- conn
+ }
+ }()
+ }
+ // Request a dial for both peers
+ go func() {
+ for i := 0; i < 2; i++ {
+ server.staticDial <- <-peers // hack piggybacking the static implementation
+ }
+ }()
+
+ // Make sure only one outbound connection goes through
+ var conn net.Conn
+
+ select {
+ case conn = <-conns:
+ case <-time.After(100 * time.Millisecond):
+ t.Fatalf("first dial timeout")
+ }
+ select {
+ case conn = <-conns:
+ t.Fatalf("second dial completed prematurely")
+ case <-time.After(100 * time.Millisecond):
+ }
+ // Finish the first dial, check the second
+ conn.Close()
+ select {
+ case conn = <-conns:
+ conn.Close()
+
+ case <-time.After(100 * time.Millisecond):
+ t.Fatalf("second dial timeout")
+ }
+}
+
+func TestServerMaxPendingAccepts(t *testing.T) {
+ defer testlog(t).detach()
+
+ // Start a test server and a peer sink for synchronization
+ started := make(chan *Peer)
+ server := &Server{
+ ListenAddr: "127.0.0.1:0",
+ PrivateKey: newkey(),
+ MaxPeers: 10,
+ MaxPendingPeers: 1,
+ NoDial: true,
+ newPeerHook: func(p *Peer) { started <- p },
+ }
+ if err := server.Start(); err != nil {
+ t.Fatal("failed to start test server: %v", err)
+ }
+ defer server.Stop()
+
+ // Try and connect to the server on multiple threads concurrently
+ conns := make([]net.Conn, 2)
+ for i := 0; i < 2; i++ {
+ dialer := &net.Dialer{Deadline: time.Now().Add(3 * time.Second)}
+
+ conn, err := dialer.Dial("tcp", server.ListenAddr)
+ if err != nil {
+ t.Fatalf("failed to dial server: %v", err)
+ }
+ conns[i] = conn
+ }
+ // Check that a handshake on the second doesn't pass
+ go func() {
+ key := newkey()
+ shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
+ if _, err := setupConn(conns[1], key, shake, server.Self(), keepalways); err != nil {
+ t.Fatalf("failed to run handshake: %v", err)
+ }
+ }()
+ select {
+ case <-started:
+ t.Fatalf("handshake on second connection accepted")
+
+ case <-time.After(time.Second):
+ }
+ // Shake on first, check that both go through
+ go func() {
+ key := newkey()
+ shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
+ if _, err := setupConn(conns[0], key, shake, server.Self(), keepalways); err != nil {
+ t.Fatalf("failed to run handshake: %v", err)
+ }
+ }()
+ for i := 0; i < 2; i++ {
+ select {
+ case <-started:
+ case <-time.After(time.Second):
+ t.Fatalf("peer %d: handshake timeout", i)
+ }
+ }
+}
+
func newkey() *ecdsa.PrivateKey {
key, err := crypto.GenerateKey()
if err != nil {
@@ -383,3 +516,7 @@ func randomID() (id discover.NodeID) {
}
return id
}
+
+func keepalways(id discover.NodeID) bool {
+ return true
+}