difftreelog
Merge pull request #332 from UniqueNetwork/feature/autoseal-every-n-secs
in: master
Feature/autoseal every n secs
4 files changed
node/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" }
node/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>,
node/cli/src/command.rsdiffbeforeafterboth--- a/node/cli/src/command.rs
+++ b/node/cli/src/command.rs
@@ -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,10 @@
if is_dev_service {
info!("Running Dev service");
+ let autoseal_interval = 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)
};
};
node/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;25use futures::StreamExt;26use futures::{27 Stream, StreamExt,28 stream::select,29 task::{Context, Poll},30};31use tokio::time::Interval;263227use unique_rpc::overrides_handle;33use unique_rpc::overrides_handle;2834111 }117 }112}118}119120pub struct AutosealInterval {121 interval: Interval,122}123124impl AutosealInterval {125 pub fn new(config: &Configuration, interval: Duration) -> Self {126 let _tokio_runtime = config.tokio_handle.enter();127 let interval = tokio::time::interval(interval);128129 Self { interval }130 }131}132133impl Stream for AutosealInterval {134 type Item = tokio::time::Instant;135136 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {137 self.interval.poll_tick(cx).map(Some)138 }139}113140114pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {141pub fn open_frontier_backend(config: &Configuration) -> Result<Arc<fc_db::Backend<Block>>, String> {115 let config_dir = config142 let config_dir = config712/// the parachain inherent739/// the parachain inherent713pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(740pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(714 config: Configuration,741 config: Configuration,742 autoseal_interval: Duration,715) -> sc_service::error::Result<TaskManager>743) -> sc_service::error::Result<TaskManager>716where744where717 Runtime: RuntimeInstance + Send + Sync + 'static,745 Runtime: RuntimeInstance + Send + Sync + 'static,735 + sp_consensus_aura::AuraApi<Block, AuraId>,763 + sp_consensus_aura::AuraApi<Block, AuraId>,736 ExecutorDispatch: NativeExecutionDispatch + 'static,764 ExecutorDispatch: NativeExecutionDispatch + 'static,737{765{738 use futures::Stream;739 use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams};766 use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams};740 use fc_consensus::FrontierBlockImport;767 use fc_consensus::FrontierBlockImport;741 use sc_client_api::HeaderBackend;768 use sc_client_api::HeaderBackend;799 telemetry.as_ref().map(|x| x.handle()),826 telemetry.as_ref().map(|x| x.handle()),800 );827 );801828802 let commands_stream: Box<dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin> =829 let transactions_commands_stream: Box<830 dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin,803 Box::new(831 > = Box::new(804 // This bit cribbed from the implementation of instant seal.805 transaction_pool832 transaction_pool806 .pool()833 .pool()807 .validated_pool()834 .validated_pool()808 .import_notification_stream()835 .import_notification_stream()809 .map(|_| EngineCommand::SealNewBlock {836 .map(|_| EngineCommand::SealNewBlock {810 create_empty: true, // was false in Moonbeam837 create_empty: true,811 finalize: false,838 finalize: false,812 parent_hash: None,839 parent_hash: None,813 sender: None,840 sender: None,814 }),841 }),815 );842 );843844 let autoseal_interval = Box::pin(AutosealInterval::new(&config, autoseal_interval));845 let idle_commands_stream: Box<846 dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin,847 > = Box::new(autoseal_interval.map(|_| EngineCommand::SealNewBlock {848 create_empty: true,849 finalize: false,850 parent_hash: None,851 sender: None,852 }));853854 let commands_stream = select(transactions_commands_stream, idle_commands_stream);816855817 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;856 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;818 let client_set_aside_for_cidp = client.clone();857 let client_set_aside_for_cidp = client.clone();