Struct SlotCalculator

Source
pub struct SlotCalculator { /* private fields */ }
Expand description

A slot calculator, which can calculate slot numbers, windows, and offsets for a given chain.

§Typing

slots are indices, and use usize for their type. timestamps are in Unix Epoch seconds, and use u64 for their type.

It is recommended that literal integers passed to these functions be explicitly typed, e.g. 0u64, 12usize, etc., to avoid confusion in calling code.

§Behavior

Chain slot behavior is a bit unintuitive, particularly for chains that have a merge or chains that have missed slots at the start of the chain (i.e. Ethereum and its testnets).

Each header occupies a slot, but not all slots contain headers. Headers contain the timestamp at the END of their respective slot.

Chains start with a first header, which contains a timestamp (the start_timestamp) and occupies the initial slot (the slot_offset). The start_timestamp is therefore the END of the initial slot, and the BEGINNING of the next slot. I.e. if the initial slot is 0, then the start of slot 1 is the start_timestamp and the end of slot 1 is start_timestamp + slot_duration.

For a given slot, we normalize its number to n by subtracting the slot offset. n is therefore in the range 1...

  • n = normalized(slot) = slot - slot_offset.

As such, we can define the slot_start(n) as

  • slot_start(n) = (n - 1) * slot_duration + start_timestamp

and slot_end(n) as

  • slot_end(n) = n * slot_duration + start_timestamp

The slot n contains the range of timestamps:

  • slot_window(n) = slot_start(n)..slot_end(n)

To calculate the slot number n for a given timestamp t, we can use the following formula:

  • slot_for(t) = ((t - start_timestamp) / slot_duration) + slot_offset + 1

The + 1 is added because the first slot is the slot at slot_offset, which ENDS at start_timestamp. I.e. a timestamp at start_timestamp is in slot slot_offset + 1.

Implementations§

Source§

impl SlotCalculator

Source

pub const fn new( start_timestamp: u64, slot_offset: usize, slot_duration: u64, ) -> Self

Creates a new slot calculator.

Source

pub const fn holesky() -> Self

Creates a new slot calculator for Holesky.

Source

pub const fn pecorino_host() -> Self

Creates a new slot calculator for Pecorino host network.

Source

pub const fn mainnet() -> Self

Creates a new slot calculator for Ethereum mainnet.

Source

pub const fn start_timestamp(&self) -> u64

The timestamp of the first PoS block in the chain.

Source

pub const fn slot_offset(&self) -> usize

The slot number of the first PoS block in the chain.

Source

pub const fn slot_duration(&self) -> u64

The slot duration, usually 12 seconds.

Source

pub const fn slot_containing(&self, timestamp: u64) -> Option<usize>

Calculates the slot that contains a given timestamp.

Returns None if the timestamp is before the chain’s start timestamp.

Source

pub const fn point_within_slot(&self, timestamp: u64) -> Option<u64>

Calculates how many seconds a given timestamp is into its containing slot.

Returns None if the timestamp is before the chain’s start.

Source

pub const fn checked_point_within_slot( &self, slot: usize, timestamp: u64, ) -> Option<u64>

Calculates how many seconds a given timestamp is into a given slot. Returns None if the timestamp is not within the slot.

Source

pub const fn slot_window(&self, slot_number: usize) -> Range<u64>

Calculates the start and end timestamps for a given slot

Source

pub const fn slot_start(&self, slot_number: usize) -> u64

Calculates the start timestamp of a given slot.

Source

pub const fn slot_end(&self, slot_number: usize) -> u64

Calculates the end timestamp of a given slot.

Source

pub const fn slot_timestamp(&self, slot_number: usize) -> u64

Calculate the timestamp that will appear in the header of the block at the given slot number (if any block is produced). This is an alias for Self::slot_end.

Source

pub const fn slot_window_for_timestamp( &self, timestamp: u64, ) -> Option<Range<u64>>

Calculates the slot window for the slot that contains to the given timestamp. Slot windows are ranges start..end, where start is the end timestamp of the slot and end is start + slot_duration.

Returns None if the timestamp is before the chain’s start timestamp.

Source

pub const fn slot_start_for_timestamp(&self, timestamp: u64) -> Option<u64>

Calcuates the start timestamp for the slot that contains the given timestamp.

Source

pub const fn slot_end_for_timestamp(&self, timestamp: u64) -> Option<u64>

Calculates the end timestamp for the slot that contains to the given timestamp.

Source

pub fn current_slot(&self) -> Option<usize>

The current slot number.

Returns None if the current time is before the chain’s start timestamp.

Source

pub fn current_point_within_slot(&self) -> Option<u64>

The current number of seconds into the slot.

Source

pub fn slot_starting_at(&self, timestamp: u64) -> Option<usize>

Calculates the slot that starts at the given timestamp. Returns None if the timestamp is not a slot boundary. Returns None if the timestamp is before the chain’s start timestamp.

Source

pub fn slot_ending_at(&self, timestamp: u64) -> Option<usize>

Calculates the slot that ends at the given timestamp. Returns None if the timestamp is not a slot boundary. Returns None if the timestamp is before the chain’s start timestamp.

Trait Implementations§

Source§

impl Clone for SlotCalculator

Source§

fn clone(&self) -> SlotCalculator

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SlotCalculator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for SlotCalculator

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl FromEnv for SlotCalculator

Source§

type Error = SlotCalculatorEnvError

Error type produced when loading from the environment.
Source§

fn inventory() -> Vec<&'static EnvItemInfo>

Get the required environment variable names for this type. Read more
Source§

fn from_env() -> Result<Self, FromEnvErr<Self::Error>>

Load from the environment.
Source§

fn check_inventory() -> Result<(), Vec<&'static EnvItemInfo>>

Get a list of missing environment variables. Read more
Source§

impl PartialEq for SlotCalculator

Source§

fn eq(&self, other: &SlotCalculator) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for SlotCalculator

Source§

impl Eq for SlotCalculator

Source§

impl StructuralPartialEq for SlotCalculator

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsOut<T> for T
where T: Copy,

Source§

fn as_out(&mut self) -> Out<'_, T>

Returns an out reference to self.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> TryClone for T
where T: Clone,

Source§

fn try_clone(&self) -> Result<T, Error>

Clones self, possibly returning an error.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<'de, T> RpcBorrow<'de> for T
where T: Deserialize<'de> + Debug + Send + Sync + Unpin,

Source§

impl<T> RpcRecv for T
where T: DeserializeOwned + Debug + Send + Sync + Unpin + 'static,