From 22fdbdd9e40c8f6caa8b4f4d5384dc2ad4d91360 Mon Sep 17 00:00:00 2001
From: bgallois <benjamin@gallois.cc>
Date: Sat, 10 Jun 2023 10:56:05 +0200
Subject: [PATCH] fix manual and instant sealing

---
 node/src/rpc.rs     | 34 ++++++++++++-----------
 node/src/service.rs | 67 +++++++++++++++++++++++++--------------------
 2 files changed, 55 insertions(+), 46 deletions(-)

diff --git a/node/src/rpc.rs b/node/src/rpc.rs
index 2d41cd61c..036f63eb8 100644
--- a/node/src/rpc.rs
+++ b/node/src/rpc.rs
@@ -37,6 +37,7 @@ use sp_keystore::KeystorePtr;
 use std::sync::Arc;
 
 /// Extra dependencies for BABE.
+#[derive(Clone)]
 pub struct BabeDeps {
     /// A handle to the BABE worker for issuing requests.
     pub babe_worker_handle: BabeWorkerHandle<Block>,
@@ -61,7 +62,7 @@ pub struct FullDeps<C, P, SC> {
         futures::channel::mpsc::Sender<sc_consensus_manual_seal::EngineCommand<sp_core::H256>>,
     >,
     /// BABE specific dependencies.
-    pub babe: BabeDeps,
+    pub babe: Option<BabeDeps>,
 }
 
 /// Instantiate all full RPC extensions.
@@ -95,23 +96,24 @@ where
         babe,
     } = deps;
 
-    let BabeDeps {
-        babe_worker_handle,
-        keystore,
-    } = babe;
-
+    if let Some(babe) = babe {
+        let BabeDeps {
+            babe_worker_handle,
+            keystore,
+        } = babe;
+        module.merge(
+            Babe::new(
+                client.clone(),
+                babe_worker_handle.clone(),
+                keystore,
+                select_chain,
+                deny_unsafe,
+            )
+            .into_rpc(),
+        )?;
+    }
     module.merge(System::new(client.clone(), pool, deny_unsafe).into_rpc())?;
     module.merge(TransactionPayment::new(client.clone()).into_rpc())?;
-    module.merge(
-        Babe::new(
-            client.clone(),
-            babe_worker_handle.clone(),
-            keystore,
-            select_chain,
-            deny_unsafe,
-        )
-        .into_rpc(),
-    )?;
     if let Some(command_sink) = command_sink_opt {
         // We provide the rpc handler with the sending end of the channel to allow the rpc
         // send EngineCommands to the background block authorship task.
diff --git a/node/src/service.rs b/node/src/service.rs
index 331a77b37..18162b7db 100644
--- a/node/src/service.rs
+++ b/node/src/service.rs
@@ -218,7 +218,7 @@ pub fn new_partial<RuntimeApi, Executor>(
                 FullGrandpaBlockImport<RuntimeApi, Executor>,
             >,
             sc_consensus_babe::BabeLink<Block>,
-            sc_consensus_babe::BabeWorkerHandle<Block>,
+            Option<sc_consensus_babe::BabeWorkerHandle<Block>>,
             sc_consensus_grandpa::LinkHalf<
                 Block,
                 FullClient<RuntimeApi, Executor>,
@@ -291,35 +291,37 @@ where
         client.clone(),
     )?;
 
-    let slot_duration = babe_link.config().slot_duration();
-    let (mut import_queue, babe_worker_handle) = sc_consensus_babe::import_queue(
-        babe_link.clone(),
-        babe_block_import.clone(),
-        Some(Box::new(justification_import)),
-        client.clone(),
-        select_chain.clone(),
-        move |_, ()| async move {
-            let timestamp = sp_timestamp::InherentDataProvider::from_system_time();
-
-            let slot =
-                sp_consensus_babe::inherents::InherentDataProvider::from_timestamp_and_slot_duration(
-                    *timestamp,
-                    slot_duration,
-                );
-            Ok((slot, timestamp))
-        },
-        &task_manager.spawn_essential_handle(),
-        config.prometheus_registry(),
-        telemetry.as_ref().map(|x| x.handle()),
-    )?;
-
-    if consensus_manual {
-        import_queue = sc_consensus_manual_seal::import_queue(
+    let (import_queue, babe_worker_handle) = if consensus_manual {
+        let import_queue = sc_consensus_manual_seal::import_queue(
             Box::new(babe_block_import.clone()),
             &task_manager.spawn_essential_handle(),
             config.prometheus_registry(),
         );
-    }
+        (import_queue, None)
+    } else {
+        let slot_duration = babe_link.config().slot_duration();
+        let (queue, handle) = sc_consensus_babe::import_queue(
+            babe_link.clone(),
+            babe_block_import.clone(),
+            Some(Box::new(justification_import)),
+            client.clone(),
+            select_chain.clone(),
+            move |_, ()| async move {
+                let timestamp = sp_timestamp::InherentDataProvider::from_system_time();
+
+                let slot =
+                    sp_consensus_babe::inherents::InherentDataProvider::from_timestamp_and_slot_duration(
+                        *timestamp,
+                        slot_duration,
+                    );
+                Ok((slot, timestamp))
+            },
+            &task_manager.spawn_essential_handle(),
+            config.prometheus_registry(),
+            telemetry.as_ref().map(|x| x.handle()),
+        )?;
+        (queue, Some(handle))
+    };
 
     Ok(sc_service::PartialComponents {
         client,
@@ -556,6 +558,14 @@ where
         let select_chain = select_chain.clone();
         let chain_spec = config.chain_spec.cloned_box();
         let keystore = keystore_container.keystore().clone();
+        let babe_deps = if let Some(babe_worker_handle) = babe_worker_handle {
+            Some(crate::rpc::BabeDeps {
+                babe_worker_handle: babe_worker_handle.clone(),
+                keystore: keystore.clone(),
+            })
+        } else {
+            None
+        };
 
         Box::new(move |deny_unsafe, _| {
             let deps = crate::rpc::FullDeps {
@@ -564,10 +574,7 @@ where
                 select_chain: select_chain.clone(),
                 chain_spec: chain_spec.cloned_box(),
                 deny_unsafe,
-                babe: crate::rpc::BabeDeps {
-                    babe_worker_handle: babe_worker_handle.clone(),
-                    keystore: keystore.clone(),
-                },
+                babe: babe_deps.clone(),
                 command_sink_opt: command_sink_opt.clone(),
             };
 
-- 
GitLab