github.com/m3shine/gochain@v2.2.26+incompatible/p2p/dial.go (about)

     1  // Copyright 2015 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  	"container/heap"
    21  	"crypto/rand"
    22  	"errors"
    23  	"fmt"
    24  	"net"
    25  	"sync"
    26  	"time"
    27  
    28  	"github.com/gochain-io/gochain/log"
    29  	"github.com/gochain-io/gochain/p2p/discover"
    30  	"github.com/gochain-io/gochain/p2p/netutil"
    31  )
    32  
    33  const (
    34  	// This is the amount of time spent waiting in between
    35  	// redialing a certain node.
    36  	dialHistoryExpiration = 30 * time.Second
    37  
    38  	// Discovery lookups are throttled and can only run
    39  	// once every few seconds.
    40  	lookupInterval = 4 * time.Second
    41  
    42  	// If no peers are found for this amount of time, the initial bootnodes are
    43  	// attempted to be connected.
    44  	fallbackInterval = 20 * time.Second
    45  
    46  	// Endpoint resolution is throttled with bounded backoff.
    47  	initialResolveDelay = 60 * time.Second
    48  	maxResolveDelay     = time.Hour
    49  )
    50  
    51  // NodeDialer is used to connect to nodes in the network, typically by using
    52  // an underlying net.Dialer but also using net.Pipe in tests
    53  type NodeDialer interface {
    54  	Dial(*discover.Node) (net.Conn, error)
    55  }
    56  
    57  // TCPDialer implements the NodeDialer interface by using a net.Dialer to
    58  // create TCP connections to nodes in the network
    59  type TCPDialer struct {
    60  	*net.Dialer
    61  }
    62  
    63  // Dial creates a TCP connection to the node
    64  func (t TCPDialer) Dial(dest *discover.Node) (net.Conn, error) {
    65  	addr := &net.TCPAddr{IP: dest.IP, Port: int(dest.TCP)}
    66  	return t.Dialer.Dial("tcp", addr.String())
    67  }
    68  
    69  // dialstate schedules dials and discovery lookups.
    70  // it get's a chance to compute new tasks on every iteration
    71  // of the main loop in Server.run.
    72  type dialstate struct {
    73  	maxDynDials int
    74  	ntab        discoverTable
    75  	netrestrict *netutil.Netlist
    76  
    77  	lookupRunning bool
    78  	dialing       map[discover.NodeID]connFlag
    79  	lookupBuf     []*discover.Node // current discovery lookup results
    80  	randomNodes   []*discover.Node // filled from Table
    81  	static        map[discover.NodeID]*dialTask
    82  	hist          *dialHistory
    83  
    84  	start     time.Time        // time when the dialer was first used
    85  	bootnodes []*discover.Node // default dials when there are no peers
    86  }
    87  
    88  type discoverTable interface {
    89  	Self() *discover.Node
    90  	Close()
    91  	Resolve(target discover.NodeID) *discover.Node
    92  	Lookup(target discover.NodeID) []*discover.Node
    93  	ReadRandomNodes([]*discover.Node) int
    94  }
    95  
    96  // the dial history remembers recent dials.
    97  type dialHistory []pastDial
    98  
    99  // pastDial is an entry in the dial history.
   100  type pastDial struct {
   101  	id  discover.NodeID
   102  	exp time.Time
   103  }
   104  
   105  type task interface {
   106  	Do(*Server)
   107  }
   108  
   109  // A dialTask is generated for each node that is dialed. Its
   110  // fields cannot be accessed while the task is running.
   111  type dialTask struct {
   112  	flags        connFlag
   113  	destMu       sync.RWMutex
   114  	dest         *discover.Node
   115  	lastResolved time.Time
   116  	resolveDelay time.Duration
   117  }
   118  
   119  // discoverTask runs discovery table operations.
   120  // Only one discoverTask is active at any time.
   121  // discoverTask.Do performs a random lookup.
   122  type discoverTask struct {
   123  	results []*discover.Node
   124  }
   125  
   126  // A waitExpireTask is generated if there are no other tasks
   127  // to keep the loop in Server.run ticking.
   128  type waitExpireTask struct {
   129  	time.Duration
   130  }
   131  
   132  func newDialState(static []*discover.Node, bootnodes []*discover.Node, ntab discoverTable, maxdyn int, netrestrict *netutil.Netlist) *dialstate {
   133  	s := &dialstate{
   134  		maxDynDials: maxdyn,
   135  		ntab:        ntab,
   136  		netrestrict: netrestrict,
   137  		static:      make(map[discover.NodeID]*dialTask),
   138  		dialing:     make(map[discover.NodeID]connFlag),
   139  		bootnodes:   make([]*discover.Node, len(bootnodes)),
   140  		randomNodes: make([]*discover.Node, maxdyn/2),
   141  		hist:        new(dialHistory),
   142  	}
   143  	copy(s.bootnodes, bootnodes)
   144  	for _, n := range static {
   145  		s.addStatic(n)
   146  	}
   147  	return s
   148  }
   149  
   150  func (s *dialstate) addStatic(n *discover.Node) {
   151  	// This overwites the task instead of updating an existing
   152  	// entry, giving users the opportunity to force a resolve operation.
   153  	dt := &dialTask{flags: staticDialedConn}
   154  	dt.destMu.Lock()
   155  	dt.dest = n
   156  	dt.destMu.Unlock()
   157  	s.static[n.ID] = dt
   158  }
   159  
   160  func (s *dialstate) removeStatic(n *discover.Node) {
   161  	// This removes a task so future attempts to connect will not be made.
   162  	delete(s.static, n.ID)
   163  	// This removes a previous dial timestamp so that application
   164  	// can force a server to reconnect with chosen peer immediately.
   165  	s.hist.remove(n.ID)
   166  }
   167  
   168  func (s *dialstate) newTasks(nRunning int, peers map[discover.NodeID]*Peer, now time.Time) []task {
   169  	if s.start.IsZero() {
   170  		s.start = now
   171  	}
   172  
   173  	var newtasks []task
   174  	addDial := func(flag connFlag, n *discover.Node) bool {
   175  		if err := s.checkDial(n, peers); err != nil {
   176  			log.Trace("Skipping dial candidate", "id", n.ID, "addr", &net.TCPAddr{IP: n.IP, Port: int(n.TCP)}, "err", err)
   177  			return false
   178  		}
   179  		s.dialing[n.ID] = flag
   180  		newtasks = append(newtasks, &dialTask{flags: flag, dest: n})
   181  		return true
   182  	}
   183  
   184  	// Compute number of dynamic dials necessary at this point.
   185  	needDynDials := s.maxDynDials
   186  	for _, p := range peers {
   187  		if p.rw.is(dynDialedConn) {
   188  			needDynDials--
   189  		}
   190  	}
   191  	for _, flag := range s.dialing {
   192  		if flag&dynDialedConn != 0 {
   193  			needDynDials--
   194  		}
   195  	}
   196  
   197  	// Expire the dial history on every invocation.
   198  	s.hist.expire(now)
   199  
   200  	// Create dials for static nodes if they are not connected.
   201  	for id, t := range s.static {
   202  		t.destMu.RLock()
   203  		dest := t.dest
   204  		t.destMu.RUnlock()
   205  		err := s.checkDial(dest, peers)
   206  		switch err {
   207  		case errNotWhitelisted, errSelf:
   208  			log.Warn("Removing static dial candidate", "id", dest.ID, "addr", &net.TCPAddr{IP: dest.IP, Port: int(dest.TCP)}, "err", err)
   209  			delete(s.static, dest.ID)
   210  		case nil:
   211  			s.dialing[id] = t.flags
   212  			newtasks = append(newtasks, t)
   213  		}
   214  	}
   215  	// If we don't have any peers whatsoever, try to dial a random bootnode. This
   216  	// scenario is useful for the testnet (and private networks) where the discovery
   217  	// table might be full of mostly bad peers, making it hard to find good ones.
   218  	if len(peers) == 0 && len(s.bootnodes) > 0 && needDynDials > 0 && now.Sub(s.start) > fallbackInterval {
   219  		bootnode := s.bootnodes[0]
   220  		s.bootnodes = append(s.bootnodes[:0], s.bootnodes[1:]...)
   221  		s.bootnodes = append(s.bootnodes, bootnode)
   222  
   223  		if addDial(dynDialedConn, bootnode) {
   224  			needDynDials--
   225  		}
   226  	}
   227  	// Use random nodes from the table for half of the necessary
   228  	// dynamic dials.
   229  	randomCandidates := needDynDials / 2
   230  	if randomCandidates > 0 {
   231  		n := s.ntab.ReadRandomNodes(s.randomNodes)
   232  		for i := 0; i < randomCandidates && i < n; i++ {
   233  			if addDial(dynDialedConn, s.randomNodes[i]) {
   234  				needDynDials--
   235  			}
   236  		}
   237  	}
   238  	// Create dynamic dials from random lookup results, removing tried
   239  	// items from the result buffer.
   240  	i := 0
   241  	for ; i < len(s.lookupBuf) && needDynDials > 0; i++ {
   242  		if addDial(dynDialedConn, s.lookupBuf[i]) {
   243  			needDynDials--
   244  		}
   245  	}
   246  	s.lookupBuf = s.lookupBuf[:copy(s.lookupBuf, s.lookupBuf[i:])]
   247  	// Launch a discovery lookup if more candidates are needed.
   248  	if len(s.lookupBuf) < needDynDials && !s.lookupRunning {
   249  		s.lookupRunning = true
   250  		newtasks = append(newtasks, &discoverTask{})
   251  	}
   252  
   253  	// Launch a timer to wait for the next node to expire if all
   254  	// candidates have been tried and no task is currently active.
   255  	// This should prevent cases where the dialer logic is not ticked
   256  	// because there are no pending events.
   257  	if nRunning == 0 && len(newtasks) == 0 && s.hist.Len() > 0 {
   258  		t := &waitExpireTask{s.hist.min().exp.Sub(now)}
   259  		newtasks = append(newtasks, t)
   260  	}
   261  	return newtasks
   262  }
   263  
   264  var (
   265  	errSelf             = errors.New("is self")
   266  	errAlreadyDialing   = errors.New("already dialing")
   267  	errAlreadyConnected = errors.New("already connected")
   268  	errRecentlyDialed   = errors.New("recently dialed")
   269  	errNotWhitelisted   = errors.New("not contained in netrestrict whitelist")
   270  )
   271  
   272  func (s *dialstate) checkDial(n *discover.Node, peers map[discover.NodeID]*Peer) error {
   273  	_, dialing := s.dialing[n.ID]
   274  	switch {
   275  	case dialing:
   276  		return errAlreadyDialing
   277  	case peers[n.ID] != nil:
   278  		return errAlreadyConnected
   279  	case s.ntab != nil && n.ID == s.ntab.Self().ID:
   280  		return errSelf
   281  	case s.netrestrict != nil && !s.netrestrict.Contains(n.IP):
   282  		return errNotWhitelisted
   283  	case s.hist.contains(n.ID):
   284  		return errRecentlyDialed
   285  	}
   286  	return nil
   287  }
   288  
   289  func (s *dialstate) taskDone(t task, now time.Time) {
   290  	switch t := t.(type) {
   291  	case *dialTask:
   292  		t.destMu.RLock()
   293  		dest := t.dest
   294  		t.destMu.RUnlock()
   295  		s.hist.add(dest.ID, now.Add(dialHistoryExpiration))
   296  		delete(s.dialing, dest.ID)
   297  	case *discoverTask:
   298  		s.lookupRunning = false
   299  		s.lookupBuf = append(s.lookupBuf, t.results...)
   300  	}
   301  }
   302  
   303  func (t *dialTask) Do(srv *Server) {
   304  	t.destMu.RLock()
   305  	dest := t.dest
   306  	t.destMu.RUnlock()
   307  	if dest.Incomplete() {
   308  		if !t.resolve(srv) {
   309  			return
   310  		}
   311  	}
   312  	t.destMu.RLock()
   313  	dest = t.dest
   314  	t.destMu.RUnlock()
   315  	err := t.dial(srv, dest)
   316  	if err != nil {
   317  		log.Trace("Dial error", "task", t, "err", err)
   318  		// Try resolving the ID of static nodes if dialing failed.
   319  		if _, ok := err.(*dialError); ok && t.flags&staticDialedConn != 0 {
   320  			if t.resolve(srv) {
   321  				t.destMu.RLock()
   322  				dest = t.dest
   323  				t.destMu.RUnlock()
   324  				if err := t.dial(srv, dest); err != nil {
   325  					log.Error("Cannot dial static node", "err", err)
   326  				}
   327  			}
   328  		}
   329  	}
   330  }
   331  
   332  // resolve attempts to find the current endpoint for the destination
   333  // using discovery.
   334  //
   335  // Resolve operations are throttled with backoff to avoid flooding the
   336  // discovery network with useless queries for nodes that don't exist.
   337  // The backoff delay resets when the node is found.
   338  func (t *dialTask) resolve(srv *Server) bool {
   339  	t.destMu.RLock()
   340  	dest := t.dest
   341  	t.destMu.RUnlock()
   342  	if srv.ntab == nil {
   343  		log.Debug("Can't resolve node", "id", dest.ID, "err", "discovery is disabled")
   344  		return false
   345  	}
   346  	if t.resolveDelay == 0 {
   347  		t.resolveDelay = initialResolveDelay
   348  	}
   349  	if time.Since(t.lastResolved) < t.resolveDelay {
   350  		return false
   351  	}
   352  	resolved := srv.ntab.Resolve(dest.ID)
   353  	t.lastResolved = time.Now()
   354  	if resolved == nil {
   355  		t.resolveDelay *= 2
   356  		if t.resolveDelay > maxResolveDelay {
   357  			t.resolveDelay = maxResolveDelay
   358  		}
   359  		log.Debug("Resolving node failed", "id", dest.ID, "newdelay", t.resolveDelay)
   360  		return false
   361  	}
   362  	// The node was found.
   363  	t.resolveDelay = initialResolveDelay
   364  	t.destMu.Lock()
   365  	t.dest = resolved
   366  	t.destMu.Unlock()
   367  	log.Debug("Resolved node", "id", resolved.ID, "addr", &net.TCPAddr{IP: resolved.IP, Port: int(resolved.TCP)})
   368  	return true
   369  }
   370  
   371  type dialError struct {
   372  	error
   373  }
   374  
   375  // dial performs the actual connection attempt.
   376  func (t *dialTask) dial(srv *Server, dest *discover.Node) error {
   377  	fd, err := srv.Dialer.Dial(dest)
   378  	if err != nil {
   379  		return &dialError{err}
   380  	}
   381  	mfd := newMeteredConn(fd, false)
   382  	return srv.SetupConn(mfd, t.flags, dest)
   383  }
   384  
   385  func (t *dialTask) String() string {
   386  	t.destMu.RLock()
   387  	dest := t.dest
   388  	t.destMu.RUnlock()
   389  	return fmt.Sprintf("%v %x %v:%d", t.flags, dest.ID[:8], dest.IP, dest.TCP)
   390  }
   391  
   392  func (t *discoverTask) Do(srv *Server) {
   393  	// newTasks generates a lookup task whenever dynamic dials are
   394  	// necessary. Lookups need to take some time, otherwise the
   395  	// event loop spins too fast.
   396  	next := srv.lastLookup.Add(lookupInterval)
   397  	if now := time.Now(); now.Before(next) {
   398  		time.Sleep(next.Sub(now))
   399  	}
   400  	srv.lastLookup = time.Now()
   401  	var target discover.NodeID
   402  	rand.Read(target[:])
   403  	t.results = srv.ntab.Lookup(target)
   404  }
   405  
   406  func (t *discoverTask) String() string {
   407  	s := "discovery lookup"
   408  	if len(t.results) > 0 {
   409  		s += fmt.Sprintf(" (%d results)", len(t.results))
   410  	}
   411  	return s
   412  }
   413  
   414  func (t waitExpireTask) Do(*Server) {
   415  	time.Sleep(t.Duration)
   416  }
   417  func (t waitExpireTask) String() string {
   418  	return fmt.Sprintf("wait for dial hist expire (%v)", t.Duration)
   419  }
   420  
   421  // Use only these methods to access or modify dialHistory.
   422  func (h dialHistory) min() pastDial {
   423  	return h[0]
   424  }
   425  func (h *dialHistory) add(id discover.NodeID, exp time.Time) {
   426  	heap.Push(h, pastDial{id, exp})
   427  
   428  }
   429  func (h *dialHistory) remove(id discover.NodeID) bool {
   430  	for i, v := range *h {
   431  		if v.id == id {
   432  			heap.Remove(h, i)
   433  			return true
   434  		}
   435  	}
   436  	return false
   437  }
   438  func (h dialHistory) contains(id discover.NodeID) bool {
   439  	for _, v := range h {
   440  		if v.id == id {
   441  			return true
   442  		}
   443  	}
   444  	return false
   445  }
   446  func (h *dialHistory) expire(now time.Time) {
   447  	for h.Len() > 0 && h.min().exp.Before(now) {
   448  		heap.Pop(h)
   449  	}
   450  }
   451  
   452  // heap.Interface boilerplate
   453  func (h dialHistory) Len() int           { return len(h) }
   454  func (h dialHistory) Less(i, j int) bool { return h[i].exp.Before(h[j].exp) }
   455  func (h dialHistory) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }
   456  func (h *dialHistory) Push(x interface{}) {
   457  	*h = append(*h, x.(pastDial))
   458  }
   459  func (h *dialHistory) Pop() interface{} {
   460  	old := *h
   461  	n := len(old)
   462  	x := old[n-1]
   463  	*h = old[0 : n-1]
   464  	return x
   465  }