difftreelog
Autoseal after idle n seconds
in: master
4 files changed
node/cli/Cargo.tomldiffbeforeafterboth294294295[dependencies]295[dependencies]296futures = '0.3.17'296futures = '0.3.17'297futures-timer = '3.0.2'297log = '0.4.14'298log = '0.4.14'298flexi_logger = "0.15.7"299flexi_logger = "0.15.7"299parking_lot = '0.11.2'300parking_lot = '0.11.2'node/cli/src/cli.rsdiffbeforeafterboth104 #[structopt(flatten)]104 #[structopt(flatten)]105 pub run: cumulus_client_cli::RunCmd,105 pub run: cumulus_client_cli::RunCmd,106107 /// When running the node in the `--dev` mode and108 /// there is no transaction in the transaction pool,109 /// an empty block will be sealed automatically110 /// after the `--idle-autoseal-interval` milliseconds.111 ///112 /// Default interval is 500 milliseconds113 #[structopt(default_value = "500", long)]114 pub idle_autoseal_interval: u64,106115107 /// Relaychain arguments116 /// Relaychain arguments108 #[structopt(raw = true)]117 #[structopt(raw = true)]node/cli/src/command.rsdiffbeforeafterboth35use 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},38 service::{new_partial, start_node, start_dev_node, AutosealInterval},39};39};404041#[cfg(feature = "unique-runtime")]41#[cfg(feature = "unique-runtime")]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};646465use unique_runtime_common::types::Block;65use unique_runtime_common::types::Block;6666405 if is_dev_service {405 if is_dev_service {406 info!("Running Dev service");406 info!("Running Dev service");407408 let autoseal_interval = AutosealInterval::new(409 Duration::from_millis(cli.idle_autoseal_interval)410 )?;407411408 return start_node_using_chain_runtime! {412 return start_node_using_chain_runtime! {409 start_dev_node(config).map_err(Into::into)413 start_dev_node(config, autoseal_interval).map_err(Into::into)410 };414 };411 };415 };412416node/cli/src/service.rsdiffbeforeafterboth21use 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;262927use unique_rpc::overrides_handle;30use unique_rpc::overrides_handle;2831111 }114 }112}115}116117pub struct AutosealInterval {118 duration: Duration,119 delay_handle: Pin<Box<Delay>>120}121122impl 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 }127128 Ok(Self {129 duration,130 delay_handle: Box::pin(Delay::new(duration))131 })132 }133}134135impl Stream for AutosealInterval {136 type Item = ();137138 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);143144 Poll::Ready(Some(()))145 }146 Poll::Pending => Poll::Pending147 }148 }149}113150114pub 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 = config712/// the parachain inherent749/// the parachain inherent713pub 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>716where754where717 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 );801838802 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_pool806 .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 );852853 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 );863864 let commands_stream = select(865 transactions_commands_stream,866 idle_commands_stream867 );816868817 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();