Skip to content

Commit

Permalink
merge bitcoin#23880: Serialize cmpctblock at most once in NewPoWValid…
Browse files Browse the repository at this point in the history
…Block

this commit will not work with `--enable-c++20` as c++20 does away with
aggregate initialization when constructors are declared. a partial
backport of bitcoin#24169 will sort that out.
  • Loading branch information
kwvg authored and PastaPastaPasta committed Aug 9, 2024
1 parent 9f7ac69 commit 27e885d
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <algorithm>
#include <atomic>
#include <chrono>
#include <future>
#include <list>
#include <memory>
#include <optional>
Expand Down Expand Up @@ -2042,6 +2043,8 @@ void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::sha
nHighestFastAnnounce = pindex->nHeight;

uint256 hashBlock(pblock->GetHash());
const std::shared_future<CSerializedNetMsg> lazy_ser{
std::async(std::launch::deferred, [&] { return msgMaker.Make(NetMsgType::CMPCTBLOCK, *pcmpctblock); })};

{
LOCK(cs_most_recent_block);
Expand All @@ -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)
Expand All @@ -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;
}
});
Expand Down

0 comments on commit 27e885d

Please sign in to comment.