mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-19 23:03:45 +01:00
Merge bitcoin/bitcoin#33870: refactor: remove incorrect lifetimebounds
99d012ec80refactor: return reference instead of pointer (Andrew Toth)f743e6c5ddrefactor: add missing LIFETIMEBOUND annotation for parameter (Andrew Toth)141117f5e8refactor: remove incorrect LIFETIMEBOUND annotations (Andrew Toth) Pull request description: The [developer-notes say](https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#lifetimebound): > You can use the attribute by adding a `LIFETIMEBOUND` annotation defined in `src/attributes.h`; please grep the codebase for examples. While grepping, I found an incorrect usage of the `LIFETIMEBOUND` annotation on `BlockManager::CheckBlockDataAvailability`. This could be misleading about usage for other greppers. As I was looking, I also noticed a missing `LIFETIMEBOUND` on `BlockManager::GetFirstBlock`. While looking more closely at that method, it should return a reference instead of a pointer. The only reason to return a pointer is if it can be null. ACKs for top commit: maflcko: review ACK99d012ec80💧 l0rinc: ACK99d012ec80stickies-v: ACK99d012ec80optout21: ACK99d012ec80vasild: ACK99d012ec80Tree-SHA512: d6c56ee223d6614d52ee6cf5cd66e787125c98c6ae37705a17e51a6e15774e260ac55b3d60f2fc818132e766ad98dd94232d6c8829119f628498e9d0d2bd977f
This commit is contained in:
@@ -588,7 +588,7 @@ bool BlockManager::IsBlockPruned(const CBlockIndex& block) const
|
||||
return m_have_pruned && !(block.nStatus & BLOCK_HAVE_DATA) && (block.nTx > 0);
|
||||
}
|
||||
|
||||
const CBlockIndex* BlockManager::GetFirstBlock(const CBlockIndex& upper_block, uint32_t status_mask, const CBlockIndex* lower_block) const
|
||||
const CBlockIndex& BlockManager::GetFirstBlock(const CBlockIndex& upper_block, uint32_t status_mask, const CBlockIndex* lower_block) const
|
||||
{
|
||||
AssertLockHeld(::cs_main);
|
||||
const CBlockIndex* last_block = &upper_block;
|
||||
@@ -596,7 +596,7 @@ const CBlockIndex* BlockManager::GetFirstBlock(const CBlockIndex& upper_block, u
|
||||
while (last_block->pprev && ((last_block->pprev->nStatus & status_mask) == status_mask)) {
|
||||
if (lower_block) {
|
||||
// Return if we reached the lower_block
|
||||
if (last_block == lower_block) return lower_block;
|
||||
if (last_block == lower_block) return *lower_block;
|
||||
// if range was surpassed, means that 'lower_block' is not part of the 'upper_block' chain
|
||||
// and so far this is not allowed.
|
||||
assert(last_block->nHeight >= lower_block->nHeight);
|
||||
@@ -604,13 +604,13 @@ const CBlockIndex* BlockManager::GetFirstBlock(const CBlockIndex& upper_block, u
|
||||
last_block = last_block->pprev;
|
||||
}
|
||||
assert(last_block != nullptr);
|
||||
return last_block;
|
||||
return *last_block;
|
||||
}
|
||||
|
||||
bool BlockManager::CheckBlockDataAvailability(const CBlockIndex& upper_block, const CBlockIndex& lower_block)
|
||||
{
|
||||
if (!(upper_block.nStatus & BLOCK_HAVE_DATA)) return false;
|
||||
return GetFirstBlock(upper_block, BLOCK_HAVE_DATA, &lower_block) == &lower_block;
|
||||
return &GetFirstBlock(upper_block, BLOCK_HAVE_DATA, &lower_block) == &lower_block;
|
||||
}
|
||||
|
||||
// If we're using -prune with -reindex, then delete block files that will be ignored by the
|
||||
|
||||
@@ -402,7 +402,7 @@ public:
|
||||
//! Check if all blocks in the [upper_block, lower_block] range have data available.
|
||||
//! The caller is responsible for ensuring that lower_block is an ancestor of upper_block
|
||||
//! (part of the same chain).
|
||||
bool CheckBlockDataAvailability(const CBlockIndex& upper_block LIFETIMEBOUND, const CBlockIndex& lower_block LIFETIMEBOUND) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
|
||||
bool CheckBlockDataAvailability(const CBlockIndex& upper_block, const CBlockIndex& lower_block) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
|
||||
|
||||
/**
|
||||
* @brief Returns the earliest block with specified `status_mask` flags set after
|
||||
@@ -422,14 +422,14 @@ public:
|
||||
* @param lower_block The earliest possible block to return. If null, the
|
||||
* search can extend to the genesis block.
|
||||
*
|
||||
* @return A non-null pointer to the earliest block between `upper_block`
|
||||
* @return A reference to the earliest block between `upper_block`
|
||||
* and `lower_block`, inclusive, such that every block between the
|
||||
* returned block and `upper_block` has `status_mask` flags set.
|
||||
*/
|
||||
const CBlockIndex* GetFirstBlock(
|
||||
const CBlockIndex& GetFirstBlock(
|
||||
const CBlockIndex& upper_block LIFETIMEBOUND,
|
||||
uint32_t status_mask,
|
||||
const CBlockIndex* lower_block = nullptr
|
||||
const CBlockIndex* lower_block LIFETIMEBOUND = nullptr
|
||||
) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
|
||||
|
||||
/** True if any block files have ever been pruned. */
|
||||
|
||||
@@ -870,7 +870,7 @@ std::optional<int> GetPruneHeight(const BlockManager& blockman, const CChain& ch
|
||||
// If the chain tip is pruned, everything is pruned.
|
||||
if (!((chain_tip->nStatus & BLOCK_HAVE_MASK) == BLOCK_HAVE_MASK)) return chain_tip->nHeight;
|
||||
|
||||
const auto& first_unpruned{*CHECK_NONFATAL(blockman.GetFirstBlock(*chain_tip, /*status_mask=*/BLOCK_HAVE_MASK, first_block))};
|
||||
const auto& first_unpruned{blockman.GetFirstBlock(*chain_tip, /*status_mask=*/BLOCK_HAVE_MASK, first_block)};
|
||||
if (&first_unpruned == first_block) {
|
||||
// All blocks between first_block and chain_tip have data, so nothing is pruned.
|
||||
return std::nullopt;
|
||||
@@ -3116,8 +3116,8 @@ static RPCHelpMan dumptxoutset()
|
||||
if (node.chainman->m_blockman.IsPruneMode()) {
|
||||
LOCK(node.chainman->GetMutex());
|
||||
const CBlockIndex* current_tip{node.chainman->ActiveChain().Tip()};
|
||||
const CBlockIndex* first_block{node.chainman->m_blockman.GetFirstBlock(*current_tip, /*status_mask=*/BLOCK_HAVE_MASK)};
|
||||
if (first_block->nHeight > target_index->nHeight) {
|
||||
const CBlockIndex& first_block{node.chainman->m_blockman.GetFirstBlock(*current_tip, /*status_mask=*/BLOCK_HAVE_MASK)};
|
||||
if (first_block.nHeight > target_index->nHeight) {
|
||||
throw JSONRPCError(RPC_MISC_ERROR, "Could not roll back to requested height since necessary block data is already pruned.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ BOOST_FIXTURE_TEST_CASE(blockmanager_block_data_availability, TestChain100Setup)
|
||||
};
|
||||
|
||||
// 1) Return genesis block when all blocks are available
|
||||
BOOST_CHECK_EQUAL(blockman.GetFirstBlock(tip, BLOCK_HAVE_DATA), chainman->ActiveChain()[0]);
|
||||
BOOST_CHECK_EQUAL(&blockman.GetFirstBlock(tip, BLOCK_HAVE_DATA), chainman->ActiveChain()[0]);
|
||||
BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *chainman->ActiveChain()[0]));
|
||||
|
||||
// 2) Check lower_block when all blocks are available
|
||||
@@ -133,7 +133,7 @@ BOOST_FIXTURE_TEST_CASE(blockmanager_block_data_availability, TestChain100Setup)
|
||||
func_prune_blocks(last_pruned_block);
|
||||
|
||||
// 3) The last block not pruned is in-between upper-block and the genesis block
|
||||
BOOST_CHECK_EQUAL(blockman.GetFirstBlock(tip, BLOCK_HAVE_DATA), first_available_block);
|
||||
BOOST_CHECK_EQUAL(&blockman.GetFirstBlock(tip, BLOCK_HAVE_DATA), first_available_block);
|
||||
BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *first_available_block));
|
||||
BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *last_pruned_block));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user