git.delta.rocks / unique-network / refs/commits / ae3d822925b8

difftreelog

Autoseal after idle n seconds

Daniel Shiposha2022-03-31parent: #20779ae.patch.diff
in: master

4 files changed

modifiednode/cli/Cargo.tomldiffbeforeafterboth
--- a/node/cli/Cargo.toml
+++ b/node/cli/Cargo.toml
@@ -294,6 +294,7 @@
 
 [dependencies]
 futures = '0.3.17'
+futures-timer = '3.0.2'
 log = '0.4.14'
 flexi_logger = "0.15.7"
 parking_lot = '0.11.2'
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.
+	///
+	/// 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
--- a/node/cli/src/command.rs
+++ b/node/cli/src/command.rs
@@ -35,7 +35,7 @@
 use crate::{
 	chain_spec::{self, RuntimeId, RuntimeIdentification, ServiceId, ServiceIdentification},
 	cli::{Cli, RelayChainCli, Subcommand},
-	service::{new_partial, start_node, start_dev_node},
+	service::{new_partial, start_node, start_dev_node, AutosealInterval},
 };
 
 #[cfg(feature = "unique-runtime")]
@@ -60,7 +60,7 @@
 };
 use sp_core::hexdisplay::HexDisplay;
 use sp_runtime::traits::Block as BlockT;
-use std::{io::Write, net::SocketAddr};
+use std::{io::Write, net::SocketAddr, time::Duration};
 
 use unique_runtime_common::types::Block;
 
@@ -405,8 +405,12 @@
 				if is_dev_service {
 					info!("Running Dev service");
 
+					let autoseal_interval = AutosealInterval::new(
+						Duration::from_millis(cli.idle_autoseal_interval)
+					)?;
+
 					return start_node_using_chain_runtime! {
-						start_dev_node(config).map_err(Into::into)
+						start_dev_node(config, autoseal_interval).map_err(Into::into)
 					};
 				};
 
modifiednode/cli/src/service.rsdiffbeforeafterboth
21use std::sync::Mutex;21use std::sync::Mutex;
22use std::collections::BTreeMap;22use std::collections::BTreeMap;
23use std::time::Duration;23use std::time::Duration;
24use std::pin::Pin;
24use fc_rpc_core::types::FeeHistoryCache;25use fc_rpc_core::types::FeeHistoryCache;
26use futures::Future;
25use futures::StreamExt;27use futures::{Stream, StreamExt, stream::select, task::{Context, Poll}};
28use futures_timer::Delay;
2629
27use unique_rpc::overrides_handle;30use unique_rpc::overrides_handle;
2831
111 }114 }
112}115}
116
117pub struct AutosealInterval {
118 duration: Duration,
119 delay_handle: Pin<Box<Delay>>
120}
121
122impl AutosealInterval {
123 pub fn new(duration: Duration) -> Result<Self, String> {
124 if duration.is_zero() {
125 return Err("Invalid autoseal interval: 0 seconds".into());
126 }
127
128 Ok(Self {
129 duration,
130 delay_handle: Box::pin(Delay::new(duration))
131 })
132 }
133}
134
135impl Stream for AutosealInterval {
136 type Item = ();
137
138 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
139 match self.delay_handle.as_mut().poll(cx) {
140 Poll::Ready(_) => {
141 let duration = self.duration;
142 self.delay_handle.reset(duration);
143
144 Poll::Ready(Some(()))
145 }
146 Poll::Pending => Poll::Pending
147 }
148 }
149}
113150
114pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {151pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {
115 let config_dir = config152 let config_dir = config
712/// the parachain inherent749/// the parachain inherent
713pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(750pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(
714 config: Configuration,751 config: Configuration,
752 autoseal_interval: AutosealInterval,
715) -> sc_service::error::Result<TaskManager>753) -> sc_service::error::Result<TaskManager>
716where754where
717 Runtime: RuntimeInstance + Send + Sync + 'static,755 Runtime: RuntimeInstance + Send + Sync + 'static,
735 + sp_consensus_aura::AuraApi<Block, AuraId>,773 + sp_consensus_aura::AuraApi<Block, AuraId>,
736 ExecutorDispatch: NativeExecutionDispatch + 'static,774 ExecutorDispatch: NativeExecutionDispatch + 'static,
737{775{
738 use futures::Stream;
739 use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams};776 use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams};
740 use fc_consensus::FrontierBlockImport;777 use fc_consensus::FrontierBlockImport;
741 use sc_client_api::HeaderBackend;778 use sc_client_api::HeaderBackend;
799 telemetry.as_ref().map(|x| x.handle()),836 telemetry.as_ref().map(|x| x.handle()),
800 );837 );
801838
802 let commands_stream: Box<dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin> =839 let transactions_commands_stream: Box<dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin> =
803 Box::new(840 Box::new(
804 // This bit cribbed from the implementation of instant seal.
805 transaction_pool841 transaction_pool
806 .pool()842 .pool()
807 .validated_pool()843 .validated_pool()
808 .import_notification_stream()844 .import_notification_stream()
809 .map(|_| EngineCommand::SealNewBlock {845 .map(|_| EngineCommand::SealNewBlock {
810 create_empty: true, // was false in Moonbeam846 create_empty: true,
811 finalize: false,847 finalize: false,
812 parent_hash: None,848 parent_hash: None,
813 sender: None,849 sender: None,
814 }),850 }),
815 );851 );
852
853 let autoseal_interval = Box::pin(autoseal_interval);
854 let idle_commands_stream: Box<dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin> =
855 Box::new(
856 autoseal_interval.map(|_| EngineCommand::SealNewBlock {
857 create_empty: true,
858 finalize: false,
859 parent_hash: None,
860 sender: None,
861 })
862 );
863
864 let commands_stream = select(
865 transactions_commands_stream,
866 idle_commands_stream
867 );
816868
817 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;869 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;
818 let client_set_aside_for_cidp = client.clone();870 let client_set_aside_for_cidp = client.clone();