git.delta.rocks / unique-network / refs/commits / 1ce1209cf657

difftreelog

Use tokio instead of future-timer

Daniel Shiposha2022-03-31parent: #2d2fd30.patch.diff
in: master

3 files changed

modifiednode/cli/Cargo.tomldiffbeforeafterboth
--- a/node/cli/Cargo.toml
+++ b/node/cli/Cargo.toml
@@ -294,13 +294,13 @@
 
 [dependencies]
 futures = '0.3.17'
-futures-timer = '3.0.2'
 log = '0.4.14'
 flexi_logger = "0.15.7"
 parking_lot = '0.11.2'
 clap = "3.1.2"
 jsonrpc-core = '18.0.0'
 jsonrpc-pubsub = "18.0.0"
+tokio = { version = "1.17.0", features = ["time"] }
 
 fc-rpc-core = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.18" }
 fc-consensus = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.18" }
modifiednode/cli/src/command.rsdiffbeforeafterboth
35use crate::{35use crate::{
36 chain_spec::{self, RuntimeId, RuntimeIdentification, ServiceId, ServiceIdentification},36 chain_spec::{self, RuntimeId, RuntimeIdentification, ServiceId, ServiceIdentification},
37 cli::{Cli, RelayChainCli, Subcommand},37 cli::{Cli, RelayChainCli, Subcommand},
38 service::{new_partial, start_node, start_dev_node, AutosealInterval},38 service::{new_partial, start_node, start_dev_node},
39};39};
4040
41#[cfg(feature = "unique-runtime")]41#[cfg(feature = "unique-runtime")]
406 info!("Running Dev service");406 info!("Running Dev service");
407407
408 let autoseal_interval =408 let autoseal_interval = Duration::from_millis(cli.idle_autoseal_interval);
409 AutosealInterval::new(Duration::from_millis(cli.idle_autoseal_interval))?;
410409
411 return start_node_using_chain_runtime! {410 return start_node_using_chain_runtime! {
412 start_dev_node(config, autoseal_interval).map_err(Into::into)411 start_dev_node(config, autoseal_interval).map_err(Into::into)
modifiednode/cli/src/service.rsdiffbeforeafterboth
--- a/node/cli/src/service.rs
+++ b/node/cli/src/service.rs
@@ -23,13 +23,12 @@
 use std::time::Duration;
 use std::pin::Pin;
 use fc_rpc_core::types::FeeHistoryCache;
-use futures::Future;
 use futures::{
 	Stream, StreamExt,
 	stream::select,
 	task::{Context, Poll},
 };
-use futures_timer::Delay;
+use tokio::time::Interval;
 
 use unique_rpc::overrides_handle;
 
@@ -119,36 +118,23 @@
 }
 
 pub struct AutosealInterval {
-	duration: Duration,
-	delay_handle: Pin<Box<Delay>>,
+	interval: Interval,
 }
 
 impl AutosealInterval {
-	pub fn new(duration: Duration) -> Result<Self, String> {
-		if duration.is_zero() {
-			return Err("Invalid autoseal interval: 0 seconds".into());
-		}
+	pub fn new(config: &Configuration, interval: Duration) -> Self {
+		let _tokio_runtime = config.tokio_handle.enter();
+		let interval = tokio::time::interval(interval);
 
-		Ok(Self {
-			duration,
-			delay_handle: Box::pin(Delay::new(duration)),
-		})
+		Self { interval }
 	}
 }
 
 impl Stream for AutosealInterval {
-	type Item = ();
+	type Item = tokio::time::Instant;
 
 	fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
-		match self.delay_handle.as_mut().poll(cx) {
-			Poll::Ready(_) => {
-				let duration = self.duration;
-				self.delay_handle.reset(duration);
-
-				Poll::Ready(Some(()))
-			}
-			Poll::Pending => Poll::Pending,
-		}
+		self.interval.poll_tick(cx).map(Some)
 	}
 }
 
@@ -753,7 +739,7 @@
 /// the parachain inherent
 pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(
 	config: Configuration,
-	autoseal_interval: AutosealInterval,
+	autoseal_interval: Duration,
 ) -> sc_service::error::Result<TaskManager>
 where
 	Runtime: RuntimeInstance + Send + Sync + 'static,
@@ -855,7 +841,7 @@
 				}),
 		);
 
-		let autoseal_interval = Box::pin(autoseal_interval);
+		let autoseal_interval = Box::pin(AutosealInterval::new(&config, autoseal_interval));
 		let idle_commands_stream: Box<
 			dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin,
 		> = Box::new(autoseal_interval.map(|_| EngineCommand::SealNewBlock {