Skip to content

Commit ac71df4

Browse files
committed
Merge bitcoin/bitcoin#33870: refactor: remove incorrect lifetimebounds
99d012e refactor: return reference instead of pointer (Andrew Toth) f743e6c refactor: add missing LIFETIMEBOUND annotation for parameter (Andrew Toth) 141117f refactor: 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 ACK 99d012e 💧 l0rinc: ACK 99d012e stickies-v: ACK 99d012e optout21: ACK 99d012e vasild: ACK 99d012e Tree-SHA512: d6c56ee223d6614d52ee6cf5cd66e787125c98c6ae37705a17e51a6e15774e260ac55b3d60f2fc818132e766ad98dd94232d6c8829119f628498e9d0d2bd977f
2 parents 6cdb51c + 99d012e commit ac71df4

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

src/node/blockstorage.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,29 +588,29 @@ bool BlockManager::IsBlockPruned(const CBlockIndex& block) const
588588
return m_have_pruned && !(block.nStatus & BLOCK_HAVE_DATA) && (block.nTx > 0);
589589
}
590590

591-
const CBlockIndex* BlockManager::GetFirstBlock(const CBlockIndex& upper_block, uint32_t status_mask, const CBlockIndex* lower_block) const
591+
const CBlockIndex& BlockManager::GetFirstBlock(const CBlockIndex& upper_block, uint32_t status_mask, const CBlockIndex* lower_block) const
592592
{
593593
AssertLockHeld(::cs_main);
594594
const CBlockIndex* last_block = &upper_block;
595595
assert((last_block->nStatus & status_mask) == status_mask); // 'upper_block' must satisfy the status mask
596596
while (last_block->pprev && ((last_block->pprev->nStatus & status_mask) == status_mask)) {
597597
if (lower_block) {
598598
// Return if we reached the lower_block
599-
if (last_block == lower_block) return lower_block;
599+
if (last_block == lower_block) return *lower_block;
600600
// if range was surpassed, means that 'lower_block' is not part of the 'upper_block' chain
601601
// and so far this is not allowed.
602602
assert(last_block->nHeight >= lower_block->nHeight);
603603
}
604604
last_block = last_block->pprev;
605605
}
606606
assert(last_block != nullptr);
607-
return last_block;
607+
return *last_block;
608608
}
609609

610610
bool BlockManager::CheckBlockDataAvailability(const CBlockIndex& upper_block, const CBlockIndex& lower_block)
611611
{
612612
if (!(upper_block.nStatus & BLOCK_HAVE_DATA)) return false;
613-
return GetFirstBlock(upper_block, BLOCK_HAVE_DATA, &lower_block) == &lower_block;
613+
return &GetFirstBlock(upper_block, BLOCK_HAVE_DATA, &lower_block) == &lower_block;
614614
}
615615

616616
// If we're using -prune with -reindex, then delete block files that will be ignored by the

src/node/blockstorage.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ class BlockManager
402402
//! Check if all blocks in the [upper_block, lower_block] range have data available.
403403
//! The caller is responsible for ensuring that lower_block is an ancestor of upper_block
404404
//! (part of the same chain).
405-
bool CheckBlockDataAvailability(const CBlockIndex& upper_block LIFETIMEBOUND, const CBlockIndex& lower_block LIFETIMEBOUND) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
405+
bool CheckBlockDataAvailability(const CBlockIndex& upper_block, const CBlockIndex& lower_block) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
406406

407407
/**
408408
* @brief Returns the earliest block with specified `status_mask` flags set after
@@ -422,14 +422,14 @@ class BlockManager
422422
* @param lower_block The earliest possible block to return. If null, the
423423
* search can extend to the genesis block.
424424
*
425-
* @return A non-null pointer to the earliest block between `upper_block`
425+
* @return A reference to the earliest block between `upper_block`
426426
* and `lower_block`, inclusive, such that every block between the
427427
* returned block and `upper_block` has `status_mask` flags set.
428428
*/
429-
const CBlockIndex* GetFirstBlock(
429+
const CBlockIndex& GetFirstBlock(
430430
const CBlockIndex& upper_block LIFETIMEBOUND,
431431
uint32_t status_mask,
432-
const CBlockIndex* lower_block = nullptr
432+
const CBlockIndex* lower_block LIFETIMEBOUND = nullptr
433433
) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
434434

435435
/** True if any block files have ever been pruned. */

src/rpc/blockchain.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ std::optional<int> GetPruneHeight(const BlockManager& blockman, const CChain& ch
870870
// If the chain tip is pruned, everything is pruned.
871871
if (!((chain_tip->nStatus & BLOCK_HAVE_MASK) == BLOCK_HAVE_MASK)) return chain_tip->nHeight;
872872

873-
const auto& first_unpruned{*CHECK_NONFATAL(blockman.GetFirstBlock(*chain_tip, /*status_mask=*/BLOCK_HAVE_MASK, first_block))};
873+
const auto& first_unpruned{blockman.GetFirstBlock(*chain_tip, /*status_mask=*/BLOCK_HAVE_MASK, first_block)};
874874
if (&first_unpruned == first_block) {
875875
// All blocks between first_block and chain_tip have data, so nothing is pruned.
876876
return std::nullopt;
@@ -3116,8 +3116,8 @@ static RPCHelpMan dumptxoutset()
31163116
if (node.chainman->m_blockman.IsPruneMode()) {
31173117
LOCK(node.chainman->GetMutex());
31183118
const CBlockIndex* current_tip{node.chainman->ActiveChain().Tip()};
3119-
const CBlockIndex* first_block{node.chainman->m_blockman.GetFirstBlock(*current_tip, /*status_mask=*/BLOCK_HAVE_MASK)};
3120-
if (first_block->nHeight > target_index->nHeight) {
3119+
const CBlockIndex& first_block{node.chainman->m_blockman.GetFirstBlock(*current_tip, /*status_mask=*/BLOCK_HAVE_MASK)};
3120+
if (first_block.nHeight > target_index->nHeight) {
31213121
throw JSONRPCError(RPC_MISC_ERROR, "Could not roll back to requested height since necessary block data is already pruned.");
31223122
}
31233123
}

src/test/blockmanager_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ BOOST_FIXTURE_TEST_CASE(blockmanager_block_data_availability, TestChain100Setup)
119119
};
120120

121121
// 1) Return genesis block when all blocks are available
122-
BOOST_CHECK_EQUAL(blockman.GetFirstBlock(tip, BLOCK_HAVE_DATA), chainman->ActiveChain()[0]);
122+
BOOST_CHECK_EQUAL(&blockman.GetFirstBlock(tip, BLOCK_HAVE_DATA), chainman->ActiveChain()[0]);
123123
BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *chainman->ActiveChain()[0]));
124124

125125
// 2) Check lower_block when all blocks are available
@@ -133,7 +133,7 @@ BOOST_FIXTURE_TEST_CASE(blockmanager_block_data_availability, TestChain100Setup)
133133
func_prune_blocks(last_pruned_block);
134134

135135
// 3) The last block not pruned is in-between upper-block and the genesis block
136-
BOOST_CHECK_EQUAL(blockman.GetFirstBlock(tip, BLOCK_HAVE_DATA), first_available_block);
136+
BOOST_CHECK_EQUAL(&blockman.GetFirstBlock(tip, BLOCK_HAVE_DATA), first_available_block);
137137
BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *first_available_block));
138138
BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *last_pruned_block));
139139
}

0 commit comments

Comments
 (0)