Skip to content
Snippets Groups Projects
Unverified Commit 9374643b authored by Lech Głowiak's avatar Lech Głowiak Committed by GitHub
Browse files

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: default avatarSquirrel <giles.cope@iohk.io>
Co-authored-by: default avatarDavide Galassi <davxy@datawok.net>
parent a67d6232
No related merge requests found
Pipeline #493797 waiting for manual action with stages
in 48 minutes and 51 seconds
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
......@@ -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()))
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment