github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/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 r.book.Stop() 175 } 176 177 // GetChannels implements Reactor 178 func (r *Reactor) GetChannels() []*conn.ChannelDescriptor { 179 return []*conn.ChannelDescriptor{ 180 { 181 ID: PexChannel, 182 Priority: 1, 183 SendQueueCapacity: 10, 184 RecvMessageCapacity: maxMsgSize, 185 }, 186 } 187 } 188 189 // AddPeer implements Reactor by adding peer to the address book (if inbound) 190 // or by requesting more addresses (if outbound). 191 func (r *Reactor) AddPeer(p Peer) { 192 if p.IsOutbound() { 193 // For outbound peers, the address is already in the books - 194 // either via DialPeersAsync or r.Receive. 195 // Ask it for more peers if we need. 196 if r.book.NeedMoreAddrs() { 197 r.RequestAddrs(p) 198 } 199 } else { 200 // inbound peer is its own source 201 addr, err := p.NodeInfo().NetAddress() 202 if err != nil { 203 r.Logger.Error("Failed to get peer NetAddress", "err", err, "peer", p) 204 return 205 } 206 207 // Make it explicit that addr and src are the same for an inbound peer. 208 src := addr 209 210 // add to book. dont RequestAddrs right away because 211 // we don't trust inbound as much - let ensurePeersRoutine handle it. 212 err = r.book.AddAddress(addr, src) 213 r.logErrAddrBook(err) 214 } 215 } 216 217 // RemovePeer implements Reactor by resetting peer's requests info. 218 func (r *Reactor) RemovePeer(p Peer, reason interface{}) { 219 id := string(p.ID()) 220 r.requestsSent.Delete(id) 221 r.lastReceivedRequests.Delete(id) 222 } 223 224 func (r *Reactor) logErrAddrBook(err error) { 225 if err != nil { 226 switch err.(type) { 227 case ErrAddrBookNilAddr: 228 r.Logger.Error("Failed to add new address", "err", err) 229 default: 230 // non-routable, self, full book, private, etc. 231 r.Logger.Debug("Failed to add new address", "err", err) 232 } 233 } 234 } 235 236 // Receive implements Reactor by handling incoming PEX messages. 237 func (r *Reactor) Receive(chID byte, src Peer, msgBytes []byte) { 238 msg, err := decodeMsg(msgBytes) 239 if err != nil { 240 r.Logger.Error("Error decoding message", "src", src, "chId", chID, "msg", msg, "err", err, "bytes", msgBytes) 241 r.Switch.StopPeerForError(src, err) 242 return 243 } 244 r.Logger.Debug("Received message", "src", src, "chId", chID, "msg", msg) 245 246 switch msg := msg.(type) { 247 case *tmp2p.PexRequest: 248 249 // NOTE: this is a prime candidate for amplification attacks, 250 // so it's important we 251 // 1) restrict how frequently peers can request 252 // 2) limit the output size 253 254 // If we're a seed and this is an inbound peer, 255 // respond once and disconnect. 256 if r.config.SeedMode && !src.IsOutbound() { 257 id := string(src.ID()) 258 v := r.lastReceivedRequests.Get(id) 259 if v != nil { 260 // FlushStop/StopPeer are already 261 // running in a go-routine. 262 return 263 } 264 r.lastReceivedRequests.Set(id, time.Now()) 265 266 // Send addrs and disconnect 267 r.SendAddrs(src, r.book.GetSelectionWithBias(biasToSelectNewPeers)) 268 go func() { 269 // In a go-routine so it doesn't block .Receive. 270 src.FlushStop() 271 r.Switch.StopPeerGracefully(src) 272 }() 273 274 } else { 275 // Check we're not receiving requests too frequently. 276 if err := r.receiveRequest(src); err != nil { 277 r.Switch.StopPeerForError(src, err) 278 r.book.MarkBad(src.SocketAddr(), defaultBanTime) 279 return 280 } 281 r.SendAddrs(src, r.book.GetSelection()) 282 } 283 284 case *tmp2p.PexAddrs: 285 // If we asked for addresses, add them to the book 286 addrs, err := p2p.NetAddressesFromProto(msg.Addrs) 287 if err != nil { 288 r.Switch.StopPeerForError(src, err) 289 r.book.MarkBad(src.SocketAddr(), defaultBanTime) 290 return 291 } 292 err = r.ReceiveAddrs(addrs, src) 293 if err != nil { 294 r.Switch.StopPeerForError(src, err) 295 if err == ErrUnsolicitedList { 296 r.book.MarkBad(src.SocketAddr(), defaultBanTime) 297 } 298 return 299 } 300 301 default: 302 r.Logger.Error(fmt.Sprintf("Unknown message type %T", msg)) 303 } 304 } 305 306 // enforces a minimum amount of time between requests 307 func (r *Reactor) receiveRequest(src Peer) error { 308 id := string(src.ID()) 309 v := r.lastReceivedRequests.Get(id) 310 if v == nil { 311 // initialize with empty time 312 lastReceived := time.Time{} 313 r.lastReceivedRequests.Set(id, lastReceived) 314 return nil 315 } 316 317 lastReceived := v.(time.Time) 318 if lastReceived.Equal(time.Time{}) { 319 // first time gets a free pass. then we start tracking the time 320 lastReceived = time.Now() 321 r.lastReceivedRequests.Set(id, lastReceived) 322 return nil 323 } 324 325 now := time.Now() 326 minInterval := r.minReceiveRequestInterval() 327 if now.Sub(lastReceived) < minInterval { 328 return fmt.Errorf( 329 "peer (%v) sent next PEX request too soon. lastReceived: %v, now: %v, minInterval: %v. Disconnecting", 330 src.ID(), 331 lastReceived, 332 now, 333 minInterval, 334 ) 335 } 336 r.lastReceivedRequests.Set(id, now) 337 return nil 338 } 339 340 // RequestAddrs asks peer for more addresses if we do not already have a 341 // request out for this peer. 342 func (r *Reactor) RequestAddrs(p Peer) { 343 id := string(p.ID()) 344 if r.requestsSent.Has(id) { 345 return 346 } 347 r.Logger.Debug("Request addrs", "from", p) 348 r.requestsSent.Set(id, struct{}{}) 349 p.Send(PexChannel, mustEncode(&tmp2p.PexRequest{})) 350 } 351 352 // ReceiveAddrs adds the given addrs to the addrbook if theres an open 353 // request for this peer and deletes the open request. 354 // If there's no open request for the src peer, it returns an error. 355 func (r *Reactor) ReceiveAddrs(addrs []*p2p.NetAddress, src Peer) error { 356 id := string(src.ID()) 357 if !r.requestsSent.Has(id) { 358 return ErrUnsolicitedList 359 } 360 r.requestsSent.Delete(id) 361 362 srcAddr, err := src.NodeInfo().NetAddress() 363 if err != nil { 364 return err 365 } 366 367 srcIsSeed := false 368 for _, seedAddr := range r.seedAddrs { 369 if seedAddr.Equals(srcAddr) { 370 srcIsSeed = true 371 break 372 } 373 } 374 375 for _, netAddr := range addrs { 376 // NOTE: we check netAddr validity and routability in book#AddAddress. 377 err = r.book.AddAddress(netAddr, srcAddr) 378 if err != nil { 379 r.logErrAddrBook(err) 380 // XXX: should we be strict about incoming data and disconnect from a 381 // peer here too? 382 continue 383 } 384 385 // If this address came from a seed node, try to connect to it without 386 // waiting (#2093) 387 if srcIsSeed { 388 r.Logger.Info("Will dial address, which came from seed", "addr", netAddr, "seed", srcAddr) 389 go func(addr *p2p.NetAddress) { 390 err := r.dialPeer(addr) 391 if err != nil { 392 switch err.(type) { 393 case errMaxAttemptsToDial, errTooEarlyToDial, p2p.ErrCurrentlyDialingOrExistingAddress: 394 r.Logger.Debug(err.Error(), "addr", addr) 395 default: 396 r.Logger.Error(err.Error(), "addr", addr) 397 } 398 } 399 }(netAddr) 400 } 401 } 402 403 return nil 404 } 405 406 // SendAddrs sends addrs to the peer. 407 func (r *Reactor) SendAddrs(p Peer, netAddrs []*p2p.NetAddress) { 408 p.Send(PexChannel, mustEncode(&tmp2p.PexAddrs{Addrs: p2p.NetAddressesToProto(netAddrs)})) 409 } 410 411 // SetEnsurePeersPeriod sets period to ensure peers connected. 412 func (r *Reactor) SetEnsurePeersPeriod(d time.Duration) { 413 r.ensurePeersPeriod = d 414 } 415 416 // Ensures that sufficient peers are connected. (continuous) 417 func (r *Reactor) ensurePeersRoutine() { 418 var ( 419 seed = tmrand.NewRand() 420 jitter = seed.Int63n(r.ensurePeersPeriod.Nanoseconds()) 421 ) 422 423 // Randomize first round of communication to avoid thundering herd. 424 // If no peers are present directly start connecting so we guarantee swift 425 // setup with the help of configured seeds. 426 if r.nodeHasSomePeersOrDialingAny() { 427 time.Sleep(time.Duration(jitter)) 428 } 429 430 // fire once immediately. 431 // ensures we dial the seeds right away if the book is empty 432 r.ensurePeers() 433 434 // fire periodically 435 ticker := time.NewTicker(r.ensurePeersPeriod) 436 for { 437 select { 438 case <-ticker.C: 439 r.ensurePeers() 440 case <-r.Quit(): 441 ticker.Stop() 442 return 443 } 444 } 445 } 446 447 // ensurePeers ensures that sufficient peers are connected. (once) 448 // 449 // heuristic that we haven't perfected yet, or, perhaps is manually edited by 450 // the node operator. It should not be used to compute what addresses are 451 // already connected or not. 452 func (r *Reactor) ensurePeers() { 453 var ( 454 out, in, dial = r.Switch.NumPeers() 455 numToDial = r.Switch.MaxNumOutboundPeers() - (out + dial) 456 ) 457 r.Logger.Info( 458 "Ensure peers", 459 "numOutPeers", out, 460 "numInPeers", in, 461 "numDialing", dial, 462 "numToDial", numToDial, 463 ) 464 465 if numToDial <= 0 { 466 return 467 } 468 469 // bias to prefer more vetted peers when we have fewer connections. 470 // not perfect, but somewhate ensures that we prioritize connecting to more-vetted 471 // NOTE: range here is [10, 90]. Too high ? 472 newBias := tmmath.MinInt(out, 8)*10 + 10 473 474 toDial := make(map[p2p.ID]*p2p.NetAddress) 475 // Try maxAttempts times to pick numToDial addresses to dial 476 maxAttempts := numToDial * 3 477 478 for i := 0; i < maxAttempts && len(toDial) < numToDial; i++ { 479 try := r.book.PickAddress(newBias) 480 if try == nil { 481 continue 482 } 483 if _, selected := toDial[try.ID]; selected { 484 continue 485 } 486 if r.Switch.IsDialingOrExistingAddress(try) { 487 continue 488 } 489 // TODO: consider moving some checks from toDial into here 490 // so we don't even consider dialing peers that we want to wait 491 // before dialling again, or have dialed too many times already 492 r.Logger.Info("Will dial address", "addr", try) 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.Error(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.Error(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 := proto.Marshal(&msg) 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 err := proto.Unmarshal(bz, pb) 792 if err != nil { 793 return nil, err 794 } 795 switch msg := pb.Sum.(type) { 796 case *tmp2p.Message_PexRequest: 797 return msg.PexRequest, nil 798 case *tmp2p.Message_PexAddrs: 799 return msg.PexAddrs, nil 800 default: 801 return nil, fmt.Errorf("unknown message: %T", msg) 802 } 803 }