From 4e7e23fa87ab7a4751c3c4abcbb4cbe107cc4974 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michael=20M=C3=BCller?= <mich@elmueller.net>
Date: Sun, 25 Sep 2022 11:24:35 +0200
Subject: [PATCH] Allow specifying immediate finalize for `manual-seal`
 (#12106)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Alexander Theißen <alex.theissen@me.com>
---
 .../client/consensus/manual-seal/src/lib.rs   | 54 +++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/substrate/client/consensus/manual-seal/src/lib.rs b/substrate/client/consensus/manual-seal/src/lib.rs
index c5dd169e281..4672e7275a5 100644
--- a/substrate/client/consensus/manual-seal/src/lib.rs
+++ b/substrate/client/consensus/manual-seal/src/lib.rs
@@ -247,6 +247,60 @@ pub async fn run_instant_seal<B, BI, CB, E, C, TP, SC, CIDP, P>(
 	.await
 }
 
+/// Runs the background authorship task for the instant seal engine.
+/// instant-seal creates a new block for every transaction imported into
+/// the transaction pool.
+///
+/// This function will finalize the block immediately as well. If you don't
+/// want this behavior use `run_instant_seal` instead.
+pub async fn run_instant_seal_and_finalize<B, BI, CB, E, C, TP, SC, CIDP, P>(
+	InstantSealParams {
+		block_import,
+		env,
+		client,
+		pool,
+		select_chain,
+		consensus_data_provider,
+		create_inherent_data_providers,
+	}: InstantSealParams<B, BI, E, C, TP, SC, CIDP, P>,
+) where
+	B: BlockT + 'static,
+	BI: BlockImport<B, Error = sp_consensus::Error, Transaction = sp_api::TransactionFor<C, B>>
+		+ Send
+		+ Sync
+		+ 'static,
+	C: HeaderBackend<B> + Finalizer<B, CB> + ProvideRuntimeApi<B> + 'static,
+	CB: ClientBackend<B> + 'static,
+	E: Environment<B> + 'static,
+	E::Proposer: Proposer<B, Proof = P, Transaction = TransactionFor<C, B>>,
+	SC: SelectChain<B> + 'static,
+	TransactionFor<C, B>: 'static,
+	TP: TransactionPool<Block = B>,
+	CIDP: CreateInherentDataProviders<B, ()>,
+	P: Send + Sync + 'static,
+{
+	// Creates and finalizes blocks as soon as transactions are imported
+	// into the transaction pool.
+	let commands_stream = pool.import_notification_stream().map(|_| EngineCommand::SealNewBlock {
+		create_empty: false,
+		finalize: true,
+		parent_hash: None,
+		sender: None,
+	});
+
+	run_manual_seal(ManualSealParams {
+		block_import,
+		env,
+		client,
+		pool,
+		commands_stream,
+		select_chain,
+		consensus_data_provider,
+		create_inherent_data_providers,
+	})
+	.await
+}
+
 #[cfg(test)]
 mod tests {
 	use super::*;
-- 
GitLab