Skip to content

Commit 6ea5682

Browse files
jonatackvasild
andcommitted
Guard CBlockIndex::nStatus/nFile/nDataPos/nUndoPos by cs_main
Co-authored-by: Vasil Dimov <[email protected]>
1 parent 5d59ae0 commit 6ea5682

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

src/chain.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,13 @@ class CBlockIndex
164164
int nHeight{0};
165165

166166
//! Which # file this block is stored in (blk?????.dat)
167-
int nFile{0};
167+
int nFile GUARDED_BY(::cs_main){0};
168168

169169
//! Byte offset within blk?????.dat where this block's data is stored
170-
unsigned int nDataPos{0};
170+
unsigned int nDataPos GUARDED_BY(::cs_main){0};
171171

172172
//! Byte offset within rev?????.dat where this block's undo data is stored
173-
unsigned int nUndoPos{0};
173+
unsigned int nUndoPos GUARDED_BY(::cs_main){0};
174174

175175
//! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
176176
arith_uint256 nChainWork{};
@@ -198,7 +198,7 @@ class CBlockIndex
198198
//! load to avoid the block index being spuriously rewound.
199199
//! @sa NeedsRedownload
200200
//! @sa ActivateSnapshot
201-
uint32_t nStatus{0};
201+
uint32_t nStatus GUARDED_BY(::cs_main){0};
202202

203203
//! block header
204204
int32_t nVersion{0};
@@ -382,6 +382,7 @@ class CDiskBlockIndex : public CBlockIndex
382382

383383
SERIALIZE_METHODS(CDiskBlockIndex, obj)
384384
{
385+
LOCK(::cs_main);
385386
int _nVersion = s.GetVersion();
386387
if (!(s.GetType() & SER_GETHASH)) READWRITE(VARINT_MODE(_nVersion, VarIntMode::NONNEGATIVE_SIGNED));
387388

src/rpc/blockchain.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,8 @@ static RPCHelpMan getblockfrompeer()
818818
throw JSONRPCError(RPC_MISC_ERROR, "Block header missing");
819819
}
820820

821-
if (index->nStatus & BLOCK_HAVE_DATA) {
821+
const bool block_has_data = WITH_LOCK(::cs_main, return index->nStatus & BLOCK_HAVE_DATA);
822+
if (block_has_data) {
822823
throw JSONRPCError(RPC_MISC_ERROR, "Block already downloaded");
823824
}
824825

src/rpc/rawtransaction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ static RPCHelpMan getrawtransaction()
240240
if (!tx) {
241241
std::string errmsg;
242242
if (blockindex) {
243-
if (!(blockindex->nStatus & BLOCK_HAVE_DATA)) {
243+
const bool block_has_data = WITH_LOCK(::cs_main, return blockindex->nStatus & BLOCK_HAVE_DATA);
244+
if (!block_has_data) {
244245
throw JSONRPCError(RPC_MISC_ERROR, "Block not available");
245246
}
246247
errmsg = "No such transaction found in the provided block";

src/test/interfaces_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ BOOST_AUTO_TEST_CASE(findCommonAncestor)
123123

124124
BOOST_AUTO_TEST_CASE(hasBlocks)
125125
{
126+
LOCK(::cs_main);
126127
auto& chain = m_node.chain;
127128
const CChain& active = Assert(m_node.chainman)->ActiveChain();
128129

src/txdb.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,23 @@ 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;
317314
pindexNew->nVersion = diskindex.nVersion;
318315
pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
319316
pindexNew->nTime = diskindex.nTime;
320317
pindexNew->nBits = diskindex.nBits;
321318
pindexNew->nNonce = diskindex.nNonce;
322-
pindexNew->nStatus = diskindex.nStatus;
323319
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+
}
324327

325-
if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, consensusParams))
328+
if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, consensusParams)) {
326329
return error("%s: CheckProofOfWork failed: %s", __func__, pindexNew->ToString());
330+
}
327331

328332
pcursor->Next();
329333
} else {

0 commit comments

Comments
 (0)