MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / FetchBlock

Method FetchBlock

src/net_processing.cpp:1985–2023  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1983}
1984
1985util::Expected<void, std::string> PeerManagerImpl::FetchBlock(NodeId peer_id, const CBlockIndex& block_index)
1986{
1987 if (m_chainman.m_blockman.LoadingBlocks()) return util::Unexpected{"Loading blocks ..."};
1988
1989 // The lock must be taken here before fetching Peer so another thread does
1990 // not delete the CNodeState from under the current thread, causing an
1991 // assertion failure in BlockRequested. This lock can be replaced with a
1992 // net-specific lock when more of CNodeState is moved into Peer.
1993 LOCK(cs_main);
1994
1995 // Ensure this peer exists and hasn't been disconnected
1996 PeerRef peer = GetPeerRef(peer_id);
1997 if (peer == nullptr) return util::Unexpected{"Peer does not exist"};
1998
1999 // Ignore pre-segwit peers
2000 if (!CanServeWitnesses(*peer)) return util::Unexpected{"Pre-SegWit peer"};
2001
2002 // Forget about all prior requests
2003 RemoveBlockRequest(block_index.GetBlockHash(), std::nullopt);
2004
2005 // Mark block as in-flight
2006 if (!BlockRequested(peer_id, block_index)) return util::Unexpected{"Already requested from this peer"};
2007
2008 // Construct message to request the block
2009 const uint256& hash{block_index.GetBlockHash()};
2010 std::vector<CInv> invs{CInv(MSG_BLOCK | MSG_WITNESS_FLAG, hash)};
2011
2012 // Send block request message to the peer
2013 bool success = m_connman.ForNode(peer_id, [this, &invs](CNode* node) {
2014 this->MakeAndPushMessage(*node, NetMsgType::GETDATA, invs);
2015 return true;
2016 });
2017
2018 if (!success) return util::Unexpected{"Peer not fully connected"};
2019
2020 LogDebug(BCLog::NET, "Requesting block %s from peer=%d\n",
2021 hash.ToString(), peer_id);
2022 return {};
2023}
2024
2025std::unique_ptr<PeerManager> PeerManager::make(CConnman& connman, AddrMan& addrman,
2026 BanMan* banman, ChainstateManager& chainman,

Callers 1

getblockfrompeerFunction · 0.80

Calls 7

MakeAndPushMessageMethod · 0.95
CanServeWitnessesFunction · 0.85
LoadingBlocksMethod · 0.80
ForNodeMethod · 0.80
CInvClass · 0.70
GetBlockHashMethod · 0.45
ToStringMethod · 0.45

Tested by

no test coverage detected