From 26eeb5ccae70c6780295e4e93cf1f18ced2d57a4 Mon Sep 17 00:00:00 2001
From: librelois <c@elo.tf>
Date: Fri, 21 Jan 2022 01:53:52 +0100
Subject: [PATCH] =?UTF-8?q?feat(node):=C2=A0allow=20manual=20seal=20everyw?=
 =?UTF-8?q?here?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 node/src/cli.rs     | 17 +++++++++++++----
 node/src/command.rs | 27 +++++++++++++--------------
 node/src/service.rs | 24 +++++++++++++-----------
 3 files changed, 39 insertions(+), 29 deletions(-)

diff --git a/node/src/cli.rs b/node/src/cli.rs
index 60d8cfe38..534ad8b38 100644
--- a/node/src/cli.rs
+++ b/node/src/cli.rs
@@ -28,8 +28,8 @@ pub struct Cli {
 
     /// When blocks should be sealed in the dev service.
     ///
-    /// Options are "instant", "manual", or timer interval in milliseconds
-    #[structopt(long, default_value = "6000")]
+    /// Options are "production", "instant", "manual", or timer interval in milliseconds
+    #[structopt(long, default_value = "production")]
     pub sealing: Sealing,
 }
 
@@ -63,9 +63,11 @@ pub enum Subcommand {
     Benchmark(frame_benchmarking_cli::BenchmarkCmd),
 }
 
-/// Block authoring scheme to be used by the dev service.
-#[derive(Debug)]
+/// Block authoring scheme to be used by the node
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
 pub enum Sealing {
+    /// Author a block using normal runtime behavior (mandatory for production networks)
+    Production,
     /// Author a block immediately upon receiving a transaction into the transaction pool
     Instant,
     /// Author a block upon receiving an RPC command
@@ -74,11 +76,18 @@ pub enum Sealing {
     Interval(u64),
 }
 
+impl Sealing {
+    pub fn is_manual_consensus(self) -> bool {
+        self != Self::Production
+    }
+}
+
 impl FromStr for Sealing {
     type Err = String;
 
     fn from_str(s: &str) -> Result<Self, Self::Err> {
         Ok(match s {
+            "production" => Self::Production,
             "instant" => Self::Instant,
             "manual" => Self::Manual,
             s => {
diff --git a/node/src/command.rs b/node/src/command.rs
index 6e3c3534a..4a07a82a2 100644
--- a/node/src/command.rs
+++ b/node/src/command.rs
@@ -152,28 +152,32 @@ pub fn run() -> sc_cli::Result<()> {
         Some(Subcommand::CheckBlock(cmd)) => {
             let runner = cli.create_runner(cmd)?;
             runner.async_run(|mut config| {
-                let (client, _, import_queue, task_manager) = service::new_chain_ops(&mut config)?;
+                let (client, _, import_queue, task_manager) =
+                    service::new_chain_ops(&mut config, cli.sealing.is_manual_consensus())?;
                 Ok((cmd.run(client, import_queue), task_manager))
             })
         }
         Some(Subcommand::ExportBlocks(cmd)) => {
             let runner = cli.create_runner(cmd)?;
             runner.async_run(|mut config| {
-                let (client, _, _, task_manager) = service::new_chain_ops(&mut config)?;
+                let (client, _, _, task_manager) =
+                    service::new_chain_ops(&mut config, cli.sealing.is_manual_consensus())?;
                 Ok((cmd.run(client, config.database), task_manager))
             })
         }
         Some(Subcommand::ExportState(cmd)) => {
             let runner = cli.create_runner(cmd)?;
             runner.async_run(|mut config| {
-                let (client, _, _, task_manager) = service::new_chain_ops(&mut config)?;
+                let (client, _, _, task_manager) =
+                    service::new_chain_ops(&mut config, cli.sealing.is_manual_consensus())?;
                 Ok((cmd.run(client, config.chain_spec), task_manager))
             })
         }
         Some(Subcommand::ImportBlocks(cmd)) => {
             let runner = cli.create_runner(cmd)?;
             runner.async_run(|mut config| {
-                let (client, _, import_queue, task_manager) = service::new_chain_ops(&mut config)?;
+                let (client, _, import_queue, task_manager) =
+                    service::new_chain_ops(&mut config, cli.sealing.is_manual_consensus())?;
                 Ok((cmd.run(client, import_queue), task_manager))
             })
         }
@@ -184,7 +188,8 @@ pub fn run() -> sc_cli::Result<()> {
         Some(Subcommand::Revert(cmd)) => {
             let runner = cli.create_runner(cmd)?;
             runner.async_run(|mut config| {
-                let (client, backend, _, task_manager) = service::new_chain_ops(&mut config)?;
+                let (client, backend, _, task_manager) =
+                    service::new_chain_ops(&mut config, cli.sealing.is_manual_consensus())?;
                 Ok((cmd.run(client, backend), task_manager))
             })
         }
@@ -215,29 +220,23 @@ pub fn run() -> sc_cli::Result<()> {
         None => {
             let runner = cli.create_runner(&cli.run)?;
             runner.run_node_until_exit(|config| async move {
-                let chain_spec_id = config.chain_spec.id();
-                let sealing_opt = if chain_spec_id.ends_with("dev") && chain_spec_id != "gdev" {
-                    Some(cli.sealing)
-                } else {
-                    None
-                };
                 match config.chain_spec.runtime_type() {
                     #[cfg(feature = "g1")]
                     RuntimeType::G1 => {
-                        service::new_full::<g1_runtime::RuntimeApi, G1Executor>(config, sealing_opt)
+                        service::new_full::<g1_runtime::RuntimeApi, G1Executor>(config, cli.sealing)
                             .map_err(sc_cli::Error::Service)
                     }
                     #[cfg(feature = "gtest")]
                     RuntimeType::GTest => service::new_full::<
                         gtest_runtime::RuntimeApi,
                         GTestExecutor,
-                    >(config, sealing_opt)
+                    >(config, cli.sealing)
                     .map_err(sc_cli::Error::Service),
                     #[cfg(feature = "gdev")]
                     RuntimeType::GDev => {
                         service::new_full::<gdev_runtime::RuntimeApi, GDevExecutor>(
                             config,
-                            sealing_opt,
+                            cli.sealing,
                         )
                         .map_err(sc_cli::Error::Service)
                     }
diff --git a/node/src/service.rs b/node/src/service.rs
index 42fda611c..9c43ee358 100644
--- a/node/src/service.rs
+++ b/node/src/service.rs
@@ -131,6 +131,7 @@ impl IdentifyRuntimeType for Box<dyn sc_chain_spec::ChainSpec> {
 #[allow(clippy::type_complexity)]
 pub fn new_chain_ops(
     config: &mut Configuration,
+    manual_consensus: bool,
 ) -> Result<
     (
         Arc<Client>,
@@ -149,7 +150,7 @@ pub fn new_chain_ops(
                 import_queue,
                 task_manager,
                 ..
-            } = new_partial::<g1_runtime::RuntimeApi, G1Executor>(config, false)?;
+            } = new_partial::<g1_runtime::RuntimeApi, G1Executor>(config, manual_consensus)?;
             Ok((
                 Arc::new(Client::G1(client)),
                 backend,
@@ -165,7 +166,7 @@ pub fn new_chain_ops(
                 import_queue,
                 task_manager,
                 ..
-            } = new_partial::<gtest_runtime::RuntimeApi, GTestExecutor>(config, false)?;
+            } = new_partial::<gtest_runtime::RuntimeApi, GTestExecutor>(config, manual_consensus)?;
             Ok((
                 Arc::new(Client::GTest(client)),
                 backend,
@@ -181,7 +182,7 @@ pub fn new_chain_ops(
                 import_queue,
                 task_manager,
                 ..
-            } = new_partial::<gdev_runtime::RuntimeApi, GDevExecutor>(config, true)?;
+            } = new_partial::<gdev_runtime::RuntimeApi, GDevExecutor>(config, manual_consensus)?;
             Ok((
                 Arc::new(Client::GDev(client)),
                 backend,
@@ -293,7 +294,7 @@ where
 
     let babe_config = babe::Config::get(&*client)?;
     let (babe_block_import, babe_link) =
-        babe::block_import(babe_config, grandpa_block_import.clone(), client.clone())?;
+        babe::block_import(babe_config, grandpa_block_import, client.clone())?;
 
     let import_queue = if consensus_manual {
         manual_seal::import_queue(
@@ -349,7 +350,7 @@ fn remote_keystore(_url: &str) -> Result<Arc<LocalKeystore>, &'static str> {
 /// Builds a new service for a full client.
 pub fn new_full<RuntimeApi, Executor>(
     mut config: Configuration,
-    sealing_opt: Option<crate::cli::Sealing>,
+    sealing: crate::cli::Sealing,
 ) -> Result<TaskManager, ServiceError>
 where
     RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, Executor>>
@@ -369,7 +370,7 @@ where
         select_chain,
         transaction_pool,
         other: (block_import, babe_link, grandpa_link, mut telemetry),
-    } = new_partial::<RuntimeApi, Executor>(&config, sealing_opt.is_some())?;
+    } = new_partial::<RuntimeApi, Executor>(&config, sealing.is_manual_consensus())?;
 
     if let Some(url) = &config.keystore_remote {
         match remote_keystore(url) {
@@ -430,7 +431,7 @@ where
             telemetry.as_ref().map(|x| x.handle()),
         );
 
-        if let Some(sealing) = sealing_opt {
+        if sealing.is_manual_consensus() {
             let commands_stream: Box<dyn Stream<Item = EngineCommand<H256>> + Send + Sync + Unpin> =
                 match sealing {
                     crate::cli::Sealing::Instant => {
@@ -463,6 +464,7 @@ where
                             sender: None,
                         },
                     )),
+                    crate::cli::Sealing::Production => unreachable!(),
                 };
 
             let babe_consensus_data_provider =
@@ -533,10 +535,10 @@ where
                         let timestamp = sp_timestamp::InherentDataProvider::from_system_time();
 
                         let slot =
-						sp_consensus_babe::inherents::InherentDataProvider::from_timestamp_and_duration(
-                                *timestamp,
-                                slot_duration,
-                            );
+							sp_consensus_babe::inherents::InherentDataProvider::from_timestamp_and_duration(
+									*timestamp,
+									slot_duration,
+								);
 
                         Ok((timestamp, slot, uncles))
                     }
-- 
GitLab