Skip to content

Commit cdf12c7

Browse files
committed
Merge bitcoin/bitcoin#22895: consensus: don't call GetBlockPos in ReadBlockFromDisk without cs_main lock
350e034 consensus: don't call GetBlockPos in ReadBlockFromDisk without lock (Jon Atack) Pull request description: Commit ccd8ef6 "Reduce cs_main lock in ReadBlockFromDisk, only read GetBlockPos under the lock" in #11281 moved the cs_main lock from caller to `ReadBlockFromDisk()` for calling `CBlockIndex::GetBlockPos()`, but the second invocation doesn't have the lock, and IIUC there is no guarantee the compiler can know if state has changed. Use the `blockPos` local variable instead, rename it to `block_pos`, and make it const. ACKs for top commit: laanwj: Code review ACK 350e034 theStack: Code-review ACK 350e034 promag: Code review ACK 350e034. Tree-SHA512: 0df0614ab1876885c85f7b53c604a759a29008da8027e95503b4726d2b820ec6d27546020c613337ff954406e01cb5d191978ba4a12124052fed6e1b0e9a226f
2 parents 58e0239 + 350e034 commit cdf12c7

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

src/node/blockstorage.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -394,18 +394,14 @@ bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::P
394394

395395
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams)
396396
{
397-
FlatFilePos blockPos;
398-
{
399-
LOCK(cs_main);
400-
blockPos = pindex->GetBlockPos();
401-
}
397+
const FlatFilePos block_pos{WITH_LOCK(cs_main, return pindex->GetBlockPos())};
402398

403-
if (!ReadBlockFromDisk(block, blockPos, consensusParams)) {
399+
if (!ReadBlockFromDisk(block, block_pos, consensusParams)) {
404400
return false;
405401
}
406402
if (block.GetHash() != pindex->GetBlockHash()) {
407403
return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
408-
pindex->ToString(), pindex->GetBlockPos().ToString());
404+
pindex->ToString(), block_pos.ToString());
409405
}
410406
return true;
411407
}

0 commit comments

Comments
 (0)