41
41
#include < algorithm>
42
42
#include < atomic>
43
43
#include < chrono>
44
+ #include < future>
44
45
#include < memory>
45
46
#include < optional>
46
47
#include < typeinfo>
@@ -1596,6 +1597,8 @@ void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::sha
1596
1597
1597
1598
bool fWitnessEnabled = DeploymentActiveAt (*pindex, m_chainparams.GetConsensus (), Consensus::DEPLOYMENT_SEGWIT);
1598
1599
uint256 hashBlock (pblock->GetHash ());
1600
+ const std::shared_future<CSerializedNetMsg> lazy_ser{
1601
+ std::async (std::launch::deferred, [&] { return msgMaker.Make (NetMsgType::CMPCTBLOCK, *pcmpctblock); })};
1599
1602
1600
1603
{
1601
1604
LOCK (cs_most_recent_block);
@@ -1605,10 +1608,9 @@ void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::sha
1605
1608
fWitnessesPresentInMostRecentCompactBlock = fWitnessEnabled ;
1606
1609
}
1607
1610
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) {
1609
1612
AssertLockHeld (::cs_main);
1610
1613
1611
- // TODO: Avoid the repeated-serialization here
1612
1614
if (pnode->GetCommonVersion () < INVALID_CB_NO_BAN_VERSION || pnode->fDisconnect )
1613
1615
return ;
1614
1616
ProcessBlockAvailability (pnode->GetId ());
@@ -1620,7 +1622,9 @@ void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::sha
1620
1622
1621
1623
LogPrint (BCLog::NET, " %s sending header-and-ids %s to peer=%d\n " , " PeerManager::NewPoWValidBlock" ,
1622
1624
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 });
1624
1628
state.pindexBestHeaderSent = pindex;
1625
1629
}
1626
1630
});
0 commit comments