Skip to content

Commit

Permalink
Responds with a maximum of 160 block headers
Browse files Browse the repository at this point in the history
  • Loading branch information
arya2 committed Oct 7, 2024
1 parent abfb9ce commit f08b7f1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 20 deletions.
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

0 comments on commit f08b7f1

Please sign in to comment.