@@ -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
@@ -843,6 +848,11 @@ void PeerLogicValidation::BlockChecked(const CBlock& block, const CValidationSta
843848 Misbehaving (it->second .first , nDoS);
844849 }
845850 }
851+ else if (state.IsValid () && !IsInitialBlockDownload () && mapBlocksInFlight.count (hash) == mapBlocksInFlight.size ()) {
852+ if (it != mapBlockSource.end ()) {
853+ MaybeSetPeerAsAnnouncingHeaderAndIDs (it->second .first , *connman);
854+ }
855+ }
846856 if (it != mapBlockSource.end ())
847857 mapBlockSource.erase (it);
848858}
@@ -1972,12 +1982,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
19721982 return true ;
19731983 }
19741984
1975- if (!fAlreadyInFlight && mapBlocksInFlight.size () == 1 && pindex->pprev ->IsValid (BLOCK_VALID_CHAIN)) {
1976- // We seem to be rather well-synced, so it appears pfrom was the first to provide us
1977- // with this block! Let's get them to announce using compact blocks in the future.
1978- MaybeSetPeerAsAnnouncingHeaderAndIDs (nodestate, pfrom, connman);
1979- }
1980-
19811985 BlockTransactionsRequest req;
19821986 for (size_t i = 0 ; i < cmpctblock.BlockTxCount (); i++) {
19831987 if (!partialBlock.IsTxAvailable (i))
@@ -2267,9 +2271,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
22672271 }
22682272 if (vGetData.size () > 0 ) {
22692273 if (nodestate->fSupportsDesiredCmpctVersion && vGetData.size () == 1 && mapBlocksInFlight.size () == 1 && pindexLast->pprev ->IsValid (BLOCK_VALID_CHAIN)) {
2270- // We seem to be rather well-synced, so it appears pfrom was the first to provide us
2271- // with this block! Let's get them to announce using compact blocks in the future.
2272- MaybeSetPeerAsAnnouncingHeaderAndIDs (nodestate, pfrom, connman);
22732274 // In any case, we want to download using a compact block, not a regular one
22742275 vGetData[0 ] = CInv (MSG_CMPCT_BLOCK, vGetData[0 ].hash );
22752276 }
0 commit comments