mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 15:04:44 +01:00
refactor: Use reference instead of pointer in IsBlockPruned
This makes it harder to pass nullptr and cause issues such as
dde7ac5c70
This commit is contained in:
parent
dce1dfbc47
commit
fa604eb6cf
5 changed files with 11 additions and 13 deletions
|
@ -582,10 +582,10 @@ const CBlockIndex* BlockManager::GetLastCheckpoint(const CCheckpointData& data)
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
bool BlockManager::IsBlockPruned(const CBlockIndex* pblockindex)
|
||||
bool BlockManager::IsBlockPruned(const CBlockIndex& block)
|
||||
{
|
||||
AssertLockHeld(::cs_main);
|
||||
return (m_have_pruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0);
|
||||
return m_have_pruned && !(block.nStatus & BLOCK_HAVE_DATA) && (block.nTx > 0);
|
||||
}
|
||||
|
||||
const CBlockIndex* BlockManager::GetFirstStoredBlock(const CBlockIndex& upper_block, const CBlockIndex* lower_block)
|
||||
|
|
|
@ -344,7 +344,7 @@ public:
|
|||
bool m_have_pruned = false;
|
||||
|
||||
//! Check whether the block associated with this index entry is pruned or not.
|
||||
bool IsBlockPruned(const CBlockIndex* pblockindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
|
||||
bool IsBlockPruned(const CBlockIndex& block) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
|
||||
|
||||
//! Create or update a prune lock identified by its name
|
||||
void UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
|
||||
|
|
|
@ -304,10 +304,9 @@ static bool rest_block(const std::any& context,
|
|||
if (!pblockindex) {
|
||||
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
|
||||
}
|
||||
|
||||
if (chainman.m_blockman.IsBlockPruned(pblockindex))
|
||||
if (chainman.m_blockman.IsBlockPruned(*pblockindex)) {
|
||||
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (!chainman.m_blockman.ReadBlockFromDisk(block, *pblockindex)) {
|
||||
|
|
|
@ -181,7 +181,7 @@ UniValue blockToJSON(BlockManager& blockman, const CBlock& block, const CBlockIn
|
|||
case TxVerbosity::SHOW_DETAILS:
|
||||
case TxVerbosity::SHOW_DETAILS_AND_PREVOUT:
|
||||
CBlockUndo blockUndo;
|
||||
const bool is_not_pruned{WITH_LOCK(::cs_main, return !blockman.IsBlockPruned(blockindex))};
|
||||
const bool is_not_pruned{WITH_LOCK(::cs_main, return !blockman.IsBlockPruned(*blockindex))};
|
||||
const bool have_undo{is_not_pruned && blockman.UndoReadFromDisk(blockUndo, *blockindex)};
|
||||
|
||||
for (size_t i = 0; i < block.vtx.size(); ++i) {
|
||||
|
@ -581,7 +581,7 @@ static CBlock GetBlockChecked(BlockManager& blockman, const CBlockIndex* pblocki
|
|||
CBlock block;
|
||||
{
|
||||
LOCK(cs_main);
|
||||
if (blockman.IsBlockPruned(pblockindex)) {
|
||||
if (blockman.IsBlockPruned(*pblockindex)) {
|
||||
throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)");
|
||||
}
|
||||
}
|
||||
|
@ -605,7 +605,7 @@ static CBlockUndo GetUndoChecked(BlockManager& blockman, const CBlockIndex* pblo
|
|||
|
||||
{
|
||||
LOCK(cs_main);
|
||||
if (blockman.IsBlockPruned(pblockindex)) {
|
||||
if (blockman.IsBlockPruned(*pblockindex)) {
|
||||
throw JSONRPCError(RPC_MISC_ERROR, "Undo data not available (pruned data)");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -394,18 +394,17 @@ static RPCHelpMan getrawtransaction()
|
|||
// If request is verbosity >= 1 but no blockhash was given, then look up the blockindex
|
||||
if (request.params[2].isNull()) {
|
||||
LOCK(cs_main);
|
||||
blockindex = chainman.m_blockman.LookupBlockIndex(hash_block);
|
||||
blockindex = chainman.m_blockman.LookupBlockIndex(hash_block); // May be nullptr for mempool transactions
|
||||
}
|
||||
if (verbosity == 1 || !blockindex) {
|
||||
if (verbosity == 1) {
|
||||
TxToJSON(*tx, hash_block, result, chainman.ActiveChainstate());
|
||||
return result;
|
||||
}
|
||||
|
||||
CBlockUndo blockUndo;
|
||||
CBlock block;
|
||||
const bool is_block_pruned{WITH_LOCK(cs_main, return chainman.m_blockman.IsBlockPruned(blockindex))};
|
||||
|
||||
if (tx->IsCoinBase() || is_block_pruned ||
|
||||
if (tx->IsCoinBase() || !blockindex || WITH_LOCK(::cs_main, return chainman.m_blockman.IsBlockPruned(*blockindex)) ||
|
||||
!(chainman.m_blockman.UndoReadFromDisk(blockUndo, *blockindex) && chainman.m_blockman.ReadBlockFromDisk(block, *blockindex))) {
|
||||
TxToJSON(*tx, hash_block, result, chainman.ActiveChainstate());
|
||||
return result;
|
||||
|
|
Loading…
Add table
Reference in a new issue