| 8611 | } |
| 8612 | |
| 8613 | bool PeerManagerImpl::SendMessages(const Config &config, CNode *pto) { |
| 8614 | AssertLockHeld(g_msgproc_mutex); |
| 8615 | |
| 8616 | PeerRef peer = GetPeerRef(pto->GetId()); |
| 8617 | if (!peer) { |
| 8618 | return false; |
| 8619 | } |
| 8620 | const Consensus::Params &consensusParams = m_chainparams.GetConsensus(); |
| 8621 | |
| 8622 | // We must call MaybeDiscourageAndDisconnect first, to ensure that we'll |
| 8623 | // disconnect misbehaving peers even before the version handshake is |
| 8624 | // complete. |
| 8625 | if (MaybeDiscourageAndDisconnect(*pto, *peer)) { |
| 8626 | return true; |
| 8627 | } |
| 8628 | |
| 8629 | // Don't send anything until the version handshake is complete |
| 8630 | if (!pto->fSuccessfullyConnected || pto->fDisconnect) { |
| 8631 | return true; |
| 8632 | } |
| 8633 | |
| 8634 | const auto current_time{GetTime<std::chrono::microseconds>()}; |
| 8635 | |
| 8636 | if (pto->IsAddrFetchConn() && |
| 8637 | current_time - pto->m_connected > 10 * AVG_ADDRESS_BROADCAST_INTERVAL) { |
| 8638 | LogPrint(BCLog::NET, |
| 8639 | "addrfetch connection timeout; disconnecting peer=%d\n", |
| 8640 | pto->GetId()); |
| 8641 | pto->fDisconnect = true; |
| 8642 | return true; |
| 8643 | } |
| 8644 | |
| 8645 | MaybeSendPing(*pto, *peer, current_time); |
| 8646 | |
| 8647 | // MaybeSendPing may have marked peer for disconnection |
| 8648 | if (pto->fDisconnect) { |
| 8649 | return true; |
| 8650 | } |
| 8651 | |
| 8652 | bool sync_blocks_and_headers_from_peer = false; |
| 8653 | |
| 8654 | MaybeSendAddr(*pto, *peer, current_time); |
| 8655 | |
| 8656 | MaybeSendSendHeaders(*pto, *peer); |
| 8657 | |
| 8658 | { |
| 8659 | LOCK(cs_main); |
| 8660 | |
| 8661 | CNodeState &state = *State(pto->GetId()); |
| 8662 | |
| 8663 | // Start block sync |
| 8664 | if (m_chainman.m_best_header == nullptr) { |
| 8665 | m_chainman.m_best_header = m_chainman.ActiveChain().Tip(); |
| 8666 | } |
| 8667 | |
| 8668 | // Determine whether we might try initial headers sync or parallel |
| 8669 | // block download from this peer -- this mostly affects behavior while |
| 8670 | // in IBD (once out of IBD, we sync from all peers). |