Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
251// Track work on the redownloaded chain252m_redownload_chain_work += GetBlockProof(header);254if (m_redownload_chain_work >= m_minimum_required_work) {255m_process_all_remaining_headers = true;256}258// If we're at a header for which we previously stored a commitment, verify259// it is correct. Failure will result in aborting download.260// Also, don't check commitments once we've gotten to our target blockhash;261// it's possible our peer has extended its chain between our first sync and262// our second, and we don't want to return failure after we've seen our263// target blockhash just because we ran out of commitments.264if (!m_process_all_remaining_headers && next_height % m_params.commitment_period == m_commit_offset) {265if (m_header_commitments.size() == 0) {266LogDebug(BCLog::NET, "Initial headers sync aborted with peer=%d: commitment overrun at height=%i (redownload phase)\n", m_id, next_height);267// Somehow our peer managed to feed us a different chain and268// we've run out of commitments.269return false;270}271bool commitment = m_hasher(header.GetHash()) & 1;272bool expected_commitment = m_header_commitments.front();273m_header_commitments.pop_front();274if (commitment != expected_commitment) {275LogDebug(BCLog::NET, "Initial headers sync aborted with peer=%d: commitment mismatch at height=%i (redownload phase)\n", m_id, next_height);276return false;277}278}280// Store this header for later processing.281m_redownloaded_headers.emplace_back(header);282m_redownload_buffer_last_height = next_height;283m_redownload_buffer_last_hash = header.GetHash();285return true;286}288std::vector<CBlockHeader> HeadersSyncState::PopHeadersReadyForAcceptance()289{290std::vector<CBlockHeader> ret;292Assume(m_download_state == State::REDOWNLOAD);293if (m_download_state != State::REDOWNLOAD) return ret;295while (m_redownloaded_headers.size() > m_params.redownload_buffer_size ||296(m_redownloaded_headers.size() > 0 && m_process_all_remaining_headers)) {297ret.emplace_back(m_redownloaded_headers.front().GetFullHeader(m_redownload_buffer_first_prev_hash));298m_redownloaded_headers.pop_front();299m_redownload_buffer_first_prev_hash = ret.back().GetHash();300}301return ret;302}304CBlockLocator HeadersSyncState::NextHeadersRequestLocator() const305{306Assume(m_download_state != State::FINAL);307if (m_download_state == State::FINAL) return {};309auto chain_start_locator = LocatorEntries(&m_chain_start);310std::vector<uint256> locator;312if (m_download_state == State::PRESYNC) {313// During pre-synchronization, we continue from the last header received.314locator.push_back(m_last_header_received.GetHash());315}317if (m_download_state == State::REDOWNLOAD) {318// During redownload, we will download from the last received header that we stored.319locator.push_back(m_redownload_buffer_last_hash);320}322locator.insert(locator.end(), chain_start_locator.begin(), chain_start_locator.end());324return CBlockLocator{std::move(locator)};325}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.