Skip to content

Commit

Permalink
remove sampling window
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique committed Dec 10, 2024
1 parent e9f0d42 commit ff5e660
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 61 deletions.
7 changes: 3 additions & 4 deletions node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use crate::syncer::{Syncer, SyncerArgs};
mod builder;

pub use self::builder::{
NodeBuilder, NodeBuilderError, DEFAULT_PRUNING_DELAY, DEFAULT_SAMPLING_WINDOW,
DEFAULT_SYNCING_WINDOW, MIN_PRUNING_DELAY, MIN_SAMPLING_WINDOW, MIN_SYNCING_WINDOW,
NodeBuilder, NodeBuilderError, DEFAULT_PRUNING_DELAY, DEFAULT_SYNCING_WINDOW,
MIN_PRUNING_DELAY, MIN_SYNCING_WINDOW,
};
pub use crate::daser::DaserError;
pub use crate::p2p::{HeaderExError, P2pError};
Expand Down Expand Up @@ -83,7 +83,6 @@ where
pub(crate) p2p_listen_on: Vec<Multiaddr>,
pub(crate) sync_batch_size: u64,
pub(crate) syncing_window: Duration,
pub(crate) sampling_window: Duration,
pub(crate) pruning_window: Duration,
}

Expand Down Expand Up @@ -166,7 +165,7 @@ where
p2p: p2p.clone(),
store: store.clone(),
event_pub: event_channel.publisher(),
sampling_window: config.sampling_window,
sampling_window: config.syncing_window,
})?);

let pruner = Arc::new(Pruner::start(PrunerArgs {
Expand Down
66 changes: 9 additions & 57 deletions node/src/node/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@ use crate::store::{InMemoryStore, Store};
const HOUR: u64 = 60 * 60;
const DAY: u64 = 24 * HOUR;

/// Default maximum age of block headers [`Node`] will synchronise and store.
/// Default maximum age of blocks [`Node`] will synchronise, sample, and store.
pub const DEFAULT_SYNCING_WINDOW: Duration = Duration::from_secs(30 * DAY);
/// Minimum configurable syncing window that can be used in [`NodeBuilder`].
pub const MIN_SYNCING_WINDOW: Duration = Duration::from_secs(60);

/// Default maximum age of block [`Node`] will sample and store.
pub const DEFAULT_SAMPLING_WINDOW: Duration = Duration::from_secs(30 * DAY);
/// Minimum configurable sampling window that can be used in [`NodeBuilder`].
pub const MIN_SAMPLING_WINDOW: Duration = Duration::from_secs(60);

/// Default delay after the syncing window before [`Node`] prunes the block.
pub const DEFAULT_PRUNING_DELAY: Duration = Duration::from_secs(HOUR);
/// Minimum pruning delay that can be used in [`NodeBuilder`].
Expand All @@ -44,7 +39,6 @@ where
listen: Vec<Multiaddr>,
sync_batch_size: Option<u64>,
syncing_window: Option<Duration>,
sampling_window: Option<Duration>,
pruning_delay: Option<Duration>,
}

Expand All @@ -55,10 +49,6 @@ pub enum NodeBuilderError {
#[error("Syncing window is {0:?} but cannot be smaller than {MIN_SYNCING_WINDOW:?}")]
SyncingWindowTooSmall(Duration),

/// Sampling window is smaller than [`MIN_SAMPLING_WINDOW`].
#[error("Sampling window is {0:?} but cannot be smaller than {MIN_SAMPLING_WINDOW:?}")]
SamplingWindowTooSmall(Duration),

/// Pruning delay is smaller than [`MIN_PRUNING_DELAY`].
#[error("Pruning delay is {0:?} but cannot be smaller than {MIN_PRUNING_DELAY:?}")]
PruningDelayTooSmall(Duration),
Expand Down Expand Up @@ -94,7 +84,6 @@ impl NodeBuilder<InMemoryBlockstore, InMemoryStore> {
listen: Vec::new(),
sync_batch_size: None,
syncing_window: None,
sampling_window: None,
pruning_delay: None,
}
}
Expand Down Expand Up @@ -142,7 +131,6 @@ where
listen: self.listen,
sync_batch_size: self.sync_batch_size,
syncing_window: self.syncing_window,
sampling_window: self.sampling_window,
pruning_delay: self.pruning_delay,
}
}
Expand All @@ -163,7 +151,6 @@ where
listen: self.listen,
sync_batch_size: self.sync_batch_size,
syncing_window: self.syncing_window,
sampling_window: self.sampling_window,
pruning_delay: self.pruning_delay,
}
}
Expand Down Expand Up @@ -222,10 +209,9 @@ where

/// Set syncing window.
///
/// Syncing window defines maximum age of headers considered for syncing.
/// Headers older than syncing window by more than an hour are eligible for pruning.
/// Syncing window defines maximum age of a block considered for syncing and sampling.
///
/// **Default if [`InMemoryStore`] is used:** 60 seconds.\
/// **Default if [`InMemoryStore`]/[`InMemoryBlockstore`] are used:** 60 seconds.\
/// **Default:** 30 days.\
/// **Minimum:** 60 seconds.
pub fn syncing_window(self, dur: Duration) -> Self {
Expand All @@ -235,27 +221,12 @@ where
}
}

/// Set sampling window.
///
/// Sampling window defines the maximum age of a block considered for sampling.
/// Sampling window will be truncated to syncing window, if latter is smaller.
///
/// **Default if [`InMemoryBlockstore`] is used:** 60 seconds.\
/// **Default:** 30 days.\
/// **Minimum:** 60 seconds.
pub fn sampling_window(self, dur: Duration) -> Self {
NodeBuilder {
sampling_window: Some(dur),
..self
}
}

/// Set pruning delay.
///
/// Pruning delay how much time the pruner should wait after syncing window in
/// order to prune the block.
///
/// **Default if [`InMemoryStore`] is used:** 60 seconds.\
/// **Default if [`InMemoryStore`]/[`InMemoryBlockstore`] are used:** 60 seconds.\
/// **Default:** 1 hour.\
/// **Minimum:** 60 seconds.
pub fn pruning_delay(self, dur: Duration) -> Self {
Expand Down Expand Up @@ -288,28 +259,20 @@ where
//
// If user implements their own in-memory stores then they are responsible
// to set the syncing window to something smaller than `DEFAULT_SYNCING_WINDOW`.
let in_memory_store_used = TypeId::of::<S>() == TypeId::of::<InMemoryStore>();
let in_memory_blockstore_used = TypeId::of::<B>() == TypeId::of::<InMemoryBlockstore>();
let in_memory_stores_used = TypeId::of::<S>() == TypeId::of::<InMemoryStore>()
|| TypeId::of::<B>() == TypeId::of::<InMemoryBlockstore>();

let syncing_window = if let Some(dur) = self.syncing_window {
dur
} else if in_memory_store_used {
} else if in_memory_stores_used {
MIN_SYNCING_WINDOW
} else {
DEFAULT_SYNCING_WINDOW
};

let sampling_window = if let Some(dur) = self.syncing_window {
dur
} else if in_memory_blockstore_used {
MIN_SAMPLING_WINDOW
} else {
DEFAULT_SAMPLING_WINDOW
};

let pruning_delay = if let Some(dur) = self.pruning_delay {
dur
} else if in_memory_store_used {
} else if in_memory_stores_used {
MIN_PRUNING_DELAY
} else {
DEFAULT_PRUNING_DELAY
Expand All @@ -319,23 +282,13 @@ where
return Err(NodeBuilderError::SyncingWindowTooSmall(syncing_window));
}

// Truncate sampling window if needed.
let sampling_window = sampling_window.min(syncing_window);

if sampling_window < MIN_SAMPLING_WINDOW {
return Err(NodeBuilderError::SamplingWindowTooSmall(sampling_window));
}

if pruning_delay < MIN_PRUNING_DELAY {
return Err(NodeBuilderError::PruningDelayTooSmall(pruning_delay));
}

let pruning_window = syncing_window.saturating_add(pruning_delay);

info!(
"Syncing window: {:?}, Sampling window: {:?}, Pruning window: {:?}",
syncing_window, sampling_window, pruning_window
);
info!("Syncing window: {syncing_window:?}, Pruning window: {pruning_window:?}",);

Ok(NodeConfig {
blockstore: self.blockstore,
Expand All @@ -346,7 +299,6 @@ where
p2p_listen_on: self.listen,
sync_batch_size: self.sync_batch_size.unwrap_or(512),
syncing_window,
sampling_window,
pruning_window,
})
}
Expand Down

0 comments on commit ff5e660

Please sign in to comment.