LCOV - code coverage report
Current view: top level - src - headerssync.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 89.6 % 144 129
Test Date: 2025-10-25 04:38:23 Functions: 100.0 % 8 8
Branches: 59.1 % 154 91

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

Generated by: LCOV version 2.0-1