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