Skip to content

Commit

Permalink
chore: move standalone types to types crate
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Nov 12, 2024
1 parent fa5daef commit 5341547
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 39 deletions.
4 changes: 1 addition & 3 deletions crates/static-file/static-file/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

mod event;
pub mod segments;
mod static_file_producer;

pub use event::StaticFileProducerEvent;
pub use static_file_producer::{
StaticFileProducer, StaticFileProducerInner, StaticFileProducerResult,
StaticFileProducerWithResult, StaticFileTargets,
StaticFileProducerWithResult,
};

// Re-export for convenience.
Expand Down
36 changes: 1 addition & 35 deletions crates/static-file/static-file/src/static_file_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use reth_provider::{
};
use reth_prune_types::PruneModes;
use reth_stages_types::StageId;
use reth_static_file_types::HighestStaticFiles;
use reth_static_file_types::{HighestStaticFiles, StaticFileTargets};
use reth_storage_errors::provider::ProviderResult;
use reth_tokio_util::{EventSender, EventStream};
use std::{
Expand Down Expand Up @@ -66,40 +66,6 @@ pub struct StaticFileProducerInner<Provider> {
event_sender: EventSender<StaticFileProducerEvent>,
}

/// Static File targets, per data segment, measured in [`BlockNumber`].
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct StaticFileTargets {
headers: Option<RangeInclusive<BlockNumber>>,
receipts: Option<RangeInclusive<BlockNumber>>,
transactions: Option<RangeInclusive<BlockNumber>>,
}

impl StaticFileTargets {
/// Returns `true` if any of the targets are [Some].
pub const fn any(&self) -> bool {
self.headers.is_some() || self.receipts.is_some() || self.transactions.is_some()
}

// Returns `true` if all targets are either [`None`] or has beginning of the range equal to the
// highest static_file.
fn is_contiguous_to_highest_static_files(&self, static_files: HighestStaticFiles) -> bool {
[
(self.headers.as_ref(), static_files.headers),
(self.receipts.as_ref(), static_files.receipts),
(self.transactions.as_ref(), static_files.transactions),
]
.iter()
.all(|(target_block_range, highest_static_fileted_block)| {
target_block_range.map_or(true, |target_block_range| {
*target_block_range.start() ==
highest_static_fileted_block.map_or(0, |highest_static_fileted_block| {
highest_static_fileted_block + 1
})
})
})
}
}

impl<Provider> StaticFileProducerInner<Provider> {
fn new(provider: Provider, prune_modes: PruneModes) -> Self {
Self { provider, prune_modes, event_sender: Default::default() }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::StaticFileTargets;
use std::time::Duration;

/// An event emitted by a [`StaticFileProducer`][crate::StaticFileProducer].
/// An event emitted by the static file producer.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum StaticFileProducerEvent {
/// Emitted when static file producer started running.
Expand Down
40 changes: 40 additions & 0 deletions crates/static-file/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

mod compression;
mod event;
mod segment;

use alloy_primitives::BlockNumber;
pub use compression::Compression;
pub use event::StaticFileProducerEvent;
pub use segment::{SegmentConfig, SegmentHeader, SegmentRangeInclusive, StaticFileSegment};
use std::ops::RangeInclusive;

/// Default static file block count.
pub const DEFAULT_BLOCKS_PER_STATIC_FILE: u64 = 500_000;
Expand Down Expand Up @@ -62,6 +65,43 @@ impl HighestStaticFiles {
}
}

/// Static File targets, per data segment, measured in [`BlockNumber`].
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct StaticFileTargets {
/// Targeted range of headers.
pub headers: Option<RangeInclusive<BlockNumber>>,
/// Targeted range of receipts.
pub receipts: Option<RangeInclusive<BlockNumber>>,
/// Targeted range of transactions.
pub transactions: Option<RangeInclusive<BlockNumber>>,
}

impl StaticFileTargets {
/// Returns `true` if any of the targets are [Some].
pub const fn any(&self) -> bool {
self.headers.is_some() || self.receipts.is_some() || self.transactions.is_some()
}

/// Returns `true` if all targets are either [`None`] or has beginning of the range equal to the
/// highest static file.
pub fn is_contiguous_to_highest_static_files(&self, static_files: HighestStaticFiles) -> bool {
[
(self.headers.as_ref(), static_files.headers),
(self.receipts.as_ref(), static_files.receipts),
(self.transactions.as_ref(), static_files.transactions),
]
.iter()
.all(|(target_block_range, highest_static_fileted_block)| {
target_block_range.map_or(true, |target_block_range| {
*target_block_range.start() ==
highest_static_fileted_block.map_or(0, |highest_static_fileted_block| {
highest_static_fileted_block + 1
})
})
})
}
}

/// Each static file has a fixed number of blocks. This gives out the range where the requested
/// block is positioned. Used for segment filename.
pub const fn find_fixed_range(
Expand Down

0 comments on commit 5341547

Please sign in to comment.