From 056285fd1ff347179683db5629b63c4e42a9390b Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Thu, 8 Sep 2022 19:36:52 +0200 Subject: [PATCH 01/14] Remove timestamp from SlotInfo --- client/consensus/slots/src/lib.rs | 10 ++++++---- client/consensus/slots/src/slots.rs | 14 +------------- primitives/timestamp/src/lib.rs | 2 +- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index 39b40a32f18ca..7fd56aa9717d0 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -255,7 +255,7 @@ pub trait SimpleSlotWorker { where Self: Sync, { - let (timestamp, slot) = (slot_info.timestamp, slot_info.slot); + let slot = slot_info.slot; let telemetry = self.telemetry(); let logging_target = self.logging_target(); @@ -319,17 +319,19 @@ pub trait SimpleSlotWorker { return None } + let timestamp = sp_timestamp::current_timestamp().as_secs(); + debug!( target: logging_target, - "Starting authorship at slot {}; timestamp = {}", slot, *timestamp, + "Starting authorship at slot {}, timestamp: {}", slot, timestamp, ); telemetry!( telemetry; CONSENSUS_DEBUG; "slots.starting_authorship"; - "slot_num" => *slot, - "timestamp" => *timestamp, + "slot_num" => slot, + "timestamp" => timestamp, ); let proposer = match self.proposer(&slot_info.chain_head).await { diff --git a/client/consensus/slots/src/slots.rs b/client/consensus/slots/src/slots.rs index accf24b6b4e78..f3dc485a8e819 100644 --- a/client/consensus/slots/src/slots.rs +++ b/client/consensus/slots/src/slots.rs @@ -50,8 +50,6 @@ pub fn time_until_next_slot(slot_duration: Duration) -> Duration { pub struct SlotInfo { /// The slot number as found in the inherent data. pub slot: Slot, - /// Current timestamp as found in the inherent data. - pub timestamp: sp_timestamp::Timestamp, /// The instant at which the slot ends. pub ends_at: Instant, /// The inherent data. @@ -72,7 +70,6 @@ impl SlotInfo { /// `ends_at` is calculated using `timestamp` and `duration`. pub fn new( slot: Slot, - timestamp: sp_timestamp::Timestamp, inherent_data: InherentData, duration: Duration, chain_head: B::Header, @@ -80,7 +77,6 @@ impl SlotInfo { ) -> Self { Self { slot, - timestamp, inherent_data, duration, chain_head, @@ -175,7 +171,6 @@ where ); } - let timestamp = inherent_data_providers.timestamp(); let slot = inherent_data_providers.slot(); let inherent_data = inherent_data_providers.create_inherent_data()?; @@ -183,14 +178,7 @@ where if slot > self.last_slot { self.last_slot = slot; - break Ok(SlotInfo::new( - slot, - timestamp, - inherent_data, - self.slot_duration, - chain_head, - None, - )) + break Ok(SlotInfo::new(slot, inherent_data, self.slot_duration, chain_head, None)) } } } diff --git a/primitives/timestamp/src/lib.rs b/primitives/timestamp/src/lib.rs index b98a87c37f69d..4264443d36e59 100644 --- a/primitives/timestamp/src/lib.rs +++ b/primitives/timestamp/src/lib.rs @@ -169,7 +169,7 @@ impl TimestampInherentData for InherentData { /// /// This timestamp is the time since the UNIX epoch. #[cfg(feature = "std")] -fn current_timestamp() -> std::time::Duration { +pub fn current_timestamp() -> std::time::Duration { use std::time::SystemTime; let now = SystemTime::now(); From 8678961e9252ecb9d0385cee8f8343471a55a8b4 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Thu, 8 Sep 2022 19:50:07 +0200 Subject: [PATCH 02/14] Expose as millis instead of secs --- client/consensus/slots/src/lib.rs | 2 +- primitives/timestamp/src/lib.rs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index 7fd56aa9717d0..c1ceb040b127f 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -319,7 +319,7 @@ pub trait SimpleSlotWorker { return None } - let timestamp = sp_timestamp::current_timestamp().as_secs(); + let timestamp = sp_timestamp::current_timestamp().as_millis(); debug!( target: logging_target, diff --git a/primitives/timestamp/src/lib.rs b/primitives/timestamp/src/lib.rs index 4264443d36e59..7e19552da0cff 100644 --- a/primitives/timestamp/src/lib.rs +++ b/primitives/timestamp/src/lib.rs @@ -169,12 +169,13 @@ impl TimestampInherentData for InherentData { /// /// This timestamp is the time since the UNIX epoch. #[cfg(feature = "std")] -pub fn current_timestamp() -> std::time::Duration { +pub fn current_timestamp() -> Timestamp { use std::time::SystemTime; let now = SystemTime::now(); now.duration_since(SystemTime::UNIX_EPOCH) .expect("Current time is always after unix epoch; qed") + .into() } /// Provide duration since unix epoch in millisecond for timestamp inherent. @@ -190,7 +191,7 @@ impl InherentDataProvider { pub fn from_system_time() -> Self { Self { max_drift: std::time::Duration::from_secs(60).into(), - timestamp: current_timestamp().into(), + timestamp: current_timestamp(), } } From 2c5126018ac4d394050efb82840364f288ab56e6 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Thu, 8 Sep 2022 20:04:45 +0200 Subject: [PATCH 03/14] Nits --- client/consensus/slots/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index c1ceb040b127f..acb6f82d3fa90 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -323,7 +323,7 @@ pub trait SimpleSlotWorker { debug!( target: logging_target, - "Starting authorship at slot {}, timestamp: {}", slot, timestamp, + "Starting authorship at slot: {slot}, timestamp: {timestamp}" ); telemetry!( @@ -337,7 +337,7 @@ pub trait SimpleSlotWorker { let proposer = match self.proposer(&slot_info.chain_head).await { Ok(p) => p, Err(err) => { - warn!(target: logging_target, "Unable to author block in slot {:?}: {}", slot, err,); + warn!(target: logging_target, "Unable to author block in slot {slot:?}: {err}"); telemetry!( telemetry; From 6d6e7e6d3e2e42d42a78fccf1810485f9dce2ef2 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Thu, 8 Sep 2022 20:08:20 +0200 Subject: [PATCH 04/14] Fix test after field removal --- client/consensus/slots/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index acb6f82d3fa90..bdd4afbab7157 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -825,7 +825,6 @@ mod test { super::slots::SlotInfo { slot: slot.into(), duration: SLOT_DURATION, - timestamp: Default::default(), inherent_data: Default::default(), ends_at: Instant::now() + SLOT_DURATION, chain_head: Header::new( From 264a5e33ddf1e808c35d74c588f85383c2b52534 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Thu, 8 Sep 2022 20:12:10 +0200 Subject: [PATCH 05/14] Yet another test fix --- client/consensus/aura/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 92fe1fa3cf29d..c56ad0fa5cfb2 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -887,7 +887,6 @@ mod tests { let res = executor::block_on(worker.on_slot(SlotInfo { slot: 0.into(), - timestamp: 0.into(), ends_at: Instant::now() + Duration::from_secs(100), inherent_data: InherentData::new(), duration: Duration::from_millis(1000), From 5a0d09ca8d25986172f2b56a055dc389813ba3f4 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Thu, 8 Sep 2022 20:41:31 +0200 Subject: [PATCH 06/14] On the fly timestamp computation --- client/consensus/slots/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index bdd4afbab7157..3d4951bf88e4c 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -319,8 +319,7 @@ pub trait SimpleSlotWorker { return None } - let timestamp = sp_timestamp::current_timestamp().as_millis(); - + let timestamp = slot_info.duration.as_millis().saturating_mul(u64::from(slot) as u128); debug!( target: logging_target, "Starting authorship at slot: {slot}, timestamp: {timestamp}" From 1c7d4d1847acfbb8e6342721f38d5abe5496577d Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Mon, 12 Sep 2022 14:38:24 +0200 Subject: [PATCH 07/14] Removed slot timestamp from logs --- client/consensus/slots/src/lib.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index 3d4951bf88e4c..b1906cb96eeb5 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -319,12 +319,9 @@ pub trait SimpleSlotWorker { return None } - let timestamp = slot_info.duration.as_millis().saturating_mul(u64::from(slot) as u128); - debug!( - target: logging_target, - "Starting authorship at slot: {slot}, timestamp: {timestamp}" - ); + debug!(target: logging_target, "Starting authorship at slot: {slot}"); + let timestamp = slot_info.duration.as_millis().saturating_mul(u64::from(slot) as u128); telemetry!( telemetry; CONSENSUS_DEBUG; From 94200cf7df97d3ff99b63071821423a8a52e610b Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Mon, 12 Sep 2022 15:58:32 +0200 Subject: [PATCH 08/14] Removed reference to timestamp from slots subsystem --- bin/node-template/node/src/service.rs | 4 +-- bin/node/cli/src/service.rs | 4 +-- breaks | 1 + client/consensus/slots/src/lib.rs | 47 +++++++++------------------ lldb.init | 5 +++ run-benches.sh | 24 ++++++++++++++ 6 files changed, 49 insertions(+), 36 deletions(-) create mode 100644 breaks create mode 100644 lldb.init create mode 100755 run-benches.sh diff --git a/bin/node-template/node/src/service.rs b/bin/node-template/node/src/service.rs index ffb2440caa0ed..dadf95829546f 100644 --- a/bin/node-template/node/src/service.rs +++ b/bin/node-template/node/src/service.rs @@ -126,7 +126,7 @@ pub fn new_partial( slot_duration, ); - Ok((timestamp, slot)) + Ok((slot, timestamp)) }, spawner: &task_manager.spawn_essential_handle(), can_author_with: sp_consensus::CanAuthorWithNativeVersion::new( @@ -275,7 +275,7 @@ pub fn new_full(mut config: Configuration) -> Result slot_duration, ); - Ok((timestamp, slot)) + Ok((slot, timestamp)) }, force_authoring, backoff_authoring_blocks, diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index 13003c1a7a41f..1b7bc19aeeada 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -223,7 +223,7 @@ pub fn new_partial( let uncles = sp_authorship::InherentDataProvider::<::Header>::check_inherents(); - Ok((timestamp, slot, uncles)) + Ok((slot, timestamp, uncles)) }, &task_manager.spawn_essential_handle(), config.prometheus_registry(), @@ -457,7 +457,7 @@ pub fn new_full_base( &parent, )?; - Ok((timestamp, slot, uncles, storage_proof)) + Ok((slot, timestamp, uncles, storage_proof)) } }, force_authoring, diff --git a/breaks b/breaks new file mode 100644 index 0000000000000..ef27fab47280a --- /dev/null +++ b/breaks @@ -0,0 +1 @@ +[{"Breakpoint":{"BKPTOptions":{"AutoContinue":false,"ConditionText":"","EnabledState":true,"IgnoreCount":0,"OneShotState":false},"BKPTResolver":{"Options":{"Column":0,"Exact":false,"FileName":"/mnt/ssd/users/develop/parity/substrate/client/consensus/babe/src/lib.rs","Inlines":true,"LineNumber":1076,"Offset":0,"SkipPrologue":true},"Type":"FileAndLine"},"Hardware":false,"SearchFilter":{"Options":{},"Type":"Unconstrained"}}}] diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index b1906cb96eeb5..30830d34cbf96 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -45,7 +45,6 @@ use sp_runtime::{ generic::BlockId, traits::{Block as BlockT, HashFor, Header as HeaderT}, }; -use sp_timestamp::Timestamp; use std::{fmt::Debug, ops::Deref, time::Duration}; /// The changes that need to applied to the storage to create the state for a block. @@ -321,14 +320,7 @@ pub trait SimpleSlotWorker { debug!(target: logging_target, "Starting authorship at slot: {slot}"); - let timestamp = slot_info.duration.as_millis().saturating_mul(u64::from(slot) as u128); - telemetry!( - telemetry; - CONSENSUS_DEBUG; - "slots.starting_authorship"; - "slot_num" => slot, - "timestamp" => timestamp, - ); + telemetry!(telemetry; CONSENSUS_DEBUG; "slots.starting_authorship"; "slot_num" => slot); let proposer = match self.proposer(&slot_info.chain_head).await { Ok(p) => p, @@ -441,44 +433,35 @@ impl + Send + Sync, B: BlockT> /// Slot specific extension that the inherent data provider needs to implement. pub trait InherentDataProviderExt { - /// The current timestamp that will be found in the - /// [`InherentData`](`sp_inherents::InherentData`). - fn timestamp(&self) -> Timestamp; - /// The current slot that will be found in the [`InherentData`](`sp_inherents::InherentData`). fn slot(&self) -> Slot; } /// Small macro for implementing `InherentDataProviderExt` for inherent data provider tuple. macro_rules! impl_inherent_data_provider_ext_tuple { - ( T, S $(, $TN:ident)* $( , )?) => { - impl InherentDataProviderExt for (T, S, $($TN),*) + ( S $(, $TN:ident)* $( , )?) => { + impl InherentDataProviderExt for (S, $($TN),*) where - T: Deref, S: Deref, { - fn timestamp(&self) -> Timestamp { - *self.0.deref() - } - fn slot(&self) -> Slot { - *self.1.deref() + *self.0.deref() } } } } -impl_inherent_data_provider_ext_tuple!(T, S); -impl_inherent_data_provider_ext_tuple!(T, S, A); -impl_inherent_data_provider_ext_tuple!(T, S, A, B); -impl_inherent_data_provider_ext_tuple!(T, S, A, B, C); -impl_inherent_data_provider_ext_tuple!(T, S, A, B, C, D); -impl_inherent_data_provider_ext_tuple!(T, S, A, B, C, D, E); -impl_inherent_data_provider_ext_tuple!(T, S, A, B, C, D, E, F); -impl_inherent_data_provider_ext_tuple!(T, S, A, B, C, D, E, F, G); -impl_inherent_data_provider_ext_tuple!(T, S, A, B, C, D, E, F, G, H); -impl_inherent_data_provider_ext_tuple!(T, S, A, B, C, D, E, F, G, H, I); -impl_inherent_data_provider_ext_tuple!(T, S, A, B, C, D, E, F, G, H, I, J); +impl_inherent_data_provider_ext_tuple!(S); +impl_inherent_data_provider_ext_tuple!(S, A); +impl_inherent_data_provider_ext_tuple!(S, A, B); +impl_inherent_data_provider_ext_tuple!(S, A, B, C); +impl_inherent_data_provider_ext_tuple!(S, A, B, C, D); +impl_inherent_data_provider_ext_tuple!(S, A, B, C, D, E); +impl_inherent_data_provider_ext_tuple!(S, A, B, C, D, E, F); +impl_inherent_data_provider_ext_tuple!(S, A, B, C, D, E, F, G); +impl_inherent_data_provider_ext_tuple!(S, A, B, C, D, E, F, G, H); +impl_inherent_data_provider_ext_tuple!(S, A, B, C, D, E, F, G, H, I); +impl_inherent_data_provider_ext_tuple!(S, A, B, C, D, E, F, G, H, I, J); /// Start a new slot worker. /// diff --git a/lldb.init b/lldb.init new file mode 100644 index 0000000000000..e41e9a422c14d --- /dev/null +++ b/lldb.init @@ -0,0 +1,5 @@ +file target/debug/node-sassafras +#file target/debug/deps/sp_keystore-a8d1167f7eadae2e) +#file target/debug/substrate +#file target/debug/deps/sc_client_db-4f42f24599f45c19 +breakpoint read -f breaks diff --git a/run-benches.sh b/run-benches.sh new file mode 100755 index 0000000000000..55073db66825b --- /dev/null +++ b/run-benches.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +#BINARY=substrate +BINARY=node-sassafras + +TARGET=debug +#TARGET=release + +PALLET=pallet_sassafras +EXTRINSIC=submit_tickets +STEPS=50 +REPEAT=20 + +OUTFILE=output.rs + +./target/$TARGET/$BINARY benchmark pallet \ + --chain dev \ + --execution wasm \ + --wasm-execution compiled \ + --pallet $PALLET \ + --extrinsic $EXTRINSIC \ + --steps $STEPS \ + --repeat $REPEAT \ + --output $OUTFILE From 1c49a653f253cfe3311006364519326e924aa523 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Mon, 12 Sep 2022 16:32:56 +0200 Subject: [PATCH 09/14] Slot based algorithm tests do not require timstamp inherent anymore --- client/consensus/aura/src/lib.rs | 6 +++--- client/consensus/babe/src/tests.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index c56ad0fa5cfb2..7779817d76f46 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -638,7 +638,7 @@ mod tests { dyn CreateInherentDataProviders< TestBlock, (), - InherentDataProviders = (TimestampInherentDataProvider, InherentDataProvider), + InherentDataProviders = (InherentDataProvider,), >, >, >; @@ -668,7 +668,7 @@ mod tests { SlotDuration::from_millis(6000), ); - Ok((timestamp, slot)) + Ok((slot,)) }), AlwaysCanAuthor, CheckForEquivocation::Yes, @@ -753,7 +753,7 @@ mod tests { SlotDuration::from_millis(6000), ); - Ok((timestamp, slot)) + Ok((slot, timestamp)) }, force_authoring: false, backoff_authoring_blocks: Some( diff --git a/client/consensus/babe/src/tests.rs b/client/consensus/babe/src/tests.rs index 7207b7a36c3d4..de3fbb7b66f12 100644 --- a/client/consensus/babe/src/tests.rs +++ b/client/consensus/babe/src/tests.rs @@ -240,7 +240,7 @@ pub struct TestVerifier { dyn CreateInherentDataProviders< TestBlock, (), - InherentDataProviders = (TimestampInherentDataProvider, InherentDataProvider), + InherentDataProviders = (InherentDataProvider,), >, >, >, @@ -328,7 +328,7 @@ impl TestNetFactory for BabeTestNet { SlotDuration::from_millis(6000), ); - Ok((timestamp, slot)) + Ok((slot,)) }), config: data.link.config.clone(), epoch_changes: data.link.epoch_changes.clone(), @@ -441,7 +441,7 @@ fn run_one_test(mutator: impl Fn(&mut TestHeader, Stage) + Send + Sync + 'static SlotDuration::from_millis(6000), ); - Ok((timestamp, slot)) + Ok((slot,)) }), force_authoring: false, backoff_authoring_blocks: Some(BackoffAuthoringOnFinalizedHeadLagging::default()), From 73c2a0f3356095208d4e848b4f0efa133e78c47d Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Mon, 12 Sep 2022 17:03:01 +0200 Subject: [PATCH 10/14] Remove junk files --- lldb.init | 5 ----- run-benches.sh | 24 ------------------------ 2 files changed, 29 deletions(-) delete mode 100644 lldb.init delete mode 100755 run-benches.sh diff --git a/lldb.init b/lldb.init deleted file mode 100644 index e41e9a422c14d..0000000000000 --- a/lldb.init +++ /dev/null @@ -1,5 +0,0 @@ -file target/debug/node-sassafras -#file target/debug/deps/sp_keystore-a8d1167f7eadae2e) -#file target/debug/substrate -#file target/debug/deps/sc_client_db-4f42f24599f45c19 -breakpoint read -f breaks diff --git a/run-benches.sh b/run-benches.sh deleted file mode 100755 index 55073db66825b..0000000000000 --- a/run-benches.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash - -#BINARY=substrate -BINARY=node-sassafras - -TARGET=debug -#TARGET=release - -PALLET=pallet_sassafras -EXTRINSIC=submit_tickets -STEPS=50 -REPEAT=20 - -OUTFILE=output.rs - -./target/$TARGET/$BINARY benchmark pallet \ - --chain dev \ - --execution wasm \ - --wasm-execution compiled \ - --pallet $PALLET \ - --extrinsic $EXTRINSIC \ - --steps $STEPS \ - --repeat $REPEAT \ - --output $OUTFILE From 516401389ad98f2802b6625db2841fa5cee5a708 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Mon, 12 Sep 2022 17:14:54 +0200 Subject: [PATCH 11/14] Further tests cleanup --- breaks | 1 - client/consensus/aura/src/lib.rs | 10 +++------- client/consensus/babe/src/tests.rs | 9 ++------- 3 files changed, 5 insertions(+), 15 deletions(-) delete mode 100644 breaks diff --git a/breaks b/breaks deleted file mode 100644 index ef27fab47280a..0000000000000 --- a/breaks +++ /dev/null @@ -1 +0,0 @@ -[{"Breakpoint":{"BKPTOptions":{"AutoContinue":false,"ConditionText":"","EnabledState":true,"IgnoreCount":0,"OneShotState":false},"BKPTResolver":{"Options":{"Column":0,"Exact":false,"FileName":"/mnt/ssd/users/develop/parity/substrate/client/consensus/babe/src/lib.rs","Inlines":true,"LineNumber":1076,"Offset":0,"SkipPrologue":true},"Type":"FileAndLine"},"Hardware":false,"SearchFilter":{"Options":{},"Type":"Unconstrained"}}}] diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 7779817d76f46..e7e2e7078a91f 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -578,7 +578,6 @@ mod tests { traits::{Block as BlockT, Header as _}, Digest, }; - use sp_timestamp::InherentDataProvider as TimestampInherentDataProvider; use std::{ task::Poll, time::{Duration, Instant}, @@ -662,12 +661,10 @@ mod tests { import_queue::AuraVerifier::new( client, Box::new(|_, _| async { - let timestamp = TimestampInherentDataProvider::from_system_time(); let slot = InherentDataProvider::from_timestamp_and_slot_duration( - *timestamp, + sp_timestamp::current_timestamp(), SlotDuration::from_millis(6000), ); - Ok((slot,)) }), AlwaysCanAuthor, @@ -747,13 +744,12 @@ mod tests { sync_oracle: DummyOracle, justification_sync_link: (), create_inherent_data_providers: |_, _| async { - let timestamp = TimestampInherentDataProvider::from_system_time(); let slot = InherentDataProvider::from_timestamp_and_slot_duration( - *timestamp, + sp_timestamp::current_timestamp(), SlotDuration::from_millis(6000), ); - Ok((slot, timestamp)) + Ok((slot,)) }, force_authoring: false, backoff_authoring_blocks: Some( diff --git a/client/consensus/babe/src/tests.rs b/client/consensus/babe/src/tests.rs index de3fbb7b66f12..72c6e9c6191b2 100644 --- a/client/consensus/babe/src/tests.rs +++ b/client/consensus/babe/src/tests.rs @@ -43,7 +43,6 @@ use sp_runtime::{ generic::{Digest, DigestItem}, traits::Block as BlockT, }; -use sp_timestamp::InherentDataProvider as TimestampInherentDataProvider; use std::{cell::RefCell, task::Poll, time::Duration}; type Item = DigestItem; @@ -322,12 +321,10 @@ impl TestNetFactory for BabeTestNet { client: client.clone(), select_chain: longest_chain, create_inherent_data_providers: Box::new(|_, _| async { - let timestamp = TimestampInherentDataProvider::from_system_time(); let slot = InherentDataProvider::from_timestamp_and_slot_duration( - *timestamp, + sp_timestamp::current_timestamp(), SlotDuration::from_millis(6000), ); - Ok((slot,)) }), config: data.link.config.clone(), @@ -435,12 +432,10 @@ fn run_one_test(mutator: impl Fn(&mut TestHeader, Stage) + Send + Sync + 'static env: environ, sync_oracle: DummyOracle, create_inherent_data_providers: Box::new(|_, _| async { - let timestamp = TimestampInherentDataProvider::from_system_time(); let slot = InherentDataProvider::from_timestamp_and_slot_duration( - *timestamp, + sp_timestamp::current_timestamp(), SlotDuration::from_millis(6000), ); - Ok((slot,)) }), force_authoring: false, From 2d94e872be14a687f6e0ff9faf7b1a13b097e2f9 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Mon, 12 Sep 2022 18:14:17 +0200 Subject: [PATCH 12/14] Trigger pipeline From 5af582e13d47b5deb6ee55e75903a5daa3e64152 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Tue, 13 Sep 2022 17:58:12 +0200 Subject: [PATCH 13/14] Apply code suggestions --- Cargo.lock | 1 - client/consensus/aura/src/lib.rs | 15 ++++++++------- client/consensus/babe/src/tests.rs | 11 +++++++---- client/consensus/slots/Cargo.toml | 1 - primitives/timestamp/src/lib.rs | 26 ++++++++++++-------------- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 335e8b3526792..5f284b3623a2e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8144,7 +8144,6 @@ dependencies = [ "sp-inherents", "sp-runtime", "sp-state-machine", - "sp-timestamp", "substrate-test-runtime-client", "thiserror", ] diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index e7e2e7078a91f..52df2a097b6b2 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -578,6 +578,7 @@ mod tests { traits::{Block as BlockT, Header as _}, Digest, }; + use sp_timestamp::Timestamp; use std::{ task::Poll, time::{Duration, Instant}, @@ -587,6 +588,8 @@ mod tests { TestClient, }; + const SLOT_DURATION_MS: u64 = 1000; + type Error = sp_blockchain::Error; struct DummyFactory(Arc); @@ -627,8 +630,6 @@ mod tests { } } - const SLOT_DURATION: u64 = 1000; - type AuraVerifier = import_queue::AuraVerifier< PeersFullClient, AuthorityPair, @@ -657,13 +658,13 @@ mod tests { let client = client.as_client(); let slot_duration = slot_duration(&*client).expect("slot duration available"); - assert_eq!(slot_duration.as_millis() as u64, SLOT_DURATION); + assert_eq!(slot_duration.as_millis() as u64, SLOT_DURATION_MS); import_queue::AuraVerifier::new( client, Box::new(|_, _| async { let slot = InherentDataProvider::from_timestamp_and_slot_duration( - sp_timestamp::current_timestamp(), - SlotDuration::from_millis(6000), + Timestamp::current(), + SlotDuration::from_millis(SLOT_DURATION_MS), ); Ok((slot,)) }), @@ -745,8 +746,8 @@ mod tests { justification_sync_link: (), create_inherent_data_providers: |_, _| async { let slot = InherentDataProvider::from_timestamp_and_slot_duration( - sp_timestamp::current_timestamp(), - SlotDuration::from_millis(6000), + Timestamp::current(), + SlotDuration::from_millis(SLOT_DURATION_MS), ); Ok((slot,)) diff --git a/client/consensus/babe/src/tests.rs b/client/consensus/babe/src/tests.rs index 72c6e9c6191b2..234e509b7e264 100644 --- a/client/consensus/babe/src/tests.rs +++ b/client/consensus/babe/src/tests.rs @@ -43,6 +43,7 @@ use sp_runtime::{ generic::{Digest, DigestItem}, traits::Block as BlockT, }; +use sp_timestamp::Timestamp; use std::{cell::RefCell, task::Poll, time::Duration}; type Item = DigestItem; @@ -67,6 +68,8 @@ type Mutator = Arc; type BabeBlockImport = PanickingBlockImport>>; +const SLOT_DURATION_MS: u64 = 1000; + #[derive(Clone)] struct DummyFactory { client: Arc, @@ -322,8 +325,8 @@ impl TestNetFactory for BabeTestNet { select_chain: longest_chain, create_inherent_data_providers: Box::new(|_, _| async { let slot = InherentDataProvider::from_timestamp_and_slot_duration( - sp_timestamp::current_timestamp(), - SlotDuration::from_millis(6000), + Timestamp::current(), + SlotDuration::from_millis(SLOT_DURATION_MS), ); Ok((slot,)) }), @@ -433,8 +436,8 @@ fn run_one_test(mutator: impl Fn(&mut TestHeader, Stage) + Send + Sync + 'static sync_oracle: DummyOracle, create_inherent_data_providers: Box::new(|_, _| async { let slot = InherentDataProvider::from_timestamp_and_slot_duration( - sp_timestamp::current_timestamp(), - SlotDuration::from_millis(6000), + Timestamp::current(), + SlotDuration::from_millis(SLOT_DURATION_MS), ); Ok((slot,)) }), diff --git a/client/consensus/slots/Cargo.toml b/client/consensus/slots/Cargo.toml index 208e31971257d..fae499ad7c7c6 100644 --- a/client/consensus/slots/Cargo.toml +++ b/client/consensus/slots/Cargo.toml @@ -31,7 +31,6 @@ sp-core = { version = "6.0.0", path = "../../../primitives/core" } sp-inherents = { version = "4.0.0-dev", path = "../../../primitives/inherents" } sp-runtime = { version = "6.0.0", path = "../../../primitives/runtime" } sp-state-machine = { version = "0.12.0", path = "../../../primitives/state-machine" } -sp-timestamp = { version = "4.0.0-dev", path = "../../../primitives/timestamp" } [dev-dependencies] substrate-test-runtime-client = { version = "2.0.0", path = "../../../test-utils/runtime/client" } diff --git a/primitives/timestamp/src/lib.rs b/primitives/timestamp/src/lib.rs index 7e19552da0cff..d88b1839babe6 100644 --- a/primitives/timestamp/src/lib.rs +++ b/primitives/timestamp/src/lib.rs @@ -56,6 +56,17 @@ impl Timestamp { pub fn checked_sub(self, other: Self) -> Option { self.0.checked_sub(other.0).map(Self) } + + /// The current timestamp using the system time. + #[cfg(feature = "std")] + pub fn current() -> Self { + use std::time::SystemTime; + + let now = SystemTime::now(); + now.duration_since(SystemTime::UNIX_EPOCH) + .expect("Current time is always after unix epoch; qed") + .into() + } } impl sp_std::ops::Deref for Timestamp { @@ -165,19 +176,6 @@ impl TimestampInherentData for InherentData { } } -/// The current timestamp using the system time. -/// -/// This timestamp is the time since the UNIX epoch. -#[cfg(feature = "std")] -pub fn current_timestamp() -> Timestamp { - use std::time::SystemTime; - - let now = SystemTime::now(); - now.duration_since(SystemTime::UNIX_EPOCH) - .expect("Current time is always after unix epoch; qed") - .into() -} - /// Provide duration since unix epoch in millisecond for timestamp inherent. #[cfg(feature = "std")] pub struct InherentDataProvider { @@ -191,7 +189,7 @@ impl InherentDataProvider { pub fn from_system_time() -> Self { Self { max_drift: std::time::Duration::from_secs(60).into(), - timestamp: current_timestamp(), + timestamp: Timestamp::current(), } } From 2cebbaedd756a4d316621cdd1d0a42ff2c0351a8 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Fri, 23 Sep 2022 19:22:32 +0200 Subject: [PATCH 14/14] Trigger pipeline