Skip to content

Commit c802092

Browse files
committed
Relay compact block messages prior to full block connection
1 parent 6987219 commit c802092

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/net_processing.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,39 @@ void PeerLogicValidation::SyncTransaction(const CTransaction& tx, const CBlockIn
752752
}
753753
}
754754

755+
void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) {
756+
CBlockHeaderAndShortTxIDs cmpctblock(*pblock, true);
757+
CNetMsgMaker msgMaker(PROTOCOL_VERSION);
758+
759+
LOCK(cs_main);
760+
761+
static int nHighestFastAnnounce = 0;
762+
if (pindex->nHeight <= nHighestFastAnnounce)
763+
return;
764+
nHighestFastAnnounce = pindex->nHeight;
765+
766+
bool fWitnessEnabled = IsWitnessEnabled(pindex->pprev, Params().GetConsensus());
767+
uint256 hashBlock(pblock->GetHash());
768+
769+
connman->ForEachNode([this, &cmpctblock, pindex, &msgMaker, fWitnessEnabled, &hashBlock](CNode* pnode) {
770+
// TODO: Avoid the repeated-serialization here
771+
if (pnode->nVersion < INVALID_CB_NO_BAN_VERSION || pnode->fDisconnect)
772+
return;
773+
ProcessBlockAvailability(pnode->GetId());
774+
CNodeState &state = *State(pnode->GetId());
775+
// If the peer has, or we announced to them the previous block already,
776+
// but we don't think they have this one, go ahead and announce it
777+
if (state.fPreferHeaderAndIDs && (!fWitnessEnabled || state.fWantsCmpctWitness) &&
778+
!PeerHasHeader(&state, pindex) && PeerHasHeader(&state, pindex->pprev)) {
779+
780+
LogPrint("net", "%s sending header-and-ids %s to peer %d\n", "PeerLogicValidation::NewPoWValidBlock",
781+
hashBlock.ToString(), pnode->id);
782+
connman->PushMessage(pnode, msgMaker.Make(NetMsgType::CMPCTBLOCK, cmpctblock));
783+
state.pindexBestHeaderSent = pindex;
784+
}
785+
});
786+
}
787+
755788
void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
756789
const int nNewHeight = pindexNew->nHeight;
757790
connman->SetBestHeight(nNewHeight);

src/net_processing.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class PeerLogicValidation : public CValidationInterface {
2424
virtual void SyncTransaction(const CTransaction& tx, const CBlockIndex* pindex, int nPosInBlock);
2525
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload);
2626
virtual void BlockChecked(const CBlock& block, const CValidationState& state);
27+
virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock);
2728
};
2829

2930
struct CNodeStateStats {

0 commit comments

Comments
 (0)