| 2987 | } |
| 2988 | |
| 2989 | void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer, |
| 2990 | std::vector<CBlockHeader>&& headers, |
| 2991 | bool via_compact_block) |
| 2992 | { |
| 2993 | size_t nCount = headers.size(); |
| 2994 | |
| 2995 | if (nCount == 0) { |
| 2996 | // Nothing interesting. Stop asking this peers for more headers. |
| 2997 | // If we were in the middle of headers sync, receiving an empty headers |
| 2998 | // message suggests that the peer suddenly has nothing to give us |
| 2999 | // (perhaps it reorged to our chain). Clear download state for this peer. |
| 3000 | LOCK(peer.m_headers_sync_mutex); |
| 3001 | if (peer.m_headers_sync) { |
| 3002 | peer.m_headers_sync.reset(nullptr); |
| 3003 | LOCK(m_headers_presync_mutex); |
| 3004 | m_headers_presync_stats.erase(pfrom.GetId()); |
| 3005 | } |
| 3006 | // A headers message with no headers cannot be an announcement, so assume |
| 3007 | // it is a response to our last getheaders request, if there is one. |
| 3008 | peer.m_last_getheaders_timestamp = {}; |
| 3009 | return; |
| 3010 | } |
| 3011 | |
| 3012 | // Before we do any processing, make sure these pass basic sanity checks. |
| 3013 | // We'll rely on headers having valid proof-of-work further down, as an |
| 3014 | // anti-DoS criteria (note: this check is required before passing any |
| 3015 | // headers into HeadersSyncState). |
| 3016 | if (!CheckHeadersPoW(headers, peer)) { |
| 3017 | // Misbehaving() calls are handled within CheckHeadersPoW(), so we can |
| 3018 | // just return. (Note that even if a header is announced via compact |
| 3019 | // block, the header itself should be valid, so this type of error can |
| 3020 | // always be punished.) |
| 3021 | return; |
| 3022 | } |
| 3023 | |
| 3024 | const CBlockIndex *pindexLast = nullptr; |
| 3025 | |
| 3026 | // We'll set already_validated_work to true if these headers are |
| 3027 | // successfully processed as part of a low-work headers sync in progress |
| 3028 | // (either in PRESYNC or REDOWNLOAD phase). |
| 3029 | // If true, this will mean that any headers returned to us (ie during |
| 3030 | // REDOWNLOAD) can be validated without further anti-DoS checks. |
| 3031 | bool already_validated_work = false; |
| 3032 | |
| 3033 | // If we're in the middle of headers sync, let it do its magic. |
| 3034 | bool have_headers_sync = false; |
| 3035 | { |
| 3036 | LOCK(peer.m_headers_sync_mutex); |
| 3037 | |
| 3038 | already_validated_work = IsContinuationOfLowWorkHeadersSync(peer, pfrom, headers); |
| 3039 | |
| 3040 | // The headers we passed in may have been: |
| 3041 | // - untouched, perhaps if no headers-sync was in progress, or some |
| 3042 | // failure occurred |
| 3043 | // - erased, such as if the headers were successfully processed and no |
| 3044 | // additional headers processing needs to take place (such as if we |
| 3045 | // are still in PRESYNC) |
| 3046 | // - replaced with headers that are now ready for validation, such as |
nothing calls this directly
no test coverage detected