LCOV - code coverage report
Current view: top level - src - headerssync.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 90.1 % 151 136
Test Date: 2026-09-05 06:08:39 Functions: 100.0 % 8 8
Branches: 58.5 % 176 103

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2022-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <headerssync.h>
       6                 :             : 
       7                 :             : #include <pow.h>
       8                 :             : #include <util/check.h>
       9                 :             : #include <util/log.h>
      10                 :             : #include <util/time.h>
      11                 :             : #include <util/vector.h>
      12                 :             : 
      13                 :             : // Our memory analysis in headerssync-params.py assumes this many bytes for a
      14                 :             : // CompressedHeader (we should re-calculate parameters if we compress further).
      15                 :             : static_assert(sizeof(CompressedHeader) == 48);
      16                 :             : 
      17                 :          14 : HeadersSyncState::HeadersSyncState(NodeId id,
      18                 :             :                                    const Consensus::Params& consensus_params,
      19                 :             :                                    const HeadersSyncParams& params,
      20                 :             :                                    const CBlockIndex& chain_start,
      21                 :          14 :                                    const arith_uint256& minimum_required_work)
      22         [ -  + ]:          28 :     : m_commit_offset((assert(params.commitment_period > 0), // HeadersSyncParams field must be initialized to non-zero.
      23                 :          14 :                        FastRandomContext().randrange(params.commitment_period))),
      24                 :          14 :       m_id(id),
      25                 :          14 :       m_consensus_params(consensus_params),
      26                 :          14 :       m_params(params),
      27                 :          14 :       m_chain_start(chain_start),
      28                 :          14 :       m_minimum_required_work(minimum_required_work),
      29                 :          14 :       m_current_chain_work(chain_start.nChainWork),
      30                 :          14 :       m_last_header_received(m_chain_start.GetBlockHeader()),
      31         [ +  - ]:          14 :       m_current_height(chain_start.nHeight)
      32                 :             : {
      33                 :             :     // Estimate the number of blocks that could possibly exist on the peer's
      34                 :             :     // chain *right now* using 6 blocks/second (fastest blockrate given the MTP
      35                 :             :     // rule) times the number of seconds from the last allowed block until
      36                 :             :     // today. This serves as a memory bound on how many commitments we might
      37                 :             :     // store from this peer, and we can safely give up syncing if the peer
      38                 :             :     // exceeds this bound, because it's not possible for a consensus-valid
      39                 :             :     // chain to be longer than this (at the current time -- in the future we
      40                 :             :     // could try again, if necessary, to sync a longer chain).
      41                 :          14 :     const auto now{NodeClock::now()};
      42                 :          14 :     const int64_t max_seconds_since_start{Ticks<std::chrono::seconds>(now - NodeSeconds{std::chrono::seconds{chain_start.GetMedianTimePast()}})
      43                 :          14 :                                           + MAX_FUTURE_BLOCK_TIME};
      44         [ +  + ]:          14 :     if (max_seconds_since_start < 0) {
      45                 :           1 :         throw SystemClockError{strprintf(
      46                 :             :             "System clock is more than %d minutes behind chain start MTP (%s vs %s).",
      47         [ +  - ]:           1 :             MAX_FUTURE_BLOCK_TIME / 60,
      48         [ +  - ]:           2 :             FormatISO8601DateTime(TicksSinceEpoch<std::chrono::seconds>(now)),
      49         [ +  - ]:           3 :             FormatISO8601DateTime(chain_start.GetMedianTimePast()))};
      50                 :             :     }
      51                 :          13 :     m_max_commitments = 6 * max_seconds_since_start / m_params.commitment_period;
      52                 :             : 
      53   [ +  -  +  -  :          13 :     LogDebug(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());
             +  -  +  - ]
      54                 :          15 : }
      55                 :             : 
      56                 :             : /** Free any memory in use, and mark this object as no longer usable. This is
      57                 :             :  * required to guarantee that we won't reuse this object with the same
      58                 :             :  * SaltedUint256Hasher for another sync. */
      59                 :          12 : void HeadersSyncState::Finalize()
      60                 :             : {
      61                 :          12 :     Assume(m_download_state != State::FINAL);
      62                 :          12 :     ClearShrink(m_header_commitments);
      63                 :          12 :     m_last_header_received.SetNull();
      64                 :          12 :     ClearShrink(m_redownloaded_headers);
      65                 :          12 :     m_redownload_buffer_last_hash.SetNull();
      66                 :          12 :     m_redownload_buffer_first_prev_hash.SetNull();
      67                 :          12 :     m_process_all_remaining_headers = false;
      68                 :          12 :     m_current_height = 0;
      69                 :             : 
      70                 :          12 :     m_download_state = State::FINAL;
      71                 :          12 : }
      72                 :             : 
      73                 :             : /** Process the next batch of headers received from our peer.
      74                 :             :  *  Validate and store commitments, and compare total chainwork to our target to
      75                 :             :  *  see if we can switch to REDOWNLOAD mode.  */
      76                 :          35 : HeadersSyncState::ProcessingResult HeadersSyncState::ProcessNextHeaders(
      77                 :             :         std::span<const CBlockHeader> received_headers, const bool full_headers_message)
      78                 :             : {
      79                 :          35 :     ProcessingResult ret;
      80                 :             : 
      81         [ +  - ]:          35 :     Assume(!received_headers.empty());
      82         [ +  - ]:          35 :     if (received_headers.empty()) return ret;
      83                 :             : 
      84         [ +  - ]:          35 :     Assume(m_download_state != State::FINAL);
      85         [ +  - ]:          35 :     if (m_download_state == State::FINAL) return ret;
      86                 :             : 
      87         [ +  + ]:          35 :     if (m_download_state == State::PRESYNC) {
      88                 :             :         // During PRESYNC, we minimally validate block headers and
      89                 :             :         // occasionally add commitments to them, until we reach our work
      90                 :             :         // threshold (at which point m_download_state is updated to REDOWNLOAD).
      91         [ +  - ]:          23 :         ret.success = ValidateAndStoreHeadersCommitments(received_headers);
      92         [ +  - ]:          23 :         if (ret.success) {
      93   [ +  +  +  + ]:          23 :             if (full_headers_message || m_download_state == State::REDOWNLOAD) {
      94                 :             :                 // A full headers message means the peer may have more to give us;
      95                 :             :                 // also if we just switched to REDOWNLOAD then we need to re-request
      96                 :             :                 // headers from the beginning.
      97                 :          16 :                 ret.request_more = true;
      98                 :             :             } else {
      99         [ +  - ]:           7 :                 Assume(m_download_state == State::PRESYNC);
     100                 :             :                 // If we're in PRESYNC and we get a non-full headers
     101                 :             :                 // message, then the peer's chain has ended and definitely doesn't
     102                 :             :                 // have enough work, so we can stop our sync.
     103   [ +  -  +  -  :           7 :                 LogDebug(BCLog::NET, "Initial headers sync aborted with peer=%d: incomplete headers message at height=%i (presync phase)\n", m_id, m_current_height);
                   +  - ]
     104                 :             :             }
     105                 :             :         }
     106         [ +  - ]:          12 :     } else if (m_download_state == State::REDOWNLOAD) {
     107                 :             :         // During REDOWNLOAD, we compare our stored commitments to what we
     108                 :             :         // receive, and add headers to our redownload buffer. When the buffer
     109                 :             :         // gets big enough (meaning that we've checked enough commitments),
     110                 :             :         // we'll return a batch of headers to the caller for processing.
     111                 :          12 :         ret.success = true;
     112         [ +  + ]:       36696 :         for (const auto& hdr : received_headers) {
     113   [ +  -  +  + ]:       36685 :             if (!ValidateAndStoreRedownloadedHeader(hdr)) {
     114                 :             :                 // Something went wrong -- the peer gave us an unexpected chain.
     115                 :             :                 // We could consider looking at the reason for failure and
     116                 :             :                 // punishing the peer, but for now just give up on sync.
     117                 :           1 :                 ret.success = false;
     118                 :           1 :                 break;
     119                 :             :             }
     120                 :             :         }
     121                 :             : 
     122         [ +  + ]:          12 :         if (ret.success) {
     123                 :             :             // Return any headers that are ready for acceptance.
     124         [ +  - ]:          11 :             ret.pow_validated_headers = PopHeadersReadyForAcceptance();
     125                 :             : 
     126                 :             :             // If we hit our target blockhash, then all remaining headers will be
     127                 :             :             // returned and we can clear any leftover internal state.
     128   [ +  +  +  - ]:          11 :             if (m_redownloaded_headers.empty() && m_process_all_remaining_headers) {
     129   [ +  -  +  -  :           4 :                 LogDebug(BCLog::NET, "Initial headers sync complete with peer=%d: releasing all at height=%i (redownload phase)\n", m_id, m_redownload_buffer_last_height);
                   +  - ]
     130         [ +  - ]:           7 :             } else if (full_headers_message) {
     131                 :             :                 // If the headers message is full, we need to request more.
     132                 :           7 :                 ret.request_more = true;
     133                 :             :             } else {
     134                 :             :                 // For some reason our peer gave us a high-work chain, but is now
     135                 :             :                 // declining to serve us that full chain again. Give up.
     136                 :             :                 // Note that there's no more processing to be done with these
     137                 :             :                 // headers, so we can still return success.
     138   [ #  #  #  #  :           0 :                 LogDebug(BCLog::NET, "Initial headers sync aborted with peer=%d: incomplete headers message at height=%i (redownload phase)\n", m_id, m_redownload_buffer_last_height);
                   #  # ]
     139                 :             :             }
     140                 :             :         }
     141                 :             :     }
     142                 :             : 
     143   [ +  +  +  +  :          35 :     if (!(ret.success && ret.request_more)) Finalize();
                   +  - ]
     144                 :             :     return ret;
     145                 :           0 : }
     146                 :             : 
     147                 :          23 : bool HeadersSyncState::ValidateAndStoreHeadersCommitments(std::span<const CBlockHeader> headers)
     148                 :             : {
     149                 :             :     // The caller should not give us an empty set of headers.
     150         [ +  - ]:          23 :     Assume(headers.size() > 0);
     151         [ +  - ]:          23 :     if (headers.size() == 0) return true;
     152                 :             : 
     153         [ +  - ]:          23 :     Assume(m_download_state == State::PRESYNC);
     154         [ +  - ]:          23 :     if (m_download_state != State::PRESYNC) return false;
     155                 :             : 
     156         [ -  + ]:          23 :     if (headers[0].hashPrevBlock != m_last_header_received.GetHash()) {
     157                 :             :         // Somehow our peer gave us a header that doesn't connect.
     158                 :             :         // This might be benign -- perhaps our peer reorged away from the chain
     159                 :             :         // they were on. Give up on this sync for now (likely we will start a
     160                 :             :         // new sync with a new starting point).
     161         [ #  # ]:           0 :         LogDebug(BCLog::NET, "Initial headers sync aborted with peer=%d: non-continuous headers at height=%i (presync phase)\n", m_id, m_current_height);
     162                 :           0 :         return false;
     163                 :             :     }
     164                 :             : 
     165                 :             :     // If it does connect, (minimally) validate and occasionally store
     166                 :             :     // commitments.
     167         [ +  + ]:       78360 :     for (const auto& hdr : headers) {
     168         [ +  - ]:       78337 :         if (!ValidateAndProcessSingleHeader(hdr)) {
     169                 :             :             return false;
     170                 :             :         }
     171                 :             :     }
     172                 :             : 
     173         [ +  + ]:          23 :     if (m_current_chain_work >= m_minimum_required_work) {
     174                 :           5 :         m_redownloaded_headers.clear();
     175                 :           5 :         m_redownload_buffer_last_height = m_chain_start.nHeight;
     176                 :           5 :         m_redownload_buffer_first_prev_hash = m_chain_start.GetBlockHash();
     177                 :           5 :         m_redownload_buffer_last_hash = m_chain_start.GetBlockHash();
     178                 :           5 :         m_redownload_chain_work = m_chain_start.nChainWork;
     179                 :           5 :         m_download_state = State::REDOWNLOAD;
     180         [ +  - ]:           5 :         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);
     181                 :             :     }
     182                 :             :     return true;
     183                 :             : }
     184                 :             : 
     185                 :       78337 : bool HeadersSyncState::ValidateAndProcessSingleHeader(const CBlockHeader& current)
     186                 :             : {
     187         [ +  - ]:       78337 :     Assume(m_download_state == State::PRESYNC);
     188         [ +  - ]:       78337 :     if (m_download_state != State::PRESYNC) return false;
     189                 :             : 
     190                 :       78337 :     int next_height = m_current_height + 1;
     191                 :             : 
     192                 :             :     // Verify that the difficulty isn't growing too fast; an adversary with
     193                 :             :     // limited hashing capability has a greater chance of producing a high
     194                 :             :     // work chain if they compress the work into as few blocks as possible,
     195                 :             :     // so don't let anyone give a chain that would violate the difficulty
     196                 :             :     // adjustment maximum.
     197         [ -  + ]:       78337 :     if (!PermittedDifficultyTransition(m_consensus_params, next_height,
     198                 :       78337 :                 m_last_header_received.nBits, current.nBits)) {
     199         [ #  # ]:           0 :         LogDebug(BCLog::NET, "Initial headers sync aborted with peer=%d: invalid difficulty transition at height=%i (presync phase)\n", m_id, next_height);
     200                 :           0 :         return false;
     201                 :             :     }
     202                 :             : 
     203         [ +  + ]:       78337 :     if (next_height % m_params.commitment_period == m_commit_offset) {
     204                 :             :         // Add a commitment.
     205                 :         168 :         m_header_commitments.push_back(m_hasher(current.GetHash()) & 1);
     206         [ -  + ]:         168 :         if (m_header_commitments.size() > m_max_commitments) {
     207                 :             :             // The peer's chain is too long; give up.
     208                 :             :             // It's possible the chain grew since we started the sync; so
     209                 :             :             // potentially we could succeed in syncing the peer's chain if we
     210                 :             :             // try again later.
     211         [ #  # ]:           0 :             LogDebug(BCLog::NET, "Initial headers sync aborted with peer=%d: exceeded max commitments at height=%i (presync phase)\n", m_id, next_height);
     212                 :           0 :             return false;
     213                 :             :         }
     214                 :             :     }
     215                 :             : 
     216                 :       78337 :     m_current_chain_work += GetBlockProof(current);
     217                 :       78337 :     m_last_header_received = current;
     218                 :       78337 :     m_current_height = next_height;
     219                 :             : 
     220                 :       78337 :     return true;
     221                 :             : }
     222                 :             : 
     223                 :       36685 : bool HeadersSyncState::ValidateAndStoreRedownloadedHeader(const CBlockHeader& header)
     224                 :             : {
     225         [ +  - ]:       36685 :     Assume(m_download_state == State::REDOWNLOAD);
     226         [ +  - ]:       36685 :     if (m_download_state != State::REDOWNLOAD) return false;
     227                 :             : 
     228                 :       36685 :     int64_t next_height = m_redownload_buffer_last_height + 1;
     229                 :             : 
     230                 :             :     // Ensure that we're working on a header that connects to the chain we're
     231                 :             :     // downloading.
     232         [ -  + ]:       36685 :     if (header.hashPrevBlock != m_redownload_buffer_last_hash) {
     233         [ #  # ]:           0 :         LogDebug(BCLog::NET, "Initial headers sync aborted with peer=%d: non-continuous headers at height=%i (redownload phase)\n", m_id, next_height);
     234                 :           0 :         return false;
     235                 :             :     }
     236                 :             : 
     237                 :             :     // Check that the difficulty adjustments are within our tolerance:
     238                 :       36685 :     uint32_t previous_nBits{0};
     239         [ +  + ]:       36685 :     if (!m_redownloaded_headers.empty()) {
     240                 :       36680 :         previous_nBits = m_redownloaded_headers.back().nBits;
     241                 :             :     } else {
     242                 :           5 :         previous_nBits = m_chain_start.nBits;
     243                 :             :     }
     244                 :             : 
     245         [ -  + ]:       36685 :     if (!PermittedDifficultyTransition(m_consensus_params, next_height,
     246                 :       36685 :                 previous_nBits, header.nBits)) {
     247         [ #  # ]:           0 :         LogDebug(BCLog::NET, "Initial headers sync aborted with peer=%d: invalid difficulty transition at height=%i (redownload phase)\n", m_id, next_height);
     248                 :           0 :         return false;
     249                 :             :     }
     250                 :             : 
     251                 :             :     // Track work on the redownloaded chain
     252                 :       36685 :     m_redownload_chain_work += GetBlockProof(header);
     253                 :             : 
     254         [ +  + ]:       36685 :     if (m_redownload_chain_work >= m_minimum_required_work) {
     255                 :         146 :         m_process_all_remaining_headers = true;
     256                 :             :     }
     257                 :             : 
     258                 :             :     // If we're at a header for which we previously stored a commitment, verify
     259                 :             :     // 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 and
     262                 :             :     // our second, and we don't want to return failure after we've seen our
     263                 :             :     // target blockhash just because we ran out of commitments.
     264   [ +  +  +  + ]:       36685 :     if (!m_process_all_remaining_headers && next_height % m_params.commitment_period == m_commit_offset) {
     265         [ -  + ]:          74 :         if (m_header_commitments.size() == 0) {
     266         [ #  # ]:           0 :             LogDebug(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 and
     268                 :             :             // we've run out of commitments.
     269                 :           0 :             return false;
     270                 :             :         }
     271                 :          74 :         bool commitment = m_hasher(header.GetHash()) & 1;
     272                 :          74 :         bool expected_commitment = m_header_commitments.front();
     273                 :          74 :         m_header_commitments.pop_front();
     274         [ +  + ]:          74 :         if (commitment != expected_commitment) {
     275         [ +  - ]:           1 :             LogDebug(BCLog::NET, "Initial headers sync aborted with peer=%d: commitment mismatch at height=%i (redownload phase)\n", m_id, next_height);
     276                 :           1 :             return false;
     277                 :             :         }
     278                 :             :     }
     279                 :             : 
     280                 :             :     // Store this header for later processing.
     281                 :       36684 :     m_redownloaded_headers.emplace_back(header);
     282                 :       36684 :     m_redownload_buffer_last_height = next_height;
     283                 :       36684 :     m_redownload_buffer_last_hash = header.GetHash();
     284                 :             : 
     285                 :       36684 :     return true;
     286                 :             : }
     287                 :             : 
     288                 :          11 : std::vector<CBlockHeader> HeadersSyncState::PopHeadersReadyForAcceptance()
     289                 :             : {
     290                 :          11 :     std::vector<CBlockHeader> ret;
     291                 :             : 
     292         [ +  - ]:          11 :     Assume(m_download_state == State::REDOWNLOAD);
     293         [ +  - ]:          11 :     if (m_download_state != State::REDOWNLOAD) return ret;
     294                 :             : 
     295   [ -  +  +  +  :       36213 :     while (m_redownloaded_headers.size() > m_params.redownload_buffer_size ||
                   +  + ]
     296   [ +  +  +  + ]:       31969 :             (m_redownloaded_headers.size() > 0 && m_process_all_remaining_headers)) {
     297         [ +  - ]:       36202 :         ret.emplace_back(m_redownloaded_headers.front().GetFullHeader(m_redownload_buffer_first_prev_hash));
     298                 :       36202 :         m_redownloaded_headers.pop_front();
     299         [ +  - ]:       36202 :         m_redownload_buffer_first_prev_hash = ret.back().GetHash();
     300                 :             :     }
     301                 :             :     return ret;
     302                 :           0 : }
     303                 :             : 
     304                 :          23 : CBlockLocator HeadersSyncState::NextHeadersRequestLocator() const
     305                 :             : {
     306         [ -  + ]:          23 :     Assume(m_download_state != State::FINAL);
     307         [ -  + ]:          23 :     if (m_download_state == State::FINAL) return {};
     308                 :             : 
     309                 :          23 :     auto chain_start_locator = LocatorEntries(&m_chain_start);
     310                 :          23 :     std::vector<uint256> locator;
     311                 :             : 
     312         [ +  + ]:          23 :     if (m_download_state == State::PRESYNC) {
     313                 :             :         // During pre-synchronization, we continue from the last header received.
     314         [ +  - ]:          11 :         locator.push_back(m_last_header_received.GetHash());
     315                 :             :     }
     316                 :             : 
     317         [ +  + ]:          23 :     if (m_download_state == State::REDOWNLOAD) {
     318                 :             :         // During redownload, we will download from the last received header that we stored.
     319         [ +  - ]:          12 :         locator.push_back(m_redownload_buffer_last_hash);
     320                 :             :     }
     321                 :             : 
     322         [ +  - ]:          23 :     locator.insert(locator.end(), chain_start_locator.begin(), chain_start_locator.end());
     323                 :             : 
     324                 :          23 :     return CBlockLocator{std::move(locator)};
     325                 :          23 : }
        

Generated by: LCOV version 2.0-1