github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/go-ethereum-master/p2p/peer.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
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"io"
    23  	"net"
    24  	"sort"
    25  	"sync"
    26  	"time"
    27  
    28  	"github.com/ethereum/go-ethereum/common/mclock"
    29  	"github.com/ethereum/go-ethereum/event"
    30  	"github.com/ethereum/go-ethereum/log"
    31  	"github.com/ethereum/go-ethereum/p2p/discover"
    32  	"github.com/ethereum/go-ethereum/rlp"
    33  )
    34  
    35  var (
    36  	ErrShuttingDown = errors.New("shutting down")
    37  )
    38  
    39  const (
    40  	baseProtocolVersion    = 5
    41  	baseProtocolLength     = uint64(16)
    42  	baseProtocolMaxMsgSize = 2 * 1024
    43  
    44  	snappyProtocolVersion = 5
    45  
    46  	pingInterval = 15 * time.Second
    47  )
    48  
    49  const (
    50  	// devp2p message codes
    51  	handshakeMsg = 0x00
    52  	discMsg      = 0x01
    53  	pingMsg      = 0x02
    54  	pongMsg      = 0x03
    55  )
    56  
    57  // protoHandshake is the RLP structure of the protocol handshake.
    58  type protoHandshake struct {
    59  	Version    uint64
    60  	Name       string
    61  	Caps       []Cap
    62  	ListenPort uint64
    63  	ID         discover.NodeID
    64  
    65  	// Ignore additional fields (for forward compatibility).
    66  	Rest []rlp.RawValue `rlp:"tail"`
    67  }
    68  
    69  // PeerEventType is the type of peer events emitted by a p2p.Server
    70  type PeerEventType string
    71  
    72  const (
    73  	// PeerEventTypeAdd is the type of event emitted when a peer is added
    74  	// to a p2p.Server
    75  	PeerEventTypeAdd PeerEventType = "add"
    76  
    77  	// PeerEventTypeDrop is the type of event emitted when a peer is
    78  	// dropped from a p2p.Server
    79  	PeerEventTypeDrop PeerEventType = "drop"
    80  
    81  	// PeerEventTypeMsgSend is the type of event emitted when a
    82  	// message is successfully sent to a peer
    83  	PeerEventTypeMsgSend PeerEventType = "msgsend"
    84  
    85  	// PeerEventTypeMsgRecv is the type of event emitted when a
    86  	// message is received from a peer
    87  	PeerEventTypeMsgRecv PeerEventType = "msgrecv"
    88  )
    89  
    90  // PeerEvent is an event emitted when peers are either added or dropped from
    91  // a p2p.Server or when a message is sent or received on a peer connection
    92  type PeerEvent struct {
    93  	Type     PeerEventType   `json:"type"`
    94  	Peer     discover.NodeID `json:"peer"`
    95  	Error    string          `json:"error,omitempty"`
    96  	Protocol string          `json:"protocol,omitempty"`
    97  	MsgCode  *uint64         `json:"msg_code,omitempty"`
    98  	MsgSize  *uint32         `json:"msg_size,omitempty"`
    99  }
   100  
   101  // Peer represents a connected remote node.
   102  type Peer struct {
   103  	rw      *conn
   104  	running map[string]*protoRW
   105  	log     log.Logger
   106  	created mclock.AbsTime
   107  
   108  	wg       sync.WaitGroup
   109  	protoErr chan error
   110  	closed   chan struct{}
   111  	disc     chan DiscReason
   112  
   113  	// events receives message send / receive events if set
   114  	events *event.Feed
   115  }
   116  
   117  // NewPeer returns a peer for testing purposes.
   118  func NewPeer(id discover.NodeID, name string, caps []Cap) *Peer {
   119  	pipe, _ := net.Pipe()
   120  	conn := &conn{fd: pipe, transport: nil, id: id, caps: caps, name: name}
   121  	peer := newPeer(conn, nil)
   122  	close(peer.closed) // ensures Disconnect doesn't block
   123  	return peer
   124  }
   125  
   126  // ID returns the node's public key.
   127  func (p *Peer) ID() discover.NodeID {
   128  	return p.rw.id
   129  }
   130  
   131  // Name returns the node name that the remote node advertised.
   132  func (p *Peer) Name() string {
   133  	return p.rw.name
   134  }
   135  
   136  // Caps returns the capabilities (supported subprotocols) of the remote peer.
   137  func (p *Peer) Caps() []Cap {
   138  	// TODO: maybe return copy
   139  	return p.rw.caps
   140  }
   141  
   142  // RemoteAddr returns the remote address of the network connection.
   143  func (p *Peer) RemoteAddr() net.Addr {
   144  	return p.rw.fd.RemoteAddr()
   145  }
   146  
   147  // LocalAddr returns the local address of the network connection.
   148  func (p *Peer) LocalAddr() net.Addr {
   149  	return p.rw.fd.LocalAddr()
   150  }
   151  
   152  // Disconnect terminates the peer connection with the given reason.
   153  // It returns immediately and does not wait until the connection is closed.
   154  func (p *Peer) Disconnect(reason DiscReason) {
   155  	select {
   156  	case p.disc <- reason:
   157  	case <-p.closed:
   158  	}
   159  }
   160  
   161  // String implements fmt.Stringer.
   162  func (p *Peer) String() string {
   163  	return fmt.Sprintf("Peer %x %v", p.rw.id[:8], p.RemoteAddr())
   164  }
   165  
   166  // Inbound returns true if the peer is an inbound connection
   167  func (p *Peer) Inbound() bool {
   168  	return p.rw.flags&inboundConn != 0
   169  }
   170  
   171  func newPeer(conn *conn, protocols []Protocol) *Peer {
   172  	protomap := matchProtocols(protocols, conn.caps, conn)
   173  	p := &Peer{
   174  		rw:       conn,
   175  		running:  protomap,
   176  		created:  mclock.Now(),
   177  		disc:     make(chan DiscReason),
   178  		protoErr: make(chan error, len(protomap)+1), // protocols + pingLoop
   179  		closed:   make(chan struct{}),
   180  		log:      log.New("id", conn.id, "conn", conn.flags),
   181  	}
   182  	return p
   183  }
   184  
   185  func (p *Peer) Log() log.Logger {
   186  	return p.log
   187  }
   188  
   189  func (p *Peer) run() (remoteRequested bool, err error) {
   190  	var (
   191  		writeStart = make(chan struct{}, 1)
   192  		writeErr   = make(chan error, 1)
   193  		readErr    = make(chan error, 1)
   194  		reason     DiscReason // sent to the peer
   195  	)
   196  	p.wg.Add(2)
   197  	go p.readLoop(readErr)
   198  	go p.pingLoop()
   199  
   200  	// Start all protocol handlers.
   201  	writeStart <- struct{}{}
   202  	p.startProtocols(writeStart, writeErr)
   203  
   204  	// Wait for an error or disconnect.
   205  loop:
   206  	for {
   207  		select {
   208  		case err = <-writeErr:
   209  			// A write finished. Allow the next write to start if
   210  			// there was no error.
   211  			if err != nil {
   212  				reason = DiscNetworkError
   213  				break loop
   214  			}
   215  			writeStart <- struct{}{}
   216  		case err = <-readErr:
   217  			if r, ok := err.(DiscReason); ok {
   218  				remoteRequested = true
   219  				reason = r
   220  			} else {
   221  				reason = DiscNetworkError
   222  			}
   223  			break loop
   224  		case err = <-p.protoErr:
   225  			reason = discReasonForError(err)
   226  			break loop
   227  		case err = <-p.disc:
   228  			reason = discReasonForError(err)
   229  			break loop
   230  		}
   231  	}
   232  
   233  	close(p.closed)
   234  	p.rw.close(reason)
   235  	p.wg.Wait()
   236  	return remoteRequested, err
   237  }
   238  
   239  func (p *Peer) pingLoop() {
   240  	ping := time.NewTimer(pingInterval)
   241  	defer p.wg.Done()
   242  	defer ping.Stop()
   243  	for {
   244  		select {
   245  		case <-ping.C:
   246  			if err := SendItems(p.rw, pingMsg); err != nil {
   247  				p.protoErr <- err
   248  				return
   249  			}
   250  			ping.Reset(pingInterval)
   251  		case <-p.closed:
   252  			return
   253  		}
   254  	}
   255  }
   256  
   257  func (p *Peer) readLoop(errc chan<- error) {
   258  	defer p.wg.Done()
   259  	for {
   260  		msg, err := p.rw.ReadMsg()
   261  		if err != nil {
   262  			errc <- err
   263  			return
   264  		}
   265  		msg.ReceivedAt = time.Now()
   266  		if err = p.handle(msg); err != nil {
   267  			errc <- err
   268  			return
   269  		}
   270  	}
   271  }
   272  
   273  func (p *Peer) handle(msg Msg) error {
   274  	switch {
   275  	case msg.Code == pingMsg:
   276  		msg.Discard()
   277  		go SendItems(p.rw, pongMsg)
   278  	case msg.Code == discMsg:
   279  		var reason [1]DiscReason
   280  		// This is the last message. We don't need to discard or
   281  		// check errors because, the connection will be closed after it.
   282  		rlp.Decode(msg.Payload, &reason)
   283  		return reason[0]
   284  	case msg.Code < baseProtocolLength:
   285  		// ignore other base protocol messages
   286  		return msg.Discard()
   287  	default:
   288  		// it's a subprotocol message
   289  		proto, err := p.getProto(msg.Code)
   290  		if err != nil {
   291  			return fmt.Errorf("msg code out of range: %v", msg.Code)
   292  		}
   293  		select {
   294  		case proto.in <- msg:
   295  			return nil
   296  		case <-p.closed:
   297  			return io.EOF
   298  		}
   299  	}
   300  	return nil
   301  }
   302  
   303  func countMatchingProtocols(protocols []Protocol, caps []Cap) int {
   304  	n := 0
   305  	for _, cap := range caps {
   306  		for _, proto := range protocols {
   307  			if proto.Name == cap.Name && proto.Version == cap.Version {
   308  				n++
   309  			}
   310  		}
   311  	}
   312  	return n
   313  }
   314  
   315  // matchProtocols creates structures for matching named subprotocols.
   316  func matchProtocols(protocols []Protocol, caps []Cap, rw MsgReadWriter) map[string]*protoRW {
   317  	sort.Sort(capsByNameAndVersion(caps))
   318  	offset := baseProtocolLength
   319  	result := make(map[string]*protoRW)
   320  
   321  outer:
   322  	for _, cap := range caps {
   323  		for _, proto := range protocols {
   324  			if proto.Name == cap.Name && proto.Version == cap.Version {
   325  				// If an old protocol version matched, revert it
   326  				if old := result[cap.Name]; old != nil {
   327  					offset -= old.Length
   328  				}
   329  				// Assign the new match
   330  				result[cap.Name] = &protoRW{Protocol: proto, offset: offset, in: make(chan Msg), w: rw}
   331  				offset += proto.Length
   332  
   333  				continue outer
   334  			}
   335  		}
   336  	}
   337  	return result
   338  }
   339  
   340  func (p *Peer) startProtocols(writeStart <-chan struct{}, writeErr chan<- error) {
   341  	p.wg.Add(len(p.running))
   342  	for _, proto := range p.running {
   343  		proto := proto
   344  		proto.closed = p.closed
   345  		proto.wstart = writeStart
   346  		proto.werr = writeErr
   347  		var rw MsgReadWriter = proto
   348  		if p.events != nil {
   349  			rw = newMsgEventer(rw, p.events, p.ID(), proto.Name)
   350  		}
   351  		p.log.Trace(fmt.Sprintf("Starting protocol %s/%d", proto.Name, proto.Version))
   352  		go func() {
   353  			err := proto.Run(p, rw)
   354  			if err == nil {
   355  				p.log.Trace(fmt.Sprintf("Protocol %s/%d returned", proto.Name, proto.Version))
   356  				err = errProtocolReturned
   357  			} else if err != io.EOF {
   358  				p.log.Trace(fmt.Sprintf("Protocol %s/%d failed", proto.Name, proto.Version), "err", err)
   359  			}
   360  			p.protoErr <- err
   361  			p.wg.Done()
   362  		}()
   363  	}
   364  }
   365  
   366  // getProto finds the protocol responsible for handling
   367  // the given message code.
   368  func (p *Peer) getProto(code uint64) (*protoRW, error) {
   369  	for _, proto := range p.running {
   370  		if code >= proto.offset && code < proto.offset+proto.Length {
   371  			return proto, nil
   372  		}
   373  	}
   374  	return nil, newPeerError(errInvalidMsgCode, "%d", code)
   375  }
   376  
   377  type protoRW struct {
   378  	Protocol
   379  	in     chan Msg        // receices read messages
   380  	closed <-chan struct{} // receives when peer is shutting down
   381  	wstart <-chan struct{} // receives when write may start
   382  	werr   chan<- error    // for write results
   383  	offset uint64
   384  	w      MsgWriter
   385  }
   386  
   387  func (rw *protoRW) WriteMsg(msg Msg) (err error) {
   388  	if msg.Code >= rw.Length {
   389  		return newPeerError(errInvalidMsgCode, "not handled")
   390  	}
   391  	msg.Code += rw.offset
   392  	select {
   393  	case <-rw.wstart:
   394  		err = rw.w.WriteMsg(msg)
   395  		// Report write status back to Peer.run. It will initiate
   396  		// shutdown if the error is non-nil and unblock the next write
   397  		// otherwise. The calling protocol code should exit for errors
   398  		// as well but we don't want to rely on that.
   399  		rw.werr <- err
   400  	case <-rw.closed:
   401  		err = ErrShuttingDown
   402  	}
   403  	return err
   404  }
   405  
   406  func (rw *protoRW) ReadMsg() (Msg, error) {
   407  	select {
   408  	case msg := <-rw.in:
   409  		msg.Code -= rw.offset
   410  		return msg, nil
   411  	case <-rw.closed:
   412  		return Msg{}, io.EOF
   413  	}
   414  }
   415  
   416  // PeerInfo represents a short summary of the information known about a connected
   417  // peer. Sub-protocol independent fields are contained and initialized here, with
   418  // protocol specifics delegated to all connected sub-protocols.
   419  type PeerInfo struct {
   420  	ID      string   `json:"id"`   // Unique node identifier (also the encryption key)
   421  	Name    string   `json:"name"` // Name of the node, including client type, version, OS, custom data
   422  	Caps    []string `json:"caps"` // Sum-protocols advertised by this particular peer
   423  	Network struct {
   424  		LocalAddress  string `json:"localAddress"`  // Local endpoint of the TCP data connection
   425  		RemoteAddress string `json:"remoteAddress"` // Remote endpoint of the TCP data connection
   426  		Inbound       bool   `json:"inbound"`
   427  		Trusted       bool   `json:"trusted"`
   428  		Static        bool   `json:"static"`
   429  	} `json:"network"`
   430  	Protocols map[string]interface{} `json:"protocols"` // Sub-protocol specific metadata fields
   431  }
   432  
   433  // Info gathers and returns a collection of metadata known about a peer.
   434  func (p *Peer) Info() *PeerInfo {
   435  	// Gather the protocol capabilities
   436  	var caps []string
   437  	for _, cap := range p.Caps() {
   438  		caps = append(caps, cap.String())
   439  	}
   440  	// Assemble the generic peer metadata
   441  	info := &PeerInfo{
   442  		ID:        p.ID().String(),
   443  		Name:      p.Name(),
   444  		Caps:      caps,
   445  		Protocols: make(map[string]interface{}),
   446  	}
   447  	info.Network.LocalAddress = p.LocalAddr().String()
   448  	info.Network.RemoteAddress = p.RemoteAddr().String()
   449  	info.Network.Inbound = p.rw.is(inboundConn)
   450  	info.Network.Trusted = p.rw.is(trustedConn)
   451  	info.Network.Static = p.rw.is(staticDialedConn)
   452  
   453  	// Gather all the running protocol infos
   454  	for _, proto := range p.running {
   455  		protoInfo := interface{}("unknown")
   456  		if query := proto.Protocol.PeerInfo; query != nil {
   457  			if metadata := query(p.ID()); metadata != nil {
   458  				protoInfo = metadata
   459  			} else {
   460  				protoInfo = "handshake"
   461  			}
   462  		}
   463  		info.Protocols[proto.Name] = protoInfo
   464  	}
   465  	return info
   466  }