Skip to content

Commit

Permalink
chore(utils): Util function for headers request (#12501)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDhejavu authored Nov 13, 2024
1 parent 001f389 commit 7a1698c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
13 changes: 7 additions & 6 deletions crates/net/downloaders/src/headers/reverse_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use reth_consensus::Consensus;
use reth_network_p2p::{
error::{DownloadError, DownloadResult, PeerRequestResult},
headers::{
client::{HeadersClient, HeadersDirection, HeadersRequest},
client::{HeadersClient, HeadersRequest},
downloader::{validate_header_download, HeaderDownloader, SyncTarget},
error::{HeadersDownloaderError, HeadersDownloaderResult},
},
Expand Down Expand Up @@ -60,9 +60,10 @@ impl<H> From<HeadersResponseError> for ReverseHeadersDownloaderError<H> {
/// tries to fill the gap between the local head of the node and the chain tip by issuing multiple
/// requests at a time but yielding them in batches on [`Stream::poll_next`].
///
/// **Note:** This downloader downloads in reverse, see also [`HeadersDirection::Falling`], this
/// means the batches of headers that this downloader yields will start at the chain tip and move
/// towards the local head: falling block numbers.
/// **Note:** This downloader downloads in reverse, see also
/// [`reth_network_p2p::headers::client::HeadersDirection`], this means the batches of headers that
/// this downloader yields will start at the chain tip and move towards the local head: falling
/// block numbers.
#[must_use = "Stream does nothing unless polled"]
#[derive(Debug)]
pub struct ReverseHeadersDownloader<H: HeadersClient> {
Expand Down Expand Up @@ -567,7 +568,7 @@ where

/// Returns the request for the `sync_target` header.
const fn get_sync_target_request(&self, start: BlockHashOrNumber) -> HeadersRequest {
HeadersRequest { start, limit: 1, direction: HeadersDirection::Falling }
HeadersRequest::falling(start, 1)
}

/// Starts a request future
Expand Down Expand Up @@ -1216,7 +1217,7 @@ fn calc_next_request(
let diff = next_request_block_number - local_head;
let limit = diff.min(request_limit);
let start = next_request_block_number;
HeadersRequest { start: start.into(), limit, direction: HeadersDirection::Falling }
HeadersRequest::falling(start.into(), limit)
}

#[cfg(test)]
Expand Down
6 changes: 1 addition & 5 deletions crates/net/p2p/src/full_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ where
start_hash: hash,
count,
request: FullBlockRangeRequest {
headers: Some(client.get_headers(HeadersRequest {
start: hash.into(),
limit: count,
direction: HeadersDirection::Falling,
})),
headers: Some(client.get_headers(HeadersRequest::falling(hash.into(), count))),
bodies: None,
},
client,
Expand Down
35 changes: 29 additions & 6 deletions crates/net/p2p/src/headers/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,34 @@ pub struct HeadersRequest {
pub direction: HeadersDirection,
}

impl HeadersRequest {
/// Creates a request for a single header (direction doesn't matter).
///
/// # Arguments
/// * `start` - The block hash or number to start from
pub const fn one(start: BlockHashOrNumber) -> Self {
Self { direction: HeadersDirection::Rising, limit: 1, start }
}

/// Creates a request for headers in rising direction (ascending block numbers).
///
/// # Arguments
/// * `start` - The block hash or number to start from
/// * `limit` - Maximum number of headers to retrieve
pub const fn rising(start: BlockHashOrNumber, limit: u64) -> Self {
Self { direction: HeadersDirection::Rising, limit, start }
}

/// Creates a request for headers in falling direction (descending block numbers).
///
/// # Arguments
/// * `start` - The block hash or number to start from
/// * `limit` - Maximum number of headers to retrieve
pub const fn falling(start: BlockHashOrNumber, limit: u64) -> Self {
Self { direction: HeadersDirection::Falling, limit, start }
}
}

/// The headers future type
pub type HeadersFut = Pin<Box<dyn Future<Output = PeerRequestResult<Vec<Header>>> + Send + Sync>>;

Expand Down Expand Up @@ -57,12 +85,7 @@ pub trait HeadersClient: DownloadClient {
start: BlockHashOrNumber,
priority: Priority,
) -> SingleHeaderRequest<Self::Output> {
let req = HeadersRequest {
start,
limit: 1,
// doesn't matter for a single header
direction: HeadersDirection::Rising,
};
let req = HeadersRequest::one(start);
let fut = self.get_headers_with_priority(req, priority);
SingleHeaderRequest { fut }
}
Expand Down

0 comments on commit 7a1698c

Please sign in to comment.