Skip to content

Commit 4efdbab

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#24197: Replace lock with thread safety annotation in CBlockTreeDB::LoadBlockIndexGuts()
20276ca Replace lock with thread safety annotation in CBlockTreeDB::LoadBlockIndexGuts() (Jon Atack) Pull request description: Following up on bitcoin/bitcoin#22932 (comment) by Marco Falke (good observation, thank you), we can replace a cs_main lock in `CBlockTreeDB::LoadBlockIndexGuts()` with a Clang thread safety annotation/assertion instead. The unlocked code is reverted to its original state before #22932. ACKs for top commit: hebasto: ACK 20276ca, I have reviewed the code and it looks OK, I agree it can be merged. Tree-SHA512: 2d91d1c962af0286d835d92a56396a27ea00e9d061b869eaff9421b448a083b0513828e1d4df7504396896057bf1e344e180a50271a5cfd1e1c7b6527155b2bb
2 parents af7b077 + 20276ca commit 4efdbab

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

src/txdb.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) {
296296

297297
bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex)
298298
{
299+
AssertLockHeld(::cs_main);
299300
std::unique_ptr<CDBIterator> pcursor(NewIterator());
300-
301301
pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
302302

303303
// Load m_block_index
@@ -311,19 +311,16 @@ bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams,
311311
CBlockIndex* pindexNew = insertBlockIndex(diskindex.GetBlockHash());
312312
pindexNew->pprev = insertBlockIndex(diskindex.hashPrev);
313313
pindexNew->nHeight = diskindex.nHeight;
314+
pindexNew->nFile = diskindex.nFile;
315+
pindexNew->nDataPos = diskindex.nDataPos;
316+
pindexNew->nUndoPos = diskindex.nUndoPos;
314317
pindexNew->nVersion = diskindex.nVersion;
315318
pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
316319
pindexNew->nTime = diskindex.nTime;
317320
pindexNew->nBits = diskindex.nBits;
318321
pindexNew->nNonce = diskindex.nNonce;
322+
pindexNew->nStatus = diskindex.nStatus;
319323
pindexNew->nTx = diskindex.nTx;
320-
{
321-
LOCK(::cs_main);
322-
pindexNew->nFile = diskindex.nFile;
323-
pindexNew->nDataPos = diskindex.nDataPos;
324-
pindexNew->nUndoPos = diskindex.nUndoPos;
325-
pindexNew->nStatus = diskindex.nStatus;
326-
}
327324

328325
if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, consensusParams)) {
329326
return error("%s: CheckProofOfWork failed: %s", __func__, pindexNew->ToString());

src/txdb.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ class CBlockTreeDB : public CDBWrapper
8686
void ReadReindexing(bool &fReindexing);
8787
bool WriteFlag(const std::string &name, bool fValue);
8888
bool ReadFlag(const std::string &name, bool &fValue);
89-
bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex);
89+
bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex)
90+
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
9091
};
9192

9293
std::optional<bilingual_str> CheckLegacyTxindex(CBlockTreeDB& block_tree_db);

0 commit comments

Comments
 (0)