| 2808 | } |
| 2809 | |
| 2810 | std::optional<std::string> |
| 2811 | PeerManagerImpl::FetchBlock(const Config &config, NodeId peer_id, |
| 2812 | const CBlockIndex &block_index) { |
| 2813 | if (m_chainman.m_blockman.LoadingBlocks()) { |
| 2814 | return "Loading blocks ..."; |
| 2815 | } |
| 2816 | |
| 2817 | LOCK(cs_main); |
| 2818 | |
| 2819 | // Ensure this peer exists and hasn't been disconnected |
| 2820 | CNodeState *state = State(peer_id); |
| 2821 | if (state == nullptr) { |
| 2822 | return "Peer does not exist"; |
| 2823 | } |
| 2824 | |
| 2825 | // Forget about all prior requests |
| 2826 | RemoveBlockRequest(block_index.GetBlockHash(), std::nullopt); |
| 2827 | |
| 2828 | // Mark block as in-flight |
| 2829 | if (!BlockRequested(config, peer_id, block_index)) { |
| 2830 | return "Already requested from this peer"; |
| 2831 | } |
| 2832 | |
| 2833 | // Construct message to request the block |
| 2834 | const BlockHash &hash{block_index.GetBlockHash()}; |
| 2835 | const std::vector<CInv> invs{CInv(MSG_BLOCK, hash)}; |
| 2836 | |
| 2837 | // Send block request message to the peer |
| 2838 | if (!m_connman.ForNode(peer_id, [this, &invs](CNode *node) { |
| 2839 | this->MakeAndPushMessage(*node, NetMsgType::GETDATA, invs); |
| 2840 | return true; |
| 2841 | })) { |
| 2842 | return "Node not fully connected"; |
| 2843 | } |
| 2844 | |
| 2845 | LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n", hash.ToString(), |
| 2846 | peer_id); |
| 2847 | return std::nullopt; |
| 2848 | } |
| 2849 | |
| 2850 | std::unique_ptr<PeerManager> |
| 2851 | PeerManager::make(CConnman &connman, AddrMan &addrman, BanMan *banman, |
no test coverage detected