4141#include < algorithm>
4242#include < atomic>
4343#include < chrono>
44+ #include < future>
4445#include < memory>
4546#include < optional>
4647#include < typeinfo>
@@ -1596,6 +1597,8 @@ void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::sha
15961597
15971598 bool fWitnessEnabled = DeploymentActiveAt (*pindex, m_chainparams.GetConsensus (), Consensus::DEPLOYMENT_SEGWIT);
15981599 uint256 hashBlock (pblock->GetHash ());
1600+ const std::shared_future<CSerializedNetMsg> lazy_ser{
1601+ std::async (std::launch::deferred, [&] { return msgMaker.Make (NetMsgType::CMPCTBLOCK, *pcmpctblock); })};
15991602
16001603 {
16011604 LOCK (cs_most_recent_block);
@@ -1605,10 +1608,9 @@ void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::sha
16051608 fWitnessesPresentInMostRecentCompactBlock = fWitnessEnabled ;
16061609 }
16071610
1608- m_connman.ForEachNode ([this , &pcmpctblock, pindex, &msgMaker, fWitnessEnabled , &hashBlock](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED (::cs_main) {
1611+ m_connman.ForEachNode ([this , pindex, fWitnessEnabled , &lazy_ser , &hashBlock](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED (::cs_main) {
16091612 AssertLockHeld (::cs_main);
16101613
1611- // TODO: Avoid the repeated-serialization here
16121614 if (pnode->GetCommonVersion () < INVALID_CB_NO_BAN_VERSION || pnode->fDisconnect )
16131615 return ;
16141616 ProcessBlockAvailability (pnode->GetId ());
@@ -1620,7 +1622,9 @@ void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::sha
16201622
16211623 LogPrint (BCLog::NET, " %s sending header-and-ids %s to peer=%d\n " , " PeerManager::NewPoWValidBlock" ,
16221624 hashBlock.ToString (), pnode->GetId ());
1623- m_connman.PushMessage (pnode, msgMaker.Make (NetMsgType::CMPCTBLOCK, *pcmpctblock));
1625+
1626+ const CSerializedNetMsg& ser_cmpctblock{lazy_ser.get ()};
1627+ m_connman.PushMessage (pnode, CSerializedNetMsg{ser_cmpctblock.data , ser_cmpctblock.m_type });
16241628 state.pindexBestHeaderSent = pindex;
16251629 }
16261630 });
0 commit comments