pub struct BlockTimeMonitor { /* private fields */ }Expand description
Monitors block times and provides estimation utilities for order expiration.
Thread-safe component that tracks rolling average block times from WebSocket data. Uses atomic operations for the hot path (height reads) and a read-write lock for less frequent operations (window updates, time estimation).
Implementations§
Source§impl BlockTimeMonitor
impl BlockTimeMonitor
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new BlockTimeMonitor with default window size.
Sourcepub fn with_window_size(window_size: usize) -> Self
pub fn with_window_size(window_size: usize) -> Self
Creates a new BlockTimeMonitor with custom window size.
Sourcepub fn record_block(&self, height: u64, time: DateTime<Utc>)
pub fn record_block(&self, height: u64, time: DateTime<Utc>)
Records a new block from WebSocket data.
Should be called whenever a block height update is received. Updates the current height atomically and adds the sample to the rolling window.
§Panics
Panics if the RwLock is poisoned (should never happen in practice).
Sourcepub fn current_block_height(&self) -> u64
pub fn current_block_height(&self) -> u64
Returns the current block height.
This is a fast, lock-free read suitable for hot paths.
Sourcepub fn current_block_time(&self) -> Option<DateTime<Utc>>
pub fn current_block_time(&self) -> Option<DateTime<Utc>>
Returns the timestamp of the most recent block.
§Panics
Panics if the RwLock is poisoned (should never happen in practice).
Sourcepub fn estimated_seconds_per_block(&self) -> Option<f64>
pub fn estimated_seconds_per_block(&self) -> Option<f64>
Returns the estimated seconds per block based on rolling average.
Returns None if fewer than MIN_SAMPLES_FOR_ESTIMATE samples are available.
§Panics
Panics if the RwLock is poisoned (should never happen in practice).
Sourcepub fn seconds_per_block_or_default(&self) -> f64
pub fn seconds_per_block_or_default(&self) -> f64
Returns estimated seconds per block, falling back to default if unavailable.
Uses DEFAULT_BLOCK_TIME_MS (500ms) when insufficient samples.
Sourcepub fn estimate_blocks_for_duration(&self, duration_secs: f64) -> u32
pub fn estimate_blocks_for_duration(&self, duration_secs: f64) -> u32
Estimates how many blocks will occur in the given duration.
Uses the rolling average if available, otherwise falls back to default block time.
Result is capped at u32::MAX to prevent overflow from edge cases.
Sourcepub fn estimate_expiry_time(&self, expiry_block: u64) -> Option<DateTime<Utc>>
pub fn estimate_expiry_time(&self, expiry_block: u64) -> Option<DateTime<Utc>>
Estimates the wall-clock time when a specific block height will be reached.
Returns None if:
- Insufficient samples for reliable estimation
- No current block time available
- Target block is in the past
Sourcepub fn estimate_remaining_lifetime_secs(&self, expiry_block: u64) -> Option<f64>
pub fn estimate_remaining_lifetime_secs(&self, expiry_block: u64) -> Option<f64>
Estimates remaining lifetime in seconds for an order expiring at the given block.
Returns None if insufficient data or block already passed.
Sourcepub fn is_ready(&self) -> bool
pub fn is_ready(&self) -> bool
Returns true if the monitor has enough samples for reliable estimation.
§Panics
Panics if the RwLock is poisoned (should never happen in practice).
Sourcepub fn sample_count(&self) -> usize
pub fn sample_count(&self) -> usize
Returns the number of samples collected in the rolling window.
§Panics
Panics if the RwLock is poisoned (should never happen in practice).
Trait Implementations§
Source§impl Debug for BlockTimeMonitor
impl Debug for BlockTimeMonitor
Auto Trait Implementations§
impl !Freeze for BlockTimeMonitor
impl RefUnwindSafe for BlockTimeMonitor
impl Send for BlockTimeMonitor
impl Sync for BlockTimeMonitor
impl Unpin for BlockTimeMonitor
impl UnsafeUnpin for BlockTimeMonitor
impl UnwindSafe for BlockTimeMonitor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].