This is a janitorial refactor, a continuation of #34440. Input method parameters of type const CBlockIndex* are changed to reference const CBlockIndex& in some places where it makes no sense to call with a nullptr.
Motivation: slight improvement in code quality, maintainability and robustness.
- more explicit representation of the non-null input invariant
- less chance of nullptr-access error with future code changes
- more explicit checks at call sites for the non-null input invariant.
The following methods are affected:
LastCommonAncestor()in chain.hPeerManagerImpl::FindNextBlocks()(net_processing.cpp)PeerManagerImpl::TryDownloadingHistoricalBlocks()(net_processing.cpp)BlockFilterIndex::LookupFilterRange(),BlockFilterIndex::LookupFilterHashRange()(blockfilterindex.h), andLookupRange()in blockfilterindex.cppBaseIndex::ProcessBlock()(base.h)
General guidelines:
- When a method has an input of type
const CBlockIndex*, and by contract it cannot be nullptr, change it toconst CBlockIndex&. The reference type is an implicit documentation of the nun-nullptr invariant. - When calling a method with reference parameter, ensure that it cannot be nullptr (it is often derived from a pointer), by/because:
- explicit prior check and error handling
- an assert/Assert/Assume
- itself being a non-nullptr invariant input
- it is being an output from a method with no-nullptr output invariant
- For local variables, use reference types whenever possible, for non-nullptr references
- For technical reasons pointers are still used even in some non-nullptr cases:
- containers/variables that can staty unitialized initially
- containers that need
reserve() - using reference type in
std::setwould result in some copy constructor calls std::optionalcannot hold a reference type