Skip to content

Commit 613a45c

Browse files
committed
net: reduce LOCK(cs_main) scope in GETBLOCKTXN
Also adds a static assertion that MAX_BLOCKTXN_DEPTH <= MIN_BLOCKS_TO_KEEP
1 parent eb0bdbd commit 613a45c

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/net_processing.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ static const unsigned int MAX_HEADERS_RESULTS = 2000;
118118
static const int MAX_CMPCTBLOCK_DEPTH = 5;
119119
/** Maximum depth of blocks we're willing to respond to GETBLOCKTXN requests for. */
120120
static const int MAX_BLOCKTXN_DEPTH = 10;
121+
static_assert(MAX_BLOCKTXN_DEPTH <= MIN_BLOCKS_TO_KEEP, "MAX_BLOCKTXN_DEPTH too high");
121122
/** Size of the "block download window": how far ahead of our current height do we fetch?
122123
* Larger windows tolerate larger download speed differences between peer, but increase the potential
123124
* 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,
43664367
return;
43674368
}
43684369

4370+
FlatFilePos block_pos{};
43694371
{
43704372
LOCK(cs_main);
43714373

@@ -4376,15 +4378,21 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
43764378
}
43774379

43784380
if (pindex->nHeight >= m_chainman.ActiveChain().Height() - MAX_BLOCKTXN_DEPTH) {
4379-
CBlock block;
4380-
const bool ret{m_chainman.m_blockman.ReadBlockFromDisk(block, *pindex)};
4381-
assert(ret);
4382-
4383-
SendBlockTransactions(pfrom, *peer, block, req);
4384-
return;
4381+
block_pos = pindex->GetBlockPos();
43854382
}
43864383
}
43874384

4385+
if (!block_pos.IsNull()) {
4386+
CBlock block;
4387+
const bool ret{m_chainman.m_blockman.ReadBlockFromDisk(block, block_pos)};
4388+
// If height is above MAX_BLOCKTXN_DEPTH then this block cannot get
4389+
// pruned after we release cs_main above, so this read should never fail.
4390+
assert(ret);
4391+
4392+
SendBlockTransactions(pfrom, *peer, block, req);
4393+
return;
4394+
}
4395+
43884396
// If an older block is requested (should never happen in practice,
43894397
// but can happen in tests) send a block response instead of a
43904398
// blocktxn response. Sending a full block response instead of a

0 commit comments

Comments
 (0)