Skip to content

Commit 5749a85

Browse files
committed
Cache most-recently-connected compact block
1 parent 9eaec08 commit 5749a85

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/net_processing.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -754,10 +754,11 @@ void PeerLogicValidation::SyncTransaction(const CTransaction& tx, const CBlockIn
754754

755755
static CCriticalSection cs_most_recent_block;
756756
static std::shared_ptr<const CBlock> most_recent_block;
757+
static std::shared_ptr<CBlockHeaderAndShortTxIDs> most_recent_compact_block;
757758
static uint256 most_recent_block_hash;
758759

759760
void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) {
760-
CBlockHeaderAndShortTxIDs cmpctblock(*pblock, true);
761+
std::shared_ptr<CBlockHeaderAndShortTxIDs> pcmpctblock = std::make_shared<CBlockHeaderAndShortTxIDs> (*pblock, true);
761762
CNetMsgMaker msgMaker(PROTOCOL_VERSION);
762763

763764
LOCK(cs_main);
@@ -774,9 +775,10 @@ void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex *pindex, const std:
774775
LOCK(cs_most_recent_block);
775776
most_recent_block_hash = hashBlock;
776777
most_recent_block = pblock;
778+
most_recent_compact_block = pcmpctblock;
777779
}
778780

779-
connman->ForEachNode([this, &cmpctblock, pindex, &msgMaker, fWitnessEnabled, &hashBlock](CNode* pnode) {
781+
connman->ForEachNode([this, &pcmpctblock, pindex, &msgMaker, fWitnessEnabled, &hashBlock](CNode* pnode) {
780782
// TODO: Avoid the repeated-serialization here
781783
if (pnode->nVersion < INVALID_CB_NO_BAN_VERSION || pnode->fDisconnect)
782784
return;
@@ -789,7 +791,7 @@ void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex *pindex, const std:
789791

790792
LogPrint("net", "%s sending header-and-ids %s to peer %d\n", "PeerLogicValidation::NewPoWValidBlock",
791793
hashBlock.ToString(), pnode->id);
792-
connman->PushMessage(pnode, msgMaker.Make(NetMsgType::CMPCTBLOCK, cmpctblock));
794+
connman->PushMessage(pnode, msgMaker.Make(NetMsgType::CMPCTBLOCK, *pcmpctblock));
793795
state.pindexBestHeaderSent = pindex;
794796
}
795797
});
@@ -2859,13 +2861,24 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
28592861
// probably means we're doing an initial-ish-sync or they're slow
28602862
LogPrint("net", "%s sending header-and-ids %s to peer %d\n", __func__,
28612863
vHeaders.front().GetHash().ToString(), pto->id);
2862-
//TODO: Shouldn't need to reload block from disk, but requires refactor
2863-
CBlock block;
2864-
bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams);
2865-
assert(ret);
2866-
CBlockHeaderAndShortTxIDs cmpctblock(block, state.fWantsCmpctWitness);
2864+
28672865
int nSendFlags = state.fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
2868-
connman.PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
2866+
2867+
LOCK(cs_most_recent_block);
2868+
if (most_recent_block_hash == pBestIndex->GetBlockHash()) {
2869+
if (state.fWantsCmpctWitness)
2870+
connman.PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, *most_recent_compact_block));
2871+
else {
2872+
CBlockHeaderAndShortTxIDs cmpctblock(*most_recent_block, state.fWantsCmpctWitness);
2873+
connman.PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
2874+
}
2875+
} else {
2876+
CBlock block;
2877+
bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams);
2878+
assert(ret);
2879+
CBlockHeaderAndShortTxIDs cmpctblock(block, state.fWantsCmpctWitness);
2880+
connman.PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
2881+
}
28692882
state.pindexBestHeaderSent = pBestIndex;
28702883
} else if (state.fPreferHeaders) {
28712884
if (vHeaders.size() > 1) {

0 commit comments

Comments
 (0)