@@ -395,33 +395,38 @@ void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
395395 }
396396}
397397
398- void MaybeSetPeerAsAnnouncingHeaderAndIDs (const CNodeState* nodestate, CNode* pfrom, CConnman& connman) {
399- if (!nodestate->fSupportsDesiredCmpctVersion ) {
398+ void MaybeSetPeerAsAnnouncingHeaderAndIDs (NodeId nodeid, CConnman& connman) {
399+ AssertLockHeld (cs_main);
400+ CNodeState* nodestate = State (nodeid);
401+ if (!nodestate || !nodestate->fSupportsDesiredCmpctVersion ) {
400402 // Never ask from peers who can't provide witnesses.
401403 return ;
402404 }
403405 if (nodestate->fProvidesHeaderAndIDs ) {
404406 for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin (); it != lNodesAnnouncingHeaderAndIDs.end (); it++) {
405- if (*it == pfrom-> GetId () ) {
407+ if (*it == nodeid ) {
406408 lNodesAnnouncingHeaderAndIDs.erase (it);
407- lNodesAnnouncingHeaderAndIDs.push_back (pfrom-> GetId () );
409+ lNodesAnnouncingHeaderAndIDs.push_back (nodeid );
408410 return ;
409411 }
410412 }
411- bool fAnnounceUsingCMPCTBLOCK = false ;
412- uint64_t nCMPCTBLOCKVersion = (pfrom->GetLocalServices () & NODE_WITNESS) ? 2 : 1 ;
413- if (lNodesAnnouncingHeaderAndIDs.size () >= 3 ) {
414- // As per BIP152, we only get 3 of our peers to announce
415- // blocks using compact encodings.
416- connman.ForNode (lNodesAnnouncingHeaderAndIDs.front (), [&connman, fAnnounceUsingCMPCTBLOCK , nCMPCTBLOCKVersion](CNode* pnodeStop){
417- connman.PushMessage (pnodeStop, CNetMsgMaker (pnodeStop->GetSendVersion ()).Make (NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK , nCMPCTBLOCKVersion));
418- return true ;
419- });
420- lNodesAnnouncingHeaderAndIDs.pop_front ();
421- }
422- fAnnounceUsingCMPCTBLOCK = true ;
423- connman.PushMessage (pfrom, CNetMsgMaker (pfrom->GetSendVersion ()).Make (NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK , nCMPCTBLOCKVersion));
424- lNodesAnnouncingHeaderAndIDs.push_back (pfrom->GetId ());
413+ connman.ForNode (nodeid, [&connman](CNode* pfrom){
414+ bool fAnnounceUsingCMPCTBLOCK = false ;
415+ uint64_t nCMPCTBLOCKVersion = (pfrom->GetLocalServices () & NODE_WITNESS) ? 2 : 1 ;
416+ if (lNodesAnnouncingHeaderAndIDs.size () >= 3 ) {
417+ // As per BIP152, we only get 3 of our peers to announce
418+ // blocks using compact encodings.
419+ connman.ForNode (lNodesAnnouncingHeaderAndIDs.front (), [&connman, fAnnounceUsingCMPCTBLOCK , nCMPCTBLOCKVersion](CNode* pnodeStop){
420+ connman.PushMessage (pnodeStop, CNetMsgMaker (pnodeStop->GetSendVersion ()).Make (NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK , nCMPCTBLOCKVersion));
421+ return true ;
422+ });
423+ lNodesAnnouncingHeaderAndIDs.pop_front ();
424+ }
425+ fAnnounceUsingCMPCTBLOCK = true ;
426+ connman.PushMessage (pfrom, CNetMsgMaker (pfrom->GetSendVersion ()).Make (NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK , nCMPCTBLOCKVersion));
427+ lNodesAnnouncingHeaderAndIDs.push_back (pfrom->GetId ());
428+ return true ;
429+ });
425430 }
426431}
427432
@@ -798,6 +803,11 @@ void PeerLogicValidation::BlockChecked(const CBlock& block, const CValidationSta
798803 Misbehaving (it->second .first , nDoS);
799804 }
800805 }
806+ else if (state.IsValid () && !IsInitialBlockDownload () && mapBlocksInFlight.count (hash) == mapBlocksInFlight.size ()) {
807+ if (it != mapBlockSource.end ()) {
808+ MaybeSetPeerAsAnnouncingHeaderAndIDs (it->second .first , *connman);
809+ }
810+ }
801811 if (it != mapBlockSource.end ())
802812 mapBlockSource.erase (it);
803813}
@@ -1853,12 +1863,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
18531863 return true ;
18541864 }
18551865
1856- if (!fAlreadyInFlight && mapBlocksInFlight.size () == 1 && pindex->pprev ->IsValid (BLOCK_VALID_CHAIN)) {
1857- // We seem to be rather well-synced, so it appears pfrom was the first to provide us
1858- // with this block! Let's get them to announce using compact blocks in the future.
1859- MaybeSetPeerAsAnnouncingHeaderAndIDs (nodestate, pfrom, connman);
1860- }
1861-
18621866 BlockTransactionsRequest req;
18631867 for (size_t i = 0 ; i < cmpctblock.BlockTxCount (); i++) {
18641868 if (!partialBlock.IsTxAvailable (i))
@@ -2143,9 +2147,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
21432147 }
21442148 if (vGetData.size () > 0 ) {
21452149 if (nodestate->fSupportsDesiredCmpctVersion && vGetData.size () == 1 && mapBlocksInFlight.size () == 1 && pindexLast->pprev ->IsValid (BLOCK_VALID_CHAIN)) {
2146- // We seem to be rather well-synced, so it appears pfrom was the first to provide us
2147- // with this block! Let's get them to announce using compact blocks in the future.
2148- MaybeSetPeerAsAnnouncingHeaderAndIDs (nodestate, pfrom, connman);
21492150 // In any case, we want to download using a compact block, not a regular one
21502151 vGetData[0 ] = CInv (MSG_CMPCT_BLOCK, vGetData[0 ].hash );
21512152 }
0 commit comments