Skip to content

Commit 9374643

Browse files
LGLOgilescopedavxy
authored
Add an utility function to get the first timestamp of a slot (#5316)
# Description Add `starting_timestamp` function for `Slot` type. ## Integration This is an addition of public function to a type, so integration should be seamless for idiomatic use of Rust. ## Review Notes Since `Slot` is just a slot number, the it's starting timestamp depends on `SlotDuration` which is a parameter to the added function. This function can be seen as dual to existing `fn from_timestamp`. Because there is a potential for overflow, the return type is `Option`. Q1: should I introduce tests for in this crate and add cases for both case: overflow (`None`) and no overflow (`Some`)? Q2: How can I add labels? IMO they should be `T0-node` and `D0-easy` but I cannot add them using GH interface. # Checklist * [x] My PR includes a detailed description as outlined in the "Description" and its two subsections above. * [ ] My PR follows the [labeling requirements](CONTRIBUTING.md#Process) of this project (at minimum one label for `T` required) * External contributors: ask maintainers to put the right label on your PR. * [ ] I have made corresponding changes to the documentation (if applicable) * [ ] I have added tests that prove my fix is effective or that my feature works (if applicable) --------- Co-authored-by: Squirrel <giles.cope@iohk.io> Co-authored-by: Davide Galassi <davxy@datawok.net>
1 parent a67d623 commit 9374643

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

prdoc/pr_5316.prdoc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
title: add timestamp function to sp-consensus-slots
2+
3+
doc:
4+
- audience: Node Dev
5+
description: |
6+
Added timestamp function to sp-consensus-slots to get the first timestamp
7+
of the given slot.
8+
9+
crates:
10+
- name: sp-consensus-slots
11+
bump: minor

substrate/primitives/consensus/slots/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ impl Slot {
9191
Slot(timestamp.as_millis() / slot_duration.as_millis())
9292
}
9393

94+
/// Timestamp of the start of the slot.
95+
///
96+
/// Returns `None` if would overflow for given `SlotDuration`.
97+
pub fn timestamp(&self, slot_duration: SlotDuration) -> Option<Timestamp> {
98+
slot_duration.as_millis().checked_mul(self.0).map(Timestamp::new)
99+
}
100+
94101
/// Saturating addition.
95102
pub fn saturating_add<T: Into<u64>>(self, rhs: T) -> Self {
96103
Self(self.0.saturating_add(rhs.into()))

0 commit comments

Comments
 (0)