github.com/DFWallet/tendermint-cosmos@v0.0.2/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/DFWallet/tendermint-cosmos/libs/cmap"
    12  	tmmath "github.com/DFWallet/tendermint-cosmos/libs/math"
    13  	tmrand "github.com/DFWallet/tendermint-cosmos/libs/rand"
    14  	"github.com/DFWallet/tendermint-cosmos/libs/service"
    15  	"github.com/DFWallet/tendermint-cosmos/p2p"
    16  	"github.com/DFWallet/tendermint-cosmos/p2p/conn"
    17  	tmp2p "github.com/DFWallet/tendermint-cosmos/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  			r.Logger.Info("Will dial address, which came from seed", "addr", netAddr, "seed", srcAddr)
   391  			go func(addr *p2p.NetAddress) {
   392  				err := r.dialPeer(addr)
   393  				if err != nil {
   394  					switch err.(type) {
   395  					case errMaxAttemptsToDial, errTooEarlyToDial, p2p.ErrCurrentlyDialingOrExistingAddress:
   396  						r.Logger.Debug(err.Error(), "addr", addr)
   397  					default:
   398  						r.Logger.Error(err.Error(), "addr", addr)
   399  					}
   400  				}
   401  			}(netAddr)
   402  		}
   403  	}
   404  
   405  	return nil
   406  }
   407  
   408  // SendAddrs sends addrs to the peer.
   409  func (r *Reactor) SendAddrs(p Peer, netAddrs []*p2p.NetAddress) {
   410  	p.Send(PexChannel, mustEncode(&tmp2p.PexAddrs{Addrs: p2p.NetAddressesToProto(netAddrs)}))
   411  }
   412  
   413  // SetEnsurePeersPeriod sets period to ensure peers connected.
   414  func (r *Reactor) SetEnsurePeersPeriod(d time.Duration) {
   415  	r.ensurePeersPeriod = d
   416  }
   417  
   418  // Ensures that sufficient peers are connected. (continuous)
   419  func (r *Reactor) ensurePeersRoutine() {
   420  	var (
   421  		seed   = tmrand.NewRand()
   422  		jitter = seed.Int63n(r.ensurePeersPeriod.Nanoseconds())
   423  	)
   424  
   425  	// Randomize first round of communication to avoid thundering herd.
   426  	// If no peers are present directly start connecting so we guarantee swift
   427  	// setup with the help of configured seeds.
   428  	if r.nodeHasSomePeersOrDialingAny() {
   429  		time.Sleep(time.Duration(jitter))
   430  	}
   431  
   432  	// fire once immediately.
   433  	// ensures we dial the seeds right away if the book is empty
   434  	r.ensurePeers()
   435  
   436  	// fire periodically
   437  	ticker := time.NewTicker(r.ensurePeersPeriod)
   438  	for {
   439  		select {
   440  		case <-ticker.C:
   441  			r.ensurePeers()
   442  		case <-r.Quit():
   443  			ticker.Stop()
   444  			return
   445  		}
   446  	}
   447  }
   448  
   449  // ensurePeers ensures that sufficient peers are connected. (once)
   450  //
   451  // heuristic that we haven't perfected yet, or, perhaps is manually edited by
   452  // the node operator. It should not be used to compute what addresses are
   453  // already connected or not.
   454  func (r *Reactor) ensurePeers() {
   455  	var (
   456  		out, in, dial = r.Switch.NumPeers()
   457  		numToDial     = r.Switch.MaxNumOutboundPeers() - (out + dial)
   458  	)
   459  	r.Logger.Info(
   460  		"Ensure peers",
   461  		"numOutPeers", out,
   462  		"numInPeers", in,
   463  		"numDialing", dial,
   464  		"numToDial", numToDial,
   465  	)
   466  
   467  	if numToDial <= 0 {
   468  		return
   469  	}
   470  
   471  	// bias to prefer more vetted peers when we have fewer connections.
   472  	// not perfect, but somewhate ensures that we prioritize connecting to more-vetted
   473  	// NOTE: range here is [10, 90]. Too high ?
   474  	newBias := tmmath.MinInt(out, 8)*10 + 10
   475  
   476  	toDial := make(map[p2p.ID]*p2p.NetAddress)
   477  	// Try maxAttempts times to pick numToDial addresses to dial
   478  	maxAttempts := numToDial * 3
   479  
   480  	for i := 0; i < maxAttempts && len(toDial) < numToDial; i++ {
   481  		try := r.book.PickAddress(newBias)
   482  		if try == nil {
   483  			continue
   484  		}
   485  		if _, selected := toDial[try.ID]; selected {
   486  			continue
   487  		}
   488  		if r.Switch.IsDialingOrExistingAddress(try) {
   489  			continue
   490  		}
   491  		// TODO: consider moving some checks from toDial into here
   492  		// so we don't even consider dialing peers that we want to wait
   493  		// before dialling again, or have dialed too many times already
   494  		r.Logger.Info("Will dial address", "addr", try)
   495  		toDial[try.ID] = try
   496  	}
   497  
   498  	// Dial picked addresses
   499  	for _, addr := range toDial {
   500  		go func(addr *p2p.NetAddress) {
   501  			err := r.dialPeer(addr)
   502  			if err != nil {
   503  				switch err.(type) {
   504  				case errMaxAttemptsToDial, errTooEarlyToDial:
   505  					r.Logger.Debug(err.Error(), "addr", addr)
   506  				default:
   507  					r.Logger.Error(err.Error(), "addr", addr)
   508  				}
   509  			}
   510  		}(addr)
   511  	}
   512  
   513  	if r.book.NeedMoreAddrs() {
   514  		// Check if banned nodes can be reinstated
   515  		r.book.ReinstateBadPeers()
   516  	}
   517  
   518  	if r.book.NeedMoreAddrs() {
   519  
   520  		// 1) Pick a random peer and ask for more.
   521  		peers := r.Switch.Peers().List()
   522  		peersCount := len(peers)
   523  		if peersCount > 0 {
   524  			peer := peers[tmrand.Int()%peersCount]
   525  			r.Logger.Info("We need more addresses. Sending pexRequest to random peer", "peer", peer)
   526  			r.RequestAddrs(peer)
   527  		}
   528  
   529  		// 2) Dial seeds if we are not dialing anyone.
   530  		// This is done in addition to asking a peer for addresses to work-around
   531  		// peers not participating in PEX.
   532  		if len(toDial) == 0 {
   533  			r.Logger.Info("No addresses to dial. Falling back to seeds")
   534  			r.dialSeeds()
   535  		}
   536  	}
   537  }
   538  
   539  func (r *Reactor) dialAttemptsInfo(addr *p2p.NetAddress) (attempts int, lastDialed time.Time) {
   540  	_attempts, ok := r.attemptsToDial.Load(addr.DialString())
   541  	if !ok {
   542  		return
   543  	}
   544  	atd := _attempts.(_attemptsToDial)
   545  	return atd.number, atd.lastDialed
   546  }
   547  
   548  func (r *Reactor) dialPeer(addr *p2p.NetAddress) error {
   549  	attempts, lastDialed := r.dialAttemptsInfo(addr)
   550  	if !r.Switch.IsPeerPersistent(addr) && attempts > maxAttemptsToDial {
   551  		r.book.MarkBad(addr, defaultBanTime)
   552  		return errMaxAttemptsToDial{}
   553  	}
   554  
   555  	// exponential backoff if it's not our first attempt to dial given address
   556  	if attempts > 0 {
   557  		jitter := time.Duration(tmrand.Float64() * float64(time.Second)) // 1s == (1e9 ns)
   558  		backoffDuration := jitter + ((1 << uint(attempts)) * time.Second)
   559  		backoffDuration = r.maxBackoffDurationForPeer(addr, backoffDuration)
   560  		sinceLastDialed := time.Since(lastDialed)
   561  		if sinceLastDialed < backoffDuration {
   562  			return errTooEarlyToDial{backoffDuration, lastDialed}
   563  		}
   564  	}
   565  
   566  	err := r.Switch.DialPeerWithAddress(addr)
   567  	if err != nil {
   568  		if _, ok := err.(p2p.ErrCurrentlyDialingOrExistingAddress); ok {
   569  			return err
   570  		}
   571  
   572  		markAddrInBookBasedOnErr(addr, r.book, err)
   573  		switch err.(type) {
   574  		case p2p.ErrSwitchAuthenticationFailure:
   575  			// NOTE: addr is removed from addrbook in markAddrInBookBasedOnErr
   576  			r.attemptsToDial.Delete(addr.DialString())
   577  		default:
   578  			r.attemptsToDial.Store(addr.DialString(), _attemptsToDial{attempts + 1, time.Now()})
   579  		}
   580  		return fmt.Errorf("dialing failed (attempts: %d): %w", attempts+1, err)
   581  	}
   582  
   583  	// cleanup any history
   584  	r.attemptsToDial.Delete(addr.DialString())
   585  	return nil
   586  }
   587  
   588  // maxBackoffDurationForPeer caps the backoff duration for persistent peers.
   589  func (r *Reactor) maxBackoffDurationForPeer(addr *p2p.NetAddress, planned time.Duration) time.Duration {
   590  	if r.config.PersistentPeersMaxDialPeriod > 0 &&
   591  		planned > r.config.PersistentPeersMaxDialPeriod &&
   592  		r.Switch.IsPeerPersistent(addr) {
   593  		return r.config.PersistentPeersMaxDialPeriod
   594  	}
   595  	return planned
   596  }
   597  
   598  // checkSeeds checks that addresses are well formed.
   599  // Returns number of seeds we can connect to, along with all seeds addrs.
   600  // return err if user provided any badly formatted seed addresses.
   601  // Doesn't error if the seed node can't be reached.
   602  // numOnline returns -1 if no seed nodes were in the initial configuration.
   603  func (r *Reactor) checkSeeds() (numOnline int, netAddrs []*p2p.NetAddress, err error) {
   604  	lSeeds := len(r.config.Seeds)
   605  	if lSeeds == 0 {
   606  		return -1, nil, nil
   607  	}
   608  	netAddrs, errs := p2p.NewNetAddressStrings(r.config.Seeds)
   609  	numOnline = lSeeds - len(errs)
   610  	for _, err := range errs {
   611  		switch e := err.(type) {
   612  		case p2p.ErrNetAddressLookup:
   613  			r.Logger.Error("Connecting to seed failed", "err", e)
   614  		default:
   615  			return 0, nil, fmt.Errorf("seed node configuration has error: %w", e)
   616  		}
   617  	}
   618  	return numOnline, netAddrs, nil
   619  }
   620  
   621  // randomly dial seeds until we connect to one or exhaust them
   622  func (r *Reactor) dialSeeds() {
   623  	perm := tmrand.Perm(len(r.seedAddrs))
   624  	// perm := r.Switch.rng.Perm(lSeeds)
   625  	for _, i := range perm {
   626  		// dial a random seed
   627  		seedAddr := r.seedAddrs[i]
   628  		err := r.Switch.DialPeerWithAddress(seedAddr)
   629  
   630  		switch err.(type) {
   631  		case nil, p2p.ErrCurrentlyDialingOrExistingAddress:
   632  			return
   633  		}
   634  		r.Switch.Logger.Error("Error dialing seed", "err", err, "seed", seedAddr)
   635  	}
   636  	// do not write error message if there were no seeds specified in config
   637  	if len(r.seedAddrs) > 0 {
   638  		r.Switch.Logger.Error("Couldn't connect to any seeds")
   639  	}
   640  }
   641  
   642  // AttemptsToDial returns the number of attempts to dial specific address. It
   643  // returns 0 if never attempted or successfully connected.
   644  func (r *Reactor) AttemptsToDial(addr *p2p.NetAddress) int {
   645  	lAttempts, attempted := r.attemptsToDial.Load(addr.DialString())
   646  	if attempted {
   647  		return lAttempts.(_attemptsToDial).number
   648  	}
   649  	return 0
   650  }
   651  
   652  //----------------------------------------------------------
   653  
   654  // Explores the network searching for more peers. (continuous)
   655  // Seed/Crawler Mode causes this node to quickly disconnect
   656  // from peers, except other seed nodes.
   657  func (r *Reactor) crawlPeersRoutine() {
   658  	// If we have any seed nodes, consult them first
   659  	if len(r.seedAddrs) > 0 {
   660  		r.dialSeeds()
   661  	} else {
   662  		// Do an initial crawl
   663  		r.crawlPeers(r.book.GetSelection())
   664  	}
   665  
   666  	// Fire periodically
   667  	ticker := time.NewTicker(crawlPeerPeriod)
   668  
   669  	for {
   670  		select {
   671  		case <-ticker.C:
   672  			r.attemptDisconnects()
   673  			r.crawlPeers(r.book.GetSelection())
   674  			r.cleanupCrawlPeerInfos()
   675  		case <-r.Quit():
   676  			return
   677  		}
   678  	}
   679  }
   680  
   681  // nodeHasSomePeersOrDialingAny returns true if the node is connected to some
   682  // peers or dialing them currently.
   683  func (r *Reactor) nodeHasSomePeersOrDialingAny() bool {
   684  	out, in, dial := r.Switch.NumPeers()
   685  	return out+in+dial > 0
   686  }
   687  
   688  // crawlPeerInfo handles temporary data needed for the network crawling
   689  // performed during seed/crawler mode.
   690  type crawlPeerInfo struct {
   691  	Addr *p2p.NetAddress `json:"addr"`
   692  	// The last time we crawled the peer or attempted to do so.
   693  	LastCrawled time.Time `json:"last_crawled"`
   694  }
   695  
   696  // crawlPeers will crawl the network looking for new peer addresses.
   697  func (r *Reactor) crawlPeers(addrs []*p2p.NetAddress) {
   698  	now := time.Now()
   699  
   700  	for _, addr := range addrs {
   701  		peerInfo, ok := r.crawlPeerInfos[addr.ID]
   702  
   703  		// Do not attempt to connect with peers we recently crawled.
   704  		if ok && now.Sub(peerInfo.LastCrawled) < minTimeBetweenCrawls {
   705  			continue
   706  		}
   707  
   708  		// Record crawling attempt.
   709  		r.crawlPeerInfos[addr.ID] = crawlPeerInfo{
   710  			Addr:        addr,
   711  			LastCrawled: now,
   712  		}
   713  
   714  		err := r.dialPeer(addr)
   715  		if err != nil {
   716  			switch err.(type) {
   717  			case errMaxAttemptsToDial, errTooEarlyToDial, p2p.ErrCurrentlyDialingOrExistingAddress:
   718  				r.Logger.Debug(err.Error(), "addr", addr)
   719  			default:
   720  				r.Logger.Error(err.Error(), "addr", addr)
   721  			}
   722  			continue
   723  		}
   724  
   725  		peer := r.Switch.Peers().Get(addr.ID)
   726  		if peer != nil {
   727  			r.RequestAddrs(peer)
   728  		}
   729  	}
   730  }
   731  
   732  func (r *Reactor) cleanupCrawlPeerInfos() {
   733  	for id, info := range r.crawlPeerInfos {
   734  		// If we did not crawl a peer for 24 hours, it means the peer was removed
   735  		// from the addrbook => remove
   736  		//
   737  		// 10000 addresses / maxGetSelection = 40 cycles to get all addresses in
   738  		// the ideal case,
   739  		// 40 * crawlPeerPeriod ~ 20 minutes
   740  		if time.Since(info.LastCrawled) > 24*time.Hour {
   741  			delete(r.crawlPeerInfos, id)
   742  		}
   743  	}
   744  }
   745  
   746  // attemptDisconnects checks if we've been with each peer long enough to disconnect
   747  func (r *Reactor) attemptDisconnects() {
   748  	for _, peer := range r.Switch.Peers().List() {
   749  		if peer.Status().Duration < r.config.SeedDisconnectWaitPeriod {
   750  			continue
   751  		}
   752  		if peer.IsPersistent() {
   753  			continue
   754  		}
   755  		r.Switch.StopPeerGracefully(peer)
   756  	}
   757  }
   758  
   759  func markAddrInBookBasedOnErr(addr *p2p.NetAddress, book AddrBook, err error) {
   760  	// TODO: detect more "bad peer" scenarios
   761  	switch err.(type) {
   762  	case p2p.ErrSwitchAuthenticationFailure:
   763  		book.MarkBad(addr, defaultBanTime)
   764  	default:
   765  		book.MarkAttempt(addr)
   766  	}
   767  }
   768  
   769  //-----------------------------------------------------------------------------
   770  // Messages
   771  
   772  // mustEncode proto encodes a tmp2p.Message
   773  func mustEncode(pb proto.Message) []byte {
   774  	msg := tmp2p.Message{}
   775  	switch pb := pb.(type) {
   776  	case *tmp2p.PexRequest:
   777  		msg.Sum = &tmp2p.Message_PexRequest{PexRequest: pb}
   778  	case *tmp2p.PexAddrs:
   779  		msg.Sum = &tmp2p.Message_PexAddrs{PexAddrs: pb}
   780  	default:
   781  		panic(fmt.Sprintf("Unknown message type %T", pb))
   782  	}
   783  
   784  	bz, err := msg.Marshal()
   785  	if err != nil {
   786  		panic(fmt.Errorf("unable to marshal %T: %w", pb, err))
   787  	}
   788  	return bz
   789  }
   790  
   791  func decodeMsg(bz []byte) (proto.Message, error) {
   792  	pb := &tmp2p.Message{}
   793  
   794  	err := pb.Unmarshal(bz)
   795  	if err != nil {
   796  		return nil, err
   797  	}
   798  
   799  	switch msg := pb.Sum.(type) {
   800  	case *tmp2p.Message_PexRequest:
   801  		return msg.PexRequest, nil
   802  	case *tmp2p.Message_PexAddrs:
   803  		return msg.PexAddrs, nil
   804  	default:
   805  		return nil, fmt.Errorf("unknown message: %T", msg)
   806  	}
   807  }