Skip to content

Commit 2f34a2e

Browse files
committed
Get our "best three" peers to announce blocks using cmpctblocks
1 parent 927f8ee commit 2f34a2e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/main.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ namespace {
204204
};
205205
map<uint256, pair<NodeId, list<QueuedBlock>::iterator> > mapBlocksInFlight;
206206

207+
/** Stack of nodes which we have set to announce using compact blocks */
208+
list<NodeId> lNodesAnnouncingHeaderAndIDs;
209+
207210
/** Number of preferable block download peers. */
208211
int nPreferredDownload = 0;
209212

@@ -456,6 +459,28 @@ void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
456459
}
457460
}
458461

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+
459484
// Requires cs_main
460485
bool CanDirectFetch(const Consensus::Params &consensusParams)
461486
{
@@ -5531,6 +5556,10 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
55315556
}
55325557
if (vGetData.size() > 0) {
55335558
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
55345563
vGetData[0] = CInv(MSG_CMPCT_BLOCK, vGetData[0].hash);
55355564
}
55365565
pfrom->PushMessage(NetMsgType::GETDATA, vGetData);

0 commit comments

Comments
 (0)