From 037d040aced4bbeae0c0060f4ad8b07056090ad5 Mon Sep 17 00:00:00 2001 From: PastaClaw Date: Mon, 8 Jun 2026 18:52:56 -0500 Subject: [PATCH] partial Merge bitcoin/bitcoin#26326: net: reduce LOCK(cs_main) scope in GETBLOCKTXN 613a45cd4b5482aedbdc7c61c839ea05996935c6 net: reduce LOCK(cs_main) scope in GETBLOCKTXN (Andrew Toth) Pull request description: Inspired by https://github.com/bitcoin/bitcoin/pull/11913 and https://github.com/bitcoin/bitcoin/pull/26308. `cs_main` doesn't need to be locked while reading blocks. This removes the locks in `net_processing`. Dash notes: This is a partial backport of bitcoin/bitcoin#26326. It includes only the GETBLOCKTXN commit, adapted to Dash's current ReadBlockFromDisk API and post-read hash validation. The sibling upstream ProcessGetBlockData commit is not included in this PR. --- src/net_processing.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index d99bcc71a399..cd28ae94c89c 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -142,6 +142,7 @@ static constexpr auto BLOCK_STALLING_TIMEOUT_MAX{64s}; static const int MAX_CMPCTBLOCK_DEPTH = 5; /** Maximum depth of blocks we're willing to respond to GETBLOCKTXN requests for. */ static const int MAX_BLOCKTXN_DEPTH = 10; +static_assert(MAX_BLOCKTXN_DEPTH <= MIN_BLOCKS_TO_KEEP, "MAX_BLOCKTXN_DEPTH too high"); /** Size of the "block download window": how far ahead of our current height do we fetch? * Larger windows tolerate larger download speed differences between peer, but increase the potential * degree of disordering of blocks on disk (which make reindexing and pruning harder). We'll probably @@ -4473,6 +4474,7 @@ void PeerManagerImpl::ProcessMessage( return; } + FlatFilePos block_pos{}; { LOCK(cs_main); @@ -4483,13 +4485,20 @@ void PeerManagerImpl::ProcessMessage( } if (pindex->nHeight >= m_chainman.ActiveChain().Height() - MAX_BLOCKTXN_DEPTH) { - CBlock block; - bool ret = ReadBlockFromDisk(block, pindex, m_chainparams.GetConsensus()); - assert(ret); + block_pos = pindex->GetBlockPos(); + } + } - SendBlockTransactions(pfrom, *peer, block, req); + if (!block_pos.IsNull()) { + CBlock block; + const auto hash = ReadBlockFromDisk(block, block_pos, m_chainparams.GetConsensus()); + if (!hash || *hash != req.blockhash) { + LogPrint(BCLog::NET, "Peer %d sent us a getblocktxn for a block we could not read\n", pfrom.GetId()); return; } + + SendBlockTransactions(pfrom, *peer, block, req); + return; } // If an older block is requested (should never happen in practice,