From 97af883a9d7c0b6e7f9bb3f9688b82484333bfda Mon Sep 17 00:00:00 2001 From: PastaClaw Date: Mon, 8 Jun 2026 18:52:56 -0500 Subject: [PATCH] fix: reduce GETBLOCKTXN disk read lock scope --- 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 015408b9306a..dfdd0fcde2f1 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -145,6 +145,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 @@ -4501,6 +4502,7 @@ void PeerManagerImpl::ProcessMessage( return; } + FlatFilePos block_pos{}; { LOCK(cs_main); @@ -4511,13 +4513,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, 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, block, req); + return; } // If an older block is requested (should never happen in practice,