From 441b6d3f765f2443a6bfd764d8db7484dc7c6738 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lech=20G=C5=82owiak?= Date: Mon, 12 Aug 2024 10:08:01 +0200 Subject: [PATCH 1/4] Add an utility function to get the first timestamp of a slot --- prdoc/pr_5316.prdoc | 11 +++++++++++ substrate/primitives/consensus/slots/src/lib.rs | 5 +++++ 2 files changed, 16 insertions(+) create mode 100644 prdoc/pr_5316.prdoc diff --git a/prdoc/pr_5316.prdoc b/prdoc/pr_5316.prdoc new file mode 100644 index 0000000000000..24eb11db7f2e7 --- /dev/null +++ b/prdoc/pr_5316.prdoc @@ -0,0 +1,11 @@ +title: add first_timestamp function to sp-consensus-slots + +doc: + - audience: Node Dev + description: | + Added first_timestamp function to sp-consensus-slots to get the first timestamp + of the given slot. + +crates: + - name: sp-consensus-slots + bump: minor diff --git a/substrate/primitives/consensus/slots/src/lib.rs b/substrate/primitives/consensus/slots/src/lib.rs index eb3b3d3a449f2..96fbb3a129036 100644 --- a/substrate/primitives/consensus/slots/src/lib.rs +++ b/substrate/primitives/consensus/slots/src/lib.rs @@ -91,6 +91,11 @@ impl Slot { Slot(timestamp.as_millis() / slot_duration.as_millis()) } + /// The first timestamp of the slot. Returns None if would overflow for given SlotDuration. + pub fn first_timestamp(&self, slot_duration: SlotDuration) -> Option { + slot_duration.as_millis().checked_mul(self.0).map(Timestamp::new) + } + /// Saturating addition. pub fn saturating_add>(self, rhs: T) -> Self { Self(self.0.saturating_add(rhs.into())) From 636c5dc207e416bd7a9058e8eae16c9ea648a9cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lech=20G=C5=82owiak?= Date: Tue, 13 Aug 2024 13:39:52 +0200 Subject: [PATCH 2/4] Rename first_timestamp to timestamp --- prdoc/pr_5316.prdoc | 4 ++-- substrate/primitives/consensus/slots/src/lib.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/prdoc/pr_5316.prdoc b/prdoc/pr_5316.prdoc index 24eb11db7f2e7..75b431c941d54 100644 --- a/prdoc/pr_5316.prdoc +++ b/prdoc/pr_5316.prdoc @@ -1,9 +1,9 @@ -title: add first_timestamp function to sp-consensus-slots +title: add timestamp function to sp-consensus-slots doc: - audience: Node Dev description: | - Added first_timestamp function to sp-consensus-slots to get the first timestamp + Added timestamp function to sp-consensus-slots to get the first timestamp of the given slot. crates: diff --git a/substrate/primitives/consensus/slots/src/lib.rs b/substrate/primitives/consensus/slots/src/lib.rs index 96fbb3a129036..c465d111c568e 100644 --- a/substrate/primitives/consensus/slots/src/lib.rs +++ b/substrate/primitives/consensus/slots/src/lib.rs @@ -92,7 +92,7 @@ impl Slot { } /// The first timestamp of the slot. Returns None if would overflow for given SlotDuration. - pub fn first_timestamp(&self, slot_duration: SlotDuration) -> Option { + pub fn timestamp(&self, slot_duration: SlotDuration) -> Option { slot_duration.as_millis().checked_mul(self.0).map(Timestamp::new) } From 033a6163365730f76c96dfb5b8656e0002dc2a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lech=20G=C5=82owiak?= Date: Wed, 14 Aug 2024 11:47:50 +0200 Subject: [PATCH 3/4] Update doc string Co-authored-by: Squirrel --- substrate/primitives/consensus/slots/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/primitives/consensus/slots/src/lib.rs b/substrate/primitives/consensus/slots/src/lib.rs index c465d111c568e..433bde48dcf7a 100644 --- a/substrate/primitives/consensus/slots/src/lib.rs +++ b/substrate/primitives/consensus/slots/src/lib.rs @@ -91,7 +91,7 @@ impl Slot { Slot(timestamp.as_millis() / slot_duration.as_millis()) } - /// The first timestamp of the slot. Returns None if would overflow for given SlotDuration. + /// Timestamp of the start of the slot. Returns None if would overflow for given SlotDuration. pub fn timestamp(&self, slot_duration: SlotDuration) -> Option { slot_duration.as_millis().checked_mul(self.0).map(Timestamp::new) } From c632df57769e66881f3a8b6a99c2dbe5c9d43a55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lech=20G=C5=82owiak?= Date: Mon, 19 Aug 2024 11:29:40 +0200 Subject: [PATCH 4/4] Update substrate/primitives/consensus/slots/src/lib.rs Co-authored-by: Davide Galassi --- substrate/primitives/consensus/slots/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/substrate/primitives/consensus/slots/src/lib.rs b/substrate/primitives/consensus/slots/src/lib.rs index 433bde48dcf7a..dfa46fcf2571f 100644 --- a/substrate/primitives/consensus/slots/src/lib.rs +++ b/substrate/primitives/consensus/slots/src/lib.rs @@ -91,7 +91,9 @@ 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. + /// Timestamp of the start of the slot. + /// + /// Returns `None` if would overflow for given `SlotDuration`. pub fn timestamp(&self, slot_duration: SlotDuration) -> Option { slot_duration.as_millis().checked_mul(self.0).map(Timestamp::new) }