2016-06-14 16:17:44 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2016-12-07 20:54:59 +00:00
|
|
|
"errors"
|
2016-06-14 16:17:44 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2017-05-01 11:09:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-11-14 07:03:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2016-12-07 20:54:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/netutil"
|
2016-06-14 16:17:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-10-04 15:21:24 +00:00
|
|
|
// This is the amount of time spent waiting in between redialing a certain node. The
|
|
|
|
// limit is a bit higher than inboundThrottleTime to prevent failing dials in small
|
|
|
|
// private networks.
|
|
|
|
dialHistoryExpiration = inboundThrottleTime + 5*time.Second
|
2016-06-14 16:17:44 +00:00
|
|
|
|
|
|
|
// Discovery lookups are throttled and can only run
|
|
|
|
// once every few seconds.
|
|
|
|
lookupInterval = 4 * time.Second
|
|
|
|
|
2017-05-01 11:09:48 +00:00
|
|
|
// If no peers are found for this amount of time, the initial bootnodes are
|
|
|
|
// attempted to be connected.
|
|
|
|
fallbackInterval = 20 * time.Second
|
|
|
|
|
2016-06-14 16:17:44 +00:00
|
|
|
// Endpoint resolution is throttled with bounded backoff.
|
|
|
|
initialResolveDelay = 60 * time.Second
|
|
|
|
maxResolveDelay = time.Hour
|
|
|
|
)
|
|
|
|
|
2017-10-17 09:58:04 +00:00
|
|
|
// NodeDialer is used to connect to nodes in the network, typically by using
|
|
|
|
// an underlying net.Dialer but also using net.Pipe in tests
|
|
|
|
type NodeDialer interface {
|
2018-11-14 07:03:58 +00:00
|
|
|
Dial(*enode.Node) (net.Conn, error)
|
2017-10-17 09:58:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TCPDialer implements the NodeDialer interface by using a net.Dialer to
|
|
|
|
// create TCP connections to nodes in the network
|
|
|
|
type TCPDialer struct {
|
|
|
|
*net.Dialer
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial creates a TCP connection to the node
|
2018-11-14 07:03:58 +00:00
|
|
|
func (t TCPDialer) Dial(dest *enode.Node) (net.Conn, error) {
|
|
|
|
addr := &net.TCPAddr{IP: dest.IP(), Port: dest.TCP()}
|
2017-10-17 09:58:04 +00:00
|
|
|
return t.Dialer.Dial("tcp", addr.String())
|
|
|
|
}
|
|
|
|
|
2016-06-14 16:17:44 +00:00
|
|
|
// dialstate schedules dials and discovery lookups.
|
2019-10-04 15:21:24 +00:00
|
|
|
// It gets a chance to compute new tasks on every iteration
|
2016-06-14 16:17:44 +00:00
|
|
|
// of the main loop in Server.run.
|
|
|
|
type dialstate struct {
|
|
|
|
maxDynDials int
|
|
|
|
ntab discoverTable
|
2016-12-07 20:54:59 +00:00
|
|
|
netrestrict *netutil.Netlist
|
2018-12-19 10:02:07 +00:00
|
|
|
self enode.ID
|
2019-10-04 15:21:24 +00:00
|
|
|
bootnodes []*enode.Node // default dials when there are no peers
|
|
|
|
log log.Logger
|
2016-06-14 16:17:44 +00:00
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
start time.Time // time when the dialer was first used
|
2016-06-14 16:17:44 +00:00
|
|
|
lookupRunning bool
|
2018-11-14 07:03:58 +00:00
|
|
|
dialing map[enode.ID]connFlag
|
|
|
|
lookupBuf []*enode.Node // current discovery lookup results
|
|
|
|
randomNodes []*enode.Node // filled from Table
|
|
|
|
static map[enode.ID]*dialTask
|
2019-10-04 15:21:24 +00:00
|
|
|
hist expHeap
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type discoverTable interface {
|
|
|
|
Close()
|
2018-11-14 07:03:58 +00:00
|
|
|
Resolve(*enode.Node) *enode.Node
|
|
|
|
LookupRandom() []*enode.Node
|
|
|
|
ReadRandomNodes([]*enode.Node) int
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type task interface {
|
|
|
|
Do(*Server)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A dialTask is generated for each node that is dialed. Its
|
|
|
|
// fields cannot be accessed while the task is running.
|
|
|
|
type dialTask struct {
|
|
|
|
flags connFlag
|
2018-11-14 07:03:58 +00:00
|
|
|
dest *enode.Node
|
2016-06-14 16:17:44 +00:00
|
|
|
lastResolved time.Time
|
|
|
|
resolveDelay time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// discoverTask runs discovery table operations.
|
|
|
|
// Only one discoverTask is active at any time.
|
|
|
|
// discoverTask.Do performs a random lookup.
|
|
|
|
type discoverTask struct {
|
2018-11-14 07:03:58 +00:00
|
|
|
results []*enode.Node
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A waitExpireTask is generated if there are no other tasks
|
|
|
|
// to keep the loop in Server.run ticking.
|
|
|
|
type waitExpireTask struct {
|
|
|
|
time.Duration
|
|
|
|
}
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
func newDialState(self enode.ID, ntab discoverTable, maxdyn int, cfg *Config) *dialstate {
|
2016-06-14 16:17:44 +00:00
|
|
|
s := &dialstate{
|
|
|
|
maxDynDials: maxdyn,
|
|
|
|
ntab: ntab,
|
2018-12-19 10:02:07 +00:00
|
|
|
self: self,
|
2019-10-04 15:21:24 +00:00
|
|
|
netrestrict: cfg.NetRestrict,
|
|
|
|
log: cfg.Logger,
|
2018-11-14 07:03:58 +00:00
|
|
|
static: make(map[enode.ID]*dialTask),
|
|
|
|
dialing: make(map[enode.ID]connFlag),
|
2019-10-04 15:21:24 +00:00
|
|
|
bootnodes: make([]*enode.Node, len(cfg.BootstrapNodes)),
|
2018-11-14 07:03:58 +00:00
|
|
|
randomNodes: make([]*enode.Node, maxdyn/2),
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
2019-10-04 15:21:24 +00:00
|
|
|
copy(s.bootnodes, cfg.BootstrapNodes)
|
|
|
|
if s.log == nil {
|
|
|
|
s.log = log.Root()
|
|
|
|
}
|
|
|
|
for _, n := range cfg.StaticNodes {
|
2016-06-14 16:17:44 +00:00
|
|
|
s.addStatic(n)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func (s *dialstate) addStatic(n *enode.Node) {
|
2018-09-25 08:55:54 +00:00
|
|
|
// This overwrites the task instead of updating an existing
|
2016-06-14 16:17:44 +00:00
|
|
|
// entry, giving users the opportunity to force a resolve operation.
|
2018-11-14 07:03:58 +00:00
|
|
|
s.static[n.ID()] = &dialTask{flags: staticDialedConn, dest: n}
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func (s *dialstate) removeStatic(n *enode.Node) {
|
2016-09-08 09:45:12 +00:00
|
|
|
// This removes a task so future attempts to connect will not be made.
|
2018-11-14 07:03:58 +00:00
|
|
|
delete(s.static, n.ID())
|
2016-09-08 09:45:12 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func (s *dialstate) newTasks(nRunning int, peers map[enode.ID]*Peer, now time.Time) []task {
|
2018-02-27 10:39:30 +00:00
|
|
|
if s.start.IsZero() {
|
2017-05-01 11:09:48 +00:00
|
|
|
s.start = now
|
|
|
|
}
|
|
|
|
|
2016-06-14 16:17:44 +00:00
|
|
|
var newtasks []task
|
2018-11-14 07:03:58 +00:00
|
|
|
addDial := func(flag connFlag, n *enode.Node) bool {
|
2016-12-07 20:54:59 +00:00
|
|
|
if err := s.checkDial(n, peers); err != nil {
|
2019-10-04 15:21:24 +00:00
|
|
|
s.log.Trace("Skipping dial candidate", "id", n.ID(), "addr", &net.TCPAddr{IP: n.IP(), Port: n.TCP()}, "err", err)
|
2016-06-14 16:17:44 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
s.dialing[n.ID()] = flag
|
2016-06-14 16:17:44 +00:00
|
|
|
newtasks = append(newtasks, &dialTask{flags: flag, dest: n})
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute number of dynamic dials necessary at this point.
|
|
|
|
needDynDials := s.maxDynDials
|
|
|
|
for _, p := range peers {
|
|
|
|
if p.rw.is(dynDialedConn) {
|
|
|
|
needDynDials--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, flag := range s.dialing {
|
|
|
|
if flag&dynDialedConn != 0 {
|
|
|
|
needDynDials--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expire the dial history on every invocation.
|
|
|
|
s.hist.expire(now)
|
|
|
|
|
|
|
|
// Create dials for static nodes if they are not connected.
|
|
|
|
for id, t := range s.static {
|
2016-12-07 20:54:59 +00:00
|
|
|
err := s.checkDial(t.dest, peers)
|
|
|
|
switch err {
|
|
|
|
case errNotWhitelisted, errSelf:
|
2019-10-04 15:21:24 +00:00
|
|
|
s.log.Warn("Removing static dial candidate", "id", t.dest.ID, "addr", &net.TCPAddr{IP: t.dest.IP(), Port: t.dest.TCP()}, "err", err)
|
2018-11-14 07:03:58 +00:00
|
|
|
delete(s.static, t.dest.ID())
|
2016-12-07 20:54:59 +00:00
|
|
|
case nil:
|
2016-06-14 16:17:44 +00:00
|
|
|
s.dialing[id] = t.flags
|
|
|
|
newtasks = append(newtasks, t)
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 11:09:48 +00:00
|
|
|
// If we don't have any peers whatsoever, try to dial a random bootnode. This
|
|
|
|
// scenario is useful for the testnet (and private networks) where the discovery
|
|
|
|
// table might be full of mostly bad peers, making it hard to find good ones.
|
|
|
|
if len(peers) == 0 && len(s.bootnodes) > 0 && needDynDials > 0 && now.Sub(s.start) > fallbackInterval {
|
|
|
|
bootnode := s.bootnodes[0]
|
|
|
|
s.bootnodes = append(s.bootnodes[:0], s.bootnodes[1:]...)
|
|
|
|
s.bootnodes = append(s.bootnodes, bootnode)
|
|
|
|
|
|
|
|
if addDial(dynDialedConn, bootnode) {
|
|
|
|
needDynDials--
|
|
|
|
}
|
|
|
|
}
|
2016-06-14 16:17:44 +00:00
|
|
|
// Use random nodes from the table for half of the necessary
|
|
|
|
// dynamic dials.
|
|
|
|
randomCandidates := needDynDials / 2
|
|
|
|
if randomCandidates > 0 {
|
|
|
|
n := s.ntab.ReadRandomNodes(s.randomNodes)
|
|
|
|
for i := 0; i < randomCandidates && i < n; i++ {
|
|
|
|
if addDial(dynDialedConn, s.randomNodes[i]) {
|
|
|
|
needDynDials--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Create dynamic dials from random lookup results, removing tried
|
|
|
|
// items from the result buffer.
|
|
|
|
i := 0
|
|
|
|
for ; i < len(s.lookupBuf) && needDynDials > 0; i++ {
|
|
|
|
if addDial(dynDialedConn, s.lookupBuf[i]) {
|
|
|
|
needDynDials--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.lookupBuf = s.lookupBuf[:copy(s.lookupBuf, s.lookupBuf[i:])]
|
|
|
|
// Launch a discovery lookup if more candidates are needed.
|
|
|
|
if len(s.lookupBuf) < needDynDials && !s.lookupRunning {
|
|
|
|
s.lookupRunning = true
|
|
|
|
newtasks = append(newtasks, &discoverTask{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Launch a timer to wait for the next node to expire if all
|
|
|
|
// candidates have been tried and no task is currently active.
|
|
|
|
// This should prevent cases where the dialer logic is not ticked
|
|
|
|
// because there are no pending events.
|
|
|
|
if nRunning == 0 && len(newtasks) == 0 && s.hist.Len() > 0 {
|
2019-10-04 15:21:24 +00:00
|
|
|
t := &waitExpireTask{s.hist.nextExpiry().Sub(now)}
|
2016-06-14 16:17:44 +00:00
|
|
|
newtasks = append(newtasks, t)
|
|
|
|
}
|
|
|
|
return newtasks
|
|
|
|
}
|
|
|
|
|
2016-12-07 20:54:59 +00:00
|
|
|
var (
|
|
|
|
errSelf = errors.New("is self")
|
|
|
|
errAlreadyDialing = errors.New("already dialing")
|
|
|
|
errAlreadyConnected = errors.New("already connected")
|
|
|
|
errRecentlyDialed = errors.New("recently dialed")
|
|
|
|
errNotWhitelisted = errors.New("not contained in netrestrict whitelist")
|
|
|
|
)
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func (s *dialstate) checkDial(n *enode.Node, peers map[enode.ID]*Peer) error {
|
|
|
|
_, dialing := s.dialing[n.ID()]
|
2016-12-07 20:54:59 +00:00
|
|
|
switch {
|
|
|
|
case dialing:
|
|
|
|
return errAlreadyDialing
|
2018-11-14 07:03:58 +00:00
|
|
|
case peers[n.ID()] != nil:
|
2016-12-07 20:54:59 +00:00
|
|
|
return errAlreadyConnected
|
2018-12-19 10:02:07 +00:00
|
|
|
case n.ID() == s.self:
|
2016-12-07 20:54:59 +00:00
|
|
|
return errSelf
|
2018-11-14 07:03:58 +00:00
|
|
|
case s.netrestrict != nil && !s.netrestrict.Contains(n.IP()):
|
2016-12-07 20:54:59 +00:00
|
|
|
return errNotWhitelisted
|
2019-10-04 15:21:24 +00:00
|
|
|
case s.hist.contains(string(n.ID().Bytes())):
|
2016-12-07 20:54:59 +00:00
|
|
|
return errRecentlyDialed
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-14 16:17:44 +00:00
|
|
|
func (s *dialstate) taskDone(t task, now time.Time) {
|
|
|
|
switch t := t.(type) {
|
|
|
|
case *dialTask:
|
2019-10-04 15:21:24 +00:00
|
|
|
s.hist.add(string(t.dest.ID().Bytes()), now.Add(dialHistoryExpiration))
|
2018-11-14 07:03:58 +00:00
|
|
|
delete(s.dialing, t.dest.ID())
|
2016-06-14 16:17:44 +00:00
|
|
|
case *discoverTask:
|
|
|
|
s.lookupRunning = false
|
|
|
|
s.lookupBuf = append(s.lookupBuf, t.results...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *dialTask) Do(srv *Server) {
|
|
|
|
if t.dest.Incomplete() {
|
|
|
|
if !t.resolve(srv) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-02-27 10:39:30 +00:00
|
|
|
err := t.dial(srv, t.dest)
|
|
|
|
if err != nil {
|
2019-10-04 15:21:24 +00:00
|
|
|
srv.log.Trace("Dial error", "task", t, "err", err)
|
2018-02-27 10:39:30 +00:00
|
|
|
// Try resolving the ID of static nodes if dialing failed.
|
|
|
|
if _, ok := err.(*dialError); ok && t.flags&staticDialedConn != 0 {
|
|
|
|
if t.resolve(srv) {
|
|
|
|
t.dial(srv, t.dest)
|
|
|
|
}
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve attempts to find the current endpoint for the destination
|
|
|
|
// using discovery.
|
|
|
|
//
|
|
|
|
// Resolve operations are throttled with backoff to avoid flooding the
|
|
|
|
// discovery network with useless queries for nodes that don't exist.
|
|
|
|
// The backoff delay resets when the node is found.
|
|
|
|
func (t *dialTask) resolve(srv *Server) bool {
|
|
|
|
if srv.ntab == nil {
|
2019-10-04 15:21:24 +00:00
|
|
|
srv.log.Debug("Can't resolve node", "id", t.dest.ID, "err", "discovery is disabled")
|
2016-06-14 16:17:44 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if t.resolveDelay == 0 {
|
|
|
|
t.resolveDelay = initialResolveDelay
|
|
|
|
}
|
|
|
|
if time.Since(t.lastResolved) < t.resolveDelay {
|
|
|
|
return false
|
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
resolved := srv.ntab.Resolve(t.dest)
|
2016-06-14 16:17:44 +00:00
|
|
|
t.lastResolved = time.Now()
|
|
|
|
if resolved == nil {
|
|
|
|
t.resolveDelay *= 2
|
|
|
|
if t.resolveDelay > maxResolveDelay {
|
|
|
|
t.resolveDelay = maxResolveDelay
|
|
|
|
}
|
2019-10-04 15:21:24 +00:00
|
|
|
srv.log.Debug("Resolving node failed", "id", t.dest.ID, "newdelay", t.resolveDelay)
|
2016-06-14 16:17:44 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
// The node was found.
|
|
|
|
t.resolveDelay = initialResolveDelay
|
|
|
|
t.dest = resolved
|
2019-10-04 15:21:24 +00:00
|
|
|
srv.log.Debug("Resolved node", "id", t.dest.ID, "addr", &net.TCPAddr{IP: t.dest.IP(), Port: t.dest.TCP()})
|
2016-06-14 16:17:44 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-02-27 10:39:30 +00:00
|
|
|
type dialError struct {
|
|
|
|
error
|
|
|
|
}
|
|
|
|
|
2016-06-14 16:17:44 +00:00
|
|
|
// dial performs the actual connection attempt.
|
2018-11-14 07:03:58 +00:00
|
|
|
func (t *dialTask) dial(srv *Server, dest *enode.Node) error {
|
2017-10-17 09:58:04 +00:00
|
|
|
fd, err := srv.Dialer.Dial(dest)
|
2016-06-14 16:17:44 +00:00
|
|
|
if err != nil {
|
2018-02-27 10:39:30 +00:00
|
|
|
return &dialError{err}
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
2018-12-19 10:02:07 +00:00
|
|
|
mfd := newMeteredConn(fd, false, dest.IP())
|
2018-02-27 10:39:30 +00:00
|
|
|
return srv.SetupConn(mfd, t.flags, dest)
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *dialTask) String() string {
|
2018-11-14 07:03:58 +00:00
|
|
|
id := t.dest.ID()
|
|
|
|
return fmt.Sprintf("%v %x %v:%d", t.flags, id[:8], t.dest.IP(), t.dest.TCP())
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *discoverTask) Do(srv *Server) {
|
|
|
|
// newTasks generates a lookup task whenever dynamic dials are
|
|
|
|
// necessary. Lookups need to take some time, otherwise the
|
|
|
|
// event loop spins too fast.
|
|
|
|
next := srv.lastLookup.Add(lookupInterval)
|
|
|
|
if now := time.Now(); now.Before(next) {
|
|
|
|
time.Sleep(next.Sub(now))
|
|
|
|
}
|
|
|
|
srv.lastLookup = time.Now()
|
2018-11-14 07:03:58 +00:00
|
|
|
t.results = srv.ntab.LookupRandom()
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *discoverTask) String() string {
|
|
|
|
s := "discovery lookup"
|
|
|
|
if len(t.results) > 0 {
|
|
|
|
s += fmt.Sprintf(" (%d results)", len(t.results))
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t waitExpireTask) Do(*Server) {
|
|
|
|
time.Sleep(t.Duration)
|
|
|
|
}
|
|
|
|
func (t waitExpireTask) String() string {
|
|
|
|
return fmt.Sprintf("wait for dial hist expire (%v)", t.Duration)
|
|
|
|
}
|