@@ -754,10 +754,11 @@ void PeerLogicValidation::SyncTransaction(const CTransaction& tx, const CBlockIn
754
754
755
755
static CCriticalSection cs_most_recent_block;
756
756
static std::shared_ptr<const CBlock> most_recent_block;
757
+ static std::shared_ptr<CBlockHeaderAndShortTxIDs> most_recent_compact_block;
757
758
static uint256 most_recent_block_hash;
758
759
759
760
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 );
761
762
CNetMsgMaker msgMaker (PROTOCOL_VERSION);
762
763
763
764
LOCK (cs_main);
@@ -774,9 +775,10 @@ void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex *pindex, const std:
774
775
LOCK (cs_most_recent_block);
775
776
most_recent_block_hash = hashBlock;
776
777
most_recent_block = pblock;
778
+ most_recent_compact_block = pcmpctblock;
777
779
}
778
780
779
- connman->ForEachNode ([this , &cmpctblock , pindex, &msgMaker, fWitnessEnabled , &hashBlock](CNode* pnode) {
781
+ connman->ForEachNode ([this , &pcmpctblock , pindex, &msgMaker, fWitnessEnabled , &hashBlock](CNode* pnode) {
780
782
// TODO: Avoid the repeated-serialization here
781
783
if (pnode->nVersion < INVALID_CB_NO_BAN_VERSION || pnode->fDisconnect )
782
784
return ;
@@ -789,7 +791,7 @@ void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex *pindex, const std:
789
791
790
792
LogPrint (" net" , " %s sending header-and-ids %s to peer %d\n " , " PeerLogicValidation::NewPoWValidBlock" ,
791
793
hashBlock.ToString (), pnode->id );
792
- connman->PushMessage (pnode, msgMaker.Make (NetMsgType::CMPCTBLOCK, cmpctblock ));
794
+ connman->PushMessage (pnode, msgMaker.Make (NetMsgType::CMPCTBLOCK, *pcmpctblock ));
793
795
state.pindexBestHeaderSent = pindex;
794
796
}
795
797
});
@@ -2859,13 +2861,24 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
2859
2861
// probably means we're doing an initial-ish-sync or they're slow
2860
2862
LogPrint (" net" , " %s sending header-and-ids %s to peer %d\n " , __func__,
2861
2863
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
+
2867
2865
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
+ }
2869
2882
state.pindexBestHeaderSent = pBestIndex;
2870
2883
} else if (state.fPreferHeaders ) {
2871
2884
if (vHeaders.size () > 1 ) {
0 commit comments