* Maintain state about the best-seen block and fast-announce a compact block * to compatible peers. */
| 1562 | * to compatible peers. |
| 1563 | */ |
| 1564 | void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) |
| 1565 | { |
| 1566 | std::shared_ptr<const CBlockHeaderAndShortTxIDs> pcmpctblock = std::make_shared<const CBlockHeaderAndShortTxIDs> (*pblock, true); |
| 1567 | const CNetMsgMaker msgMaker(PROTOCOL_VERSION); |
| 1568 | |
| 1569 | LOCK(cs_main); |
| 1570 | |
| 1571 | static int nHighestFastAnnounce = 0; |
| 1572 | if (pindex->nHeight <= nHighestFastAnnounce) |
| 1573 | return; |
| 1574 | nHighestFastAnnounce = pindex->nHeight; |
| 1575 | |
| 1576 | bool fWitnessEnabled = DeploymentActiveAt(*pindex, m_chainparams.GetConsensus(), Consensus::DEPLOYMENT_SEGWIT); |
| 1577 | uint256 hashBlock(pblock->GetHash()); |
| 1578 | |
| 1579 | { |
| 1580 | LOCK(cs_most_recent_block); |
| 1581 | most_recent_block_hash = hashBlock; |
| 1582 | most_recent_block = pblock; |
| 1583 | most_recent_compact_block = pcmpctblock; |
| 1584 | fWitnessesPresentInMostRecentCompactBlock = fWitnessEnabled; |
| 1585 | } |
| 1586 | |
| 1587 | m_connman.ForEachNode([this, &pcmpctblock, pindex, &msgMaker, fWitnessEnabled, &hashBlock](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) { |
| 1588 | AssertLockHeld(::cs_main); |
| 1589 | |
| 1590 | // TODO: Avoid the repeated-serialization here |
| 1591 | if (pnode->GetCommonVersion() < INVALID_CB_NO_BAN_VERSION || pnode->fDisconnect) |
| 1592 | return; |
| 1593 | ProcessBlockAvailability(pnode->GetId()); |
| 1594 | CNodeState &state = *State(pnode->GetId()); |
| 1595 | // If the peer has, or we announced to them the previous block already, |
| 1596 | // but we don't think they have this one, go ahead and announce it |
| 1597 | if (state.fPreferHeaderAndIDs && (!fWitnessEnabled || state.fWantsCmpctWitness) && |
| 1598 | !PeerHasHeader(&state, pindex) && PeerHasHeader(&state, pindex->pprev)) { |
| 1599 | |
| 1600 | LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", "PeerManager::NewPoWValidBlock", |
| 1601 | hashBlock.ToString(), pnode->GetId()); |
| 1602 | m_connman.PushMessage(pnode, msgMaker.Make(NetMsgType::CMPCTBLOCK, *pcmpctblock)); |
| 1603 | state.pindexBestHeaderSent = pindex; |
| 1604 | } |
| 1605 | }); |
| 1606 | } |
| 1607 | |
| 1608 | /** |
| 1609 | * Update our best height and announce any block hashes which weren't previously |
no test coverage detected