Skip to content

Commit 8edd0d3

Browse files
committed
refactor: reduce scope of lock m_most_recent_block_mutex
This avoids calling the non-trivial method `CConnman::PushMessage` within the critical section.
1 parent aa3200d commit 8edd0d3

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/net_processing.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4759,15 +4759,16 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
47594759
LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", __func__,
47604760
vHeaders.front().GetHash().ToString(), pto->GetId());
47614761

4762-
bool fGotBlockFromCache = false;
4762+
std::optional<CSerializedNetMsg> cached_cmpctblock_msg;
47634763
{
47644764
LOCK(m_most_recent_block_mutex);
47654765
if (m_most_recent_block_hash == pBestIndex->GetBlockHash()) {
4766-
m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::CMPCTBLOCK, *m_most_recent_compact_block));
4767-
fGotBlockFromCache = true;
4766+
cached_cmpctblock_msg = msgMaker.Make(NetMsgType::CMPCTBLOCK, *m_most_recent_compact_block);
47684767
}
47694768
}
4770-
if (!fGotBlockFromCache) {
4769+
if (cached_cmpctblock_msg.has_value()) {
4770+
m_connman.PushMessage(pto, std::move(cached_cmpctblock_msg.value()));
4771+
} else {
47714772
CBlock block;
47724773
bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams);
47734774
assert(ret);

0 commit comments

Comments
 (0)