diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 61738e14f707c..f99ca7ccab95a 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -2042,6 +2043,8 @@ void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::sha nHighestFastAnnounce = pindex->nHeight; uint256 hashBlock(pblock->GetHash()); + const std::shared_future lazy_ser{ + std::async(std::launch::deferred, [&] { return msgMaker.Make(NetMsgType::CMPCTBLOCK, *pcmpctblock); })}; { LOCK(cs_most_recent_block); @@ -2050,7 +2053,7 @@ void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::sha most_recent_compact_block = pcmpctblock; } - m_connman.ForEachNode([this, &pcmpctblock, pindex, &msgMaker, &hashBlock](CNode* pnode) { + m_connman.ForEachNode([this, pindex, &lazy_ser, &hashBlock](CNode* pnode) { LockAssertion lock(::cs_main); // TODO: Avoid the repeated-serialization here if (pnode->fDisconnect) @@ -2062,7 +2065,9 @@ void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::sha if (state.m_requested_hb_cmpctblocks && !PeerHasHeader(&state, pindex) && PeerHasHeader(&state, pindex->pprev)) { LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", "PeerManager::NewPoWValidBlock", hashBlock.ToString(), pnode->GetId()); - m_connman.PushMessage(pnode, msgMaker.Make(NetMsgType::CMPCTBLOCK, *pcmpctblock)); + + const CSerializedNetMsg& ser_cmpctblock{lazy_ser.get()}; + m_connman.PushMessage(pnode, CSerializedNetMsg{ser_cmpctblock.data, ser_cmpctblock.m_type}); state.pindexBestHeaderSent = pindex; } });