diff --git a/bin/node/test-runner-example/src/lib.rs b/bin/node/test-runner-example/src/lib.rs index 8a5fbdad885c..8a3f5560ec86 100644 --- a/bin/node/test-runner-example/src/lib.rs +++ b/bin/node/test-runner-example/src/lib.rs @@ -27,7 +27,9 @@ use sc_consensus_babe::BabeBlockImport; use sp_keystore::SyncCryptoStorePtr; use sp_keyring::sr25519::Keyring::Alice; use sp_consensus_babe::AuthorityId; -use sc_consensus_manual_seal::{ConsensusDataProvider, consensus::babe::BabeConsensusDataProvider}; +use sc_consensus_manual_seal::{ + ConsensusDataProvider, consensus::babe::{BabeConsensusDataProvider, SlotTimestampProvider}, +}; use sp_runtime::{traits::IdentifyAccount, MultiSigner, generic::Era}; use node_cli::chain_spec::development_config; @@ -59,10 +61,7 @@ impl ChainInfo for NodeTemplateChainInfo { Self::SelectChain, >; type SignedExtras = node_runtime::SignedExtra; - type InherentDataProviders = ( - sp_timestamp::InherentDataProvider, - sp_consensus_babe::inherents::InherentDataProvider, - ); + type InherentDataProviders = (SlotTimestampProvider, sp_consensus_babe::inherents::InherentDataProvider); fn signed_extras(from: ::AccountId) -> Self::SignedExtras { ( @@ -139,20 +138,16 @@ impl ChainInfo for NodeTemplateChainInfo { .expect("failed to create ConsensusDataProvider"); Ok(( - client, + client.clone(), backend, keystore.sync_keystore(), task_manager, Box::new(move |_, _| { - let slot_duration = slot_duration.clone(); + let client = client.clone(); async move { - let timestamp = sp_timestamp::InherentDataProvider::from_system_time(); - let slot = sp_consensus_babe::inherents::InherentDataProvider::from_timestamp_and_duration( - *timestamp, - slot_duration.slot_duration(), - ); - - Ok((timestamp, slot)) + let timestamp = SlotTimestampProvider::new(client.clone()).map_err(|err| format!("{:?}", err))?; + let babe = sp_consensus_babe::inherents::InherentDataProvider::new(timestamp.slot().into()); + Ok((timestamp, babe)) } }), Some(Box::new(consensus_data_provider)), diff --git a/client/consensus/manual-seal/src/consensus/babe.rs b/client/consensus/manual-seal/src/consensus/babe.rs index 29fea05d8366..949e1b6fd719 100644 --- a/client/consensus/manual-seal/src/consensus/babe.rs +++ b/client/consensus/manual-seal/src/consensus/babe.rs @@ -260,7 +260,7 @@ impl SlotTimestampProvider { // looks like this isn't the first block, rehydrate the fake time. // otherwise we'd be producing blocks for older slots. - let duration = if info.best_number != Zero::zero() { + let time = if info.best_number != Zero::zero() { let header = client.header(BlockId::Hash(info.best_hash))?.unwrap(); let slot = find_pre_digest::(&header).unwrap().slot(); // add the slot duration so there's no collision of slots @@ -274,10 +274,15 @@ impl SlotTimestampProvider { }; Ok(Self { - time: atomic::AtomicU64::new(duration), + time: atomic::AtomicU64::new(time), slot_duration, }) } + + /// Get the current slot number + pub fn slot(&self) -> u64 { + self.time.load(atomic::Ordering::SeqCst) / self.slot_duration + } } #[async_trait::async_trait] diff --git a/client/consensus/manual-seal/src/seal_block.rs b/client/consensus/manual-seal/src/seal_block.rs index 6f2b613cd939..4aecfc213ab4 100644 --- a/client/consensus/manual-seal/src/seal_block.rs +++ b/client/consensus/manual-seal/src/seal_block.rs @@ -111,10 +111,7 @@ pub async fn seal_block( let inherent_data_providers = create_inherent_data_providers - .create_inherent_data_providers( - parent.hash(), - (), - ) + .create_inherent_data_providers(parent.hash(), ()) .await .map_err(|e| Error::Other(e))?;