Skip to content

Commit fa604eb

Browse files
author
MarcoFalke
committed
refactor: Use reference instead of pointer in IsBlockPruned
This makes it harder to pass nullptr and cause issues such as bitcoin@dde7ac5
1 parent dce1dfb commit fa604eb

File tree

5 files changed

+11
-13
lines changed

5 files changed

+11
-13
lines changed

src/node/blockstorage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,10 +582,10 @@ const CBlockIndex* BlockManager::GetLastCheckpoint(const CCheckpointData& data)
582582
return nullptr;
583583
}
584584

585-
bool BlockManager::IsBlockPruned(const CBlockIndex* pblockindex)
585+
bool BlockManager::IsBlockPruned(const CBlockIndex& block)
586586
{
587587
AssertLockHeld(::cs_main);
588-
return (m_have_pruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0);
588+
return m_have_pruned && !(block.nStatus & BLOCK_HAVE_DATA) && (block.nTx > 0);
589589
}
590590

591591
const CBlockIndex* BlockManager::GetFirstStoredBlock(const CBlockIndex& upper_block, const CBlockIndex* lower_block)

src/node/blockstorage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ class BlockManager
344344
bool m_have_pruned = false;
345345

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

349349
//! Create or update a prune lock identified by its name
350350
void UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);

src/rest.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,9 @@ static bool rest_block(const std::any& context,
304304
if (!pblockindex) {
305305
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
306306
}
307-
308-
if (chainman.m_blockman.IsBlockPruned(pblockindex))
307+
if (chainman.m_blockman.IsBlockPruned(*pblockindex)) {
309308
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)");
310-
309+
}
311310
}
312311

313312
if (!chainman.m_blockman.ReadBlockFromDisk(block, *pblockindex)) {

src/rpc/blockchain.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ UniValue blockToJSON(BlockManager& blockman, const CBlock& block, const CBlockIn
181181
case TxVerbosity::SHOW_DETAILS:
182182
case TxVerbosity::SHOW_DETAILS_AND_PREVOUT:
183183
CBlockUndo blockUndo;
184-
const bool is_not_pruned{WITH_LOCK(::cs_main, return !blockman.IsBlockPruned(blockindex))};
184+
const bool is_not_pruned{WITH_LOCK(::cs_main, return !blockman.IsBlockPruned(*blockindex))};
185185
const bool have_undo{is_not_pruned && blockman.UndoReadFromDisk(blockUndo, *blockindex)};
186186

187187
for (size_t i = 0; i < block.vtx.size(); ++i) {
@@ -581,7 +581,7 @@ static CBlock GetBlockChecked(BlockManager& blockman, const CBlockIndex* pblocki
581581
CBlock block;
582582
{
583583
LOCK(cs_main);
584-
if (blockman.IsBlockPruned(pblockindex)) {
584+
if (blockman.IsBlockPruned(*pblockindex)) {
585585
throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)");
586586
}
587587
}
@@ -605,7 +605,7 @@ static CBlockUndo GetUndoChecked(BlockManager& blockman, const CBlockIndex* pblo
605605

606606
{
607607
LOCK(cs_main);
608-
if (blockman.IsBlockPruned(pblockindex)) {
608+
if (blockman.IsBlockPruned(*pblockindex)) {
609609
throw JSONRPCError(RPC_MISC_ERROR, "Undo data not available (pruned data)");
610610
}
611611
}

src/rpc/rawtransaction.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,18 +394,17 @@ static RPCHelpMan getrawtransaction()
394394
// If request is verbosity >= 1 but no blockhash was given, then look up the blockindex
395395
if (request.params[2].isNull()) {
396396
LOCK(cs_main);
397-
blockindex = chainman.m_blockman.LookupBlockIndex(hash_block);
397+
blockindex = chainman.m_blockman.LookupBlockIndex(hash_block); // May be nullptr for mempool transactions
398398
}
399-
if (verbosity == 1 || !blockindex) {
399+
if (verbosity == 1) {
400400
TxToJSON(*tx, hash_block, result, chainman.ActiveChainstate());
401401
return result;
402402
}
403403

404404
CBlockUndo blockUndo;
405405
CBlock block;
406-
const bool is_block_pruned{WITH_LOCK(cs_main, return chainman.m_blockman.IsBlockPruned(blockindex))};
407406

408-
if (tx->IsCoinBase() || is_block_pruned ||
407+
if (tx->IsCoinBase() || !blockindex || WITH_LOCK(::cs_main, return chainman.m_blockman.IsBlockPruned(*blockindex)) ||
409408
!(chainman.m_blockman.UndoReadFromDisk(blockUndo, *blockindex) && chainman.m_blockman.ReadBlockFromDisk(block, *blockindex))) {
410409
TxToJSON(*tx, hash_block, result, chainman.ActiveChainstate());
411410
return result;

0 commit comments

Comments
 (0)