git.delta.rocks / unique-network / refs/commits / 219f5f01d1e5

difftreelog

Merge pull request #332 from UniqueNetwork/feature/autoseal-every-n-secs

kozyrevdev2022-04-01parents: #62bde24 #7e36fe0.patch.diff
in: master
Feature/autoseal every n secs

4 files changed

modifiednode/cli/Cargo.tomldiffbeforeafterboth
--- a/node/cli/Cargo.toml
+++ b/node/cli/Cargo.toml
@@ -300,6 +300,7 @@
 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/cli.rsdiffbeforeafterboth
--- a/node/cli/src/cli.rs
+++ b/node/cli/src/cli.rs
@@ -104,6 +104,15 @@
 	#[structopt(flatten)]
 	pub run: cumulus_client_cli::RunCmd,
 
+	/// When running the node in the `--dev` mode and
+	/// there is no transaction in the transaction pool,
+	/// an empty block will be sealed automatically
+	/// after the `--idle-autoseal-interval` milliseconds.
+	///
+	/// The default interval is 500 milliseconds
+	#[structopt(default_value = "500", long)]
+	pub idle_autoseal_interval: u64,
+
 	/// Relaychain arguments
 	#[structopt(raw = true)]
 	pub relaychain_args: Vec<String>,
modifiednode/cli/src/command.rsdiffbeforeafterboth
60};60};
61use sp_core::hexdisplay::HexDisplay;61use sp_core::hexdisplay::HexDisplay;
62use sp_runtime::traits::Block as BlockT;62use sp_runtime::traits::Block as BlockT;
63use std::{io::Write, net::SocketAddr};63use std::{io::Write, net::SocketAddr, time::Duration};
6464
65use unique_runtime_common::types::Block;65use unique_runtime_common::types::Block;
6666
405 if is_dev_service {405 if is_dev_service {
406 info!("Running Dev service");406 info!("Running Dev service");
407
408 let autoseal_interval = Duration::from_millis(cli.idle_autoseal_interval);
407409
408 return start_node_using_chain_runtime! {410 return start_node_using_chain_runtime! {
409 start_dev_node(config).map_err(Into::into)411 start_dev_node(config, autoseal_interval).map_err(Into::into)
410 };412 };
411 };413 };
412414
modifiednode/cli/src/service.rsdiffbeforeafterboth
--- a/node/cli/src/service.rs
+++ b/node/cli/src/service.rs
@@ -21,8 +21,14 @@
 use std::sync::Mutex;
 use std::collections::BTreeMap;
 use std::time::Duration;
+use std::pin::Pin;
 use fc_rpc_core::types::FeeHistoryCache;
-use futures::StreamExt;
+use futures::{
+	Stream, StreamExt,
+	stream::select,
+	task::{Context, Poll},
+};
+use tokio::time::Interval;
 
 use unique_rpc::overrides_handle;
 
@@ -111,6 +117,27 @@
 	}
 }
 
+pub struct AutosealInterval {
+	interval: Interval,
+}
+
+impl AutosealInterval {
+	pub fn new(config: &Configuration, interval: Duration) -> Self {
+		let _tokio_runtime = config.tokio_handle.enter();
+		let interval = tokio::time::interval(interval);
+
+		Self { interval }
+	}
+}
+
+impl Stream for AutosealInterval {
+	type Item = tokio::time::Instant;
+
+	fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
+		self.interval.poll_tick(cx).map(Some)
+	}
+}
+
 pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {
 	let config_dir = config
 		.base_path
@@ -712,6 +739,7 @@
 /// the parachain inherent
 pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(
 	config: Configuration,
+	autoseal_interval: Duration,
 ) -> sc_service::error::Result<TaskManager>
 where
 	Runtime: RuntimeInstance + Send + Sync + 'static,
@@ -735,7 +763,6 @@
 		+ sp_consensus_aura::AuraApi<Block, AuraId>,
 	ExecutorDispatch: NativeExecutionDispatch + 'static,
 {
-	use futures::Stream;
 	use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams};
 	use fc_consensus::FrontierBlockImport;
 	use sc_client_api::HeaderBackend;
@@ -799,20 +826,32 @@
 			telemetry.as_ref().map(|x| x.handle()),
 		);
 
-		let commands_stream: Box<dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin> =
-			Box::new(
-				// This bit cribbed from the implementation of instant seal.
-				transaction_pool
-					.pool()
-					.validated_pool()
-					.import_notification_stream()
-					.map(|_| EngineCommand::SealNewBlock {
-						create_empty: true, // was false in Moonbeam
-						finalize: false,
-						parent_hash: None,
-						sender: None,
-					}),
-			);
+		let transactions_commands_stream: Box<
+			dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin,
+		> = Box::new(
+			transaction_pool
+				.pool()
+				.validated_pool()
+				.import_notification_stream()
+				.map(|_| EngineCommand::SealNewBlock {
+					create_empty: true,
+					finalize: false,
+					parent_hash: None,
+					sender: None,
+				}),
+		);
+
+		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 {
+			create_empty: true,
+			finalize: false,
+			parent_hash: None,
+			sender: None,
+		}));
+
+		let commands_stream = select(transactions_commands_stream, idle_commands_stream);
 
 		let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;
 		let client_set_aside_for_cidp = client.clone();