Skip to content

Commit

Permalink
Require IsBlockPruned() to hold mutex cs_main
Browse files Browse the repository at this point in the history
Summary:
Co-authored-by: Vasil Dimov <[email protected]>

This is a partial backport of [[bitcoin/bitcoin#22932 | core#22932]]
bitcoin/bitcoin@eaeeb88

Depends on D13036

Test Plan:
With clang and DEBUG:

`ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D13037
  • Loading branch information
jonatack authored and PiRK committed Jan 24, 2023
1 parent 20237de commit bd5235a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ BlockManager::GetLastCheckpoint(const CCheckpointData &data) {
}

bool IsBlockPruned(const CBlockIndex *pblockindex) {
AssertLockHeld(::cs_main);
return (fHavePruned && !pblockindex->nStatus.hasData() &&
pblockindex->nTx > 0);
}
Expand Down
3 changes: 2 additions & 1 deletion src/node/blockstorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ class BlockManager {
};

//! Check whether the block associated with this index entry is pruned or not.
bool IsBlockPruned(const CBlockIndex *pblockindex);
bool IsBlockPruned(const CBlockIndex *pblockindex)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);

void CleanupBlockRevFiles();

Expand Down
13 changes: 9 additions & 4 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ UniValue blockToJSON(const CBlock &block, const CBlockIndex *tip,
UniValue txs(UniValue::VARR);
if (txDetails) {
CBlockUndo blockUndo;
const bool have_undo = !IsBlockPruned(blockindex) &&
UndoReadFromDisk(blockUndo, blockindex);
const bool have_undo{WITH_LOCK(
::cs_main, return !IsBlockPruned(blockindex) &&
UndoReadFromDisk(blockUndo, blockindex))};
for (size_t i = 0; i < block.vtx.size(); ++i) {
const CTransactionRef &tx = block.vtx.at(i);
// coinbase transaction (i == 0) doesn't have undo data
Expand Down Expand Up @@ -1065,7 +1066,9 @@ static RPCHelpMan getblockheader() {
}

static CBlock GetBlockChecked(const Config &config,
const CBlockIndex *pblockindex) {
const CBlockIndex *pblockindex)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
AssertLockHeld(::cs_main);
CBlock block;
if (IsBlockPruned(pblockindex)) {
throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)");
Expand All @@ -1082,7 +1085,9 @@ static CBlock GetBlockChecked(const Config &config,
return block;
}

static CBlockUndo GetUndoChecked(const CBlockIndex *pblockindex) {
static CBlockUndo GetUndoChecked(const CBlockIndex *pblockindex)
EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
AssertLockHeld(::cs_main);
CBlockUndo blockUndo;
if (IsBlockPruned(pblockindex)) {
throw JSONRPCError(RPC_MISC_ERROR,
Expand Down

0 comments on commit bd5235a

Please sign in to comment.