From 184b30a86bc89ae1a2c7e1511a4a86d227a0619d Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Mon, 2 Oct 2023 16:36:08 +0200 Subject: [PATCH] feat: finalization can be enabled in dev mode --- node/cli/src/cli.rs | 8 +++++++- node/cli/src/command.rs | 5 +---- node/cli/src/service.rs | 31 +++++++++++++++++++++++++------ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/node/cli/src/cli.rs b/node/cli/src/cli.rs index 54f8975fb0..3a9b612cb1 100644 --- a/node/cli/src/cli.rs +++ b/node/cli/src/cli.rs @@ -80,7 +80,7 @@ pub struct Cli { /// an empty block will be sealed automatically /// after the `--idle-autoseal-interval` milliseconds. /// - /// The default interval is 500 milliseconds + /// The default interval is 500 milliseconds. #[structopt(default_value = "500", long)] pub idle_autoseal_interval: u64, @@ -88,6 +88,12 @@ pub struct Cli { #[structopt(long)] pub disable_autoseal_on_tx: bool, + /// Finalization delay (in seconds) of auto-sealed blocks in the `--dev` mode. + /// + /// Disabled by default. + #[structopt(long)] + pub autoseal_finalization_delay: Option, + /// Disable automatic hardware benchmarks. /// /// By default these benchmarks are automatically ran at startup and measure diff --git a/node/cli/src/command.rs b/node/cli/src/command.rs index 64b0548da6..3db301a27a 100644 --- a/node/cli/src/command.rs +++ b/node/cli/src/command.rs @@ -62,7 +62,6 @@ use sc_cli::{ use sc_service::config::{BasePath, PrometheusConfig}; use sp_core::hexdisplay::HexDisplay; use sp_runtime::traits::{AccountIdConversion, Block as BlockT}; -use std::{time::Duration}; use up_common::types::opaque::{Block, RuntimeId}; @@ -481,14 +480,12 @@ pub fn run() -> Result<()> { if is_dev_service { info!("Running Dev service"); - let autoseal_interval = Duration::from_millis(cli.idle_autoseal_interval); - let mut config = config; config.state_pruning = Some(sc_service::PruningMode::ArchiveAll); return start_node_using_chain_runtime! { - start_dev_node(config, autoseal_interval, cli.disable_autoseal_on_tx).map_err(Into::into) + start_dev_node(config, cli.idle_autoseal_interval, cli.autoseal_finalization_delay, cli.disable_autoseal_on_tx).map_err(Into::into) }; }; diff --git a/node/cli/src/service.rs b/node/cli/src/service.rs index 63183991ba..b50c6e2fcb 100644 --- a/node/cli/src/service.rs +++ b/node/cli/src/service.rs @@ -169,9 +169,9 @@ pub struct AutosealInterval { } impl AutosealInterval { - pub fn new(config: &Configuration, interval: Duration) -> Self { + pub fn new(config: &Configuration, interval: u64) -> Self { let _tokio_runtime = config.tokio_handle.enter(); - let interval = tokio::time::interval(interval); + let interval = tokio::time::interval(Duration::from_millis(interval)); Self { interval } } @@ -885,7 +885,8 @@ pub struct OtherPartial { /// the parachain inherent pub fn start_dev_node( config: Configuration, - autoseal_interval: Duration, + autoseal_interval: u64, + autoseal_finalize_delay: Option, disable_autoseal_on_tx: bool, ) -> sc_service::error::Result where @@ -913,7 +914,10 @@ where + sp_consensus_aura::AuraApi, ExecutorDispatch: NativeExecutionDispatch + 'static, { - use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams}; + use sc_consensus_manual_seal::{ + run_manual_seal, run_delayed_finalize, EngineCommand, ManualSealParams, + DelayedFinalizeParams, + }; use fc_consensus::FrontierBlockImport; let sc_service::PartialComponents { @@ -984,18 +988,19 @@ where .filter(move |_| futures::future::ready(!disable_autoseal_on_tx)) .map(|_| EngineCommand::SealNewBlock { create_empty: true, - finalize: false, // todo:collator finalize 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> + Send + Sync + Unpin, > = Box::new(autoseal_interval.map(|_| EngineCommand::SealNewBlock { create_empty: true, - finalize: false, // todo:collator finalize true + finalize: false, parent_hash: None, sender: None, })); @@ -1005,6 +1010,20 @@ where let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?; let client_set_aside_for_cidp = client.clone(); + if let Some(delay_sec) = autoseal_finalize_delay { + let spawn_handle = task_manager.spawn_handle(); + + task_manager.spawn_essential_handle().spawn_blocking( + "finalization_task", + Some("block-authoring"), + run_delayed_finalize(DelayedFinalizeParams { + client: client.clone(), + delay_sec, + spawn_handle, + }), + ); + } + task_manager.spawn_essential_handle().spawn_blocking( "authorship_task", Some("block-authoring"),