@@ -204,6 +204,9 @@ namespace {
204
204
};
205
205
map<uint256, pair<NodeId, list<QueuedBlock>::iterator> > mapBlocksInFlight;
206
206
207
+ /* * Stack of nodes which we have set to announce using compact blocks */
208
+ list<NodeId> lNodesAnnouncingHeaderAndIDs;
209
+
207
210
/* * Number of preferable block download peers. */
208
211
int nPreferredDownload = 0 ;
209
212
@@ -456,6 +459,28 @@ void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
456
459
}
457
460
}
458
461
462
+ void MaybeSetPeerAsAnnouncingHeaderAndIDs (const CNodeState* nodestate, CNode* pfrom) {
463
+ if (nodestate->fProvidesHeaderAndIDs ) {
464
+ BOOST_FOREACH (const NodeId nodeid, lNodesAnnouncingHeaderAndIDs)
465
+ if (nodeid == pfrom->GetId ())
466
+ return ;
467
+ bool fAnnounceUsingCMPCTBLOCK = false ;
468
+ uint64_t nCMPCTBLOCKVersion = 1 ;
469
+ if (lNodesAnnouncingHeaderAndIDs.size () >= 3 ) {
470
+ // As per BIP152, we only get 3 of our peers to announce
471
+ // blocks using compact encodings.
472
+ CNode* pnodeStop = FindNode (lNodesAnnouncingHeaderAndIDs.front ());
473
+ if (pnodeStop) {
474
+ pnodeStop->PushMessage (NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK , nCMPCTBLOCKVersion);
475
+ lNodesAnnouncingHeaderAndIDs.pop_front ();
476
+ }
477
+ }
478
+ fAnnounceUsingCMPCTBLOCK = true ;
479
+ pfrom->PushMessage (NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK , nCMPCTBLOCKVersion);
480
+ lNodesAnnouncingHeaderAndIDs.push_back (pfrom->GetId ());
481
+ }
482
+ }
483
+
459
484
// Requires cs_main
460
485
bool CanDirectFetch (const Consensus::Params &consensusParams)
461
486
{
@@ -5531,6 +5556,10 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
5531
5556
}
5532
5557
if (vGetData.size () > 0 ) {
5533
5558
if (nodestate->fProvidesHeaderAndIDs && vGetData.size () == 1 && mapBlocksInFlight.size () == 1 && pindexLast->pprev ->IsValid (BLOCK_VALID_CHAIN)) {
5559
+ // We seem to be rather well-synced, so it appears pfrom was the first to provide us
5560
+ // with this block! Let's get them to announce using compact blocks in the future.
5561
+ MaybeSetPeerAsAnnouncingHeaderAndIDs (nodestate, pfrom);
5562
+ // In any case, we want to download using a compact block, not a regular one
5534
5563
vGetData[0 ] = CInv (MSG_CMPCT_BLOCK, vGetData[0 ].hash );
5535
5564
}
5536
5565
pfrom->PushMessage (NetMsgType::GETDATA, vGetData);
0 commit comments