github.com/quinndk/ethereum_read@v0.0.0-20181211143958-29c55eec3237/go-ethereum-master_read/p2p/server.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package p2p implements the Ethereum p2p network protocols.
    18  package p2p
    19  
    20  import (
    21  	"crypto/ecdsa"
    22  	"errors"
    23  	"fmt"
    24  	"net"
    25  	"sync"
    26  	"time"
    27  
    28  	"github.com/ethereum/go-ethereum/common"
    29  	"github.com/ethereum/go-ethereum/common/mclock"
    30  	"github.com/ethereum/go-ethereum/event"
    31  	"github.com/ethereum/go-ethereum/log"
    32  	"github.com/ethereum/go-ethereum/p2p/discover"
    33  	"github.com/ethereum/go-ethereum/p2p/discv5"
    34  	"github.com/ethereum/go-ethereum/p2p/nat"
    35  	"github.com/ethereum/go-ethereum/p2p/netutil"
    36  )
    37  
    38  const (
    39  
    40  	// 默认的连接超时时间
    41  	defaultDialTimeout = 15 * time.Second
    42  
    43  	// Connectivity defaults.
    44  	// 默认连接属性
    45  	maxActiveDialTasks     = 16
    46  	defaultMaxPendingPeers = 50
    47  	defaultDialRatio       = 3
    48  
    49  	// Maximum time allowed for reading a complete message.
    50  	// This is effectively the amount of time a connection can be idle.
    51  	// 读取一个完整消息需要的最长时间(连接空闲时间)
    52  	frameReadTimeout = 30 * time.Second
    53  
    54  	// Maximum amount of time allowed for writing a complete message.
    55  	// 构造一个完整消息徐亚的最长时间
    56  	frameWriteTimeout = 20 * time.Second
    57  )
    58  
    59  var errServerStopped = errors.New("server stopped")
    60  
    61  // Config holds Server options.
    62  type Config struct {
    63  	// This field must be set to a valid secp256k1 private key.
    64  	// 节点私钥
    65  	PrivateKey *ecdsa.PrivateKey `toml:"-"`
    66  
    67  	// MaxPeers is the maximum number of peers that can be
    68  	// connected. It must be greater than zero.
    69  	// 允许连接的最大节点数
    70  	MaxPeers int
    71  
    72  	// MaxPendingPeers is the maximum number of peers that can be pending in the
    73  	// handshake phase, counted separately for inbound and outbound connections.
    74  	// Zero defaults to preset values.
    75  	// 握手阶段可以建立的最大连接数
    76  	MaxPendingPeers int `toml:",omitempty"`
    77  
    78  	// DialRatio controls the ratio of inbound to dialed connections.
    79  	// Example: a DialRatio of 2 allows 1/2 of connections to be dialed.
    80  	// Setting DialRatio to zero defaults it to 3.
    81  	// 控制拨号比例 eg:DialRatio = 2 允许拨打1/2连接
    82  	DialRatio int `toml:",omitempty"`
    83  
    84  	// NoDiscovery can be used to disable the peer discovery mechanism.
    85  	// Disabling is useful for protocol debugging (manual topology).
    86  	// 禁用发现
    87  	NoDiscovery bool
    88  
    89  	// DiscoveryV5 specifies whether the the new topic-discovery based V5 discovery
    90  	// protocol should be started or not.
    91  	// 是否启用新的发现协议
    92  	DiscoveryV5 bool `toml:",omitempty"`
    93  
    94  	// Name sets the node name of this server.
    95  	// Use common.MakeName to create a name that follows existing conventions.
    96  	// 节点名称
    97  	Name string `toml:"-"`
    98  
    99  	// BootstrapNodes are used to establish connectivity
   100  	// with the rest of the network.
   101  	// 用于和网络其余部分建立连接
   102  	BootstrapNodes []*discover.Node
   103  
   104  	// BootstrapNodesV5 are used to establish connectivity
   105  	// with the rest of the network using the V5 discovery
   106  	// protocol.
   107  	// 新的发现协议使用
   108  	BootstrapNodesV5 []*discv5.Node `toml:",omitempty"`
   109  
   110  	// Static nodes are used as pre-configured connections which are always
   111  	// maintained and re-connected on disconnects.
   112  	// 静态节点
   113  	StaticNodes []*discover.Node
   114  
   115  	// Trusted nodes are used as pre-configured connections which are always
   116  	// allowed to connect, even above the peer limit.
   117  	// 可信节点,始终允许连接
   118  	TrustedNodes []*discover.Node
   119  
   120  	// Connectivity can be restricted to certain IP networks.
   121  	// If this option is set to a non-nil value, only hosts which match one of the
   122  	// IP networks contained in the list are considered.
   123  	// 限制连接设置
   124  	// NetRestrict != 0 则只考虑与列表中包含的IP进行连接
   125  	NetRestrict *netutil.Netlist `toml:",omitempty"`
   126  
   127  	// NodeDatabase is the path to the database containing the previously seen
   128  	// live nodes in the network.
   129  	// 节点数据库路径
   130  	NodeDatabase string `toml:",omitempty"`
   131  
   132  	// Protocols should contain the protocols supported
   133  	// by the server. Matching protocols are launched for
   134  	// each peer.
   135  	// 协议集
   136  	Protocols []Protocol `toml:"-"`
   137  
   138  	// If ListenAddr is set to a non-nil address, the server
   139  	// will listen for incoming connections.
   140  	//
   141  	// If the port is zero, the operating system will pick a port. The
   142  	// ListenAddr field will be updated with the actual address when
   143  	// the server is started.
   144  	// 监听地址
   145  	// 如果ListenAddr设置为非零地址,则服务器将侦听传入连接
   146  	// ListenAddr = 0 则系统选择一个端口
   147  	ListenAddr string
   148  
   149  	// If set to a non-nil value, the given NAT port mapper
   150  	// is used to make the listening port available to the
   151  	// Internet.
   152  	// NAT端口
   153  	NAT nat.Interface `toml:",omitempty"`
   154  
   155  	// If Dialer is set to a non-nil value, the given Dialer
   156  	// is used to dial outbound peer connections.
   157  	// 拨号方
   158  	Dialer NodeDialer `toml:"-"`
   159  
   160  	// If NoDial is true, the server will not dial any peers.
   161  	// 不与任何节点发起连接的标志
   162  	NoDial bool `toml:",omitempty"`
   163  
   164  	// If EnableMsgEvents is set then the server will emit PeerEvents
   165  	// whenever a message is sent to or received from a peer
   166  	// EnableMsgEvents = yes 则只要发起对等连接或接收消息,服务器就会发出PeerEvent
   167  	EnableMsgEvents bool
   168  
   169  	// Logger is a custom logger to use with the p2p.Server.
   170  	// 客户端日志
   171  	Logger log.Logger `toml:",omitempty"`
   172  }
   173  
   174  // Server manages all peer connections.
   175  // 点对点连接的服务器管理者
   176  type Server struct {
   177  	// Config fields may not be modified while the server is running.
   178  	// 服务器配置
   179  	Config
   180  
   181  	// Hooks for testing. These are useful because we can inhibit
   182  	// the whole protocol stack.
   183  	// 下层传输的实现,定义了握手中的数据加密方式
   184  	newTransport func(net.Conn) transport
   185  	newPeerHook  func(*Peer)
   186  
   187  	// 同步锁
   188  	lock    sync.Mutex // protects running
   189  	running bool
   190  
   191  	// 节点列表
   192  	ntab         discoverTable
   193  	listener     net.Listener
   194  	ourHandshake *protoHandshake
   195  	lastLookup   time.Time
   196  	DiscV5       *discv5.Network
   197  
   198  	// These are for Peers, PeerCount (and nothing else).
   199  	peerOp     chan peerOpFunc
   200  	peerOpDone chan struct{}
   201  
   202  	quit          chan struct{}
   203  	addstatic     chan *discover.Node
   204  	removestatic  chan *discover.Node
   205  	posthandshake chan *conn
   206  	addpeer       chan *conn
   207  	delpeer       chan peerDrop
   208  	loopWG        sync.WaitGroup // loop, listenLoop
   209  	peerFeed      event.Feed
   210  	log           log.Logger
   211  }
   212  
   213  type peerOpFunc func(map[discover.NodeID]*Peer)
   214  
   215  type peerDrop struct {
   216  	*Peer
   217  	err       error
   218  	requested bool // true if signaled by the peer
   219  }
   220  
   221  type connFlag int
   222  
   223  const (
   224  	dynDialedConn connFlag = 1 << iota
   225  	staticDialedConn
   226  	inboundConn
   227  	trustedConn
   228  )
   229  
   230  // conn wraps a network connection with information gathered
   231  // during the two handshakes.
   232  type conn struct {
   233  	fd net.Conn
   234  	transport
   235  	flags connFlag
   236  	cont  chan error      // The run loop uses cont to signal errors to SetupConn.
   237  	id    discover.NodeID // valid after the encryption handshake
   238  	caps  []Cap           // valid after the protocol handshake
   239  	name  string          // valid after the protocol handshake
   240  }
   241  
   242  type transport interface {
   243  	// The two handshakes.
   244  	doEncHandshake(prv *ecdsa.PrivateKey, dialDest *discover.Node) (discover.NodeID, error)
   245  	doProtoHandshake(our *protoHandshake) (*protoHandshake, error)
   246  	// The MsgReadWriter can only be used after the encryption
   247  	// handshake has completed. The code uses conn.id to track this
   248  	// by setting it to a non-nil value after the encryption handshake.
   249  	MsgReadWriter
   250  	// transports must provide Close because we use MsgPipe in some of
   251  	// the tests. Closing the actual network connection doesn't do
   252  	// anything in those tests because NsgPipe doesn't use it.
   253  	close(err error)
   254  }
   255  
   256  func (c *conn) String() string {
   257  	s := c.flags.String()
   258  	if (c.id != discover.NodeID{}) {
   259  		s += " " + c.id.String()
   260  	}
   261  	s += " " + c.fd.RemoteAddr().String()
   262  	return s
   263  }
   264  
   265  func (f connFlag) String() string {
   266  	s := ""
   267  	if f&trustedConn != 0 {
   268  		s += "-trusted"
   269  	}
   270  	if f&dynDialedConn != 0 {
   271  		s += "-dyndial"
   272  	}
   273  	if f&staticDialedConn != 0 {
   274  		s += "-staticdial"
   275  	}
   276  	if f&inboundConn != 0 {
   277  		s += "-inbound"
   278  	}
   279  	if s != "" {
   280  		s = s[1:]
   281  	}
   282  	return s
   283  }
   284  
   285  func (c *conn) is(f connFlag) bool {
   286  	return c.flags&f != 0
   287  }
   288  
   289  // Peers returns all connected peers.
   290  func (srv *Server) Peers() []*Peer {
   291  	var ps []*Peer
   292  	select {
   293  	// Note: We'd love to put this function into a variable but
   294  	// that seems to cause a weird compiler error in some
   295  	// environments.
   296  	case srv.peerOp <- func(peers map[discover.NodeID]*Peer) {
   297  		for _, p := range peers {
   298  			ps = append(ps, p)
   299  		}
   300  	}:
   301  		<-srv.peerOpDone
   302  	case <-srv.quit:
   303  	}
   304  	return ps
   305  }
   306  
   307  // PeerCount returns the number of connected peers.
   308  func (srv *Server) PeerCount() int {
   309  	var count int
   310  	select {
   311  	case srv.peerOp <- func(ps map[discover.NodeID]*Peer) { count = len(ps) }:
   312  		<-srv.peerOpDone
   313  	case <-srv.quit:
   314  	}
   315  	return count
   316  }
   317  
   318  // AddPeer connects to the given node and maintains the connection until the
   319  // server is shut down. If the connection fails for any reason, the server will
   320  // attempt to reconnect the peer.
   321  func (srv *Server) AddPeer(node *discover.Node) {
   322  	select {
   323  	case srv.addstatic <- node:
   324  	case <-srv.quit:
   325  	}
   326  }
   327  
   328  // RemovePeer disconnects from the given node
   329  func (srv *Server) RemovePeer(node *discover.Node) {
   330  	select {
   331  	case srv.removestatic <- node:
   332  	case <-srv.quit:
   333  	}
   334  }
   335  
   336  // SubscribePeers subscribes the given channel to peer events
   337  func (srv *Server) SubscribeEvents(ch chan *PeerEvent) event.Subscription {
   338  	return srv.peerFeed.Subscribe(ch)
   339  }
   340  
   341  // Self returns the local node's endpoint information.
   342  func (srv *Server) Self() *discover.Node {
   343  	srv.lock.Lock()
   344  	defer srv.lock.Unlock()
   345  
   346  	if !srv.running {
   347  		return &discover.Node{IP: net.ParseIP("0.0.0.0")}
   348  	}
   349  	return srv.makeSelf(srv.listener, srv.ntab)
   350  }
   351  
   352  func (srv *Server) makeSelf(listener net.Listener, ntab discoverTable) *discover.Node {
   353  	// If the server's not running, return an empty node.
   354  	// If the node is running but discovery is off, manually assemble the node infos.
   355  	if ntab == nil {
   356  		// Inbound connections disabled, use zero address.
   357  		if listener == nil {
   358  			return &discover.Node{IP: net.ParseIP("0.0.0.0"), ID: discover.PubkeyID(&srv.PrivateKey.PublicKey)}
   359  		}
   360  		// Otherwise inject the listener address too
   361  		addr := listener.Addr().(*net.TCPAddr)
   362  		return &discover.Node{
   363  			ID:  discover.PubkeyID(&srv.PrivateKey.PublicKey),
   364  			IP:  addr.IP,
   365  			TCP: uint16(addr.Port),
   366  		}
   367  	}
   368  	// Otherwise return the discovery node.
   369  	return ntab.Self()
   370  }
   371  
   372  // Stop terminates the server and all active peer connections.
   373  // It blocks until all active connections have been closed.
   374  func (srv *Server) Stop() {
   375  	srv.lock.Lock()
   376  	defer srv.lock.Unlock()
   377  	if !srv.running {
   378  		return
   379  	}
   380  	srv.running = false
   381  	if srv.listener != nil {
   382  		// this unblocks listener Accept
   383  		srv.listener.Close()
   384  	}
   385  	close(srv.quit)
   386  	srv.loopWG.Wait()
   387  }
   388  
   389  // sharedUDPConn implements a shared connection. Write sends messages to the underlying connection while read returns
   390  // messages that were found unprocessable and sent to the unhandled channel by the primary listener.
   391  type sharedUDPConn struct {
   392  	*net.UDPConn
   393  	unhandled chan discover.ReadPacket
   394  }
   395  
   396  // ReadFromUDP implements discv5.conn
   397  func (s *sharedUDPConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) {
   398  	packet, ok := <-s.unhandled
   399  	if !ok {
   400  		return 0, nil, fmt.Errorf("Connection was closed")
   401  	}
   402  	l := len(packet.Data)
   403  	if l > len(b) {
   404  		l = len(b)
   405  	}
   406  	copy(b[:l], packet.Data[:l])
   407  	return l, packet.Addr, nil
   408  }
   409  
   410  // Close implements discv5.conn
   411  func (s *sharedUDPConn) Close() error {
   412  	return nil
   413  }
   414  
   415  // Start starts running the server.
   416  // Servers can not be re-used after stopping.
   417  // 启动p2p
   418  func (srv *Server) Start() (err error) {
   419  
   420  	// 避免重复多次启动
   421  	srv.lock.Lock()
   422  	defer srv.lock.Unlock()
   423  	if srv.running {
   424  		return errors.New("server already running")
   425  	}
   426  	srv.running = true
   427  	srv.log = srv.Config.Logger
   428  	if srv.log == nil {
   429  		srv.log = log.New()
   430  	}
   431  	srv.log.Info("Starting P2P networking")
   432  
   433  	// static fields
   434  	if srv.PrivateKey == nil {
   435  		return fmt.Errorf("Server.PrivateKey must be set to a non-nil key")
   436  	}
   437  	if srv.newTransport == nil {
   438  
   439  		// 加密链路使用RLPX协议
   440  		srv.newTransport = newRLPX
   441  	}
   442  	if srv.Dialer == nil {
   443  		// 使用TCPDialer
   444  		srv.Dialer = TCPDialer{&net.Dialer{Timeout: defaultDialTimeout}}
   445  	}
   446  
   447  	// 各种channel通道
   448  	srv.quit = make(chan struct{})
   449  	srv.addpeer = make(chan *conn)
   450  	srv.delpeer = make(chan peerDrop)
   451  	srv.posthandshake = make(chan *conn)
   452  	srv.addstatic = make(chan *discover.Node)
   453  	srv.removestatic = make(chan *discover.Node)
   454  	srv.peerOp = make(chan peerOpFunc)
   455  	srv.peerOpDone = make(chan struct{})
   456  
   457  	var (
   458  		conn      *net.UDPConn
   459  		sconn     *sharedUDPConn
   460  		realaddr  *net.UDPAddr
   461  		unhandled chan discover.ReadPacket
   462  	)
   463  
   464  	if !srv.NoDiscovery || srv.DiscoveryV5 {
   465  		// 启动discover网络
   466  		addr, err := net.ResolveUDPAddr("udp", srv.ListenAddr)
   467  		if err != nil {
   468  			return err
   469  		}
   470  		// 开启UDP监听
   471  		conn, err = net.ListenUDP("udp", addr)
   472  		if err != nil {
   473  			return err
   474  		}
   475  		realaddr = conn.LocalAddr().(*net.UDPAddr)
   476  		if srv.NAT != nil {
   477  			// 通过nat暴露udp端口 以太坊主要有upnp和pmp两种nat协议
   478  			if !realaddr.IP.IsLoopback() {
   479  				go nat.Map(srv.NAT, srv.quit, "udp", realaddr.Port, realaddr.Port, "ethereum discovery")
   480  			}
   481  			// TODO: react to external IP changes over time.
   482  			if ext, err := srv.NAT.ExternalIP(); err == nil {
   483  				realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port}
   484  			}
   485  		}
   486  	}
   487  
   488  	if !srv.NoDiscovery && srv.DiscoveryV5 {
   489  		// 新的节点发现协议
   490  		unhandled = make(chan discover.ReadPacket, 100)
   491  		sconn = &sharedUDPConn{conn, unhandled}
   492  	}
   493  
   494  	// node table
   495  	if !srv.NoDiscovery {
   496  		cfg := discover.Config{
   497  			PrivateKey:   srv.PrivateKey,
   498  			AnnounceAddr: realaddr,
   499  			NodeDBPath:   srv.NodeDatabase,
   500  			NetRestrict:  srv.NetRestrict,
   501  			Bootnodes:    srv.BootstrapNodes,
   502  			Unhandled:    unhandled,
   503  		}
   504  		ntab, err := discover.ListenUDP(conn, cfg)
   505  		if err != nil {
   506  			return err
   507  		}
   508  		srv.ntab = ntab
   509  	}
   510  
   511  	if srv.DiscoveryV5 {
   512  		var (
   513  			ntab *discv5.Network
   514  			err  error
   515  		)
   516  		if sconn != nil {
   517  			ntab, err = discv5.ListenUDP(srv.PrivateKey, sconn, realaddr, "", srv.NetRestrict) //srv.NodeDatabase)
   518  		} else {
   519  			ntab, err = discv5.ListenUDP(srv.PrivateKey, conn, realaddr, "", srv.NetRestrict) //srv.NodeDatabase)
   520  		}
   521  		if err != nil {
   522  			return err
   523  		}
   524  		if err := ntab.SetFallbackNodes(srv.BootstrapNodesV5); err != nil {
   525  			return err
   526  		}
   527  		srv.DiscV5 = ntab
   528  	}
   529  
   530  	dynPeers := srv.maxDialedConns()
   531  	// 新建dialerstate用来处理与节点的连接
   532  	dialer := newDialState(srv.StaticNodes, srv.BootstrapNodes, srv.ntab, dynPeers, srv.NetRestrict)
   533  
   534  	// handshake
   535  	// 握手协议
   536  	srv.ourHandshake = &protoHandshake{Version: baseProtocolVersion, Name: srv.Name, ID: discover.PubkeyID(&srv.PrivateKey.PublicKey)}
   537  	for _, p := range srv.Protocols {
   538  		srv.ourHandshake.Caps = append(srv.ourHandshake.Caps, p.cap())
   539  	}
   540  	// listen/dial
   541  	if srv.ListenAddr != "" {
   542  		// 开始监听TCP端口
   543  		if err := srv.startListening(); err != nil {
   544  			return err
   545  		}
   546  	}
   547  	if srv.NoDial && srv.ListenAddr == "" {
   548  		srv.log.Warn("P2P server will be useless, neither dialing nor listening")
   549  	}
   550  
   551  	srv.loopWG.Add(1)
   552  	// 开启协程来处理主动连接外部节点的流程和已经连接的checkpoint逻辑
   553  	go srv.run(dialer)
   554  	srv.running = true
   555  	return nil
   556  }
   557  
   558  func (srv *Server) startListening() error {
   559  	// Launch the TCP listener.
   560  	listener, err := net.Listen("tcp", srv.ListenAddr)
   561  	if err != nil {
   562  		return err
   563  	}
   564  	laddr := listener.Addr().(*net.TCPAddr)
   565  	srv.ListenAddr = laddr.String()
   566  	srv.listener = listener
   567  	srv.loopWG.Add(1)
   568  	go srv.listenLoop()
   569  	// Map the TCP listening port if NAT is configured.
   570  	// 匹配TCP端口
   571  	if !laddr.IP.IsLoopback() && srv.NAT != nil {
   572  		srv.loopWG.Add(1)
   573  		go func() {
   574  			nat.Map(srv.NAT, srv.quit, "tcp", laddr.Port, laddr.Port, "ethereum p2p")
   575  			srv.loopWG.Done()
   576  		}()
   577  	}
   578  	return nil
   579  }
   580  
   581  type dialer interface {
   582  	newTasks(running int, peers map[discover.NodeID]*Peer, now time.Time) []task
   583  	taskDone(task, time.Time)
   584  	addStatic(*discover.Node)
   585  	removeStatic(*discover.Node)
   586  }
   587  
   588  func (srv *Server) run(dialstate dialer) {
   589  	defer srv.loopWG.Done()
   590  	var (
   591  		peers        = make(map[discover.NodeID]*Peer)
   592  		inboundCount = 0
   593  		trusted      = make(map[discover.NodeID]bool, len(srv.TrustedNodes))
   594  		taskdone     = make(chan task, maxActiveDialTasks)
   595  		runningTasks []task
   596  		queuedTasks  []task // tasks that can't run yet
   597  	)
   598  	// Put trusted nodes into a map to speed up checks.
   599  	// Trusted peers are loaded on startup and cannot be
   600  	// modified while the server is running.
   601  	// 将信任节点加入map以快速检错
   602  	for _, n := range srv.TrustedNodes {
   603  		trusted[n.ID] = true
   604  	}
   605  
   606  	// removes t from runningTasks
   607  	// delTask函数用来从runningTasks删除一个task
   608  	delTask := func(t task) {
   609  		for i := range runningTasks {
   610  			if runningTasks[i] == t {
   611  				runningTasks = append(runningTasks[:i], runningTasks[i+1:]...)
   612  				break
   613  			}
   614  		}
   615  	}
   616  	// starts until max number of active tasks is satisfied
   617  	// 启动任务,直到任务数达到最大
   618  	startTasks := func(ts []task) (rest []task) {
   619  		i := 0
   620  		for ; len(runningTasks) < maxActiveDialTasks && i < len(ts); i++ {
   621  			t := ts[i]
   622  			srv.log.Trace("New dial task", "task", t)
   623  			go func() { t.Do(srv); taskdone <- t }()
   624  			runningTasks = append(runningTasks, t)
   625  		}
   626  		return ts[i:]
   627  	}
   628  	scheduleTasks := func() {
   629  		// Start from queue first.
   630  		// 启动queuedTasks中的第一个
   631  		queuedTasks = append(queuedTasks[:0], startTasks(queuedTasks)...)
   632  		// Query dialer for new tasks and start as many as possible now.
   633  		// 调用newTasks来生成任务,并尝试用startTasks启动
   634  		// 把暂时无法启动的放入queuedTasks队列
   635  		if len(runningTasks) < maxActiveDialTasks {
   636  			nt := dialstate.newTasks(len(runningTasks)+len(queuedTasks), peers, time.Now())
   637  			queuedTasks = append(queuedTasks, startTasks(nt)...)
   638  		}
   639  	}
   640  
   641  running:
   642  	for {
   643  		scheduleTasks()
   644  
   645  		select {
   646  		case <-srv.quit:
   647  			// The server was stopped. Run the cleanup logic.
   648  			// 服务器停止
   649  			break running
   650  		case n := <-srv.addstatic:
   651  			// This channel is used by AddPeer to add to the
   652  			// ephemeral static peer list. Add it to the dialer,
   653  			// it will keep the node connected.
   654  			// 添加静态节点
   655  			srv.log.Trace("Adding static node", "node", n)
   656  			dialstate.addStatic(n)
   657  		case n := <-srv.removestatic:
   658  			// This channel is used by RemovePeer to send a
   659  			// disconnect request to a peer and begin the
   660  			// stop keeping the node connected
   661  			// 移除静态节点
   662  			srv.log.Trace("Removing static node", "node", n)
   663  			dialstate.removeStatic(n)
   664  			if p, ok := peers[n.ID]; ok {
   665  				p.Disconnect(DiscRequested)
   666  			}
   667  		case op := <-srv.peerOp:
   668  			// This channel is used by Peers and PeerCount.
   669  			op(peers)
   670  			srv.peerOpDone <- struct{}{}
   671  		case t := <-taskdone:
   672  			// A task got done. Tell dialstate about it so it
   673  			// can update its state and remove it from the active
   674  			// tasks list.
   675  			srv.log.Trace("Dial task done", "task", t)
   676  			dialstate.taskDone(t, time.Now())
   677  			delTask(t)
   678  		case c := <-srv.posthandshake:
   679  			// A connection has passed the encryption handshake so
   680  			// the remote identity is known (but hasn't been verified yet).
   681  			// 之前调用checkpoint()会把连接发送到这
   682  			if trusted[c.id] {
   683  				// Ensure that the trusted flag is set before checking against MaxPeers.
   684  				// 确保在检查MaxPeers之前设置了可信标志。
   685  				c.flags |= trustedConn
   686  			}
   687  			// TODO: track in-progress inbound node IDs (pre-Peer) to avoid dialing them.
   688  			select {
   689  			case c.cont <- srv.encHandshakeChecks(peers, inboundCount, c):
   690  			case <-srv.quit:
   691  				break running
   692  			}
   693  		case c := <-srv.addpeer:
   694  			// At this point the connection is past the protocol handshake.
   695  			// Its capabilities are known and the remote identity is verified.
   696  			// 已经通过握手协议
   697  			err := srv.protoHandshakeChecks(peers, inboundCount, c)
   698  			if err == nil {
   699  				// The handshakes are done and it passed all checks.
   700  				// 节点通过验证
   701  				p := newPeer(c, srv.Protocols)
   702  				// If message events are enabled, pass the peerFeed
   703  				// to the peer
   704  				// 如果启用了消息事件,请将peerFeed传递给peer
   705  				if srv.EnableMsgEvents {
   706  					p.events = &srv.peerFeed
   707  				}
   708  				name := truncateName(c.name)
   709  				srv.log.Debug("Adding p2p peer", "name", name, "addr", c.fd.RemoteAddr(), "peers", len(peers)+1)
   710  				// 添加p2p节点
   711  				go srv.runPeer(p)
   712  				peers[c.id] = p
   713  				if p.Inbound() {
   714  					inboundCount++
   715  				}
   716  			}
   717  			// The dialer logic relies on the assumption that
   718  			// dial tasks complete after the peer has been added or
   719  			// discarded. Unblock the task last.
   720  			select {
   721  			case c.cont <- err:
   722  			case <-srv.quit:
   723  				break running
   724  			}
   725  		case pd := <-srv.delpeer:
   726  			// A peer disconnected.
   727  			// 删除peer
   728  			d := common.PrettyDuration(mclock.Now() - pd.created)
   729  			pd.log.Debug("Removing p2p peer", "duration", d, "peers", len(peers)-1, "req", pd.requested, "err", pd.err)
   730  			delete(peers, pd.ID())
   731  			if pd.Inbound() {
   732  				inboundCount--
   733  			}
   734  		}
   735  	}
   736  
   737  	srv.log.Trace("P2P networking is spinning down")
   738  
   739  	// Terminate discovery. If there is a running lookup it will terminate soon.
   740  	// 停止发现节点
   741  	if srv.ntab != nil {
   742  		srv.ntab.Close()
   743  	}
   744  	if srv.DiscV5 != nil {
   745  		srv.DiscV5.Close()
   746  	}
   747  	// Disconnect all peers.
   748  	// 断开与所有peer的连接
   749  	for _, p := range peers {
   750  		p.Disconnect(DiscQuitting)
   751  	}
   752  	// Wait for peers to shut down. Pending connections and tasks are
   753  	// not handled here and will terminate soon-ish because srv.quit
   754  	// is closed.
   755  	// 等待peer关闭
   756  	for len(peers) > 0 {
   757  		p := <-srv.delpeer
   758  		p.log.Trace("<-delpeer (spindown)", "remainingTasks", len(runningTasks))
   759  		delete(peers, p.ID())
   760  	}
   761  }
   762  
   763  func (srv *Server) protoHandshakeChecks(peers map[discover.NodeID]*Peer, inboundCount int, c *conn) error {
   764  	// Drop connections with no matching protocols.
   765  	if len(srv.Protocols) > 0 && countMatchingProtocols(srv.Protocols, c.caps) == 0 {
   766  		return DiscUselessPeer
   767  	}
   768  	// Repeat the encryption handshake checks because the
   769  	// peer set might have changed between the handshakes.
   770  	return srv.encHandshakeChecks(peers, inboundCount, c)
   771  }
   772  
   773  func (srv *Server) encHandshakeChecks(peers map[discover.NodeID]*Peer, inboundCount int, c *conn) error {
   774  	switch {
   775  	case !c.is(trustedConn|staticDialedConn) && len(peers) >= srv.MaxPeers:
   776  		return DiscTooManyPeers
   777  	case !c.is(trustedConn) && c.is(inboundConn) && inboundCount >= srv.maxInboundConns():
   778  		return DiscTooManyPeers
   779  	case peers[c.id] != nil:
   780  		return DiscAlreadyConnected
   781  	case c.id == srv.Self().ID:
   782  		return DiscSelf
   783  	default:
   784  		return nil
   785  	}
   786  }
   787  
   788  func (srv *Server) maxInboundConns() int {
   789  	return srv.MaxPeers - srv.maxDialedConns()
   790  }
   791  
   792  func (srv *Server) maxDialedConns() int {
   793  	if srv.NoDiscovery || srv.NoDial {
   794  		return 0
   795  	}
   796  	r := srv.DialRatio
   797  	if r == 0 {
   798  		r = defaultDialRatio
   799  	}
   800  	return srv.MaxPeers / r
   801  }
   802  
   803  type tempError interface {
   804  	Temporary() bool
   805  }
   806  
   807  // listenLoop runs in its own goroutine and accepts
   808  // inbound connections.
   809  // 循环监听的一个协程
   810  func (srv *Server) listenLoop() {
   811  	defer srv.loopWG.Done()
   812  	srv.log.Info("RLPx listener up", "self", srv.makeSelf(srv.listener, srv.ntab))
   813  
   814  	// 连接处理数
   815  	tokens := defaultMaxPendingPeers
   816  	if srv.MaxPendingPeers > 0 {
   817  		tokens = srv.MaxPendingPeers
   818  	}
   819  	slots := make(chan struct{}, tokens)
   820  	for i := 0; i < tokens; i++ {
   821  		slots <- struct{}{}
   822  	}
   823  
   824  	for {
   825  		// Wait for a handshake slot before accepting.
   826  		// 等待握手信号槽
   827  		<-slots
   828  
   829  		var (
   830  			fd  net.Conn
   831  			err error
   832  		)
   833  		for {
   834  			fd, err = srv.listener.Accept()
   835  			if tempErr, ok := err.(tempError); ok && tempErr.Temporary() {
   836  				srv.log.Debug("Temporary read error", "err", err)
   837  				continue
   838  			} else if err != nil {
   839  				srv.log.Debug("Read error", "err", err)
   840  				return
   841  			}
   842  			break
   843  		}
   844  
   845  		// Reject connections that do not match NetRestrict.
   846  		// 拒绝不在白名单NetRestrict里的连接
   847  		if srv.NetRestrict != nil {
   848  			if tcp, ok := fd.RemoteAddr().(*net.TCPAddr); ok && !srv.NetRestrict.Contains(tcp.IP) {
   849  				srv.log.Debug("Rejected conn (not whitelisted in NetRestrict)", "addr", fd.RemoteAddr())
   850  				fd.Close()
   851  				slots <- struct{}{}
   852  				continue
   853  			}
   854  		}
   855  
   856  		fd = newMeteredConn(fd, true)
   857  		srv.log.Trace("Accepted connection", "addr", fd.RemoteAddr())
   858  		go func() {
   859  			// 执行连接
   860  			srv.SetupConn(fd, inboundConn, nil)
   861  			slots <- struct{}{}
   862  		}()
   863  	}
   864  }
   865  
   866  // SetupConn runs the handshakes and attempts to add the connection
   867  // as a peer. It returns when the connection has been added as a peer
   868  // or the handshakes have failed.
   869  // 执行握手并尝试将连接方作为一个peer
   870  func (srv *Server) SetupConn(fd net.Conn, flags connFlag, dialDest *discover.Node) error {
   871  	self := srv.Self()
   872  	if self == nil {
   873  		return errors.New("shutdown")
   874  	}
   875  	// 创建conn对象
   876  	c := &conn{fd: fd, transport: srv.newTransport(fd), flags: flags, cont: make(chan error)}
   877  	err := srv.setupConn(c, flags, dialDest)
   878  	if err != nil {
   879  		c.close(err)
   880  		srv.log.Trace("Setting up connection failed", "id", c.id, "err", err)
   881  	}
   882  	return err
   883  }
   884  
   885  func (srv *Server) setupConn(c *conn, flags connFlag, dialDest *discover.Node) error {
   886  	// Prevent leftover pending conns from entering the handshake.
   887  	srv.lock.Lock()
   888  	running := srv.running
   889  	srv.lock.Unlock()
   890  	if !running {
   891  		return errServerStopped
   892  	}
   893  	// Run the encryption handshake.
   894  	var err error
   895  	// 第一次握手:加密握手
   896  	if c.id, err = c.doEncHandshake(srv.PrivateKey, dialDest); err != nil {
   897  		srv.log.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err)
   898  		return err
   899  	}
   900  	clog := srv.log.New("id", c.id, "addr", c.fd.RemoteAddr(), "conn", c.flags)
   901  	// For dialed connections, check that the remote public key matches.
   902  	// 连接握手的ID和对应的ID不匹配
   903  	if dialDest != nil && c.id != dialDest.ID {
   904  		clog.Trace("Dialed identity mismatch", "want", c, dialDest.ID)
   905  		return DiscUnexpectedIdentity
   906  	}
   907  	// 将c发送给srv.posthandshake
   908  	err = srv.checkpoint(c, srv.posthandshake)
   909  	if err != nil {
   910  		clog.Trace("Rejected peer before protocol handshake", "err", err)
   911  		return err
   912  	}
   913  	// Run the protocol handshake
   914  	// 第二次握手:协议握手
   915  	phs, err := c.doProtoHandshake(srv.ourHandshake)
   916  	if err != nil {
   917  		clog.Trace("Failed proto handshake", "err", err)
   918  		return err
   919  	}
   920  	if phs.ID != c.id {
   921  		clog.Trace("Wrong devp2p handshake identity", "err", phs.ID)
   922  		return DiscUnexpectedIdentity
   923  	}
   924  	c.caps, c.name = phs.Caps, phs.Name
   925  	// 将c发送给addpeer队列
   926  	err = srv.checkpoint(c, srv.addpeer)
   927  	if err != nil {
   928  		clog.Trace("Rejected peer", "err", err)
   929  		return err
   930  	}
   931  	// If the checks completed successfully, runPeer has now been
   932  	// launched by run.
   933  	clog.Trace("connection set up", "inbound", dialDest == nil)
   934  	return nil
   935  }
   936  
   937  func truncateName(s string) string {
   938  	if len(s) > 20 {
   939  		return s[:20] + "..."
   940  	}
   941  	return s
   942  }
   943  
   944  // checkpoint sends the conn to run, which performs the
   945  // post-handshake checks for the stage (posthandshake, addpeer).
   946  func (srv *Server) checkpoint(c *conn, stage chan<- *conn) error {
   947  	select {
   948  	case stage <- c:
   949  	case <-srv.quit:
   950  		return errServerStopped
   951  	}
   952  	select {
   953  	case err := <-c.cont:
   954  		return err
   955  	case <-srv.quit:
   956  		return errServerStopped
   957  	}
   958  }
   959  
   960  // runPeer runs in its own goroutine for each peer.
   961  // it waits until the Peer logic returns and removes
   962  // the peer.
   963  func (srv *Server) runPeer(p *Peer) {
   964  	if srv.newPeerHook != nil {
   965  		srv.newPeerHook(p)
   966  	}
   967  
   968  	// broadcast peer add
   969  	// 广播节点添加
   970  	srv.peerFeed.Send(&PeerEvent{
   971  		Type: PeerEventTypeAdd,
   972  		Peer: p.ID(),
   973  	})
   974  
   975  	// run the protocol
   976  	// 运行协议
   977  	remoteRequested, err := p.run()
   978  
   979  	// broadcast peer drop
   980  	srv.peerFeed.Send(&PeerEvent{
   981  		Type:  PeerEventTypeDrop,
   982  		Peer:  p.ID(),
   983  		Error: err.Error(),
   984  	})
   985  
   986  	// Note: run waits for existing peers to be sent on srv.delpeer
   987  	// before returning, so this send should not select on srv.quit.
   988  	srv.delpeer <- peerDrop{p, err, remoteRequested}
   989  }
   990  
   991  // NodeInfo represents a short summary of the information known about the host.
   992  type NodeInfo struct {
   993  	ID    string `json:"id"`    // Unique node identifier (also the encryption key)
   994  	Name  string `json:"name"`  // Name of the node, including client type, version, OS, custom data
   995  	Enode string `json:"enode"` // Enode URL for adding this peer from remote peers
   996  	IP    string `json:"ip"`    // IP address of the node
   997  	Ports struct {
   998  		Discovery int `json:"discovery"` // UDP listening port for discovery protocol
   999  		Listener  int `json:"listener"`  // TCP listening port for RLPx
  1000  	} `json:"ports"`
  1001  	ListenAddr string                 `json:"listenAddr"`
  1002  	Protocols  map[string]interface{} `json:"protocols"`
  1003  }
  1004  
  1005  // NodeInfo gathers and returns a collection of metadata known about the host.
  1006  func (srv *Server) NodeInfo() *NodeInfo {
  1007  	node := srv.Self()
  1008  
  1009  	// Gather and assemble the generic node infos
  1010  	info := &NodeInfo{
  1011  		Name:       srv.Name,
  1012  		Enode:      node.String(),
  1013  		ID:         node.ID.String(),
  1014  		IP:         node.IP.String(),
  1015  		ListenAddr: srv.ListenAddr,
  1016  		Protocols:  make(map[string]interface{}),
  1017  	}
  1018  	info.Ports.Discovery = int(node.UDP)
  1019  	info.Ports.Listener = int(node.TCP)
  1020  
  1021  	// Gather all the running protocol infos (only once per protocol type)
  1022  	for _, proto := range srv.Protocols {
  1023  		if _, ok := info.Protocols[proto.Name]; !ok {
  1024  			nodeInfo := interface{}("unknown")
  1025  			if query := proto.NodeInfo; query != nil {
  1026  				nodeInfo = proto.NodeInfo()
  1027  			}
  1028  			info.Protocols[proto.Name] = nodeInfo
  1029  		}
  1030  	}
  1031  	return info
  1032  }
  1033  
  1034  // PeersInfo returns an array of metadata objects describing connected peers.
  1035  func (srv *Server) PeersInfo() []*PeerInfo {
  1036  	// Gather all the generic and sub-protocol specific infos
  1037  	infos := make([]*PeerInfo, 0, srv.PeerCount())
  1038  	for _, peer := range srv.Peers() {
  1039  		if peer != nil {
  1040  			infos = append(infos, peer.Info())
  1041  		}
  1042  	}
  1043  	// Sort the result array alphabetically by node identifier
  1044  	for i := 0; i < len(infos); i++ {
  1045  		for j := i + 1; j < len(infos); j++ {
  1046  			if infos[i].ID > infos[j].ID {
  1047  				infos[i], infos[j] = infos[j], infos[i]
  1048  			}
  1049  		}
  1050  	}
  1051  	return infos
  1052  }