gitlab.com/gpdionisio/tendermint@v0.34.19-dev2/p2p/pex/pex_reactor.go (about)

     1  package pex
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"sync"
     7  	"time"
     8  
     9  	"github.com/gogo/protobuf/proto"
    10  
    11  	"github.com/tendermint/tendermint/libs/cmap"
    12  	tmmath "github.com/tendermint/tendermint/libs/math"
    13  	tmrand "github.com/tendermint/tendermint/libs/rand"
    14  	"github.com/tendermint/tendermint/libs/service"
    15  	"github.com/tendermint/tendermint/p2p"
    16  	"github.com/tendermint/tendermint/p2p/conn"
    17  	tmp2p "github.com/tendermint/tendermint/proto/tendermint/p2p"
    18  )
    19  
    20  type Peer = p2p.Peer
    21  
    22  const (
    23  	// PexChannel is a channel for PEX messages
    24  	PexChannel = byte(0x00)
    25  
    26  	// over-estimate of max NetAddress size
    27  	// hexID (40) + IP (16) + Port (2) + Name (100) ...
    28  	// NOTE: dont use massive DNS name ..
    29  	maxAddressSize = 256
    30  
    31  	// NOTE: amplificaiton factor!
    32  	// small request results in up to maxMsgSize response
    33  	maxMsgSize = maxAddressSize * maxGetSelection
    34  
    35  	// ensure we have enough peers
    36  	defaultEnsurePeersPeriod = 30 * time.Second
    37  
    38  	// Seed/Crawler constants
    39  
    40  	// minTimeBetweenCrawls is a minimum time between attempts to crawl a peer.
    41  	minTimeBetweenCrawls = 2 * time.Minute
    42  
    43  	// check some peers every this
    44  	crawlPeerPeriod = 30 * time.Second
    45  
    46  	maxAttemptsToDial = 16 // ~ 35h in total (last attempt - 18h)
    47  
    48  	// if node connects to seed, it does not have any trusted peers.
    49  	// Especially in the beginning, node should have more trusted peers than
    50  	// untrusted.
    51  	biasToSelectNewPeers = 30 // 70 to select good peers
    52  
    53  	// if a peer is marked bad, it will be banned for at least this time period
    54  	defaultBanTime = 24 * time.Hour
    55  )
    56  
    57  type errMaxAttemptsToDial struct {
    58  }
    59  
    60  func (e errMaxAttemptsToDial) Error() string {
    61  	return fmt.Sprintf("reached max attempts %d to dial", maxAttemptsToDial)
    62  }
    63  
    64  type errTooEarlyToDial struct {
    65  	backoffDuration time.Duration
    66  	lastDialed      time.Time
    67  }
    68  
    69  func (e errTooEarlyToDial) Error() string {
    70  	return fmt.Sprintf(
    71  		"too early to dial (backoff duration: %d, last dialed: %v, time since: %v)",
    72  		e.backoffDuration, e.lastDialed, time.Since(e.lastDialed))
    73  }
    74  
    75  // Reactor handles PEX (peer exchange) and ensures that an
    76  // adequate number of peers are connected to the switch.
    77  //
    78  // It uses `AddrBook` (address book) to store `NetAddress`es of the peers.
    79  //
    80  // ## Preventing abuse
    81  //
    82  // Only accept pexAddrsMsg from peers we sent a corresponding pexRequestMsg too.
    83  // Only accept one pexRequestMsg every ~defaultEnsurePeersPeriod.
    84  type Reactor struct {
    85  	p2p.BaseReactor
    86  
    87  	book              AddrBook
    88  	config            *ReactorConfig
    89  	ensurePeersPeriod time.Duration // TODO: should go in the config
    90  
    91  	// maps to prevent abuse
    92  	requestsSent         *cmap.CMap // ID->struct{}: unanswered send requests
    93  	lastReceivedRequests *cmap.CMap // ID->time.Time: last time peer requested from us
    94  
    95  	seedAddrs []*p2p.NetAddress
    96  
    97  	attemptsToDial sync.Map // address (string) -> {number of attempts (int), last time dialed (time.Time)}
    98  
    99  	// seed/crawled mode fields
   100  	crawlPeerInfos map[p2p.ID]crawlPeerInfo
   101  }
   102  
   103  func (r *Reactor) minReceiveRequestInterval() time.Duration {
   104  	// NOTE: must be less than ensurePeersPeriod, otherwise we'll request
   105  	// peers too quickly from others and they'll think we're bad!
   106  	return r.ensurePeersPeriod / 3
   107  }
   108  
   109  // ReactorConfig holds reactor specific configuration data.
   110  type ReactorConfig struct {
   111  	// Seed/Crawler mode
   112  	SeedMode bool
   113  
   114  	// We want seeds to only advertise good peers. Therefore they should wait at
   115  	// least as long as we expect it to take for a peer to become good before
   116  	// disconnecting.
   117  	SeedDisconnectWaitPeriod time.Duration
   118  
   119  	// Maximum pause when redialing a persistent peer (if zero, exponential backoff is used)
   120  	PersistentPeersMaxDialPeriod time.Duration
   121  
   122  	// Seeds is a list of addresses reactor may use
   123  	// if it can't connect to peers in the addrbook.
   124  	Seeds []string
   125  }
   126  
   127  type _attemptsToDial struct {
   128  	number     int
   129  	lastDialed time.Time
   130  }
   131  
   132  // NewReactor creates new PEX reactor.
   133  func NewReactor(b AddrBook, config *ReactorConfig) *Reactor {
   134  	r := &Reactor{
   135  		book:                 b,
   136  		config:               config,
   137  		ensurePeersPeriod:    defaultEnsurePeersPeriod,
   138  		requestsSent:         cmap.NewCMap(),
   139  		lastReceivedRequests: cmap.NewCMap(),
   140  		crawlPeerInfos:       make(map[p2p.ID]crawlPeerInfo),
   141  	}
   142  	r.BaseReactor = *p2p.NewBaseReactor("PEX", r)
   143  	return r
   144  }
   145  
   146  // OnStart implements BaseService
   147  func (r *Reactor) OnStart() error {
   148  	err := r.book.Start()
   149  	if err != nil && err != service.ErrAlreadyStarted {
   150  		return err
   151  	}
   152  
   153  	numOnline, seedAddrs, err := r.checkSeeds()
   154  	if err != nil {
   155  		return err
   156  	} else if numOnline == 0 && r.book.Empty() {
   157  		return errors.New("address book is empty and couldn't resolve any seed nodes")
   158  	}
   159  
   160  	r.seedAddrs = seedAddrs
   161  
   162  	// Check if this node should run
   163  	// in seed/crawler mode
   164  	if r.config.SeedMode {
   165  		go r.crawlPeersRoutine()
   166  	} else {
   167  		go r.ensurePeersRoutine()
   168  	}
   169  	return nil
   170  }
   171  
   172  // OnStop implements BaseService
   173  func (r *Reactor) OnStop() {
   174  	if err := r.book.Stop(); err != nil {
   175  		r.Logger.Error("Error stopping address book", "err", err)
   176  	}
   177  }
   178  
   179  // GetChannels implements Reactor
   180  func (r *Reactor) GetChannels() []*conn.ChannelDescriptor {
   181  	return []*conn.ChannelDescriptor{
   182  		{
   183  			ID:                  PexChannel,
   184  			Priority:            1,
   185  			SendQueueCapacity:   10,
   186  			RecvMessageCapacity: maxMsgSize,
   187  		},
   188  	}
   189  }
   190  
   191  // AddPeer implements Reactor by adding peer to the address book (if inbound)
   192  // or by requesting more addresses (if outbound).
   193  func (r *Reactor) AddPeer(p Peer) {
   194  	if p.IsOutbound() {
   195  		// For outbound peers, the address is already in the books -
   196  		// either via DialPeersAsync or r.Receive.
   197  		// Ask it for more peers if we need.
   198  		if r.book.NeedMoreAddrs() {
   199  			r.RequestAddrs(p)
   200  		}
   201  	} else {
   202  		// inbound peer is its own source
   203  		addr, err := p.NodeInfo().NetAddress()
   204  		if err != nil {
   205  			r.Logger.Error("Failed to get peer NetAddress", "err", err, "peer", p)
   206  			return
   207  		}
   208  
   209  		// Make it explicit that addr and src are the same for an inbound peer.
   210  		src := addr
   211  
   212  		// add to book. dont RequestAddrs right away because
   213  		// we don't trust inbound as much - let ensurePeersRoutine handle it.
   214  		err = r.book.AddAddress(addr, src)
   215  		r.logErrAddrBook(err)
   216  	}
   217  }
   218  
   219  // RemovePeer implements Reactor by resetting peer's requests info.
   220  func (r *Reactor) RemovePeer(p Peer, reason interface{}) {
   221  	id := string(p.ID())
   222  	r.requestsSent.Delete(id)
   223  	r.lastReceivedRequests.Delete(id)
   224  }
   225  
   226  func (r *Reactor) logErrAddrBook(err error) {
   227  	if err != nil {
   228  		switch err.(type) {
   229  		case ErrAddrBookNilAddr:
   230  			r.Logger.Error("Failed to add new address", "err", err)
   231  		default:
   232  			// non-routable, self, full book, private, etc.
   233  			r.Logger.Debug("Failed to add new address", "err", err)
   234  		}
   235  	}
   236  }
   237  
   238  // Receive implements Reactor by handling incoming PEX messages.
   239  func (r *Reactor) Receive(chID byte, src Peer, msgBytes []byte) {
   240  	msg, err := decodeMsg(msgBytes)
   241  	if err != nil {
   242  		r.Logger.Error("Error decoding message", "src", src, "chId", chID, "err", err)
   243  		r.Switch.StopPeerForError(src, err)
   244  		return
   245  	}
   246  	r.Logger.Debug("Received message", "src", src, "chId", chID, "msg", msg)
   247  
   248  	switch msg := msg.(type) {
   249  	case *tmp2p.PexRequest:
   250  
   251  		// NOTE: this is a prime candidate for amplification attacks,
   252  		// so it's important we
   253  		// 1) restrict how frequently peers can request
   254  		// 2) limit the output size
   255  
   256  		// If we're a seed and this is an inbound peer,
   257  		// respond once and disconnect.
   258  		if r.config.SeedMode && !src.IsOutbound() {
   259  			id := string(src.ID())
   260  			v := r.lastReceivedRequests.Get(id)
   261  			if v != nil {
   262  				// FlushStop/StopPeer are already
   263  				// running in a go-routine.
   264  				return
   265  			}
   266  			r.lastReceivedRequests.Set(id, time.Now())
   267  
   268  			// Send addrs and disconnect
   269  			r.SendAddrs(src, r.book.GetSelectionWithBias(biasToSelectNewPeers))
   270  			go func() {
   271  				// In a go-routine so it doesn't block .Receive.
   272  				src.FlushStop()
   273  				r.Switch.StopPeerGracefully(src)
   274  			}()
   275  
   276  		} else {
   277  			// Check we're not receiving requests too frequently.
   278  			if err := r.receiveRequest(src); err != nil {
   279  				r.Switch.StopPeerForError(src, err)
   280  				r.book.MarkBad(src.SocketAddr(), defaultBanTime)
   281  				return
   282  			}
   283  			r.SendAddrs(src, r.book.GetSelection())
   284  		}
   285  
   286  	case *tmp2p.PexAddrs:
   287  		// If we asked for addresses, add them to the book
   288  		addrs, err := p2p.NetAddressesFromProto(msg.Addrs)
   289  		if err != nil {
   290  			r.Switch.StopPeerForError(src, err)
   291  			r.book.MarkBad(src.SocketAddr(), defaultBanTime)
   292  			return
   293  		}
   294  		err = r.ReceiveAddrs(addrs, src)
   295  		if err != nil {
   296  			r.Switch.StopPeerForError(src, err)
   297  			if err == ErrUnsolicitedList {
   298  				r.book.MarkBad(src.SocketAddr(), defaultBanTime)
   299  			}
   300  			return
   301  		}
   302  
   303  	default:
   304  		r.Logger.Error(fmt.Sprintf("Unknown message type %T", msg))
   305  	}
   306  }
   307  
   308  // enforces a minimum amount of time between requests
   309  func (r *Reactor) receiveRequest(src Peer) error {
   310  	id := string(src.ID())
   311  	v := r.lastReceivedRequests.Get(id)
   312  	if v == nil {
   313  		// initialize with empty time
   314  		lastReceived := time.Time{}
   315  		r.lastReceivedRequests.Set(id, lastReceived)
   316  		return nil
   317  	}
   318  
   319  	lastReceived := v.(time.Time)
   320  	if lastReceived.Equal(time.Time{}) {
   321  		// first time gets a free pass. then we start tracking the time
   322  		lastReceived = time.Now()
   323  		r.lastReceivedRequests.Set(id, lastReceived)
   324  		return nil
   325  	}
   326  
   327  	now := time.Now()
   328  	minInterval := r.minReceiveRequestInterval()
   329  	if now.Sub(lastReceived) < minInterval {
   330  		return fmt.Errorf(
   331  			"peer (%v) sent next PEX request too soon. lastReceived: %v, now: %v, minInterval: %v. Disconnecting",
   332  			src.ID(),
   333  			lastReceived,
   334  			now,
   335  			minInterval,
   336  		)
   337  	}
   338  	r.lastReceivedRequests.Set(id, now)
   339  	return nil
   340  }
   341  
   342  // RequestAddrs asks peer for more addresses if we do not already have a
   343  // request out for this peer.
   344  func (r *Reactor) RequestAddrs(p Peer) {
   345  	id := string(p.ID())
   346  	if r.requestsSent.Has(id) {
   347  		return
   348  	}
   349  	r.Logger.Debug("Request addrs", "from", p)
   350  	r.requestsSent.Set(id, struct{}{})
   351  	p.Send(PexChannel, mustEncode(&tmp2p.PexRequest{}))
   352  }
   353  
   354  // ReceiveAddrs adds the given addrs to the addrbook if theres an open
   355  // request for this peer and deletes the open request.
   356  // If there's no open request for the src peer, it returns an error.
   357  func (r *Reactor) ReceiveAddrs(addrs []*p2p.NetAddress, src Peer) error {
   358  	id := string(src.ID())
   359  	if !r.requestsSent.Has(id) {
   360  		return ErrUnsolicitedList
   361  	}
   362  	r.requestsSent.Delete(id)
   363  
   364  	srcAddr, err := src.NodeInfo().NetAddress()
   365  	if err != nil {
   366  		return err
   367  	}
   368  
   369  	srcIsSeed := false
   370  	for _, seedAddr := range r.seedAddrs {
   371  		if seedAddr.Equals(srcAddr) {
   372  			srcIsSeed = true
   373  			break
   374  		}
   375  	}
   376  
   377  	for _, netAddr := range addrs {
   378  		// NOTE: we check netAddr validity and routability in book#AddAddress.
   379  		err = r.book.AddAddress(netAddr, srcAddr)
   380  		if err != nil {
   381  			r.logErrAddrBook(err)
   382  			// XXX: should we be strict about incoming data and disconnect from a
   383  			// peer here too?
   384  			continue
   385  		}
   386  
   387  		// If this address came from a seed node, try to connect to it without
   388  		// waiting (#2093)
   389  		if srcIsSeed {
   390  			go func(addr *p2p.NetAddress) {
   391  				err := r.dialPeer(addr)
   392  				if err != nil {
   393  					switch err.(type) {
   394  					case errMaxAttemptsToDial, errTooEarlyToDial, p2p.ErrCurrentlyDialingOrExistingAddress:
   395  						r.Logger.Debug(err.Error(), "addr", addr)
   396  					default:
   397  						r.Logger.Debug(err.Error(), "addr", addr)
   398  					}
   399  				}
   400  			}(netAddr)
   401  		}
   402  	}
   403  
   404  	return nil
   405  }
   406  
   407  // SendAddrs sends addrs to the peer.
   408  func (r *Reactor) SendAddrs(p Peer, netAddrs []*p2p.NetAddress) {
   409  	p.Send(PexChannel, mustEncode(&tmp2p.PexAddrs{Addrs: p2p.NetAddressesToProto(netAddrs)}))
   410  }
   411  
   412  // SetEnsurePeersPeriod sets period to ensure peers connected.
   413  func (r *Reactor) SetEnsurePeersPeriod(d time.Duration) {
   414  	r.ensurePeersPeriod = d
   415  }
   416  
   417  // Ensures that sufficient peers are connected. (continuous)
   418  func (r *Reactor) ensurePeersRoutine() {
   419  	var (
   420  		seed   = tmrand.NewRand()
   421  		jitter = seed.Int63n(r.ensurePeersPeriod.Nanoseconds())
   422  	)
   423  
   424  	// Randomize first round of communication to avoid thundering herd.
   425  	// If no peers are present directly start connecting so we guarantee swift
   426  	// setup with the help of configured seeds.
   427  	if r.nodeHasSomePeersOrDialingAny() {
   428  		time.Sleep(time.Duration(jitter))
   429  	}
   430  
   431  	// fire once immediately.
   432  	// ensures we dial the seeds right away if the book is empty
   433  	r.ensurePeers()
   434  
   435  	// fire periodically
   436  	ticker := time.NewTicker(r.ensurePeersPeriod)
   437  	for {
   438  		select {
   439  		case <-ticker.C:
   440  			r.ensurePeers()
   441  		case <-r.Quit():
   442  			ticker.Stop()
   443  			return
   444  		}
   445  	}
   446  }
   447  
   448  // ensurePeers ensures that sufficient peers are connected. (once)
   449  //
   450  // heuristic that we haven't perfected yet, or, perhaps is manually edited by
   451  // the node operator. It should not be used to compute what addresses are
   452  // already connected or not.
   453  func (r *Reactor) ensurePeers() {
   454  	var (
   455  		out, in, dial = r.Switch.NumPeers()
   456  		numToDial     = r.Switch.MaxNumOutboundPeers() - (out + dial)
   457  	)
   458  	r.Logger.Info(
   459  		"Ensure peers",
   460  		"numOutPeers", out,
   461  		"numInPeers", in,
   462  		"numDialing", dial,
   463  		"numToDial", numToDial,
   464  	)
   465  
   466  	if numToDial <= 0 {
   467  		return
   468  	}
   469  
   470  	// bias to prefer more vetted peers when we have fewer connections.
   471  	// not perfect, but somewhate ensures that we prioritize connecting to more-vetted
   472  	// NOTE: range here is [10, 90]. Too high ?
   473  	newBias := tmmath.MinInt(out, 8)*10 + 10
   474  
   475  	toDial := make(map[p2p.ID]*p2p.NetAddress)
   476  	// Try maxAttempts times to pick numToDial addresses to dial
   477  	maxAttempts := numToDial * 3
   478  
   479  	for i := 0; i < maxAttempts && len(toDial) < numToDial; i++ {
   480  		try := r.book.PickAddress(newBias)
   481  		if try == nil {
   482  			continue
   483  		}
   484  		if _, selected := toDial[try.ID]; selected {
   485  			continue
   486  		}
   487  		if r.Switch.IsDialingOrExistingAddress(try) {
   488  			continue
   489  		}
   490  		// TODO: consider moving some checks from toDial into here
   491  		// so we don't even consider dialing peers that we want to wait
   492  		// before dialling again, or have dialed too many times already
   493  		toDial[try.ID] = try
   494  	}
   495  
   496  	// Dial picked addresses
   497  	for _, addr := range toDial {
   498  		go func(addr *p2p.NetAddress) {
   499  			err := r.dialPeer(addr)
   500  			if err != nil {
   501  				switch err.(type) {
   502  				case errMaxAttemptsToDial, errTooEarlyToDial:
   503  					r.Logger.Debug(err.Error(), "addr", addr)
   504  				default:
   505  					r.Logger.Debug(err.Error(), "addr", addr)
   506  				}
   507  			}
   508  		}(addr)
   509  	}
   510  
   511  	if r.book.NeedMoreAddrs() {
   512  		// Check if banned nodes can be reinstated
   513  		r.book.ReinstateBadPeers()
   514  	}
   515  
   516  	if r.book.NeedMoreAddrs() {
   517  
   518  		// 1) Pick a random peer and ask for more.
   519  		peers := r.Switch.Peers().List()
   520  		peersCount := len(peers)
   521  		if peersCount > 0 {
   522  			peer := peers[tmrand.Int()%peersCount]
   523  			r.Logger.Info("We need more addresses. Sending pexRequest to random peer", "peer", peer)
   524  			r.RequestAddrs(peer)
   525  		}
   526  
   527  		// 2) Dial seeds if we are not dialing anyone.
   528  		// This is done in addition to asking a peer for addresses to work-around
   529  		// peers not participating in PEX.
   530  		if len(toDial) == 0 {
   531  			r.Logger.Info("No addresses to dial. Falling back to seeds")
   532  			r.dialSeeds()
   533  		}
   534  	}
   535  }
   536  
   537  func (r *Reactor) dialAttemptsInfo(addr *p2p.NetAddress) (attempts int, lastDialed time.Time) {
   538  	_attempts, ok := r.attemptsToDial.Load(addr.DialString())
   539  	if !ok {
   540  		return
   541  	}
   542  	atd := _attempts.(_attemptsToDial)
   543  	return atd.number, atd.lastDialed
   544  }
   545  
   546  func (r *Reactor) dialPeer(addr *p2p.NetAddress) error {
   547  	attempts, lastDialed := r.dialAttemptsInfo(addr)
   548  	if !r.Switch.IsPeerPersistent(addr) && attempts > maxAttemptsToDial {
   549  		r.book.MarkBad(addr, defaultBanTime)
   550  		return errMaxAttemptsToDial{}
   551  	}
   552  
   553  	// exponential backoff if it's not our first attempt to dial given address
   554  	if attempts > 0 {
   555  		jitter := time.Duration(tmrand.Float64() * float64(time.Second)) // 1s == (1e9 ns)
   556  		backoffDuration := jitter + ((1 << uint(attempts)) * time.Second)
   557  		backoffDuration = r.maxBackoffDurationForPeer(addr, backoffDuration)
   558  		sinceLastDialed := time.Since(lastDialed)
   559  		if sinceLastDialed < backoffDuration {
   560  			return errTooEarlyToDial{backoffDuration, lastDialed}
   561  		}
   562  	}
   563  
   564  	err := r.Switch.DialPeerWithAddress(addr)
   565  	if err != nil {
   566  		if _, ok := err.(p2p.ErrCurrentlyDialingOrExistingAddress); ok {
   567  			return err
   568  		}
   569  
   570  		markAddrInBookBasedOnErr(addr, r.book, err)
   571  		switch err.(type) {
   572  		case p2p.ErrSwitchAuthenticationFailure:
   573  			// NOTE: addr is removed from addrbook in markAddrInBookBasedOnErr
   574  			r.attemptsToDial.Delete(addr.DialString())
   575  		default:
   576  			r.attemptsToDial.Store(addr.DialString(), _attemptsToDial{attempts + 1, time.Now()})
   577  		}
   578  		return fmt.Errorf("dialing failed (attempts: %d): %w", attempts+1, err)
   579  	}
   580  
   581  	// cleanup any history
   582  	r.attemptsToDial.Delete(addr.DialString())
   583  	return nil
   584  }
   585  
   586  // maxBackoffDurationForPeer caps the backoff duration for persistent peers.
   587  func (r *Reactor) maxBackoffDurationForPeer(addr *p2p.NetAddress, planned time.Duration) time.Duration {
   588  	if r.config.PersistentPeersMaxDialPeriod > 0 &&
   589  		planned > r.config.PersistentPeersMaxDialPeriod &&
   590  		r.Switch.IsPeerPersistent(addr) {
   591  		return r.config.PersistentPeersMaxDialPeriod
   592  	}
   593  	return planned
   594  }
   595  
   596  // checkSeeds checks that addresses are well formed.
   597  // Returns number of seeds we can connect to, along with all seeds addrs.
   598  // return err if user provided any badly formatted seed addresses.
   599  // Doesn't error if the seed node can't be reached.
   600  // numOnline returns -1 if no seed nodes were in the initial configuration.
   601  func (r *Reactor) checkSeeds() (numOnline int, netAddrs []*p2p.NetAddress, err error) {
   602  	lSeeds := len(r.config.Seeds)
   603  	if lSeeds == 0 {
   604  		return -1, nil, nil
   605  	}
   606  	netAddrs, errs := p2p.NewNetAddressStrings(r.config.Seeds)
   607  	numOnline = lSeeds - len(errs)
   608  	for _, err := range errs {
   609  		switch e := err.(type) {
   610  		case p2p.ErrNetAddressLookup:
   611  			r.Logger.Error("Connecting to seed failed", "err", e)
   612  		default:
   613  			return 0, nil, fmt.Errorf("seed node configuration has error: %w", e)
   614  		}
   615  	}
   616  	return numOnline, netAddrs, nil
   617  }
   618  
   619  // randomly dial seeds until we connect to one or exhaust them
   620  func (r *Reactor) dialSeeds() {
   621  	perm := tmrand.Perm(len(r.seedAddrs))
   622  	// perm := r.Switch.rng.Perm(lSeeds)
   623  	for _, i := range perm {
   624  		// dial a random seed
   625  		seedAddr := r.seedAddrs[i]
   626  		err := r.Switch.DialPeerWithAddress(seedAddr)
   627  
   628  		switch err.(type) {
   629  		case nil, p2p.ErrCurrentlyDialingOrExistingAddress:
   630  			return
   631  		}
   632  		r.Switch.Logger.Error("Error dialing seed", "err", err, "seed", seedAddr)
   633  	}
   634  	// do not write error message if there were no seeds specified in config
   635  	if len(r.seedAddrs) > 0 {
   636  		r.Switch.Logger.Error("Couldn't connect to any seeds")
   637  	}
   638  }
   639  
   640  // AttemptsToDial returns the number of attempts to dial specific address. It
   641  // returns 0 if never attempted or successfully connected.
   642  func (r *Reactor) AttemptsToDial(addr *p2p.NetAddress) int {
   643  	lAttempts, attempted := r.attemptsToDial.Load(addr.DialString())
   644  	if attempted {
   645  		return lAttempts.(_attemptsToDial).number
   646  	}
   647  	return 0
   648  }
   649  
   650  //----------------------------------------------------------
   651  
   652  // Explores the network searching for more peers. (continuous)
   653  // Seed/Crawler Mode causes this node to quickly disconnect
   654  // from peers, except other seed nodes.
   655  func (r *Reactor) crawlPeersRoutine() {
   656  	// If we have any seed nodes, consult them first
   657  	if len(r.seedAddrs) > 0 {
   658  		r.dialSeeds()
   659  	} else {
   660  		// Do an initial crawl
   661  		r.crawlPeers(r.book.GetSelection())
   662  	}
   663  
   664  	// Fire periodically
   665  	ticker := time.NewTicker(crawlPeerPeriod)
   666  
   667  	for {
   668  		select {
   669  		case <-ticker.C:
   670  			r.attemptDisconnects()
   671  			r.crawlPeers(r.book.GetSelection())
   672  			r.cleanupCrawlPeerInfos()
   673  		case <-r.Quit():
   674  			return
   675  		}
   676  	}
   677  }
   678  
   679  // nodeHasSomePeersOrDialingAny returns true if the node is connected to some
   680  // peers or dialing them currently.
   681  func (r *Reactor) nodeHasSomePeersOrDialingAny() bool {
   682  	out, in, dial := r.Switch.NumPeers()
   683  	return out+in+dial > 0
   684  }
   685  
   686  // crawlPeerInfo handles temporary data needed for the network crawling
   687  // performed during seed/crawler mode.
   688  type crawlPeerInfo struct {
   689  	Addr *p2p.NetAddress `json:"addr"`
   690  	// The last time we crawled the peer or attempted to do so.
   691  	LastCrawled time.Time `json:"last_crawled"`
   692  }
   693  
   694  // crawlPeers will crawl the network looking for new peer addresses.
   695  func (r *Reactor) crawlPeers(addrs []*p2p.NetAddress) {
   696  	now := time.Now()
   697  
   698  	for _, addr := range addrs {
   699  		peerInfo, ok := r.crawlPeerInfos[addr.ID]
   700  
   701  		// Do not attempt to connect with peers we recently crawled.
   702  		if ok && now.Sub(peerInfo.LastCrawled) < minTimeBetweenCrawls {
   703  			continue
   704  		}
   705  
   706  		// Record crawling attempt.
   707  		r.crawlPeerInfos[addr.ID] = crawlPeerInfo{
   708  			Addr:        addr,
   709  			LastCrawled: now,
   710  		}
   711  
   712  		err := r.dialPeer(addr)
   713  		if err != nil {
   714  			switch err.(type) {
   715  			case errMaxAttemptsToDial, errTooEarlyToDial, p2p.ErrCurrentlyDialingOrExistingAddress:
   716  				r.Logger.Debug(err.Error(), "addr", addr)
   717  			default:
   718  				r.Logger.Debug(err.Error(), "addr", addr)
   719  			}
   720  			continue
   721  		}
   722  
   723  		peer := r.Switch.Peers().Get(addr.ID)
   724  		if peer != nil {
   725  			r.RequestAddrs(peer)
   726  		}
   727  	}
   728  }
   729  
   730  func (r *Reactor) cleanupCrawlPeerInfos() {
   731  	for id, info := range r.crawlPeerInfos {
   732  		// If we did not crawl a peer for 24 hours, it means the peer was removed
   733  		// from the addrbook => remove
   734  		//
   735  		// 10000 addresses / maxGetSelection = 40 cycles to get all addresses in
   736  		// the ideal case,
   737  		// 40 * crawlPeerPeriod ~ 20 minutes
   738  		if time.Since(info.LastCrawled) > 24*time.Hour {
   739  			delete(r.crawlPeerInfos, id)
   740  		}
   741  	}
   742  }
   743  
   744  // attemptDisconnects checks if we've been with each peer long enough to disconnect
   745  func (r *Reactor) attemptDisconnects() {
   746  	for _, peer := range r.Switch.Peers().List() {
   747  		if peer.Status().Duration < r.config.SeedDisconnectWaitPeriod {
   748  			continue
   749  		}
   750  		if peer.IsPersistent() {
   751  			continue
   752  		}
   753  		r.Switch.StopPeerGracefully(peer)
   754  	}
   755  }
   756  
   757  func markAddrInBookBasedOnErr(addr *p2p.NetAddress, book AddrBook, err error) {
   758  	// TODO: detect more "bad peer" scenarios
   759  	switch err.(type) {
   760  	case p2p.ErrSwitchAuthenticationFailure:
   761  		book.MarkBad(addr, defaultBanTime)
   762  	default:
   763  		book.MarkAttempt(addr)
   764  	}
   765  }
   766  
   767  //-----------------------------------------------------------------------------
   768  // Messages
   769  
   770  // mustEncode proto encodes a tmp2p.Message
   771  func mustEncode(pb proto.Message) []byte {
   772  	msg := tmp2p.Message{}
   773  	switch pb := pb.(type) {
   774  	case *tmp2p.PexRequest:
   775  		msg.Sum = &tmp2p.Message_PexRequest{PexRequest: pb}
   776  	case *tmp2p.PexAddrs:
   777  		msg.Sum = &tmp2p.Message_PexAddrs{PexAddrs: pb}
   778  	default:
   779  		panic(fmt.Sprintf("Unknown message type %T", pb))
   780  	}
   781  
   782  	bz, err := msg.Marshal()
   783  	if err != nil {
   784  		panic(fmt.Errorf("unable to marshal %T: %w", pb, err))
   785  	}
   786  	return bz
   787  }
   788  
   789  func decodeMsg(bz []byte) (proto.Message, error) {
   790  	pb := &tmp2p.Message{}
   791  
   792  	err := pb.Unmarshal(bz)
   793  	if err != nil {
   794  		return nil, err
   795  	}
   796  
   797  	switch msg := pb.Sum.(type) {
   798  	case *tmp2p.Message_PexRequest:
   799  		return msg.PexRequest, nil
   800  	case *tmp2p.Message_PexAddrs:
   801  		return msg.PexAddrs, nil
   802  	default:
   803  		return nil, fmt.Errorf("unknown message: %T", msg)
   804  	}
   805  }