diff options
author | Andrew Toth <andrewstoth@gmail.com> | 2024-05-04 15:33:36 -0400 |
---|---|---|
committer | Andrew Toth <andrewstoth@gmail.com> | 2024-05-04 15:33:36 -0400 |
commit | 613a45cd4b5482aedbdc7c61c839ea05996935c6 (patch) | |
tree | 3764236d5cd97247afa810225a9837ba525a8ee4 /src/net_processing.cpp | |
parent | eb0bdbdd753bca97120247b921fd29d606fea6e9 (diff) |
net: reduce LOCK(cs_main) scope in GETBLOCKTXN
Also adds a static assertion that MAX_BLOCKTXN_DEPTH <= MIN_BLOCKS_TO_KEEP
Diffstat (limited to 'src/net_processing.cpp')
-rw-r--r-- | src/net_processing.cpp | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 9b5983b9d0..84f9a5dc8b 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -118,6 +118,7 @@ static const unsigned int MAX_HEADERS_RESULTS = 2000; 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 @@ -4366,6 +4367,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, return; } + FlatFilePos block_pos{}; { LOCK(cs_main); @@ -4376,15 +4378,21 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, } if (pindex->nHeight >= m_chainman.ActiveChain().Height() - MAX_BLOCKTXN_DEPTH) { - CBlock block; - const bool ret{m_chainman.m_blockman.ReadBlockFromDisk(block, *pindex)}; - assert(ret); - - SendBlockTransactions(pfrom, *peer, block, req); - return; + block_pos = pindex->GetBlockPos(); } } + if (!block_pos.IsNull()) { + CBlock block; + const bool ret{m_chainman.m_blockman.ReadBlockFromDisk(block, block_pos)}; + // If height is above MAX_BLOCKTXN_DEPTH then this block cannot get + // pruned after we release cs_main above, so this read should never fail. + assert(ret); + + SendBlockTransactions(pfrom, *peer, block, req); + return; + } + // If an older block is requested (should never happen in practice, // but can happen in tests) send a block response instead of a // blocktxn response. Sending a full block response instead of a |