MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / ValidateAndStoreHeadersCommitments

Method ValidateAndStoreHeadersCommitments

src/headerssync.cpp:139–175  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

137}
138
139bool HeadersSyncState::ValidateAndStoreHeadersCommitments(std::span<const CBlockHeader> headers)
140{
141 // The caller should not give us an empty set of headers.
142 Assume(headers.size() > 0);
143 if (headers.size() == 0) return true;
144
145 Assume(m_download_state == State::PRESYNC);
146 if (m_download_state != State::PRESYNC) return false;
147
148 if (headers[0].hashPrevBlock != m_last_header_received.GetHash()) {
149 // Somehow our peer gave us a header that doesn't connect.
150 // This might be benign -- perhaps our peer reorged away from the chain
151 // they were on. Give up on this sync for now (likely we will start a
152 // new sync with a new starting point).
153 LogDebug(BCLog::NET, "Initial headers sync aborted with peer=%d: non-continuous headers at height=%i (presync phase)\n", m_id, m_current_height);
154 return false;
155 }
156
157 // If it does connect, (minimally) validate and occasionally store
158 // commitments.
159 for (const auto& hdr : headers) {
160 if (!ValidateAndProcessSingleHeader(hdr)) {
161 return false;
162 }
163 }
164
165 if (m_current_chain_work >= m_minimum_required_work) {
166 m_redownloaded_headers.clear();
167 m_redownload_buffer_last_height = m_chain_start.nHeight;
168 m_redownload_buffer_first_prev_hash = m_chain_start.GetBlockHash();
169 m_redownload_buffer_last_hash = m_chain_start.GetBlockHash();
170 m_redownload_chain_work = m_chain_start.nChainWork;
171 m_download_state = State::REDOWNLOAD;
172 LogDebug(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);
173 }
174 return true;
175}
176
177bool HeadersSyncState::ValidateAndProcessSingleHeader(const CBlockHeader& current)
178{

Callers

nothing calls this directly

Calls 4

sizeMethod · 0.45
GetHashMethod · 0.45
clearMethod · 0.45
GetBlockHashMethod · 0.45

Tested by

no test coverage detected