Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an utility function to get the first timestamp of a slot #5316

Merged
merged 6 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions prdoc/pr_5316.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
title: add timestamp function to sp-consensus-slots

doc:
- audience: Node Dev
description: |
Added timestamp function to sp-consensus-slots to get the first timestamp
of the given slot.

crates:
- name: sp-consensus-slots
bump: minor
7 changes: 7 additions & 0 deletions substrate/primitives/consensus/slots/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ impl Slot {
Slot(timestamp.as_millis() / slot_duration.as_millis())
}

/// Timestamp of the start of the slot.
///
/// Returns `None` if would overflow for given `SlotDuration`.
pub fn timestamp(&self, slot_duration: SlotDuration) -> Option<Timestamp> {
slot_duration.as_millis().checked_mul(self.0).map(Timestamp::new)
}

/// Saturating addition.
pub fn saturating_add<T: Into<u64>>(self, rhs: T) -> Self {
Self(self.0.saturating_add(rhs.into()))
Expand Down
Loading