Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(network): Respond to getheaders requests with a maximum of 160 block headers #8913

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions zebra-state/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,7 @@ pub const MAX_NON_FINALIZED_CHAIN_FORKS: usize = 10;
pub const MAX_FIND_BLOCK_HASHES_RESULTS: u32 = 500;

/// The maximum number of block headers allowed in `getheaders` responses in the Zcash network protocol.
const MAX_FIND_BLOCK_HEADERS_RESULTS_FOR_PROTOCOL: u32 = 160;

/// The maximum number of block headers sent by Zebra in `getheaders` responses.
///
/// Older versions of Zcashd will blindly request more block headers as long as it
/// got 160 block headers in response to a previous query,
/// _even if those headers are already known_.
///
/// To avoid this behavior, return slightly fewer than the maximum,
/// so `zcashd` thinks it has reached our chain tip.
///
/// <https://github.com/bitcoin/bitcoin/pull/4468/files#r17026905>
pub const MAX_FIND_BLOCK_HEADERS_RESULTS_FOR_ZEBRA: u32 =
MAX_FIND_BLOCK_HEADERS_RESULTS_FOR_PROTOCOL - 2;
pub const MAX_FIND_BLOCK_HEADERS_RESULTS: u32 = 160;

/// These database versions can be recreated from their directly preceding versions.
pub const RESTORABLE_DB_VERSIONS: [u64; 1] = [26];
Expand Down
6 changes: 3 additions & 3 deletions zebra-state/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use zebra_chain::{
/// will work with inline links.
#[allow(unused_imports)]
use crate::{
constants::{MAX_FIND_BLOCK_HASHES_RESULTS, MAX_FIND_BLOCK_HEADERS_RESULTS_FOR_ZEBRA},
constants::{MAX_FIND_BLOCK_HASHES_RESULTS, MAX_FIND_BLOCK_HEADERS_RESULTS},
ReadResponse, Response,
};

Expand Down Expand Up @@ -721,7 +721,7 @@ pub enum Request {
/// Stops the list of headers after:
/// * adding the best tip,
/// * adding the header matching the `stop` hash to the list, if it is in the best chain, or
/// * adding [`MAX_FIND_BLOCK_HEADERS_RESULTS_FOR_ZEBRA`] headers to the list.
/// * adding [`MAX_FIND_BLOCK_HEADERS_RESULTS`] headers to the list.
///
/// Returns an empty list if the state is empty.
///
Expand Down Expand Up @@ -925,7 +925,7 @@ pub enum ReadRequest {
/// Stops the list of headers after:
/// * adding the best tip,
/// * adding the header matching the `stop` hash to the list, if it is in the best chain, or
/// * adding [`MAX_FIND_BLOCK_HEADERS_RESULTS_FOR_ZEBRA`] headers to the list.
/// * adding [`MAX_FIND_BLOCK_HEADERS_RESULTS`] headers to the list.
///
/// Returns an empty list if the state is empty.
///
Expand Down
5 changes: 2 additions & 3 deletions zebra-state/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ use zebra_chain::{block::Height, serialization::ZcashSerialize};

use crate::{
constants::{
MAX_FIND_BLOCK_HASHES_RESULTS, MAX_FIND_BLOCK_HEADERS_RESULTS_FOR_ZEBRA,
MAX_LEGACY_CHAIN_BLOCKS,
MAX_FIND_BLOCK_HASHES_RESULTS, MAX_FIND_BLOCK_HEADERS_RESULTS, MAX_LEGACY_CHAIN_BLOCKS,
},
service::{
block_iter::any_ancestor_blocks,
Expand Down Expand Up @@ -1476,7 +1475,7 @@ impl Service<ReadRequest> for ReadStateService {
&state.db,
known_blocks,
stop,
MAX_FIND_BLOCK_HEADERS_RESULTS_FOR_ZEBRA,
MAX_FIND_BLOCK_HEADERS_RESULTS,
)
},
);
Expand Down
Loading