unrelated, but in this line: A raw pointer that is never null is better passed as a reference.
0diff --git a/src/headerssync.cpp b/src/headerssync.cpp
1index 757b942cd9..18ffe65161 100644
2--- a/src/headerssync.cpp
3+++ b/src/headerssync.cpp
4@@ -23,14 +23,14 @@ constexpr size_t REDOWNLOAD_BUFFER_SIZE{13959}; // 13959/584 = ~23.9 commitments
5 static_assert(sizeof(CompressedHeader) == 48);
6
7 HeadersSyncState::HeadersSyncState(NodeId id, const Consensus::Params& consensus_params,
8- const CBlockIndex* chain_start, const arith_uint256& minimum_required_work) :
9+ const CBlockIndex& chain_start, const arith_uint256& minimum_required_work) :
10 m_id(id), m_consensus_params(consensus_params),
11 m_chain_start(chain_start),
12 m_minimum_required_work(minimum_required_work),
13- m_current_chain_work(chain_start->nChainWork),
14+ m_current_chain_work{chain_start.nChainWork},
15 m_commit_offset(GetRand<unsigned>(HEADER_COMMITMENT_PERIOD)),
16- m_last_header_received(m_chain_start->GetBlockHeader()),
17- m_current_height(chain_start->nHeight)
18+ m_last_header_received{chain_start.GetBlockHeader()},
19+ m_current_height{chain_start.nHeight}
20 {
21 // Estimate the number of blocks that could possibly exist on the peer's
22 // chain *right now* using 6 blocks/second (fastest blockrate given the MTP
23@@ -40,7 +40,7 @@ HeadersSyncState::HeadersSyncState(NodeId id, const Consensus::Params& consensus
24 // exceeds this bound, because it's not possible for a consensus-valid
25 // chain to be longer than this (at the current time -- in the future we
26 // could try again, if necessary, to sync a longer chain).
27- m_max_commitments = 6*(Ticks<std::chrono::seconds>(GetAdjustedTime() - NodeSeconds{std::chrono::seconds{chain_start->GetMedianTimePast()}}) + MAX_FUTURE_BLOCK_TIME) / HEADER_COMMITMENT_PERIOD;
28+ m_max_commitments = 6*(Ticks<std::chrono::seconds>(GetAdjustedTime() - NodeSeconds{std::chrono::seconds{chain_start.GetMedianTimePast()}}) + MAX_FUTURE_BLOCK_TIME) / HEADER_COMMITMENT_PERIOD;
29
30 LogPrint(BCLog::NET, "Initial headers sync started with peer=%d: height=%i, max_commitments=%i, min_work=%s\n", m_id, m_current_height, m_max_commitments, m_minimum_required_work.ToString());
31 }
32@@ -164,10 +164,10 @@ bool HeadersSyncState::ValidateAndStoreHeadersCommitments(const std::vector<CBlo
33
34 if (m_current_chain_work >= m_minimum_required_work) {
35 m_redownloaded_headers.clear();
36- m_redownload_buffer_last_height = m_chain_start->nHeight;
37- m_redownload_buffer_first_prev_hash = m_chain_start->GetBlockHash();
38- m_redownload_buffer_last_hash = m_chain_start->GetBlockHash();
39- m_redownload_chain_work = m_chain_start->nChainWork;
40+ m_redownload_buffer_last_height = m_chain_start.nHeight;
41+ m_redownload_buffer_first_prev_hash = m_chain_start.GetBlockHash();
42+ m_redownload_buffer_last_hash = m_chain_start.GetBlockHash();
43+ m_redownload_chain_work = m_chain_start.nChainWork;
44 m_download_state = State::REDOWNLOAD;
45 LogPrint(BCLog::NET, "Initial headers sync transition with peer=%d: reached sufficient work at height=%i, redownloading from height=%i\n", m_id, m_current_height, m_redownload_buffer_last_height);
46 }
47@@ -231,7 +231,7 @@ bool HeadersSyncState::ValidateAndStoreRedownloadedHeader(const CBlockHeader& he
48 if (!m_redownloaded_headers.empty()) {
49 previous_nBits = m_redownloaded_headers.back().nBits;
50 } else {
51- previous_nBits = m_chain_start->nBits;
52+ previous_nBits = m_chain_start.nBits;
53 }
54
55 if (!PermittedDifficultyTransition(m_consensus_params, next_height,
56@@ -298,7 +298,7 @@ CBlockLocator HeadersSyncState::NextHeadersRequestLocator() const
57 Assume(m_download_state != State::FINAL);
58 if (m_download_state == State::FINAL) return {};
59
60- auto chain_start_locator = LocatorEntries(m_chain_start);
61+ auto chain_start_locator = LocatorEntries(&m_chain_start);
62 std::vector<uint256> locator;
63
64 if (m_download_state == State::PRESYNC) {
65diff --git a/src/headerssync.h b/src/headerssync.h
66index 16da964246..6455b7dae6 100644
67--- a/src/headerssync.h
68+++ b/src/headerssync.h
69@@ -136,7 +136,7 @@ public:
70 * minimum_required_work: amount of chain work required to accept the chain
71 */
72 HeadersSyncState(NodeId id, const Consensus::Params& consensus_params,
73- const CBlockIndex* chain_start, const arith_uint256& minimum_required_work);
74+ const CBlockIndex& chain_start, const arith_uint256& minimum_required_work);
75
76 /** Result data structure for ProcessNextHeaders. */
77 struct ProcessingResult {
78@@ -208,7 +208,7 @@ private:
79 const Consensus::Params& m_consensus_params;
80
81 /** Store the last block in our block index that the peer's chain builds from */
82- const CBlockIndex* m_chain_start{nullptr};
83+ const CBlockIndex& m_chain_start;
84
85 /** Minimum work that we're looking for on this chain. */
86 const arith_uint256 m_minimum_required_work;
87diff --git a/src/net_processing.cpp b/src/net_processing.cpp
88index 8e80bd7aef..2dbac5f6cb 100644
89--- a/src/net_processing.cpp
90+++ b/src/net_processing.cpp
91@@ -647,7 +647,7 @@ private:
92 * otherwise.
93 */
94 bool TryLowWorkHeadersSync(Peer& peer, CNode& pfrom,
95- const CBlockIndex* chain_start_header,
96+ const CBlockIndex& chain_start_header,
97 std::vector<CBlockHeader>& headers)
98 EXCLUSIVE_LOCKS_REQUIRED(!peer.m_headers_sync_mutex, !m_peer_mutex, !m_headers_presync_mutex);
99
100@@ -2528,10 +2528,10 @@ bool PeerManagerImpl::IsContinuationOfLowWorkHeadersSync(Peer& peer, CNode& pfro
101 return false;
102 }
103
104-bool PeerManagerImpl::TryLowWorkHeadersSync(Peer& peer, CNode& pfrom, const CBlockIndex* chain_start_header, std::vector<CBlockHeader>& headers)
105+bool PeerManagerImpl::TryLowWorkHeadersSync(Peer& peer, CNode& pfrom, const CBlockIndex& chain_start_header, std::vector<CBlockHeader>& headers)
106 {
107 // Calculate the total work on this chain.
108- arith_uint256 total_work = chain_start_header->nChainWork + CalculateHeadersWork(headers);
109+ arith_uint256 total_work = chain_start_header.nChainWork + CalculateHeadersWork(headers);
110
111 // Our dynamic anti-DoS threshold (minimum work required on a headers chain
112 // before we'll store it)
113@@ -2561,7 +2561,7 @@ bool PeerManagerImpl::TryLowWorkHeadersSync(Peer& peer, CNode& pfrom, const CBlo
114 // process the headers using it as normal.
115 return IsContinuationOfLowWorkHeadersSync(peer, pfrom, headers);
116 } else {
117- LogPrint(BCLog::NET, "Ignoring low-work chain (height=%u) from peer=%d\n", chain_start_header->nHeight + headers.size(), pfrom.GetId());
118+ LogPrint(BCLog::NET, "Ignoring low-work chain (height=%u) from peer=%d\n", chain_start_header.nHeight + headers.size(), pfrom.GetId());
119 // Since this is a low-work headers chain, no further processing is required.
120 headers = {};
121 return true;
122@@ -2831,7 +2831,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
123 // Do anti-DoS checks to determine if we should process or store for later
124 // processing.
125 if (!already_validated_work && TryLowWorkHeadersSync(peer, pfrom,
126- chain_start_header, headers)) {
127+ *chain_start_header, headers)) {
128 // If we successfully started a low-work headers sync, then there
129 // should be no headers to process any further.
130 Assume(headers.empty());
131diff --git a/src/test/headers_sync_chainwork_tests.cpp b/src/test/headers_sync_chainwork_tests.cpp
132index 41241ebee2..f7be84a356 100644
133--- a/src/test/headers_sync_chainwork_tests.cpp
134+++ b/src/test/headers_sync_chainwork_tests.cpp
135@@ -85,7 +85,7 @@ BOOST_AUTO_TEST_CASE(headers_sync_state)
136 Params().GenesisBlock().nVersion, Params().GenesisBlock().nTime,
137 ArithToUint256(1), Params().GenesisBlock().nBits);
138
139- const CBlockIndex* chain_start = WITH_LOCK(::cs_main, return m_node.chainman->m_blockman.LookupBlockIndex(Params().GenesisBlock().GetHash()));
140+ const CBlockIndex& chain_start { *Assert(WITH_LOCK(::cs_main, return m_node.chainman->m_blockman.LookupBlockIndex(Params().GenesisBlock().GetHash())))};
141 std::vector<CBlockHeader> headers_batch;
142
143 // Feed the first chain to HeadersSyncState, by delivering 1 header