MCPcopy Create free account
hub / github.com/Bitcoin-ABC/bitcoin-abc / FetchBlock

Method FetchBlock

src/net_processing.cpp:2810–2848  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2808}
2809
2810std::optional<std::string>
2811PeerManagerImpl::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
2850std::unique_ptr<PeerManager>
2851PeerManager::make(CConnman &connman, AddrMan &addrman, BanMan *banman,

Callers 1

getblockfrompeerFunction · 0.80

Calls 7

MakeAndPushMessageMethod · 0.95
LoadingBlocksMethod · 0.80
ForNodeMethod · 0.80
StateEnum · 0.70
CInvClass · 0.70
GetBlockHashMethod · 0.45
ToStringMethod · 0.45

Tested by

no test coverage detected