以太坊原始碼分析(48)p2p-server.go原始碼分析

尹成發表於2018-05-14
server是p2p的最主要的部分。集合了所有之前的元件。

首先看看Server的結構

    
    // Server manages all peer connections.
    type Server struct {
        // Config fields may not be modified while the server is running.
        Config
    
        // Hooks for testing. These are useful because we can inhibit
        // the whole protocol stack.
        newTransport func(net.Conn) transport
        newPeerHook func(*Peer)
    
        lock sync.Mutex // protects running
        running bool
    
        ntab discoverTable
        listener net.Listener
        ourHandshake *protoHandshake
        lastLookup time.Time
        DiscV5 *discv5.Network
    
        // These are for Peers, PeerCount (and nothing else).
        peerOp chan peerOpFunc
        peerOpDone chan struct{}
    
        quit chan struct{}
        addstatic chan *discover.Node
        removestatic chan *discover.Node
        posthandshake chan *conn
        addpeer chan *conn
        delpeer chan peerDrop
        loopWG sync.WaitGroup // loop, listenLoop
        peerFeed event.Feed
    }

    // conn wraps a network connection with information gathered
    // during the two handshakes.
    type conn struct {
        fd net.Conn
        transport
        flags connFlag
        cont chan error // The run loop uses cont to signal errors to SetupConn.
        id discover.NodeID // valid after the encryption handshake
        caps []Cap // valid after the protocol handshake
        name string // valid after the protocol handshake
    }

    type transport interface {
        // The two handshakes.
        doEncHandshake(prv *ecdsa.PrivateKey, dialDest *discover.Node) (discover.NodeID, error)
        doProtoHandshake(our *protoHandshake) (*protoHandshake, error)
        // The MsgReadWriter can only be used after the encryption
        // handshake has completed. The code uses conn.id to track this
        // by setting it to a non-nil value after the encryption handshake.
        MsgReadWriter
        // transports must provide Close because we use MsgPipe in some of
        // the tests. Closing the actual network connection doesn't do
        // anything in those tests because NsgPipe doesn't use it.
        close(err error)
    }

並不存在一個newServer的方法。 初始化的工作放在Start()方法中。


    // Start starts running the server.
    // Servers can not be re-used after stopping.
    func (srv *Server) Start() (err error) {
        srv.lock.Lock()
        defer srv.lock.Unlock()
        if srv.running { //避免多次啟動。 srv.lock為了避免多執行緒重複啟動
            return errors.New("server already running")
        }
        srv.running = true
        log.Info("Starting P2P networking")
    
        // static fields
        if srv.PrivateKey == nil {
            return fmt.Errorf("Server.PrivateKey must be set to a non-nil key")
        }
        if srv.newTransport == nil {        //這裡注意的是Transport使用了newRLPX 使用了rlpx.go中的網路協議。
            srv.newTransport = newRLPX
        }
        if srv.Dialer == nil { //使用了TCLPDialer
            srv.Dialer = TCPDialer{&net.Dialer{Timeout: defaultDialTimeout}}
        }
        srv.quit = make(chan struct{})
        srv.addpeer = make(chan *conn)
        srv.delpeer = make(chan peerDrop)
        srv.posthandshake = make(chan *conn)
        srv.addstatic = make(chan *discover.Node)
        srv.removestatic = make(chan *discover.Node)
        srv.peerOp = make(chan peerOpFunc)
        srv.peerOpDone = make(chan struct{})
    
        // node table
        if !srv.NoDiscovery { //啟動discover網路。 開啟UDP的監聽。
            ntab, err := discover.ListenUDP(srv.PrivateKey, srv.ListenAddr, srv.NAT, srv.NodeDatabase, srv.NetRestrict)
            if err != nil {
                return err
            }
            //設定最開始的啟動節點。當找不到其他的節點的時候。 那麼就連線這些啟動節點。這些節點的資訊是寫死在配置檔案裡面的。
            if err := ntab.SetFallbackNodes(srv.BootstrapNodes); err != nil {
                return err
            }
            srv.ntab = ntab
        }
    
        if srv.DiscoveryV5 {//這是新的節點發現協議。 暫時還沒有使用。 這裡暫時沒有分析。
            ntab, err := discv5.ListenUDP(srv.PrivateKey, srv.DiscoveryV5Addr, srv.NAT, "", srv.NetRestrict) //srv.NodeDatabase)
            if err != nil {
                return err
            }
            if err := ntab.SetFallbackNodes(srv.BootstrapNodesV5); err != nil {
                return err
            }
            srv.DiscV5 = ntab
        }
    
        dynPeers := (srv.MaxPeers + 1) / 2
        if srv.NoDiscovery {
            dynPeers = 0
        }   
        //建立dialerstate。
        dialer := newDialState(srv.StaticNodes, srv.BootstrapNodes, srv.ntab, dynPeers, srv.NetRestrict)
    
        // handshake
        //我們自己的協議的handShake
        srv.ourHandshake = &protoHandshake{Version: baseProtocolVersion, Name: srv.Name, ID: discover.PubkeyID(&srv.PrivateKey.PublicKey)}
        for _, p := range srv.Protocols {//增加所有的協議的Caps
            srv.ourHandshake.Caps = append(srv.ourHandshake.Caps, p.cap())
        }
        // listen/dial
        if srv.ListenAddr != "" {
            //開始監聽TCP埠
            if err := srv.startListening(); err != nil {
                return err
            }
        }
        if srv.NoDial && srv.ListenAddr == "" {
            log.Warn("P2P server will be useless, neither dialing nor listening")
        }
    
        srv.loopWG.Add(1)
        //啟動goroutine 來處理程式。
        go srv.run(dialer)
        srv.running = true
        return nil
    }


啟動監聽。 可以看到是TCP協議。 這裡的監聽埠和UDP的埠是一樣的。 預設都是30303
    
    func (srv *Server) startListening() error {
        // Launch the TCP listener.
        listener, err := net.Listen("tcp", srv.ListenAddr)
        if err != nil {
            return err
        }
        laddr := listener.Addr().(*net.TCPAddr)
        srv.ListenAddr = laddr.String()
        srv.listener = listener
        srv.loopWG.Add(1)
        go srv.listenLoop()
        // Map the TCP listening port if NAT is configured.
        if !laddr.IP.IsLoopback() && srv.NAT != nil {
            srv.loopWG.Add(1)
            go func() {
                nat.Map(srv.NAT, srv.quit, "tcp", laddr.Port, laddr.Port, "ethereum p2p")
                srv.loopWG.Done()
            }()
        }
        return nil
    }

listenLoop()。 這是一個死迴圈的goroutine。 會監聽埠並接收外部的請求。
    
    // listenLoop runs in its own goroutine and accepts
    // inbound connections.
    func (srv *Server) listenLoop() {
        defer srv.loopWG.Done()
        log.Info("RLPx listener up", "self", srv.makeSelf(srv.listener, srv.ntab))
    
        // 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.
        tokens := maxAcceptConns
        if srv.MaxPendingPeers > 0 {
            tokens = srv.MaxPendingPeers
        }
        //建立maxAcceptConns個槽位。 我們只同時處理這麼多連線。 多了也不要。
        slots := make(chan struct{}, tokens)
        //把槽位填滿。
        for i := 0; i < tokens; i++ {
            slots <- struct{}{}
        }
    
        for {
            // Wait for a handshake slot before accepting.
            <-slots
    
            var (
                fd net.Conn
                err error
            )
            for {
                fd, err = srv.listener.Accept()
                if tempErr, ok := err.(tempError); ok && tempErr.Temporary() {
                    log.Debug("Temporary read error", "err", err)
                    continue
                } else if err != nil {
                    log.Debug("Read error", "err", err)
                    return
                }
                break
            }
    
            // Reject connections that do not match NetRestrict.
            // 白名單。 如果不在白名單裡面。那麼關閉連線。
            if srv.NetRestrict != nil {
                if tcp, ok := fd.RemoteAddr().(*net.TCPAddr); ok && !srv.NetRestrict.Contains(tcp.IP) {
                    log.Debug("Rejected conn (not whitelisted in NetRestrict)", "addr", fd.RemoteAddr())
                    fd.Close()
                    slots <- struct{}{}
                    continue
                }
            }
    
            fd = newMeteredConn(fd, true)
            log.Trace("Accepted connection", "addr", fd.RemoteAddr())
    
            // Spawn the handler. It will give the slot back when the connection
            // has been established.
            go func() {
                //看來只要連線建立完成之後。 槽位就會歸還。 SetupConn這個函式我們記得再dialTask.Do裡面也有呼叫, 這個函式主要是執行連線的幾次握手。
                srv.SetupConn(fd, inboundConn, nil)
                slots <- struct{}{}
            }()
        }
    }

SetupConn,這個函式執行握手協議,並嘗試把連線建立位一個peer物件。


    // SetupConn runs the handshakes and attempts to add the connection
    // as a peer. It returns when the connection has been added as a peer
    // or the handshakes have failed.
    func (srv *Server) SetupConn(fd net.Conn, flags connFlag, dialDest *discover.Node) {
        // Prevent leftover pending conns from entering the handshake.
        srv.lock.Lock()
        running := srv.running
        srv.lock.Unlock()
        //建立了一個conn物件。 newTransport指標實際上指向的newRLPx方法。 實際上是把fd用rlpx協議包裝了一下。
        c := &conn{fd: fd, transport: srv.newTransport(fd), flags: flags, cont: make(chan error)}
        if !running {
            c.close(errServerStopped)
            return
        }
        // Run the encryption handshake.
        var err error
        //這裡實際上執行的是rlpx.go裡面的doEncHandshake.因為transport是conn的一個匿名欄位。 匿名欄位的方法會直接作為conn的一個方法。
        if c.id, err = c.doEncHandshake(srv.PrivateKey, dialDest); err != nil {
            log.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err)
            c.close(err)
            return
        }
        clog := log.New("id", c.id, "addr", c.fd.RemoteAddr(), "conn", c.flags)
        // For dialed connections, check that the remote public key matches.
        // 如果連線握手的ID和對應的ID不匹配
        if dialDest != nil && c.id != dialDest.ID {
            c.close(DiscUnexpectedIdentity)
            clog.Trace("Dialed identity mismatch", "want", c, dialDest.ID)
            return
        }
        // 這個checkpoint其實就是把第一個引數傳送給第二個引數指定的佇列。然後從c.cout接收返回資訊。 是一個同步的方法。
        //至於這裡,後續的操作只是檢查了一下連線是否合法就返回了。
        if err := srv.checkpoint(c, srv.posthandshake); err != nil {
            clog.Trace("Rejected peer before protocol handshake", "err", err)
            c.close(err)
            return
        }
        // Run the protocol handshake
        phs, err := c.doProtoHandshake(srv.ourHandshake)
        if err != nil {
            clog.Trace("Failed proto handshake", "err", err)
            c.close(err)
            return
        }
        if phs.ID != c.id {
            clog.Trace("Wrong devp2p handshake identity", "err", phs.ID)
            c.close(DiscUnexpectedIdentity)
            return
        }
        c.caps, c.name = phs.Caps, phs.Name
        // 這裡兩次握手都已經完成了。 把c傳送給addpeer佇列。 後臺處理這個佇列的時候,會處理這個連線
        if err := srv.checkpoint(c, srv.addpeer); err != nil {
            clog.Trace("Rejected peer", "err", err)
            c.close(err)
            return
        }
        // If the checks completed successfully, runPeer has now been
        // launched by run.
    }


上面說到的流程是listenLoop的流程,listenLoop主要是用來接收外部主動連線者的。 還有部分情況是節點需要主動發起連線來連線外部節點的流程。 以及處理剛才上面的checkpoint佇列資訊的流程。這部分程式碼都在server.run這個goroutine裡面。



    func (srv *Server) run(dialstate dialer) {
        defer srv.loopWG.Done()
        var (
            peers = make(map[discover.NodeID]*Peer)
            trusted = make(map[discover.NodeID]bool, len(srv.TrustedNodes))
            taskdone = make(chan task, maxActiveDialTasks)
            runningTasks []task
            queuedTasks []task // tasks that can't run yet
        )
        // Put trusted nodes into a map to speed up checks.
        // Trusted peers are loaded on startup and cannot be
        // modified while the server is running.
        // 被信任的節點又這樣一個特性, 如果連線太多,那麼其他節點會被拒絕掉。但是被信任的節點會被接收。
        for _, n := range srv.TrustedNodes {
            trusted[n.ID] = true
        }
    
        // removes t from runningTasks
        // 定義了一個函式,用來從runningTasks佇列刪除某個Task
        delTask := func(t task) {
            for i := range runningTasks {
                if runningTasks[i] == t {
                    runningTasks = append(runningTasks[:i], runningTasks[i+1:]...)
                    break
                }
            }
        }
        // starts until max number of active tasks is satisfied
        // 同時開始連線的節點數量是16個。 遍歷 runningTasks佇列,並啟動這些任務。
        startTasks := func(ts []task) (rest []task) {
            i := 0
            for ; len(runningTasks) < maxActiveDialTasks && i < len(ts); i++ {
                t := ts[i]
                log.Trace("New dial task", "task", t)
                go func() { t.Do(srv); taskdone <- t }()
                runningTasks = append(runningTasks, t)
            }
            return ts[i:]
        }
        scheduleTasks := func() {
            // Start from queue first.
            // 首先呼叫startTasks啟動一部分,把剩下的返回給queuedTasks.
            queuedTasks = append(queuedTasks[:0], startTasks(queuedTasks)...)
            // Query dialer for new tasks and start as many as possible now.
            // 呼叫newTasks來生成任務,並嘗試用startTasks啟動。並把暫時無法啟動的放入queuedTasks佇列
            if len(runningTasks) < maxActiveDialTasks {
                nt := dialstate.newTasks(len(runningTasks)+len(queuedTasks), peers, time.Now())
                queuedTasks = append(queuedTasks, startTasks(nt)...)
            }
        }
    
    running:
        for {
            //呼叫 dialstate.newTasks來生成新任務。 並呼叫startTasks啟動新任務。
            //如果 dialTask已經全部啟動,那麼會生成一個睡眠超時任務。
            scheduleTasks()
    
            select {
            case <-srv.quit:
                // The server was stopped. Run the cleanup logic.
                break running
            case n := <-srv.addstatic:
                // This channel is used by AddPeer to add to the
                // ephemeral static peer list. Add it to the dialer,
                // it will keep the node connected.
                log.Debug("Adding static node", "node", n)
                dialstate.addStatic(n)
            case n := <-srv.removestatic:
                // This channel is used by RemovePeer to send a
                // disconnect request to a peer and begin the
                // stop keeping the node connected
                log.Debug("Removing static node", "node", n)
                dialstate.removeStatic(n)
                if p, ok := peers[n.ID]; ok {
                    p.Disconnect(DiscRequested)
                }
            case op := <-srv.peerOp:
                // This channel is used by Peers and PeerCount.
                op(peers)
                srv.peerOpDone <- struct{}{}
            case t := <-taskdone:
                // A task got done. Tell dialstate about it so it
                // can update its state and remove it from the active
                // tasks list.
                log.Trace("Dial task done", "task", t)
                dialstate.taskDone(t, time.Now())
                delTask(t)
            case c := <-srv.posthandshake:
                // A connection has passed the encryption handshake so
                // the remote identity is known (but hasn't been verified yet).
                // 記得之前呼叫checkpoint方法,會把連線傳送給這個channel。
                if trusted[c.id] {
                    // Ensure that the trusted flag is set before checking against MaxPeers.
                    c.flags |= trustedConn
                }
                // TODO: track in-progress inbound node IDs (pre-Peer) to avoid dialing them.
                select {
                case c.cont <- srv.encHandshakeChecks(peers, c):
                case <-srv.quit:
                    break running
                }
            case c := <-srv.addpeer:
                // At this point the connection is past the protocol handshake.
                // Its capabilities are known and the remote identity is verified.
                // 兩次握手之後會呼叫checkpoint把連線傳送到addpeer這個channel。
                // 然後通過newPeer建立了Peer物件。
                // 啟動一個goroutine 啟動peer物件。 呼叫了peer.run方法。
                err := srv.protoHandshakeChecks(peers, c)
                if err == nil {
                    // The handshakes are done and it passed all checks.
                    p := newPeer(c, srv.Protocols)
                    // If message events are enabled, pass the peerFeed
                    // to the peer
                    if srv.EnableMsgEvents {
                        p.events = &srv.peerFeed
                    }
                    name := truncateName(c.name)
                    log.Debug("Adding p2p peer", "id", c.id, "name", name, "addr", c.fd.RemoteAddr(), "peers", len(peers)+1)
                    peers[c.id] = p
                    go srv.runPeer(p)
                }
                // The dialer logic relies on the assumption that
                // dial tasks complete after the peer has been added or
                // discarded. Unblock the task last.
                select {
                case c.cont <- err:
                case <-srv.quit:
                    break running
                }
            case pd := <-srv.delpeer:
                // A peer disconnected.
                d := common.PrettyDuration(mclock.Now() - pd.created)
                pd.log.Debug("Removing p2p peer", "duration", d, "peers", len(peers)-1, "req", pd.requested, "err", pd.err)
                delete(peers, pd.ID())
            }
        }
    
        log.Trace("P2P networking is spinning down")
    
        // Terminate discovery. If there is a running lookup it will terminate soon.
        if srv.ntab != nil {
            srv.ntab.Close()
        }
        if srv.DiscV5 != nil {
            srv.DiscV5.Close()
        }
        // Disconnect all peers.
        for _, p := range peers {
            p.Disconnect(DiscQuitting)
        }
        // Wait for peers to shut down. Pending connections and tasks are
        // not handled here and will terminate soon-ish because srv.quit
        // is closed.
        for len(peers) > 0 {
            p := <-srv.delpeer
            p.log.Trace("<-delpeer (spindown)", "remainingTasks", len(runningTasks))
            delete(peers, p.ID())
        }
    }


runPeer方法

    // runPeer runs in its own goroutine for each peer.
    // it waits until the Peer logic returns and removes
    // the peer.
    func (srv *Server) runPeer(p *Peer) {
        if srv.newPeerHook != nil {
            srv.newPeerHook(p)
        }
    
        // broadcast peer add
        srv.peerFeed.Send(&PeerEvent{
            Type: PeerEventTypeAdd,
            Peer: p.ID(),
        })
    
        // run the protocol
        remoteRequested, err := p.run()
    
        // broadcast peer drop
        srv.peerFeed.Send(&PeerEvent{
            Type: PeerEventTypeDrop,
            Peer: p.ID(),
            Error: err.Error(),
        })
    
        // Note: run waits for existing peers to be sent on srv.delpeer
        // before returning, so this send should not select on srv.quit.
        srv.delpeer <- peerDrop{p, err, remoteRequested}
    }


總結:

server物件主要完成的工作把之前介紹的所有元件組合在一起。 使用rlpx.go來處理加密鏈路。 使用discover來處理節點發現和查詢。 使用dial來生成和連線需要連線的節點。 使用peer物件來處理每個連線。

server啟動了一個listenLoop來監聽和接收新的連線。 啟動一個run的goroutine來呼叫dialstate生成新的dial任務並進行連線。 goroutine之間使用channel來進行通訊和配合。




網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN



相關文章