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

Method MaybeSetPeerAsAnnouncingHeaderAndIDs

src/net_processing.cpp:1284–1341  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1282}
1283
1284void PeerManagerImpl::MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid)
1285{
1286 AssertLockHeld(cs_main);
1287
1288 // When in -blocksonly mode, never request high-bandwidth mode from peers. Our
1289 // mempool will not contain the transactions necessary to reconstruct the
1290 // compact block.
1291 if (m_opts.ignore_incoming_txs) return;
1292
1293 CNodeState* nodestate = State(nodeid);
1294 PeerRef peer{GetPeerRef(nodeid)};
1295 if (!nodestate || !nodestate->m_provides_cmpctblocks) {
1296 // Don't request compact blocks if the peer has not signalled support
1297 return;
1298 }
1299
1300 int num_outbound_hb_peers = 0;
1301 for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); it != lNodesAnnouncingHeaderAndIDs.end(); it++) {
1302 if (*it == nodeid) {
1303 lNodesAnnouncingHeaderAndIDs.erase(it);
1304 lNodesAnnouncingHeaderAndIDs.push_back(nodeid);
1305 return;
1306 }
1307 PeerRef peer_ref{GetPeerRef(*it)};
1308 if (peer_ref && !peer_ref->m_is_inbound) ++num_outbound_hb_peers;
1309 }
1310 if (peer && peer->m_is_inbound) {
1311 // If we're adding an inbound HB peer, make sure we're not removing
1312 // our last outbound HB peer in the process.
1313 if (lNodesAnnouncingHeaderAndIDs.size() >= 3 && num_outbound_hb_peers == 1) {
1314 PeerRef remove_peer{GetPeerRef(lNodesAnnouncingHeaderAndIDs.front())};
1315 if (remove_peer && !remove_peer->m_is_inbound) {
1316 // Put the HB outbound peer in the second slot, so that it
1317 // doesn't get removed.
1318 std::swap(lNodesAnnouncingHeaderAndIDs.front(), *std::next(lNodesAnnouncingHeaderAndIDs.begin()));
1319 }
1320 }
1321 }
1322 const bool nodeid_was_appended{m_connman.ForNode(nodeid, [this](CNode* pfrom) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
1323 AssertLockHeld(::cs_main);
1324 MakeAndPushMessage(*pfrom, NetMsgType::SENDCMPCT, /*high_bandwidth=*/true, /*version=*/CMPCTBLOCKS_VERSION);
1325 // save BIP152 bandwidth state: we select peer to be high-bandwidth
1326 pfrom->m_bip152_highbandwidth_to = true;
1327 lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
1328 return true;
1329 })};
1330 if (nodeid_was_appended && lNodesAnnouncingHeaderAndIDs.size() > 3) {
1331 // As per BIP152, we only get 3 of our peers to announce
1332 // blocks using compact encodings.
1333 m_connman.ForNode(lNodesAnnouncingHeaderAndIDs.front(), [this](CNode* pnodeStop) {
1334 MakeAndPushMessage(*pnodeStop, NetMsgType::SENDCMPCT, /*high_bandwidth=*/false, /*version=*/CMPCTBLOCKS_VERSION);
1335 // save BIP152 bandwidth state: we select peer to be low-bandwidth
1336 pnodeStop->m_bip152_highbandwidth_to = false;
1337 return true;
1338 });
1339 lNodesAnnouncingHeaderAndIDs.pop_front();
1340 }
1341}

Callers

nothing calls this directly

Calls 11

ForNodeMethod · 0.80
GetIdMethod · 0.80
StateEnum · 0.70
EXCLUSIVE_LOCKS_REQUIREDFunction · 0.70
beginMethod · 0.45
endMethod · 0.45
eraseMethod · 0.45
push_backMethod · 0.45
sizeMethod · 0.45
frontMethod · 0.45
pop_frontMethod · 0.45

Tested by

no test coverage detected